Get the inode from the file instead of ->private, declare the show function and let procfs open and free the seq_file. Signed-off-by: Christian Brauner (Amutable) --- drivers/nubus/proc.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/drivers/nubus/proc.c b/drivers/nubus/proc.c index 3ea3d1738eae..cb21ad1d9964 100644 --- a/drivers/nubus/proc.c +++ b/drivers/nubus/proc.c @@ -107,7 +107,7 @@ nubus_proc_alloc_pde_data(unsigned char *ptr, unsigned int size) static int nubus_proc_rsrc_show(struct seq_file *m, void *v) { - struct inode *inode = m->private; + struct inode *inode = file_inode(m->file); struct nubus_proc_pde_data *pded; pded = pde_data(inode); @@ -138,16 +138,8 @@ static int nubus_proc_rsrc_show(struct seq_file *m, void *v) return 0; } -static int nubus_rsrc_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, nubus_proc_rsrc_show, inode); -} - static const struct proc_ops nubus_rsrc_proc_ops = { - .proc_open = nubus_rsrc_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = nubus_proc_rsrc_show, }; void nubus_proc_add_rsrc_mem(struct proc_dir_entry *procdir, -- 2.53.0 Implement ->proc_read_buf() so procfs copies what is read from /proc/bus/zorro/ to userspace after it unpinned the entry. Signed-off-by: Christian Brauner (Amutable) --- drivers/zorro/proc.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/drivers/zorro/proc.c b/drivers/zorro/proc.c index f916bf60b312..4e4ba04752ea 100644 --- a/drivers/zorro/proc.c +++ b/drivers/zorro/proc.c @@ -28,7 +28,7 @@ proc_bus_zorro_lseek(struct file *file, loff_t off, int whence) } static ssize_t -proc_bus_zorro_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos) +proc_bus_zorro_read(struct file *file, char *buf, size_t nbytes, loff_t *ppos) { struct zorro_dev *z = pde_data(file_inode(file)); struct ConfigDev cd; @@ -49,8 +49,7 @@ proc_bus_zorro_read(struct file *file, char __user *buf, size_t nbytes, loff_t * cd.cd_BoardAddr = cpu_to_be32(zorro_resource_start(z)); cd.cd_BoardSize = cpu_to_be32(zorro_resource_len(z)); - if (copy_to_user(buf, (void *)&cd + pos, nbytes)) - return -EFAULT; + memcpy(buf, (void *)&cd + pos, nbytes); *ppos += nbytes; return nbytes; @@ -58,7 +57,7 @@ proc_bus_zorro_read(struct file *file, char __user *buf, size_t nbytes, loff_t * static const struct proc_ops bus_zorro_proc_ops = { .proc_lseek = proc_bus_zorro_lseek, - .proc_read = proc_bus_zorro_read, + .proc_read_buf = proc_bus_zorro_read, }; static void * zorro_seq_start(struct seq_file *m, loff_t *pos) -- 2.53.0 Implement ->proc_write_buf() so procfs copies in what is written before it pins the entry, and write the config space from that copy with get_unaligned_le16() and get_unaligned_le32() instead of get_user(). The copy holds at most PCI_CFG_SPACE_EXP_SIZE bytes. Larger writes come up short with PROC_ENTRY_SHORT_WRITES, like they did before because they were cut at the end of the config space. A buffer that can't be read used to write zeroes to the config space and report success. It fails with -EFAULT now and nothing is written. Signed-off-by: Christian Brauner (Amutable) --- drivers/pci/proc.c | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/drivers/pci/proc.c b/drivers/pci/proc.c index e0dc70123f82..d420353ee2b3 100644 --- a/drivers/pci/proc.c +++ b/drivers/pci/proc.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include "pci.h" @@ -114,8 +115,8 @@ static ssize_t proc_bus_pci_read(struct file *file, char __user *buf, return nbytes; } -static ssize_t proc_bus_pci_write(struct file *file, const char __user *buf, - size_t nbytes, loff_t *ppos) +static ssize_t proc_bus_pci_write(struct file *file, char *buf, size_t nbytes, + loff_t *ppos) { struct inode *ino = file_inode(file); struct pci_dev *dev = pde_data(ino); @@ -145,51 +146,38 @@ static ssize_t proc_bus_pci_write(struct file *file, const char __user *buf, nbytes = size - pos; cnt = nbytes; - if (!access_ok(buf, cnt)) - return -EINVAL; - pci_config_pm_runtime_get(dev); if ((pos & 1) && cnt) { - unsigned char val; - get_user(val, buf); - pci_user_write_config_byte(dev, pos, val); + pci_user_write_config_byte(dev, pos, *buf); buf++; pos++; cnt--; } if ((pos & 3) && cnt > 2) { - __le16 val; - get_user(val, (__le16 __user *) buf); - pci_user_write_config_word(dev, pos, le16_to_cpu(val)); + pci_user_write_config_word(dev, pos, get_unaligned_le16(buf)); buf += 2; pos += 2; cnt -= 2; } while (cnt >= 4) { - __le32 val; - get_user(val, (__le32 __user *) buf); - pci_user_write_config_dword(dev, pos, le32_to_cpu(val)); + pci_user_write_config_dword(dev, pos, get_unaligned_le32(buf)); buf += 4; pos += 4; cnt -= 4; } if (cnt >= 2) { - __le16 val; - get_user(val, (__le16 __user *) buf); - pci_user_write_config_word(dev, pos, le16_to_cpu(val)); + pci_user_write_config_word(dev, pos, get_unaligned_le16(buf)); buf += 2; pos += 2; cnt -= 2; } if (cnt) { - unsigned char val; - get_user(val, buf); - pci_user_write_config_byte(dev, pos, val); + pci_user_write_config_byte(dev, pos, *buf); pos++; } @@ -337,9 +325,11 @@ static int proc_bus_pci_release(struct inode *inode, struct file *file) #endif /* HAVE_PCI_MMAP */ static const struct proc_ops proc_bus_pci_ops = { + .proc_flags = PROC_ENTRY_SHORT_WRITES, .proc_lseek = proc_bus_pci_lseek, .proc_read = proc_bus_pci_read, - .proc_write = proc_bus_pci_write, + .proc_write_buf = proc_bus_pci_write, + .proc_write_size = PCI_CFG_SPACE_EXP_SIZE, .proc_ioctl = proc_bus_pci_ioctl, #ifdef CONFIG_COMPAT .proc_compat_ioctl = proc_bus_pci_ioctl, -- 2.53.0 Implement ->proc_read_buf() so procfs copies what is read to userspace after it unpinned the entry, and read the config space into procfs' buffer with put_unaligned_le16() and put_unaligned_le32() instead of put_user(). A read into a buffer that can't be written used to report success. It fails with -EFAULT now. A page holds all of the config space, so a single read of it still works, libpci treats a short read as an error. Signed-off-by: Christian Brauner (Amutable) --- drivers/pci/proc.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/drivers/pci/proc.c b/drivers/pci/proc.c index d420353ee2b3..a8d7469f222b 100644 --- a/drivers/pci/proc.c +++ b/drivers/pci/proc.c @@ -29,8 +29,8 @@ static loff_t proc_bus_pci_lseek(struct file *file, loff_t off, int whence) return fixed_size_llseek(file, off, whence, dev->cfg_size); } -static ssize_t proc_bus_pci_read(struct file *file, char __user *buf, - size_t nbytes, loff_t *ppos) +static ssize_t proc_bus_pci_read(struct file *file, char *buf, size_t nbytes, + loff_t *ppos) { struct pci_dev *dev = pde_data(file_inode(file)); unsigned int pos, cnt, size; @@ -60,15 +60,12 @@ static ssize_t proc_bus_pci_read(struct file *file, char __user *buf, nbytes = size - pos; cnt = nbytes; - if (!access_ok(buf, cnt)) - return -EINVAL; - pci_config_pm_runtime_get(dev); if ((pos & 1) && cnt) { unsigned char val; pci_user_read_config_byte(dev, pos, &val); - put_user(val, buf); + *buf = val; buf++; pos++; cnt--; @@ -77,7 +74,7 @@ static ssize_t proc_bus_pci_read(struct file *file, char __user *buf, if ((pos & 3) && cnt > 2) { unsigned short val; pci_user_read_config_word(dev, pos, &val); - put_user(cpu_to_le16(val), (__le16 __user *) buf); + put_unaligned_le16(val, buf); buf += 2; pos += 2; cnt -= 2; @@ -86,7 +83,7 @@ static ssize_t proc_bus_pci_read(struct file *file, char __user *buf, while (cnt >= 4) { unsigned int val; pci_user_read_config_dword(dev, pos, &val); - put_user(cpu_to_le32(val), (__le32 __user *) buf); + put_unaligned_le32(val, buf); buf += 4; pos += 4; cnt -= 4; @@ -96,7 +93,7 @@ static ssize_t proc_bus_pci_read(struct file *file, char __user *buf, if (cnt >= 2) { unsigned short val; pci_user_read_config_word(dev, pos, &val); - put_user(cpu_to_le16(val), (__le16 __user *) buf); + put_unaligned_le16(val, buf); buf += 2; pos += 2; cnt -= 2; @@ -105,7 +102,7 @@ static ssize_t proc_bus_pci_read(struct file *file, char __user *buf, if (cnt) { unsigned char val; pci_user_read_config_byte(dev, pos, &val); - put_user(val, buf); + *buf = val; pos++; } @@ -327,7 +324,7 @@ static int proc_bus_pci_release(struct inode *inode, struct file *file) static const struct proc_ops proc_bus_pci_ops = { .proc_flags = PROC_ENTRY_SHORT_WRITES, .proc_lseek = proc_bus_pci_lseek, - .proc_read = proc_bus_pci_read, + .proc_read_buf = proc_bus_pci_read, .proc_write_buf = proc_bus_pci_write, .proc_write_size = PCI_CFG_SPACE_EXP_SIZE, .proc_ioctl = proc_bus_pci_ioctl, -- 2.53.0 The PCIIOC_* commands take and return plain values and don't touch userspace memory. Implement ->proc_ioctl_buf() and list them with nothing to copy. 32-bit tasks go through the same table, so the compat hook goes away. Unknown commands fail with -ENOTTY from procfs now instead of -EINVAL, and instead of -EPERM under lockdown, since they never reach the handler. Signed-off-by: Christian Brauner (Amutable) --- drivers/pci/proc.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/drivers/pci/proc.c b/drivers/pci/proc.c index a8d7469f222b..6b0cb5bcbea3 100644 --- a/drivers/pci/proc.c +++ b/drivers/pci/proc.c @@ -192,8 +192,7 @@ struct pci_filp_private { }; #endif /* HAVE_PCI_MMAP */ -static long proc_bus_pci_ioctl(struct file *file, unsigned int cmd, - unsigned long arg) +static long proc_bus_pci_ioctl(struct file *file, unsigned int cmd, void *arg) { struct pci_dev *dev = pde_data(file_inode(file)); #ifdef HAVE_PCI_MMAP @@ -321,16 +320,23 @@ static int proc_bus_pci_release(struct inode *inode, struct file *file) } #endif /* HAVE_PCI_MMAP */ +/* The commands take and return plain values. */ +static const struct proc_ioctl_arg proc_bus_pci_ioctl_args[] = { + PROC_IOCTL_ARG(PCIIOC_CONTROLLER, 0, 0), + PROC_IOCTL_ARG(PCIIOC_MMAP_IS_IO, 0, 0), + PROC_IOCTL_ARG(PCIIOC_MMAP_IS_MEM, 0, 0), + PROC_IOCTL_ARG(PCIIOC_WRITE_COMBINE, 0, 0), +}; + static const struct proc_ops proc_bus_pci_ops = { .proc_flags = PROC_ENTRY_SHORT_WRITES, .proc_lseek = proc_bus_pci_lseek, .proc_read_buf = proc_bus_pci_read, .proc_write_buf = proc_bus_pci_write, .proc_write_size = PCI_CFG_SPACE_EXP_SIZE, - .proc_ioctl = proc_bus_pci_ioctl, -#ifdef CONFIG_COMPAT - .proc_compat_ioctl = proc_bus_pci_ioctl, -#endif + .proc_ioctl_buf = proc_bus_pci_ioctl, + .proc_ioctl_args = proc_bus_pci_ioctl_args, + .proc_nr_ioctl_args = ARRAY_SIZE(proc_bus_pci_ioctl_args), #ifdef HAVE_PCI_MMAP .proc_open = proc_bus_pci_open, .proc_release = proc_bus_pci_release, -- 2.53.0 Declare the seq_operations and let procfs open and free the seq_file. ->proc_open() only records the event count for ->proc_poll() now. Signed-off-by: Christian Brauner (Amutable) --- drivers/md/md.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/drivers/md/md.c b/drivers/md/md.c index 680b34a63cb3..e642b763ebc2 100644 --- a/drivers/md/md.c +++ b/drivers/md/md.c @@ -9141,18 +9141,13 @@ static const struct seq_operations md_seq_ops = { .show = md_seq_show, }; +/* procfs opens the seq_file */ static int md_seq_open(struct inode *inode, struct file *file) { - struct seq_file *seq; - int error; - - error = seq_open(file, &md_seq_ops); - if (error) - return error; + struct seq_file *seq = file->private_data; - seq = file->private_data; seq->poll_event = atomic_read(&md_event_count); - return error; + return 0; } static int md_unloading; @@ -9174,10 +9169,8 @@ static __poll_t mdstat_poll(struct file *filp, poll_table *wait) } static const struct proc_ops mdstat_proc_ops = { + .proc_seq_ops = &md_seq_ops, .proc_open = md_seq_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = seq_release, .proc_poll = mdstat_poll, }; -- 2.53.0 Implement ->proc_write_buf() so procfs copies in what is written before it pins the entry, and parse the command from that copy. Commands longer than PAGE_SIZE - 1 bytes are rejected now instead of cut at 64k. Signed-off-by: Christian Brauner (Amutable) --- drivers/s390/block/dasd_proc.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/drivers/s390/block/dasd_proc.c b/drivers/s390/block/dasd_proc.c index 86399427f01d..6c3bbb7dfe85 100644 --- a/drivers/s390/block/dasd_proc.c +++ b/drivers/s390/block/dasd_proc.c @@ -15,7 +15,6 @@ #include #include #include -#include #include #include @@ -247,17 +246,15 @@ static int dasd_stats_proc_show(struct seq_file *m, void *v) } static ssize_t dasd_stats_proc_write(struct file *file, - const char __user *user_buf, size_t user_len, loff_t *pos) + char *buffer, size_t user_len, loff_t *pos) { #ifdef CONFIG_DASD_PROFILE - char *buffer, *str; + char *str; int rc; - if (user_len > 65536) - user_len = 65536; - buffer = dasd_get_user_string(user_buf, user_len); - if (IS_ERR(buffer)) - return PTR_ERR(buffer); + /* strip linefeed */ + if (user_len && buffer[user_len - 1] == '\n') + buffer[user_len - 1] = 0; /* check for valid verbs */ str = skip_spaces(buffer); @@ -296,13 +293,11 @@ static ssize_t dasd_stats_proc_write(struct file *file, pr_info("The statistics have been reset\n"); } else goto out_parse_error; - vfree(buffer); return user_len; out_parse_error: rc = -EINVAL; pr_warn("%s is not a supported value for /proc/dasd/statistics\n", str); out_error: - vfree(buffer); return rc; #else pr_warn("/proc/dasd/statistics: is not activated in this kernel\n"); @@ -312,7 +307,7 @@ out_error: static const struct proc_ops dasd_stats_proc_ops = { .proc_show = dasd_stats_proc_show, - .proc_write = dasd_stats_proc_write, + .proc_write_buf = dasd_stats_proc_write, }; /* -- 2.53.0 Implement ->proc_write_buf() for cio_ignore and cio_settle so procfs copies in what is written before it pins the entry. cio_ignore parses the list from that copy, up to the first NUL like strncpy_from_user() handed it to it before. Writes larger than 64k still come up short, now through PROC_ENTRY_SHORT_WRITES. Writes to cio_settle larger than PAGE_SIZE - 1 bytes fail with -EINVAL now, it never looked at what was written. Signed-off-by: Christian Brauner (Amutable) --- drivers/s390/cio/blacklist.c | 33 ++++++++++----------------------- drivers/s390/cio/css.c | 4 ++-- 2 files changed, 12 insertions(+), 25 deletions(-) diff --git a/drivers/s390/cio/blacklist.c b/drivers/s390/cio/blacklist.c index 4e02f78f6102..1ec7915be6b3 100644 --- a/drivers/s390/cio/blacklist.c +++ b/drivers/s390/cio/blacklist.c @@ -12,7 +12,7 @@ #include #include -#include +#include #include #include #include @@ -355,39 +355,24 @@ cio_ignore_proc_seq_show(struct seq_file *s, void *it) } static ssize_t -cio_ignore_write(struct file *file, const char __user *user_buf, +cio_ignore_write(struct file *file, char *buf, size_t user_len, loff_t *offset) { - char *buf; - ssize_t rc, ret, i; + ssize_t ret, i; if (*offset) return -EINVAL; - if (user_len > 65536) - user_len = 65536; - buf = vzalloc(user_len + 1); /* maybe better use the stack? */ - if (buf == NULL) - return -ENOMEM; - if (strncpy_from_user (buf, user_buf, user_len) < 0) { - rc = -EFAULT; - goto out_free; - } - - i = user_len - 1; + /* the parser never saw what follows a NUL */ + i = strnlen(buf, user_len) - 1; while ((i >= 0) && (isspace(buf[i]) || (buf[i] == 0))) { buf[i] = '\0'; i--; } ret = blacklist_parse_proc_parameters(buf); if (ret) - rc = ret; - else - rc = user_len; - -out_free: - vfree (buf); - return rc; + return ret; + return user_len; } static const struct seq_operations cio_ignore_proc_seq_ops = { @@ -398,9 +383,11 @@ static const struct seq_operations cio_ignore_proc_seq_ops = { }; static const struct proc_ops cio_ignore_proc_ops = { + .proc_flags = PROC_ENTRY_SHORT_WRITES, .proc_seq_ops = &cio_ignore_proc_seq_ops, .proc_state_size = sizeof(struct ccwdev_iter), - .proc_write = cio_ignore_write, + .proc_write_buf = cio_ignore_write, + .proc_write_size = SZ_64K, }; static int diff --git a/drivers/s390/cio/css.c b/drivers/s390/cio/css.c index 54b8b8639694..efd3df3e90ba 100644 --- a/drivers/s390/cio/css.c +++ b/drivers/s390/cio/css.c @@ -1278,7 +1278,7 @@ static int __init channel_subsystem_init_sync(void) subsys_initcall_sync(channel_subsystem_init_sync); #ifdef CONFIG_PROC_FS -static ssize_t cio_settle_write(struct file *file, const char __user *buf, +static ssize_t cio_settle_write(struct file *file, char *buf, size_t count, loff_t *ppos) { int ret; @@ -1292,7 +1292,7 @@ static ssize_t cio_settle_write(struct file *file, const char __user *buf, static const struct proc_ops cio_settle_proc_ops = { .proc_open = nonseekable_open, - .proc_write = cio_settle_write, + .proc_write_buf = cio_settle_write, }; static int __init cio_settle_init(void) -- 2.53.0 Declare the show function and let procfs open and free the seq_file of /proc/scsi//. ->proc_open() only preallocates the buffer now. Reading the file then no longer holds up its removal when the host goes away. Signed-off-by: Christian Brauner (Amutable) --- drivers/scsi/scsi_proc.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/drivers/scsi/scsi_proc.c b/drivers/scsi/scsi_proc.c index 3e49c4c8704f..59229bf9938e 100644 --- a/drivers/scsi/scsi_proc.c +++ b/drivers/scsi/scsi_proc.c @@ -94,10 +94,10 @@ static int proc_scsi_show(struct seq_file *m, void *v) return shost->hostt->show_info(m, shost); } +/* procfs opens the seq_file */ static int proc_scsi_host_open(struct inode *inode, struct file *file) { - return single_open_size(file, proc_scsi_show, pde_data(inode), - 4 * PAGE_SIZE); + return seq_preallocate(file->private_data, 4 * PAGE_SIZE); } static struct scsi_proc_entry * @@ -140,10 +140,8 @@ scsi_template_proc_dir(const struct scsi_host_template *sht) EXPORT_SYMBOL_GPL(scsi_template_proc_dir); static const struct proc_ops proc_scsi_ops = { + .proc_show = proc_scsi_show, .proc_open = proc_scsi_host_open, - .proc_release = single_release, - .proc_read = seq_read, - .proc_lseek = seq_lseek, .proc_write = proc_scsi_host_write }; -- 2.53.0 Implement ->proc_write_buf() for /proc/scsi/scsi, /proc/scsi/device_info and the host files so procfs copies in what is written before it pins the entry, and parse what was written from that copy. /proc/scsi/scsi and device_info take up to PAGE_SIZE bytes like before. The host files pass the copy to ->write_info(), writes to them that are larger than PAGE_SIZE - 1 bytes fail with -EINVAL from procfs instead of -EOVERFLOW. Signed-off-by: Christian Brauner (Amutable) --- drivers/scsi/scsi_devinfo.c | 29 +++++----------------------- drivers/scsi/scsi_proc.c | 47 +++++++++++---------------------------------- 2 files changed, 16 insertions(+), 60 deletions(-) diff --git a/drivers/scsi/scsi_devinfo.c b/drivers/scsi/scsi_devinfo.c index 0b8f5ff40543..d9093851b23e 100644 --- a/drivers/scsi/scsi_devinfo.c +++ b/drivers/scsi/scsi_devinfo.c @@ -678,39 +678,20 @@ static const struct seq_operations scsi_devinfo_seq_ops = { * To use, echo "vendor:model:flag" > /proc/scsi/device_info */ static ssize_t proc_scsi_devinfo_write(struct file *file, - const char __user *buf, + char *buffer, size_t length, loff_t *ppos) { - char *buffer; - ssize_t err = length; - - if (!buf || length>PAGE_SIZE) + if (length == PAGE_SIZE && buffer[PAGE_SIZE-1]) return -EINVAL; - buffer = kmalloc(PAGE_SIZE, GFP_KERNEL); - if (!buffer) - return -ENOMEM; - if (copy_from_user(buffer, buf, length)) { - err =-EFAULT; - goto out; - } - - if (length < PAGE_SIZE) - buffer[length] = '\0'; - else if (buffer[PAGE_SIZE-1]) { - err = -EINVAL; - goto out; - } scsi_dev_info_list_add_str(buffer); - -out: - kfree(buffer); - return err; + return length; } static const struct proc_ops scsi_devinfo_proc_ops = { .proc_seq_ops = &scsi_devinfo_seq_ops, - .proc_write = proc_scsi_devinfo_write, + .proc_write_buf = proc_scsi_devinfo_write, + .proc_write_size = PAGE_SIZE, }; #endif /* CONFIG_SCSI_PROC_FS */ diff --git a/drivers/scsi/scsi_proc.c b/drivers/scsi/scsi_proc.c index 59229bf9938e..5962e671cc83 100644 --- a/drivers/scsi/scsi_proc.c +++ b/drivers/scsi/scsi_proc.c @@ -62,30 +62,18 @@ struct scsi_proc_entry { unsigned int present; }; -static ssize_t proc_scsi_host_write(struct file *file, const char __user *buf, +static ssize_t proc_scsi_host_write(struct file *file, char *buf, size_t count, loff_t *ppos) { struct Scsi_Host *shost = pde_data(file_inode(file)); - ssize_t ret = -ENOMEM; - char *page; - + if (count > PROC_BLOCK_SIZE) return -EOVERFLOW; if (!shost->hostt->write_info) return -EINVAL; - page = kmalloc(PAGE_SIZE, GFP_KERNEL); - if (page) { - ret = -EFAULT; - if (copy_from_user(page, buf, count)) - goto out; - page[count] = '\0'; - ret = shost->hostt->write_info(shost, page, count); - } -out: - kfree(page); - return ret; + return shost->hostt->write_info(shost, buf, count); } static int proc_scsi_show(struct seq_file *m, void *v) @@ -142,7 +130,7 @@ EXPORT_SYMBOL_GPL(scsi_template_proc_dir); static const struct proc_ops proc_scsi_ops = { .proc_show = proc_scsi_show, .proc_open = proc_scsi_host_open, - .proc_write = proc_scsi_host_write + .proc_write_buf = proc_scsi_host_write }; /** @@ -386,8 +374,8 @@ static int scsi_remove_single_device(uint host, uint channel, uint id, uint lun) /** * proc_scsi_write - handle writes to /proc/scsi/scsi * @file: not used - * @buf: buffer to write - * @length: length of buf, at most PAGE_SIZE + * @buffer: buffer to write + * @length: length of buffer, at most PAGE_SIZE * @ppos: not used * * Description: this provides a legacy mechanism to add or remove devices by @@ -402,32 +390,20 @@ static int scsi_remove_single_device(uint host, uint channel, uint id, uint lun) */ -static ssize_t proc_scsi_write(struct file *file, const char __user *buf, +static ssize_t proc_scsi_write(struct file *file, char *buffer, size_t length, loff_t *ppos) { int host, channel, id, lun; - char *buffer, *end, *p; + char *end, *p; int err; - if (!buf || length > PAGE_SIZE) - return -EINVAL; - - buffer = kmalloc(PAGE_SIZE, GFP_KERNEL); - if (!buffer) - return -ENOMEM; - - err = -EFAULT; - if (copy_from_user(buffer, buf, length)) - goto out; - err = -EINVAL; if (length < PAGE_SIZE) { end = buffer + length; - *end = '\0'; } else { end = buffer + PAGE_SIZE - 1; if (*end) - goto out; + return err; } /* @@ -466,8 +442,6 @@ static ssize_t proc_scsi_write(struct file *file, const char __user *buf, if (!err) err = length; - out: - kfree(buffer); return err; } @@ -521,7 +495,8 @@ static const struct seq_operations scsi_seq_ops = { static const struct proc_ops scsi_scsi_proc_ops = { .proc_seq_ops = &scsi_seq_ops, - .proc_write = proc_scsi_write, + .proc_write_buf = proc_scsi_write, + .proc_write_size = PAGE_SIZE, }; /** -- 2.53.0 Write to /proc/scsi/scsi_debug/ from a buffer whose page is backed by userfaultfd and remove the host while procfs copies in what is written: - removing the entry doesn't wait for the writer - the write fails with -EIO once the fault is resolved The test loads scsi_debug if it isn't there and skips without it. Signed-off-by: Christian Brauner (Amutable) --- tools/testing/selftests/proc/.gitignore | 1 + tools/testing/selftests/proc/Makefile | 1 + tools/testing/selftests/proc/config | 3 + .../selftests/proc/proc-remove-slow-writer.c | 203 +++++++++++++++++++++ 4 files changed, 208 insertions(+) diff --git a/tools/testing/selftests/proc/.gitignore b/tools/testing/selftests/proc/.gitignore index e2566cc0ae07..51969f81afc5 100644 --- a/tools/testing/selftests/proc/.gitignore +++ b/tools/testing/selftests/proc/.gitignore @@ -24,6 +24,7 @@ /proc-remove-slow-sysctl /proc-remove-slow-reader /proc-remove-slow-readlink +/proc-remove-slow-writer /read /self /setns-dcache diff --git a/tools/testing/selftests/proc/Makefile b/tools/testing/selftests/proc/Makefile index 6a9bdc999f54..aecec6b93173 100644 --- a/tools/testing/selftests/proc/Makefile +++ b/tools/testing/selftests/proc/Makefile @@ -35,5 +35,6 @@ TEST_GEN_PROGS += proc-pidns TEST_GEN_PROGS += proc-remove-slow-sysctl TEST_GEN_PROGS += proc-remove-slow-reader TEST_GEN_PROGS += proc-remove-slow-readlink +TEST_GEN_PROGS += proc-remove-slow-writer include ../lib.mk diff --git a/tools/testing/selftests/proc/config b/tools/testing/selftests/proc/config index 7b0e1d9be1a2..c7c61a2d7927 100644 --- a/tools/testing/selftests/proc/config +++ b/tools/testing/selftests/proc/config @@ -5,3 +5,6 @@ CONFIG_USERFAULTFD=y CONFIG_SOUND=y CONFIG_SND=y CONFIG_SND_DUMMY=m +CONFIG_SCSI=y +CONFIG_SCSI_PROC_FS=y +CONFIG_SCSI_DEBUG=m diff --git a/tools/testing/selftests/proc/proc-remove-slow-writer.c b/tools/testing/selftests/proc/proc-remove-slow-writer.c new file mode 100644 index 000000000000..f69ca75ba87c --- /dev/null +++ b/tools/testing/selftests/proc/proc-remove-slow-writer.c @@ -0,0 +1,203 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * A write that faults while procfs copies it in must not hold up removal of + * the /proc entry. Removing a scsi_debug host removes its + * /proc/scsi/scsi_debug/ file. + */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest_harness.h" + +#define ADD_HOST "/sys/bus/pseudo/drivers/scsi_debug/add_host" +#define HOST_DIR "/proc/scsi/scsi_debug" +#define TIMEOUT_MS 5000 +#define MAX_HOSTS 4096 +#define HOST_BITS (8 * sizeof(unsigned long)) + +struct writer { + int fd; + char *buf; + ssize_t ret; + int err; +}; + +struct remover { + int done; + int ret; +}; + +/* a positive @nr adds hosts, a negative one removes the last ones added */ +static int add_host(int nr) +{ + char s[16]; + int fd, len, ret; + + fd = open(ADD_HOST, O_WRONLY | O_CLOEXEC); + if (fd < 0) + return -errno; + len = snprintf(s, sizeof(s), "%d", nr); + ret = write(fd, s, len) == len ? 0 : -errno; + close(fd); + return ret; +} + +/* the numbers of the hosts in /proc/scsi/scsi_debug as a bitmap */ +static int list_hosts(unsigned long *hosts) +{ + struct dirent *d; + DIR *dir; + + memset(hosts, 0, MAX_HOSTS / 8); + dir = opendir(HOST_DIR); + if (!dir) + /* the directory comes with the first host and goes with the last */ + return errno == ENOENT ? 0 : -errno; + while ((d = readdir(dir))) { + char *end; + long nr = strtol(d->d_name, &end, 10); + + if (end != d->d_name && !*end && nr >= 0 && nr < MAX_HOSTS) + hosts[nr / HOST_BITS] |= 1UL << (nr % HOST_BITS); + } + closedir(dir); + return 0; +} + +/* the host that is in @after but wasn't in @before */ +static int new_host(const unsigned long *before, const unsigned long *after) +{ + for (int i = 0; i < MAX_HOSTS / HOST_BITS; i++) { + unsigned long new = after[i] & ~before[i]; + + if (new) + return i * HOST_BITS + __builtin_ctzl(new); + } + return -1; +} + +static void *write_thread(void *arg) +{ + struct writer *w = arg; + + w->ret = write(w->fd, w->buf, 2); + w->err = errno; + return NULL; +} + +static void *remove_thread(void *arg) +{ + struct remover *r = arg; + ssize_t ret; + + r->ret = add_host(-1); + ret = write(r->done, "x", 1); + return (void *)ret; +} + +FIXTURE(host) { + char path[PATH_MAX]; + int host; +}; + +FIXTURE_SETUP(host) +{ + unsigned long before[MAX_HOSTS / HOST_BITS]; + unsigned long after[MAX_HOSTS / HOST_BITS]; + + self->host = -1; + if (access(ADD_HOST, W_OK) && system("modprobe -q scsi_debug add_host=0")) + SKIP(return, "no scsi_debug"); + if (access(ADD_HOST, W_OK)) + SKIP(return, ADD_HOST ": %s", strerror(errno)); + + /* host numbers are reused, so look for the one that's new */ + ASSERT_EQ(list_hosts(before), 0); + ASSERT_EQ(add_host(1), 0); + ASSERT_EQ(list_hosts(after), 0); + self->host = new_host(before, after); + ASSERT_GE(self->host, 0); + snprintf(self->path, sizeof(self->path), HOST_DIR "/%d", self->host); +} + +FIXTURE_TEARDOWN(host) +{ + if (self->host >= 0 && !access(self->path, F_OK)) + add_host(-1); +} + +TEST_F(host, remove_while_writer_faults) +{ + struct uffdio_api api = { .api = UFFD_API }; + struct uffdio_register reg = {}; + struct uffdio_zeropage zero = {}; + long psz = sysconf(_SC_PAGESIZE); + struct writer w = {}; + struct remover r = {}; + struct uffd_msg msg; + struct pollfd pfd; + pthread_t wt, rt; + int uffd, done[2]; + char *buf; + + w.fd = open(self->path, O_WRONLY | O_CLOEXEC); + if (w.fd < 0) + SKIP(return, "%s: %s", self->path, strerror(errno)); + + uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK); + if (uffd < 0) + SKIP(return, "userfaultfd: %s", strerror(errno)); + ASSERT_EQ(ioctl(uffd, UFFDIO_API, &api), 0); + + buf = mmap(NULL, psz, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + ASSERT_NE(buf, MAP_FAILED); + reg.range.start = (unsigned long)buf; + reg.range.len = psz; + reg.mode = UFFDIO_REGISTER_MODE_MISSING; + ASSERT_EQ(ioctl(uffd, UFFDIO_REGISTER, ®), 0); + + w.buf = buf; + ASSERT_EQ(pthread_create(&wt, NULL, write_thread, &w), 0); + + /* The writer now sleeps while what it writes is copied in. */ + pfd = (struct pollfd){ .fd = uffd, .events = POLLIN }; + ASSERT_EQ(poll(&pfd, 1, TIMEOUT_MS), 1); + ASSERT_EQ(read(uffd, &msg, sizeof(msg)), sizeof(msg)); + ASSERT_EQ(msg.event, UFFD_EVENT_PAGEFAULT); + + /* Removing the host removes the file that is being written. */ + ASSERT_EQ(pipe2(done, O_CLOEXEC), 0); + r.done = done[1]; + ASSERT_EQ(pthread_create(&rt, NULL, remove_thread, &r), 0); + pfd = (struct pollfd){ .fd = done[0], .events = POLLIN }; + EXPECT_EQ(poll(&pfd, 1, TIMEOUT_MS), 1) { + TH_LOG("removing %s waited for the writer", self->path); + } + + zero.range.start = (unsigned long)buf; + zero.range.len = psz; + ASSERT_EQ(ioctl(uffd, UFFDIO_ZEROPAGE, &zero), 0); + ASSERT_EQ(pthread_join(wt, NULL), 0); + ASSERT_EQ(pthread_join(rt, NULL), 0); + EXPECT_EQ(r.ret, 0); + + /* The entry was gone by the time the write got to it. */ + EXPECT_EQ(w.ret, -1); + EXPECT_EQ(w.err, EIO); +} + +TEST_HARNESS_MAIN -- 2.53.0 Pass the integer they show as the data of the entry and declare the show function, procfs then opens and frees the seq_file. That makes the open functions unnecessary. Signed-off-by: Christian Brauner (Amutable) --- drivers/scsi/sg.c | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c index 3f9e08725602..c7386fbe5580 100644 --- a/drivers/scsi/sg.c +++ b/drivers/scsi/sg.c @@ -2287,26 +2287,18 @@ sg_get_dev(int dev) #ifdef CONFIG_SCSI_PROC_FS static int sg_proc_seq_show_int(struct seq_file *s, void *v); -static int sg_proc_single_open_adio(struct inode *inode, struct file *file); static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer, size_t count, loff_t *off); static const struct proc_ops adio_proc_ops = { - .proc_open = sg_proc_single_open_adio, - .proc_read = seq_read, - .proc_lseek = seq_lseek, + .proc_show = sg_proc_seq_show_int, .proc_write = sg_proc_write_adio, - .proc_release = single_release, }; -static int sg_proc_single_open_dressz(struct inode *inode, struct file *file); static ssize_t sg_proc_write_dressz(struct file *filp, const char __user *buffer, size_t count, loff_t *off); static const struct proc_ops dressz_proc_ops = { - .proc_open = sg_proc_single_open_dressz, - .proc_read = seq_read, - .proc_lseek = seq_lseek, + .proc_show = sg_proc_seq_show_int, .proc_write = sg_proc_write_dressz, - .proc_release = single_release, }; static int sg_proc_seq_show_version(struct seq_file *s, void *v); @@ -2347,9 +2339,11 @@ sg_proc_init(void) if (!p) return 1; - proc_create("allow_dio", S_IRUGO | S_IWUSR, p, &adio_proc_ops); + proc_create_data("allow_dio", S_IRUGO | S_IWUSR, p, &adio_proc_ops, + &sg_allow_dio); proc_create_seq("debug", S_IRUGO, p, &debug_seq_ops); - proc_create("def_reserved_size", S_IRUGO | S_IWUSR, p, &dressz_proc_ops); + proc_create_data("def_reserved_size", S_IRUGO | S_IWUSR, p, + &dressz_proc_ops, &def_reserved_size); proc_create_single("device_hdr", S_IRUGO, p, sg_proc_seq_show_devhdr); proc_create_seq("devices", S_IRUGO, p, &dev_seq_ops); proc_create_seq("device_strs", S_IRUGO, p, &devstrs_seq_ops); @@ -2364,11 +2358,6 @@ static int sg_proc_seq_show_int(struct seq_file *s, void *v) return 0; } -static int sg_proc_single_open_adio(struct inode *inode, struct file *file) -{ - return single_open(file, sg_proc_seq_show_int, &sg_allow_dio); -} - static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer, size_t count, loff_t *off) @@ -2385,11 +2374,6 @@ sg_proc_write_adio(struct file *filp, const char __user *buffer, return count; } -static int sg_proc_single_open_dressz(struct inode *inode, struct file *file) -{ - return single_open(file, sg_proc_seq_show_int, &def_reserved_size); -} - static ssize_t sg_proc_write_dressz(struct file *filp, const char __user *buffer, size_t count, loff_t *off) -- 2.53.0 Implement ->proc_write_buf() for allow_dio and def_reserved_size so procfs copies in what is written before it pins the entry, and parse the number from that copy. kstrtoul_from_user() looked at the first 66 bytes of whatever was written. Now the whole write is parsed, and anything larger than PAGE_SIZE - 1 bytes fails with -EINVAL from procfs before the capability check. Signed-off-by: Christian Brauner (Amutable) --- drivers/scsi/sg.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c index c7386fbe5580..87569e302f26 100644 --- a/drivers/scsi/sg.c +++ b/drivers/scsi/sg.c @@ -2287,18 +2287,18 @@ sg_get_dev(int dev) #ifdef CONFIG_SCSI_PROC_FS static int sg_proc_seq_show_int(struct seq_file *s, void *v); -static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer, +static ssize_t sg_proc_write_adio(struct file *filp, char *buffer, size_t count, loff_t *off); static const struct proc_ops adio_proc_ops = { .proc_show = sg_proc_seq_show_int, - .proc_write = sg_proc_write_adio, + .proc_write_buf = sg_proc_write_adio, }; static ssize_t sg_proc_write_dressz(struct file *filp, - const char __user *buffer, size_t count, loff_t *off); + char *buffer, size_t count, loff_t *off); static const struct proc_ops dressz_proc_ops = { .proc_show = sg_proc_seq_show_int, - .proc_write = sg_proc_write_dressz, + .proc_write_buf = sg_proc_write_dressz, }; static int sg_proc_seq_show_version(struct seq_file *s, void *v); @@ -2359,7 +2359,7 @@ static int sg_proc_seq_show_int(struct seq_file *s, void *v) } static ssize_t -sg_proc_write_adio(struct file *filp, const char __user *buffer, +sg_proc_write_adio(struct file *filp, char *buffer, size_t count, loff_t *off) { int err; @@ -2367,7 +2367,7 @@ sg_proc_write_adio(struct file *filp, const char __user *buffer, if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) return -EACCES; - err = kstrtoul_from_user(buffer, count, 0, &num); + err = kstrtoul(buffer, 0, &num); if (err) return err; sg_allow_dio = num ? 1 : 0; @@ -2375,7 +2375,7 @@ sg_proc_write_adio(struct file *filp, const char __user *buffer, } static ssize_t -sg_proc_write_dressz(struct file *filp, const char __user *buffer, +sg_proc_write_dressz(struct file *filp, char *buffer, size_t count, loff_t *off) { int err; @@ -2384,7 +2384,7 @@ sg_proc_write_dressz(struct file *filp, const char __user *buffer, if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) return -EACCES; - err = kstrtoul_from_user(buffer, count, 0, &k); + err = kstrtoul(buffer, 0, &k); if (err) return err; if (k <= 1048576) { /* limit "big buff" to 1 MB */ -- 2.53.0 Move the work of esas2r_ioctl_handler() into esas2r_ioctl_buf() which works on a kernel copy of struct atto_express_ioctl. The SCSI ioctl and the character device keep copying in and out in esas2r_ioctl_handler(). The procfs file implements ->proc_ioctl_buf() with esas2r_ioctl_buf() and lists every command from EXPRESS_IOCTL_MIN to EXPRESS_IOCTL_MAX, so procfs copies the struct in before it pins the entry and back out after unpinning it. Commands outside of that range fail with -ENOTTY instead of -ENOTSUPP now, a NULL argument with -EFAULT. Signed-off-by: Christian Brauner (Amutable) --- drivers/scsi/esas2r/esas2r.h | 2 ++ drivers/scsi/esas2r/esas2r_ioctl.c | 58 ++++++++++++++++++++++---------------- drivers/scsi/esas2r/esas2r_main.c | 44 +++++++++++++++++++++++------ 3 files changed, 72 insertions(+), 32 deletions(-) diff --git a/drivers/scsi/esas2r/esas2r.h b/drivers/scsi/esas2r/esas2r.h index 10101f50b282..4d75b8adbb3b 100644 --- a/drivers/scsi/esas2r/esas2r.h +++ b/drivers/scsi/esas2r/esas2r.h @@ -964,6 +964,8 @@ struct esas2r_adapter { const char *esas2r_info(struct Scsi_Host *); int esas2r_write_params(struct esas2r_adapter *a, struct esas2r_request *rq, struct esas2r_sas_nvram *data); +int esas2r_ioctl_buf(void *hostdata, unsigned int cmd, + struct atto_express_ioctl *ioctl); int esas2r_ioctl_handler(void *hostdata, unsigned int cmd, void __user *arg); int esas2r_ioctl(struct scsi_device *dev, unsigned int cmd, void __user *arg); u8 handle_hba_ioctl(struct esas2r_adapter *a, diff --git a/drivers/scsi/esas2r/esas2r_ioctl.c b/drivers/scsi/esas2r/esas2r_ioctl.c index f99318f61c93..eb39ba8263b3 100644 --- a/drivers/scsi/esas2r/esas2r_ioctl.c +++ b/drivers/scsi/esas2r/esas2r_ioctl.c @@ -1279,37 +1279,25 @@ int esas2r_write_params(struct esas2r_adapter *a, struct esas2r_request *rq, } -/* This function only cares about ATTO-specific ioctls (atto_express_ioctl) */ -int esas2r_ioctl_handler(void *hostdata, unsigned int cmd, void __user *arg) +/* + * This function only cares about ATTO-specific ioctls (atto_express_ioctl). + * @ioctl is a kernel copy of what the caller passed. It goes back to the + * caller unless this fails. + */ +int esas2r_ioctl_buf(void *hostdata, unsigned int cmd, + struct atto_express_ioctl *ioctl) { - struct atto_express_ioctl *ioctl = NULL; struct esas2r_adapter *a; struct esas2r_request *rq; u16 code; int err; - esas2r_log(ESAS2R_LOG_DEBG, "ioctl (%p, %x, %p)", hostdata, cmd, arg); - - if ((arg == NULL) - || (cmd < EXPRESS_IOCTL_MIN) - || (cmd > EXPRESS_IOCTL_MAX)) - return -ENOTSUPP; - - ioctl = memdup_user(arg, sizeof(struct atto_express_ioctl)); - if (IS_ERR(ioctl)) { - esas2r_log(ESAS2R_LOG_WARN, - "ioctl_handler access_ok failed for cmd %u, address %p", - cmd, arg); - return PTR_ERR(ioctl); - } - /* verify the signature */ if (memcmp(ioctl->header.signature, EXPRESS_IOCTL_SIGNATURE, EXPRESS_IOCTL_SIGNATURE_SIZE) != 0) { esas2r_log(ESAS2R_LOG_WARN, "invalid signature"); - kfree(ioctl); return -ENOTSUPP; } @@ -1350,7 +1338,6 @@ int esas2r_ioctl_handler(void *hostdata, unsigned int cmd, void __user *arg) esas2r_adapters[ioctl->header.channel] == NULL) { ioctl->header.return_code = IOCTL_BAD_CHANNEL; esas2r_log(ESAS2R_LOG_WARN, "bad channel value"); - kfree(ioctl); return -ENOTSUPP; } @@ -1403,7 +1390,6 @@ int esas2r_ioctl_handler(void *hostdata, unsigned int cmd, void __user *arg) rq = esas2r_alloc_request(a); if (rq == NULL) { - kfree(ioctl); esas2r_log(ESAS2R_LOG_WARN, "could not allocate an internal request"); return -ENOMEM; @@ -1515,19 +1501,43 @@ ioctl_done: } + return 0; +} + +int esas2r_ioctl_handler(void *hostdata, unsigned int cmd, void __user *arg) +{ + struct atto_express_ioctl *ioctl __free(kfree) = NULL; + int err; + + esas2r_log(ESAS2R_LOG_DEBG, "ioctl (%p, %x, %p)", hostdata, cmd, arg); + + if ((arg == NULL) + || (cmd < EXPRESS_IOCTL_MIN) + || (cmd > EXPRESS_IOCTL_MAX)) + return -ENOTSUPP; + + ioctl = memdup_user(arg, sizeof(struct atto_express_ioctl)); + if (IS_ERR(ioctl)) { + esas2r_log(ESAS2R_LOG_WARN, + "ioctl_handler access_ok failed for cmd %u, address %p", + cmd, arg); + return PTR_ERR(ioctl); + } + + err = esas2r_ioctl_buf(hostdata, cmd, ioctl); + if (err) + return err; + /* Always copy the buffer back, if only to pick up the status */ err = copy_to_user(arg, ioctl, sizeof(struct atto_express_ioctl)); if (err != 0) { esas2r_log(ESAS2R_LOG_WARN, "ioctl_handler copy_to_user didn't copy everything (err %d, cmd %u)", err, cmd); - kfree(ioctl); return -EFAULT; } - kfree(ioctl); - return 0; } diff --git a/drivers/scsi/esas2r/esas2r_main.c b/drivers/scsi/esas2r/esas2r_main.c index 75a70ae9441b..720be2a0907d 100644 --- a/drivers/scsi/esas2r/esas2r_main.c +++ b/drivers/scsi/esas2r/esas2r_main.c @@ -613,14 +613,6 @@ static const struct file_operations esas2r_proc_fops = { .unlocked_ioctl = esas2r_proc_ioctl, }; -static const struct proc_ops esas2r_proc_ops = { - .proc_lseek = default_llseek, - .proc_ioctl = esas2r_proc_ioctl, -#ifdef CONFIG_COMPAT - .proc_compat_ioctl = compat_ptr_ioctl, -#endif -}; - static struct Scsi_Host *esas2r_proc_host; static int esas2r_proc_major; @@ -630,6 +622,42 @@ long esas2r_proc_ioctl(struct file *fp, unsigned int cmd, unsigned long arg) cmd, (void __user *)arg); } +static long esas2r_proc_ioctl_buf(struct file *fp, unsigned int cmd, void *arg) +{ + return esas2r_ioctl_buf(esas2r_proc_host->hostdata, cmd, arg); +} + +#define ESAS2R_PROC_IOCTL(_cmd) \ + PROC_IOCTL_ARG(_cmd, sizeof(struct atto_express_ioctl), \ + sizeof(struct atto_express_ioctl)) + +/* esas2r_ioctl_buf() takes every command from EXPRESS_IOCTL_MIN to _MAX. */ +static const struct proc_ioctl_arg esas2r_proc_ioctl_args[] = { + ESAS2R_PROC_IOCTL(EXPRESS_IOCTL_RW_FIRMWARE), + ESAS2R_PROC_IOCTL(EXPRESS_IOCTL_READ_PARAMS), + ESAS2R_PROC_IOCTL(EXPRESS_IOCTL_WRITE_PARAMS), + ESAS2R_PROC_IOCTL(EXPRESS_IOCTL_FC_API), + ESAS2R_PROC_IOCTL(EXPRESS_IOCTL_GET_CHANNELS), + ESAS2R_PROC_IOCTL(EXPRESS_IOCTL_CHAN_INFO), + ESAS2R_PROC_IOCTL(EXPRESS_IOCTL_DEFAULT_PARAMS), + ESAS2R_PROC_IOCTL(EXPRESS_ADDR_MEMORY), + ESAS2R_PROC_IOCTL(EXPRESS_RW_MEMORY), + ESAS2R_PROC_IOCTL(EXPRESS_TSDK_DUMP), + ESAS2R_PROC_IOCTL(EXPRESS_IOCTL_SMP), + ESAS2R_PROC_IOCTL(EXPRESS_CSMI), + ESAS2R_PROC_IOCTL(EXPRESS_IOCTL_HBA), + ESAS2R_PROC_IOCTL(EXPRESS_IOCTL_VDA), + ESAS2R_PROC_IOCTL(EXPRESS_IOCTL_GET_ID), + ESAS2R_PROC_IOCTL(EXPRESS_IOCTL_GET_MOD_INFO), +}; + +static const struct proc_ops esas2r_proc_ops = { + .proc_lseek = default_llseek, + .proc_ioctl_buf = esas2r_proc_ioctl_buf, + .proc_ioctl_args = esas2r_proc_ioctl_args, + .proc_nr_ioctl_args = ARRAY_SIZE(esas2r_proc_ioctl_args), +}; + static void __exit esas2r_exit(void) { esas2r_log(ESAS2R_LOG_INFO, "%s called", __func__); -- 2.53.0 Implement ->proc_ioctl_buf() so procfs copies the arguments in before it pins the entry and back out after unpinning it. The command numbers in encode the wrong sizes, so the table lists what is really copied: an int or the 16 bytes of the machine id out, and one or two ints in. A NULL argument fails with -EFAULT now instead of -EINVAL, and so does a bad pointer to SET_FAN from an unprivileged task, which got -EPERM before, since procfs copies the argument in before the entry's own checks run. 32-bit tasks can use the commands whose numbers don't depend on the size of size_t. Signed-off-by: Christian Brauner (Amutable) --- drivers/hwmon/dell-smm-hwmon.c | 48 +++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/drivers/hwmon/dell-smm-hwmon.c b/drivers/hwmon/dell-smm-hwmon.c index 492b2b6c6d7e..67b8c6e5752f 100644 --- a/drivers/hwmon/dell-smm-hwmon.c +++ b/drivers/hwmon/dell-smm-hwmon.c @@ -549,16 +549,13 @@ static int i8k_get_power_status(const struct dell_smm_data *data) * Procfs interface */ -static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg) +static long i8k_ioctl(struct file *fp, unsigned int cmd, void *arg) { struct dell_smm_data *data = pde_data(file_inode(fp)); - int __user *argp = (int __user *)arg; + int *argp = arg; int speed, err; int val = 0; - if (!argp) - return -EINVAL; - switch (cmd) { case I8K_BIOS_VERSION: if (!isdigit(data->bios_version[0]) || !isdigit(data->bios_version[1]) || @@ -568,17 +565,13 @@ static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg) val = (data->bios_version[0] << 16) | (data->bios_version[1] << 8) | data->bios_version[2]; - if (copy_to_user(argp, &val, sizeof(val))) - return -EFAULT; - + *argp = val; return 0; case I8K_MACHINE_ID: if (restricted && !capable(CAP_SYS_ADMIN)) return -EPERM; - if (copy_to_user(argp, data->bios_machineid, sizeof(data->bios_machineid))) - return -EFAULT; - + memcpy(argp, data->bios_machineid, sizeof(data->bios_machineid)); return 0; case I8K_FN_STATUS: val = i8k_get_fn_status(data); @@ -593,8 +586,7 @@ static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg) break; case I8K_GET_SPEED: - if (copy_from_user(&val, argp, sizeof(int))) - return -EFAULT; + val = *argp; if (val > U8_MAX || val < 0) return -EINVAL; @@ -603,8 +595,7 @@ static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg) break; case I8K_GET_FAN: - if (copy_from_user(&val, argp, sizeof(int))) - return -EFAULT; + val = *argp; if (val > U8_MAX || val < 0) return -EINVAL; @@ -616,14 +607,11 @@ static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg) if (restricted && !capable(CAP_SYS_ADMIN)) return -EPERM; - if (copy_from_user(&val, argp, sizeof(int))) - return -EFAULT; - + val = argp[0]; if (val > U8_MAX || val < 0) return -EINVAL; - if (copy_from_user(&speed, argp + 1, sizeof(int))) - return -EFAULT; + speed = argp[1]; speed = clamp_val(speed, 0, data->i8k_fan_max); @@ -643,12 +631,22 @@ static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg) if (val < 0) return val; - if (copy_to_user(argp, &val, sizeof(int))) - return -EFAULT; - + *argp = val; return 0; } +/* The command numbers don't encode what is copied, see . */ +static const struct proc_ioctl_arg i8k_ioctl_args[] = { + PROC_IOCTL_ARG(I8K_BIOS_VERSION, 0, sizeof(int)), + PROC_IOCTL_ARG(I8K_MACHINE_ID, 0, sizeof_field(struct dell_smm_data, bios_machineid)), + PROC_IOCTL_ARG(I8K_FN_STATUS, 0, sizeof(int)), + PROC_IOCTL_ARG(I8K_POWER_STATUS, 0, sizeof(int)), + PROC_IOCTL_ARG(I8K_GET_TEMP, 0, sizeof(int)), + PROC_IOCTL_ARG(I8K_GET_SPEED, sizeof(int), sizeof(int)), + PROC_IOCTL_ARG(I8K_GET_FAN, sizeof(int), sizeof(int)), + PROC_IOCTL_ARG(I8K_SET_FAN, 2 * sizeof(int), sizeof(int)), +}; + /* * Print the information for /proc/i8k. */ @@ -696,7 +694,9 @@ static int i8k_proc_show(struct seq_file *seq, void *offset) static const struct proc_ops i8k_proc_ops = { .proc_show = i8k_proc_show, - .proc_ioctl = i8k_ioctl, + .proc_ioctl_buf = i8k_ioctl, + .proc_ioctl_args = i8k_ioctl_args, + .proc_nr_ioctl_args = ARRAY_SIZE(i8k_ioctl_args), }; static void i8k_exit_procfs(void *param) -- 2.53.0 Implement ->proc_write_buf() for the files in /proc/viafb so procfs copies in what is written before it pins the entry, and parse what was written from that copy. Writes larger than PAGE_SIZE - 1 bytes fail with -EINVAL now, the output device files refused anything over 63 bytes already. kstrtou8_from_user() looked at the first 10 bytes of what was written to dfph and dfpl, now the whole write is parsed. Signed-off-by: Christian Brauner (Amutable) --- drivers/video/fbdev/via/viafbdev.c | 46 +++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/drivers/video/fbdev/via/viafbdev.c b/drivers/video/fbdev/via/viafbdev.c index 4b617e80a77c..94b303006915 100644 --- a/drivers/video/fbdev/via/viafbdev.c +++ b/drivers/video/fbdev/via/viafbdev.c @@ -1121,7 +1121,7 @@ static int viafb_dvp0_proc_show(struct seq_file *m, void *v) } static ssize_t viafb_dvp0_proc_write(struct file *file, - const char __user *buffer, size_t count, loff_t *pos) + char *buffer, size_t count, loff_t *pos) { char buf[20], *value, *pbuf; u8 reg_val = 0; @@ -1129,8 +1129,7 @@ static ssize_t viafb_dvp0_proc_write(struct file *file, if (count < 1) return -EINVAL; length = count > 20 ? 20 : count; - if (copy_from_user(&buf[0], buffer, length)) - return -EFAULT; + memcpy(&buf[0], buffer, length); buf[length - 1] = '\0'; /*Ensure end string */ pbuf = &buf[0]; for (i = 0; i < 3; i++) { @@ -1169,7 +1168,7 @@ static ssize_t viafb_dvp0_proc_write(struct file *file, static const struct proc_ops viafb_dvp0_proc_ops = { .proc_show = viafb_dvp0_proc_show, - .proc_write = viafb_dvp0_proc_write, + .proc_write_buf = viafb_dvp0_proc_write, }; static int viafb_dvp1_proc_show(struct seq_file *m, void *v) @@ -1183,7 +1182,7 @@ static int viafb_dvp1_proc_show(struct seq_file *m, void *v) } static ssize_t viafb_dvp1_proc_write(struct file *file, - const char __user *buffer, size_t count, loff_t *pos) + char *buffer, size_t count, loff_t *pos) { char buf[20], *value, *pbuf; u8 reg_val = 0; @@ -1191,8 +1190,7 @@ static ssize_t viafb_dvp1_proc_write(struct file *file, if (count < 1) return -EINVAL; length = count > 20 ? 20 : count; - if (copy_from_user(&buf[0], buffer, length)) - return -EFAULT; + memcpy(&buf[0], buffer, length); buf[length - 1] = '\0'; /*Ensure end string */ pbuf = &buf[0]; for (i = 0; i < 3; i++) { @@ -1225,7 +1223,7 @@ static ssize_t viafb_dvp1_proc_write(struct file *file, static const struct proc_ops viafb_dvp1_proc_ops = { .proc_show = viafb_dvp1_proc_show, - .proc_write = viafb_dvp1_proc_write, + .proc_write_buf = viafb_dvp1_proc_write, }; static int viafb_dfph_proc_show(struct seq_file *m, void *v) @@ -1237,11 +1235,11 @@ static int viafb_dfph_proc_show(struct seq_file *m, void *v) } static ssize_t viafb_dfph_proc_write(struct file *file, - const char __user *buffer, size_t count, loff_t *pos) + char *buffer, size_t count, loff_t *pos) { int err; u8 reg_val; - err = kstrtou8_from_user(buffer, count, 0, ®_val); + err = kstrtou8(buffer, 0, ®_val); if (err) return err; @@ -1251,7 +1249,7 @@ static ssize_t viafb_dfph_proc_write(struct file *file, static const struct proc_ops viafb_dfph_proc_ops = { .proc_show = viafb_dfph_proc_show, - .proc_write = viafb_dfph_proc_write, + .proc_write_buf = viafb_dfph_proc_write, }; static int viafb_dfpl_proc_show(struct seq_file *m, void *v) @@ -1263,11 +1261,11 @@ static int viafb_dfpl_proc_show(struct seq_file *m, void *v) } static ssize_t viafb_dfpl_proc_write(struct file *file, - const char __user *buffer, size_t count, loff_t *pos) + char *buffer, size_t count, loff_t *pos) { int err; u8 reg_val; - err = kstrtou8_from_user(buffer, count, 0, ®_val); + err = kstrtou8(buffer, 0, ®_val); if (err) return err; @@ -1277,7 +1275,7 @@ static ssize_t viafb_dfpl_proc_write(struct file *file, static const struct proc_ops viafb_dfpl_proc_ops = { .proc_show = viafb_dfpl_proc_show, - .proc_write = viafb_dfpl_proc_write, + .proc_write_buf = viafb_dfpl_proc_write, }; static int viafb_vt1636_proc_show(struct seq_file *m, void *v) @@ -1313,7 +1311,7 @@ static int viafb_vt1636_proc_show(struct seq_file *m, void *v) } static ssize_t viafb_vt1636_proc_write(struct file *file, - const char __user *buffer, size_t count, loff_t *pos) + char *buffer, size_t count, loff_t *pos) { char buf[30], *value, *pbuf; struct IODATA reg_val; @@ -1321,8 +1319,7 @@ static ssize_t viafb_vt1636_proc_write(struct file *file, if (count < 1) return -EINVAL; length = count > 30 ? 30 : count; - if (copy_from_user(&buf[0], buffer, length)) - return -EFAULT; + memcpy(&buf[0], buffer, length); buf[length - 1] = '\0'; /*Ensure end string */ pbuf = &buf[0]; switch (viaparinfo->chip_info->lvds_chip_info.lvds_chip_name) { @@ -1404,7 +1401,7 @@ static ssize_t viafb_vt1636_proc_write(struct file *file, static const struct proc_ops viafb_vt1636_proc_ops = { .proc_show = viafb_vt1636_proc_show, - .proc_write = viafb_vt1636_proc_write, + .proc_write_buf = viafb_vt1636_proc_write, }; #endif /* CONFIG_FB_VIA_DIRECT_PROCFS */ @@ -1416,7 +1413,7 @@ static int __maybe_unused viafb_sup_odev_proc_show(struct seq_file *m, void *v) return 0; } -static ssize_t odev_update(const char __user *buffer, size_t count, u32 *odev) +static ssize_t odev_update(const char *buffer, size_t count, u32 *odev) { char buf[64], *ptr = buf; u32 devices; @@ -1424,8 +1421,7 @@ static ssize_t odev_update(const char __user *buffer, size_t count, u32 *odev) if (count < 1 || count > 63) return -EINVAL; - if (copy_from_user(&buf[0], buffer, count)) - return -EFAULT; + memcpy(&buf[0], buffer, count); buf[count] = '\0'; add = buf[0] == '+'; sub = buf[0] == '-'; @@ -1452,7 +1448,7 @@ static int viafb_iga1_odev_proc_show(struct seq_file *m, void *v) } static ssize_t viafb_iga1_odev_proc_write(struct file *file, - const char __user *buffer, size_t count, loff_t *pos) + char *buffer, size_t count, loff_t *pos) { u32 dev_on, dev_off, dev_old, dev_new; ssize_t res; @@ -1473,7 +1469,7 @@ static ssize_t viafb_iga1_odev_proc_write(struct file *file, static const struct proc_ops viafb_iga1_odev_proc_ops = { .proc_show = viafb_iga1_odev_proc_show, - .proc_write = viafb_iga1_odev_proc_write, + .proc_write_buf = viafb_iga1_odev_proc_write, }; static int viafb_iga2_odev_proc_show(struct seq_file *m, void *v) @@ -1483,7 +1479,7 @@ static int viafb_iga2_odev_proc_show(struct seq_file *m, void *v) } static ssize_t viafb_iga2_odev_proc_write(struct file *file, - const char __user *buffer, size_t count, loff_t *pos) + char *buffer, size_t count, loff_t *pos) { u32 dev_on, dev_off, dev_old, dev_new; ssize_t res; @@ -1504,7 +1500,7 @@ static ssize_t viafb_iga2_odev_proc_write(struct file *file, static const struct proc_ops viafb_iga2_odev_proc_ops = { .proc_show = viafb_iga2_odev_proc_show, - .proc_write = viafb_iga2_odev_proc_write, + .proc_write_buf = viafb_iga2_odev_proc_write, }; #define IS_VT1636(lvds_chip) ((lvds_chip).lvds_chip_name == VT1636_LVDS) -- 2.53.0 Implement ->proc_write_buf() so procfs copies in what is written to /proc/driver/rndis- before it pins the entry, and handle the commands from that copy. Writes larger than PAGE_SIZE - 1 bytes fail with -EINVAL now. Signed-off-by: Christian Brauner (Amutable) --- drivers/usb/gadget/function/rndis.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/drivers/usb/gadget/function/rndis.c b/drivers/usb/gadget/function/rndis.c index 3c7bd30b8a3a..e05e0c6ae8fb 100644 --- a/drivers/usb/gadget/function/rndis.c +++ b/drivers/usb/gadget/function/rndis.c @@ -1138,7 +1138,7 @@ static int rndis_proc_show(struct seq_file *m, void *v) return 0; } -static ssize_t rndis_proc_write(struct file *file, const char __user *buffer, +static ssize_t rndis_proc_write(struct file *file, char *buffer, size_t count, loff_t *ppos) { rndis_params *p = pde_data(file_inode(file)); @@ -1146,9 +1146,8 @@ static ssize_t rndis_proc_write(struct file *file, const char __user *buffer, int i, fl_speed = 0; for (i = 0; i < count; i++) { - char c; - if (get_user(c, buffer)) - return -EFAULT; + char c = *buffer; + switch (c) { case '0': case '1': @@ -1185,7 +1184,7 @@ static ssize_t rndis_proc_write(struct file *file, const char __user *buffer, static const struct proc_ops rndis_proc_ops = { .proc_show = rndis_proc_show, - .proc_write = rndis_proc_write, + .proc_write_buf = rndis_proc_write, }; #define NAME_TEMPLATE "driver/rndis-%03d" -- 2.53.0 Implement ->proc_write_buf() so procfs copies in what is written before it pins the entry, and parse the level from that copy. Writes larger than PAGE_SIZE - 1 bytes fail with -EINVAL now, and a buffer that can't be read fails with -EFAULT instead of looking like it was written. Signed-off-by: Christian Brauner (Amutable) --- drivers/net/wireless/intel/ipw2x00/libipw_module.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/drivers/net/wireless/intel/ipw2x00/libipw_module.c b/drivers/net/wireless/intel/ipw2x00/libipw_module.c index 15d9a5efb581..08cc0d7f57c1 100644 --- a/drivers/net/wireless/intel/ipw2x00/libipw_module.c +++ b/drivers/net/wireless/intel/ipw2x00/libipw_module.c @@ -216,14 +216,13 @@ static int debug_level_proc_show(struct seq_file *m, void *v) } static ssize_t debug_level_proc_write(struct file *file, - const char __user *buffer, size_t count, loff_t *pos) + char *buffer, size_t count, loff_t *pos) { char buf[] = "0x00000000\n"; size_t len = min(sizeof(buf) - 1, count); unsigned long val; - if (copy_from_user(buf, buffer, len)) - return count; + memcpy(buf, buffer, len); buf[len] = 0; if (sscanf(buf, "%li", &val) != 1) printk(KERN_INFO DRV_NAME @@ -236,7 +235,7 @@ static ssize_t debug_level_proc_write(struct file *file, static const struct proc_ops debug_level_proc_ops = { .proc_show = debug_level_proc_show, - .proc_write = debug_level_proc_write, + .proc_write_buf = debug_level_proc_write, }; #endif /* CONFIG_LIBIPW_DEBUG */ -- 2.53.0 Implement ->proc_write_buf() so procfs copies in what is written before it pins the entry, and handle the keys from that copy. Writes larger than PAGE_SIZE - 1 bytes fail with -EINVAL now. They used to trigger the first key, or all of them in bulk mode. procfs allocates the copy, which is as small as the write, opening the file allocated as much already. Signed-off-by: Christian Brauner (Amutable) --- drivers/tty/sysrq.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/drivers/tty/sysrq.c b/drivers/tty/sysrq.c index f4571d05b496..99363e1b59ed 100644 --- a/drivers/tty/sysrq.c +++ b/drivers/tty/sysrq.c @@ -1200,17 +1200,14 @@ EXPORT_SYMBOL(unregister_sysrq_key); * However, if the first character is an underscore, * all characters are processed. */ -static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf, +static ssize_t write_sysrq_trigger(struct file *file, char *buf, size_t count, loff_t *ppos) { bool bulk = false; size_t i; for (i = 0; i < count; i++) { - char c; - - if (get_user(c, buf + i)) - return -EFAULT; + char c = buf[i]; if (c == '_') bulk = true; @@ -1225,7 +1222,7 @@ static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf, } static const struct proc_ops sysrq_trigger_proc_ops = { - .proc_write = write_sysrq_trigger, + .proc_write_buf = write_sysrq_trigger, .proc_lseek = noop_llseek, }; -- 2.53.0 Declare the show function and let procfs open and free the seq_file. ->proc_open() only sets up the private data and preallocates the buffer now and ->proc_release() no longer releases the seq_file. Reading an entry then no longer holds up its removal when the card goes away. Signed-off-by: Christian Brauner (Amutable) --- sound/core/info.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/sound/core/info.c b/sound/core/info.c index 982087bc8575..8f905f398a0d 100644 --- a/sound/core/info.c +++ b/sound/core/info.c @@ -346,9 +346,11 @@ static int snd_info_seq_show(struct seq_file *seq, void *p) return 0; } +/* procfs opens the seq_file */ static int snd_info_text_entry_open(struct inode *inode, struct file *file) { struct snd_info_entry *entry = pde_data(inode); + struct seq_file *m = file->private_data; struct snd_info_private_data *data; int err; @@ -362,13 +364,12 @@ static int snd_info_text_entry_open(struct inode *inode, struct file *file) err = -ENOMEM; goto error; } - if (entry->size) - err = single_open_size(file, snd_info_seq_show, data, - entry->size); - else - err = single_open(file, snd_info_seq_show, data); - if (err < 0) - goto error; + if (entry->size) { + err = seq_preallocate(m, entry->size); + if (err < 0) + goto error; + } + m->private = data; return 0; error: @@ -387,7 +388,6 @@ static int snd_info_text_entry_release(struct inode *inode, struct file *file) if (data->wbuffer && entry->c.text.write) entry->c.text.write(entry, data->wbuffer); - single_release(inode, file); kfree(data->rbuffer); if (data->wbuffer) { kvfree(data->wbuffer->buffer); @@ -401,11 +401,10 @@ static int snd_info_text_entry_release(struct inode *inode, struct file *file) static const struct proc_ops snd_info_text_entry_ops = { + .proc_show = snd_info_seq_show, .proc_open = snd_info_text_entry_open, .proc_release = snd_info_text_entry_release, .proc_write = snd_info_text_entry_write, - .proc_lseek = seq_lseek, - .proc_read = seq_read, }; static struct snd_info_entry *create_subdir(struct module *mod, -- 2.53.0 Implement ->proc_write_buf() for data and text entries so procfs copies in what is written before it pins the entry. Data entries take writes a page at a time with PROC_ENTRY_SHORT_WRITES. They used to get the whole write at once. ->write() of struct snd_info_entry_ops gets the kernel copy and opl4, its only user, writes it to the sample memory without bouncing it through a vmalloc() buffer of its own. Text entries take up to SND_INFO_TEXT_WRITE_MAX bytes at once like before. A single write that is larger fails with -EINVAL from procfs instead of -EIO now. Signed-off-by: Christian Brauner (Amutable) --- include/sound/info.h | 2 +- sound/core/info.c | 21 ++++++++++++--------- sound/drivers/opl4/opl4_proc.c | 11 +---------- 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/include/sound/info.h b/include/sound/info.h index adbc506860d6..32fc608d056c 100644 --- a/include/sound/info.h +++ b/include/sound/info.h @@ -42,7 +42,7 @@ struct snd_info_entry_ops { struct file *file, char __user *buf, size_t count, loff_t pos); ssize_t (*write)(struct snd_info_entry *entry, void *file_private_data, - struct file *file, const char __user *buf, + struct file *file, const char *buf, size_t count, loff_t pos); loff_t (*llseek)(struct snd_info_entry *entry, void *file_private_data, struct file *file, diff --git a/sound/core/info.c b/sound/core/info.c index 8f905f398a0d..09d4420202e5 100644 --- a/sound/core/info.c +++ b/sound/core/info.c @@ -160,7 +160,7 @@ static ssize_t snd_info_entry_read(struct file *file, char __user *buffer, return size; } -static ssize_t snd_info_entry_write(struct file *file, const char __user *buffer, +static ssize_t snd_info_entry_write(struct file *file, char *buffer, size_t count, loff_t * offset) { struct snd_info_private_data *data = file->private_data; @@ -277,9 +277,11 @@ static int snd_info_entry_release(struct inode *inode, struct file *file) static const struct proc_ops snd_info_entry_operations = { + .proc_flags = PROC_ENTRY_SHORT_WRITES, .proc_lseek = snd_info_entry_llseek, .proc_read = snd_info_entry_read, - .proc_write = snd_info_entry_write, + .proc_write_buf = snd_info_entry_write, + .proc_write_size = PAGE_SIZE, .proc_poll = snd_info_entry_poll, .proc_ioctl = snd_info_entry_ioctl, .proc_mmap = snd_info_entry_mmap, @@ -287,11 +289,13 @@ static const struct proc_ops snd_info_entry_operations = .proc_release = snd_info_entry_release, }; +/* don't handle too large text inputs */ +#define SND_INFO_TEXT_WRITE_MAX (16 * 1024) + /* * file ops for text proc files */ -static ssize_t snd_info_text_entry_write(struct file *file, - const char __user *buffer, +static ssize_t snd_info_text_entry_write(struct file *file, char *buffer, size_t count, loff_t *offset) { struct seq_file *m = file->private_data; @@ -307,8 +311,7 @@ static ssize_t snd_info_text_entry_write(struct file *file, if (!valid_pos(pos, count)) return -EIO; next = pos + count; - /* don't handle too large text inputs */ - if (next > 16 * 1024) + if (next > SND_INFO_TEXT_WRITE_MAX) return -EIO; guard(mutex)(&entry->access); buf = data->wbuffer; @@ -325,8 +328,7 @@ static ssize_t snd_info_text_entry_write(struct file *file, buf->buffer = nbuf; buf->len = PAGE_ALIGN(next); } - if (copy_from_user(buf->buffer + pos, buffer, count)) - return -EFAULT; + memcpy(buf->buffer + pos, buffer, count); buf->size = next; *offset = next; return count; @@ -404,7 +406,8 @@ static const struct proc_ops snd_info_text_entry_ops = .proc_show = snd_info_seq_show, .proc_open = snd_info_text_entry_open, .proc_release = snd_info_text_entry_release, - .proc_write = snd_info_text_entry_write, + .proc_write_buf = snd_info_text_entry_write, + .proc_write_size = SND_INFO_TEXT_WRITE_MAX, }; static struct snd_info_entry *create_subdir(struct module *mod, diff --git a/sound/drivers/opl4/opl4_proc.c b/sound/drivers/opl4/opl4_proc.c index fd0ba4704d9f..bdcae02a4dc2 100644 --- a/sound/drivers/opl4/opl4_proc.c +++ b/sound/drivers/opl4/opl4_proc.c @@ -54,21 +54,12 @@ static ssize_t snd_opl4_mem_proc_read(struct snd_info_entry *entry, static ssize_t snd_opl4_mem_proc_write(struct snd_info_entry *entry, void *file_private_data, struct file *file, - const char __user *_buf, + const char *buf, size_t count, loff_t pos) { struct snd_opl4 *opl4 = entry->private_data; - char *buf; - buf = vmalloc(count); - if (!buf) - return -ENOMEM; - if (copy_from_user(buf, _buf, count)) { - vfree(buf); - return -EFAULT; - } snd_opl4_write_memory(opl4, buf, pos, count); - vfree(buf); return count; } -- 2.53.0 Implement ->proc_read_buf() for data entries so procfs copies what is read to userspace after it unpinned the entry. ->read() of struct snd_info_entry_ops fills procfs' buffer now. opl4 reads the sample memory into it without a vmalloc() buffer of its own, gus, cs46xx, emu10k1, scarlett2, cs4281 and mixart copy into it instead of to userspace. The driver guide still told raw callbacks to copy to and from userspace, so update its example as well. Data entries ask for PROC_ENTRY_FULL_READS so that reading all of an entry at once keeps working. A read that faults on its buffer in a later piece returns the pieces before it now instead of failing with -EFAULT. Unbinding a card then no longer waits for a reader that faults on its buffer. Signed-off-by: Christian Brauner (Amutable) --- Documentation/sound/kernel-api/writing-an-alsa-driver.rst | 10 +++++----- include/sound/gus.h | 2 +- include/sound/info.h | 2 +- sound/core/info.c | 6 +++--- sound/drivers/opl4/opl4_proc.c | 12 +----------- sound/isa/gus/gus_dram.c | 7 +++---- sound/isa/gus/gus_mem_proc.c | 2 +- sound/pci/cs4281.c | 10 ++++------ sound/pci/cs46xx/cs46xx_lib.c | 5 ++--- sound/pci/emu10k1/emuproc.c | 8 +++----- sound/pci/mixart/mixart.c | 10 ++++------ sound/usb/mixer_scarlett2.c | 9 ++++----- 12 files changed, 32 insertions(+), 51 deletions(-) diff --git a/Documentation/sound/kernel-api/writing-an-alsa-driver.rst b/Documentation/sound/kernel-api/writing-an-alsa-driver.rst index 86b64de03140..d4b4753879b6 100644 --- a/Documentation/sound/kernel-api/writing-an-alsa-driver.rst +++ b/Documentation/sound/kernel-api/writing-an-alsa-driver.rst @@ -3679,9 +3679,10 @@ For raw data, ``size`` field must be set properly. This specifies the maximum size of the proc file access. The read/write callbacks of raw mode are more direct than the text mode. -You need to use a low-level I/O functions such as -:c:func:`copy_from_user()` and :c:func:`copy_to_user()` to transfer the -data:: +They get a kernel buffer that procfs copies to or from userspace, so they +just copy the data. The buffer holds at most a page and isn't zeroed, so +a read callback must return exactly what it copied into it; a larger +read comes back for the rest:: static ssize_t my_file_io_read(struct snd_info_entry *entry, void *file_private_data, @@ -3690,8 +3691,7 @@ data:: size_t count, loff_t pos) { - if (copy_to_user(buf, local_data + pos, count)) - return -EFAULT; + memcpy(buf, local_data + pos, count); return count; } diff --git a/include/sound/gus.h b/include/sound/gus.h index 3feb42627de1..1b504f516ebf 100644 --- a/include/sound/gus.h +++ b/include/sound/gus.h @@ -594,7 +594,7 @@ void snd_gf1_uart_resume(struct snd_gus_card *gus); /* gus_dram.c */ int snd_gus_dram_write(struct snd_gus_card *gus, char __user *ptr, unsigned int addr, unsigned int size); -int snd_gus_dram_read(struct snd_gus_card *gus, char __user *ptr, +int snd_gus_dram_read(struct snd_gus_card *gus, char *ptr, unsigned int addr, unsigned int size, int rom); /* gus_timer.c */ diff --git a/include/sound/info.h b/include/sound/info.h index 32fc608d056c..b4b5bdd6898d 100644 --- a/include/sound/info.h +++ b/include/sound/info.h @@ -39,7 +39,7 @@ struct snd_info_entry_ops { int (*release)(struct snd_info_entry *entry, unsigned short mode, void *file_private_data); ssize_t (*read)(struct snd_info_entry *entry, void *file_private_data, - struct file *file, char __user *buf, + struct file *file, char *buf, size_t count, loff_t pos); ssize_t (*write)(struct snd_info_entry *entry, void *file_private_data, struct file *file, const char *buf, diff --git a/sound/core/info.c b/sound/core/info.c index 09d4420202e5..9665600ca5ac 100644 --- a/sound/core/info.c +++ b/sound/core/info.c @@ -138,7 +138,7 @@ static loff_t snd_info_entry_llseek(struct file *file, loff_t offset, int orig) return offset; } -static ssize_t snd_info_entry_read(struct file *file, char __user *buffer, +static ssize_t snd_info_entry_read(struct file *file, char *buffer, size_t count, loff_t * offset) { struct snd_info_private_data *data = file->private_data; @@ -277,9 +277,9 @@ static int snd_info_entry_release(struct inode *inode, struct file *file) static const struct proc_ops snd_info_entry_operations = { - .proc_flags = PROC_ENTRY_SHORT_WRITES, + .proc_flags = PROC_ENTRY_SHORT_WRITES | PROC_ENTRY_FULL_READS, .proc_lseek = snd_info_entry_llseek, - .proc_read = snd_info_entry_read, + .proc_read_buf = snd_info_entry_read, .proc_write_buf = snd_info_entry_write, .proc_write_size = PAGE_SIZE, .proc_poll = snd_info_entry_poll, diff --git a/sound/drivers/opl4/opl4_proc.c b/sound/drivers/opl4/opl4_proc.c index bdcae02a4dc2..b8b282cb0a02 100644 --- a/sound/drivers/opl4/opl4_proc.c +++ b/sound/drivers/opl4/opl4_proc.c @@ -5,7 +5,6 @@ */ #include "opl4_local.h" -#include #include #include @@ -33,21 +32,12 @@ static int snd_opl4_mem_proc_release(struct snd_info_entry *entry, static ssize_t snd_opl4_mem_proc_read(struct snd_info_entry *entry, void *file_private_data, - struct file *file, char __user *_buf, + struct file *file, char *buf, size_t count, loff_t pos) { struct snd_opl4 *opl4 = entry->private_data; - char* buf; - buf = vmalloc(count); - if (!buf) - return -ENOMEM; snd_opl4_read_memory(opl4, buf, pos, count); - if (copy_to_user(_buf, buf, count)) { - vfree(buf); - return -EFAULT; - } - vfree(buf); return count; } diff --git a/sound/isa/gus/gus_dram.c b/sound/isa/gus/gus_dram.c index 50fe738ee3ea..98b94db8e819 100644 --- a/sound/isa/gus/gus_dram.c +++ b/sound/isa/gus/gus_dram.c @@ -45,7 +45,7 @@ int snd_gus_dram_write(struct snd_gus_card *gus, char __user *buffer, return snd_gus_dram_poke(gus, buffer, address, size); } -static int snd_gus_dram_peek(struct snd_gus_card *gus, char __user *_buffer, +static int snd_gus_dram_peek(struct snd_gus_card *gus, char *_buffer, unsigned int address, unsigned int size, int rom) { @@ -67,15 +67,14 @@ static int snd_gus_dram_peek(struct snd_gus_card *gus, char __user *_buffer, while (size2--) *pbuffer++ = snd_gf1_peek(gus, address++); } - if (copy_to_user(_buffer, buffer, size1)) - return -EFAULT; + memcpy(_buffer, buffer, size1); size -= size1; _buffer += size1; } return 0; } -int snd_gus_dram_read(struct snd_gus_card *gus, char __user *buffer, +int snd_gus_dram_read(struct snd_gus_card *gus, char *buffer, unsigned int address, unsigned int size, int rom) { diff --git a/sound/isa/gus/gus_mem_proc.c b/sound/isa/gus/gus_mem_proc.c index 04c212882718..c939bfd5a966 100644 --- a/sound/isa/gus/gus_mem_proc.c +++ b/sound/isa/gus/gus_mem_proc.c @@ -18,7 +18,7 @@ struct gus_proc_private { static ssize_t snd_gf1_mem_proc_dump(struct snd_info_entry *entry, void *file_private_data, - struct file *file, char __user *buf, + struct file *file, char *buf, size_t count, loff_t pos) { struct gus_proc_private *priv = entry->private_data; diff --git a/sound/pci/cs4281.c b/sound/pci/cs4281.c index b831f1d50750..1444e39fdbc1 100644 --- a/sound/pci/cs4281.c +++ b/sound/pci/cs4281.c @@ -1104,25 +1104,23 @@ static void snd_cs4281_proc_read(struct snd_info_entry *entry, static ssize_t snd_cs4281_BA0_read(struct snd_info_entry *entry, void *file_private_data, - struct file *file, char __user *buf, + struct file *file, char *buf, size_t count, loff_t pos) { struct cs4281 *chip = entry->private_data; - if (copy_to_user_fromio(buf, chip->ba0 + pos, count)) - return -EFAULT; + memcpy_fromio(buf, chip->ba0 + pos, count); return count; } static ssize_t snd_cs4281_BA1_read(struct snd_info_entry *entry, void *file_private_data, - struct file *file, char __user *buf, + struct file *file, char *buf, size_t count, loff_t pos) { struct cs4281 *chip = entry->private_data; - if (copy_to_user_fromio(buf, chip->ba1 + pos, count)) - return -EFAULT; + memcpy_fromio(buf, chip->ba1 + pos, count); return count; } diff --git a/sound/pci/cs46xx/cs46xx_lib.c b/sound/pci/cs46xx/cs46xx_lib.c index 797a5f09cade..2a4910e5b497 100644 --- a/sound/pci/cs46xx/cs46xx_lib.c +++ b/sound/pci/cs46xx/cs46xx_lib.c @@ -2741,13 +2741,12 @@ static inline void snd_cs46xx_remove_gameport(struct snd_cs46xx *chip) { } static ssize_t snd_cs46xx_io_read(struct snd_info_entry *entry, void *file_private_data, - struct file *file, char __user *buf, + struct file *file, char *buf, size_t count, loff_t pos) { struct snd_cs46xx_region *region = entry->private_data; - if (copy_to_user_fromio(buf, region->remap_addr + pos, count)) - return -EFAULT; + memcpy_fromio(buf, region->remap_addr + pos, count); return count; } diff --git a/sound/pci/emu10k1/emuproc.c b/sound/pci/emu10k1/emuproc.c index f6186b5be049..acacce976415 100644 --- a/sound/pci/emu10k1/emuproc.c +++ b/sound/pci/emu10k1/emuproc.c @@ -382,7 +382,7 @@ static void snd_emu10k1_proc_acode_read(struct snd_info_entry *entry, static ssize_t snd_emu10k1_fx8010_read(struct snd_info_entry *entry, void *file_private_data, - struct file *file, char __user *buf, + struct file *file, char *buf, size_t count, loff_t pos) { struct snd_emu10k1 *emu = entry->private_data; @@ -415,10 +415,8 @@ static ssize_t snd_emu10k1_fx8010_read(struct snd_info_entry *entry, } tmp[idx] = val; } - if (copy_to_user(buf, ((char *)tmp) + (pos & 3), count)) - res = -EFAULT; - else - res = count; + memcpy(buf, ((char *)tmp) + (pos & 3), count); + res = count; kfree(tmp); return res; } diff --git a/sound/pci/mixart/mixart.c b/sound/pci/mixart/mixart.c index f451554cff5f..c44001f0d320 100644 --- a/sound/pci/mixart/mixart.c +++ b/sound/pci/mixart/mixart.c @@ -1110,14 +1110,13 @@ static int snd_mixart_free(struct mixart_mgr *mgr) */ static ssize_t snd_mixart_BA0_read(struct snd_info_entry *entry, void *file_private_data, - struct file *file, char __user *buf, + struct file *file, char *buf, size_t count, loff_t pos) { struct mixart_mgr *mgr = entry->private_data; count = count & ~3; /* make sure the read size is a multiple of 4 bytes */ - if (copy_to_user_fromio(buf, MIXART_MEM(mgr, pos), count)) - return -EFAULT; + memcpy_fromio(buf, MIXART_MEM(mgr, pos), count); return count; } @@ -1126,14 +1125,13 @@ static ssize_t snd_mixart_BA0_read(struct snd_info_entry *entry, */ static ssize_t snd_mixart_BA1_read(struct snd_info_entry *entry, void *file_private_data, - struct file *file, char __user *buf, + struct file *file, char *buf, size_t count, loff_t pos) { struct mixart_mgr *mgr = entry->private_data; count = count & ~3; /* make sure the read size is a multiple of 4 bytes */ - if (copy_to_user_fromio(buf, MIXART_REG(mgr, pos), count)) - return -EFAULT; + memcpy_fromio(buf, MIXART_REG(mgr, pos), count); return count; } diff --git a/sound/usb/mixer_scarlett2.c b/sound/usb/mixer_scarlett2.c index ed5fe746d438..48557235c389 100644 --- a/sound/usb/mixer_scarlett2.c +++ b/sound/usb/mixer_scarlett2.c @@ -9763,7 +9763,7 @@ static ssize_t scarlett2_devmap_read( struct snd_info_entry *entry, void *file_private_data, struct file *file, - char __user *buf, + char *buf, size_t count, loff_t pos) { @@ -9785,7 +9785,7 @@ static ssize_t scarlett2_devmap_read( while (count > 0) { /* SCARLETT2_USB_GET_DEVMAP reads only on block boundaries, * so we need to read a whole block and copy the requested - * chunk to userspace. + * chunk out of it. */ __le32 req; @@ -9800,7 +9800,7 @@ static ssize_t scarlett2_devmap_read( block_size, entry->size - block_start); - /* size of the chunk to copy to userspace */ + /* size of the chunk to copy out of the block */ size_t copy_size = min_t(size_t, count, read_size - offset); /* request the block */ @@ -9810,8 +9810,7 @@ static ssize_t scarlett2_devmap_read( if (err < 0) return copied ? copied : err; - if (copy_to_user(buf, resp_buf + offset, copy_size)) - return -EFAULT; + memcpy(buf, resp_buf + offset, copy_size); buf += copy_size; pos += copy_size; -- 2.53.0 No driver implements ->ioctl() of struct snd_info_entry_ops. Signed-off-by: Christian Brauner (Amutable) --- include/sound/info.h | 2 -- sound/core/info.c | 13 ------------- 2 files changed, 15 deletions(-) diff --git a/include/sound/info.h b/include/sound/info.h index b4b5bdd6898d..f32e81ef0c8f 100644 --- a/include/sound/info.h +++ b/include/sound/info.h @@ -50,8 +50,6 @@ struct snd_info_entry_ops { __poll_t (*poll)(struct snd_info_entry *entry, void *file_private_data, struct file *file, poll_table *wait); - int (*ioctl)(struct snd_info_entry *entry, void *file_private_data, - struct file *file, unsigned int cmd, unsigned long arg); int (*mmap)(struct snd_info_entry *entry, void *file_private_data, struct inode *inode, struct file *file, struct vm_area_struct *vma); diff --git a/sound/core/info.c b/sound/core/info.c index 9665600ca5ac..639554bb17c8 100644 --- a/sound/core/info.c +++ b/sound/core/info.c @@ -201,18 +201,6 @@ static __poll_t snd_info_entry_poll(struct file *file, poll_table *wait) return mask; } -static long snd_info_entry_ioctl(struct file *file, unsigned int cmd, - unsigned long arg) -{ - struct snd_info_private_data *data = file->private_data; - struct snd_info_entry *entry = data->entry; - - if (!entry->c.ops->ioctl) - return -ENOTTY; - return entry->c.ops->ioctl(entry, data->file_private_data, - file, cmd, arg); -} - static int snd_info_entry_mmap(struct file *file, struct vm_area_struct *vma) { struct inode *inode = file_inode(file); @@ -283,7 +271,6 @@ static const struct proc_ops snd_info_entry_operations = .proc_write_buf = snd_info_entry_write, .proc_write_size = PAGE_SIZE, .proc_poll = snd_info_entry_poll, - .proc_ioctl = snd_info_entry_ioctl, .proc_mmap = snd_info_entry_mmap, .proc_open = snd_info_entry_open, .proc_release = snd_info_entry_release, -- 2.53.0