1
linux/drivers/char/adi.c
Linus Torvalds 3352633ce6 vfs-6.12.file
-----BEGIN PGP SIGNATURE-----
 
 iHUEABYKAB0WIQRAhzRXHqcMeLMyaSiRxhvAZXjcogUCZuQEwAAKCRCRxhvAZXjc
 osS0AQCgIpvey9oW5DMyMw6Bv0hFMRv95gbNQZfHy09iK+NMNAD9GALhb/4cMIVB
 7YrZGXEz454lpgcs8AnrOVjVNfctOQg=
 =e9s9
 -----END PGP SIGNATURE-----

Merge tag 'vfs-6.12.file' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs

Pull vfs file updates from Christian Brauner:
 "This is the work to cleanup and shrink struct file significantly.

  Right now, (focusing on x86) struct file is 232 bytes. After this
  series struct file will be 184 bytes aka 3 cacheline and a spare 8
  bytes for future extensions at the end of the struct.

  With struct file being as ubiquitous as it is this should make a
  difference for file heavy workloads and allow further optimizations in
  the future.

   - struct fown_struct was embedded into struct file letting it take up
     32 bytes in total when really it shouldn't even be embedded in
     struct file in the first place. Instead, actual users of struct
     fown_struct now allocate the struct on demand. This frees up 24
     bytes.

   - Move struct file_ra_state into the union containg the cleanup hooks
     and move f_iocb_flags out of the union. This closes a 4 byte hole
     we created earlier and brings struct file to 192 bytes. Which means
     struct file is 3 cachelines and we managed to shrink it by 40
     bytes.

   - Reorder struct file so that nothing crosses a cacheline.

     I suspect that in the future we will end up reordering some members
     to mitigate false sharing issues or just because someone does
     actually provide really good perf data.

   - Shrinking struct file to 192 bytes is only part of the work.

     Files use a slab that is SLAB_TYPESAFE_BY_RCU and when a kmem cache
     is created with SLAB_TYPESAFE_BY_RCU the free pointer must be
     located outside of the object because the cache doesn't know what
     part of the memory can safely be overwritten as it may be needed to
     prevent object recycling.

     That has the consequence that SLAB_TYPESAFE_BY_RCU may end up
     adding a new cacheline.

     So this also contains work to add a new kmem_cache_create_rcu()
     function that allows the caller to specify an offset where the
     freelist pointer is supposed to be placed. Thus avoiding the
     implicit addition of a fourth cacheline.

   - And finally this removes the f_version member in struct file.

     The f_version member isn't particularly well-defined. It is mainly
     used as a cookie to detect concurrent seeks when iterating
     directories. But it is also abused by some subsystems for
     completely unrelated things.

     It is mostly a directory and filesystem specific thing that doesn't
     really need to live in struct file and with its wonky semantics it
     really lacks a specific function.

     For pipes, f_version is (ab)used to defer poll notifications until
     a write has happened. And struct pipe_inode_info is used by
     multiple struct files in their ->private_data so there's no chance
     of pushing that down into file->private_data without introducing
     another pointer indirection.

     But pipes don't rely on f_pos_lock so this adds a union into struct
     file encompassing f_pos_lock and a pipe specific f_pipe member that
     pipes can use. This union of course can be extended to other file
     types and is similar to what we do in struct inode already"

* tag 'vfs-6.12.file' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs: (26 commits)
  fs: remove f_version
  pipe: use f_pipe
  fs: add f_pipe
  ubifs: store cookie in private data
  ufs: store cookie in private data
  udf: store cookie in private data
  proc: store cookie in private data
  ocfs2: store cookie in private data
  input: remove f_version abuse
  ext4: store cookie in private data
  ext2: store cookie in private data
  affs: store cookie in private data
  fs: add generic_llseek_cookie()
  fs: use must_set_pos()
  fs: add must_set_pos()
  fs: add vfs_setpos_cookie()
  s390: remove unused f_version
  ceph: remove unused f_version
  adi: remove unused f_version
  mm: Removed @freeptr_offset to prevent doc warning
  ...
2024-09-16 09:14:02 +02:00

233 lines
4.3 KiB
C

// SPDX-License-Identifier: GPL-2.0
/*
* Privileged ADI driver for sparc64
*
* Author: Tom Hromatka <tom.hromatka@oracle.com>
*/
#include <linux/kernel.h>
#include <linux/miscdevice.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <asm/asi.h>
#define MAX_BUF_SZ PAGE_SIZE
static int read_mcd_tag(unsigned long addr)
{
long err;
int ver;
__asm__ __volatile__(
"1: ldxa [%[addr]] %[asi], %[ver]\n"
" mov 0, %[err]\n"
"2:\n"
" .section .fixup,#alloc,#execinstr\n"
" .align 4\n"
"3: sethi %%hi(2b), %%g1\n"
" jmpl %%g1 + %%lo(2b), %%g0\n"
" mov %[invalid], %[err]\n"
" .previous\n"
" .section __ex_table, \"a\"\n"
" .align 4\n"
" .word 1b, 3b\n"
" .previous\n"
: [ver] "=r" (ver), [err] "=r" (err)
: [addr] "r" (addr), [invalid] "i" (EFAULT),
[asi] "i" (ASI_MCD_REAL)
: "memory", "g1"
);
if (err)
return -EFAULT;
else
return ver;
}
static ssize_t adi_read(struct file *file, char __user *buf,
size_t count, loff_t *offp)
{
size_t ver_buf_sz, bytes_read = 0;
int ver_buf_idx = 0;
loff_t offset;
u8 *ver_buf;
ssize_t ret;
ver_buf_sz = min_t(size_t, count, MAX_BUF_SZ);
ver_buf = kmalloc(ver_buf_sz, GFP_KERNEL);
if (!ver_buf)
return -ENOMEM;
offset = (*offp) * adi_blksize();
while (bytes_read < count) {
ret = read_mcd_tag(offset);
if (ret < 0)
goto out;
ver_buf[ver_buf_idx] = (u8)ret;
ver_buf_idx++;
offset += adi_blksize();
if (ver_buf_idx >= ver_buf_sz) {
if (copy_to_user(buf + bytes_read, ver_buf,
ver_buf_sz)) {
ret = -EFAULT;
goto out;
}
bytes_read += ver_buf_sz;
ver_buf_idx = 0;
ver_buf_sz = min(count - bytes_read,
(size_t)MAX_BUF_SZ);
}
}
(*offp) += bytes_read;
ret = bytes_read;
out:
kfree(ver_buf);
return ret;
}
static int set_mcd_tag(unsigned long addr, u8 ver)
{
long err;
__asm__ __volatile__(
"1: stxa %[ver], [%[addr]] %[asi]\n"
" mov 0, %[err]\n"
"2:\n"
" .section .fixup,#alloc,#execinstr\n"
" .align 4\n"
"3: sethi %%hi(2b), %%g1\n"
" jmpl %%g1 + %%lo(2b), %%g0\n"
" mov %[invalid], %[err]\n"
" .previous\n"
" .section __ex_table, \"a\"\n"
" .align 4\n"
" .word 1b, 3b\n"
" .previous\n"
: [err] "=r" (err)
: [ver] "r" (ver), [addr] "r" (addr),
[invalid] "i" (EFAULT), [asi] "i" (ASI_MCD_REAL)
: "memory", "g1"
);
if (err)
return -EFAULT;
else
return ver;
}
static ssize_t adi_write(struct file *file, const char __user *buf,
size_t count, loff_t *offp)
{
size_t ver_buf_sz, bytes_written = 0;
loff_t offset;
u8 *ver_buf;
ssize_t ret;
int i;
if (count <= 0)
return -EINVAL;
ver_buf_sz = min_t(size_t, count, MAX_BUF_SZ);
ver_buf = kmalloc(ver_buf_sz, GFP_KERNEL);
if (!ver_buf)
return -ENOMEM;
offset = (*offp) * adi_blksize();
do {
if (copy_from_user(ver_buf, &buf[bytes_written],
ver_buf_sz)) {
ret = -EFAULT;
goto out;
}
for (i = 0; i < ver_buf_sz; i++) {
ret = set_mcd_tag(offset, ver_buf[i]);
if (ret < 0)
goto out;
offset += adi_blksize();
}
bytes_written += ver_buf_sz;
ver_buf_sz = min(count - bytes_written, (size_t)MAX_BUF_SZ);
} while (bytes_written < count);
(*offp) += bytes_written;
ret = bytes_written;
out:
__asm__ __volatile__("membar #Sync");
kfree(ver_buf);
return ret;
}
static loff_t adi_llseek(struct file *file, loff_t offset, int whence)
{
loff_t ret = -EINVAL;
switch (whence) {
case SEEK_END:
case SEEK_DATA:
case SEEK_HOLE:
/* unsupported */
return -EINVAL;
case SEEK_CUR:
if (offset == 0)
return file->f_pos;
offset += file->f_pos;
break;
case SEEK_SET:
break;
}
if (offset != file->f_pos) {
file->f_pos = offset;
ret = offset;
}
return ret;
}
static const struct file_operations adi_fops = {
.owner = THIS_MODULE,
.llseek = adi_llseek,
.read = adi_read,
.write = adi_write,
.fop_flags = FOP_UNSIGNED_OFFSET,
};
static struct miscdevice adi_miscdev = {
.minor = MISC_DYNAMIC_MINOR,
.name = KBUILD_MODNAME,
.fops = &adi_fops,
};
static int __init adi_init(void)
{
if (!adi_capable())
return -EPERM;
return misc_register(&adi_miscdev);
}
static void __exit adi_exit(void)
{
misc_deregister(&adi_miscdev);
}
module_init(adi_init);
module_exit(adi_exit);
MODULE_AUTHOR("Tom Hromatka <tom.hromatka@oracle.com>");
MODULE_DESCRIPTION("Privileged interface to ADI");
MODULE_VERSION("1.0");
MODULE_LICENSE("GPL v2");