From 8d8ae17eb0de1fcdff6e7ddee3b641a16eefe8f6 Mon Sep 17 00:00:00 2001 From: Andy Shevchenko Date: Mon, 16 Oct 2023 16:31:33 +0300 Subject: parport: Use kasprintf() instead of fixed buffer formatting Improve readability and maintainability by replacing a hardcoded string allocation and formatting by the use of the kasprintf() helper. Signed-off-by: Andy Shevchenko Link: https://lore.kernel.org/r/20231016133135.1203643-2-andriy.shevchenko@linux.intel.com Signed-off-by: Greg Kroah-Hartman --- drivers/parport/procfs.c | 53 ++++++++---------------------------------------- drivers/parport/share.c | 15 ++++++-------- 2 files changed, 15 insertions(+), 53 deletions(-) (limited to 'drivers/parport') diff --git a/drivers/parport/procfs.c b/drivers/parport/procfs.c index 4e5b972c3e26..7aa99c65b934 100644 --- a/drivers/parport/procfs.c +++ b/drivers/parport/procfs.c @@ -32,13 +32,6 @@ #define PARPORT_MAX_TIMESLICE_VALUE ((unsigned long) HZ) #define PARPORT_MIN_SPINTIME_VALUE 1 #define PARPORT_MAX_SPINTIME_VALUE 1000 -/* - * PARPORT_BASE_* is the size of the known parts of the sysctl path - * in dev/partport/%s/devices/%s. "dev/parport/"(12), "/devices/"(9 - * and null char(1). - */ -#define PARPORT_BASE_PATH_SIZE 13 -#define PARPORT_BASE_DEVICES_PATH_SIZE 22 static int do_active_device(struct ctl_table *table, int write, void *result, size_t *lenp, loff_t *ppos) @@ -431,8 +424,7 @@ int parport_proc_register(struct parport *port) { struct parport_sysctl_table *t; char *tmp_dir_path; - size_t tmp_path_len, port_name_len; - int bytes_written, i, err = 0; + int i, err = 0; t = kmemdup(&parport_sysctl_template, sizeof(*t), GFP_KERNEL); if (t == NULL) @@ -446,35 +438,23 @@ int parport_proc_register(struct parport *port) t->vars[5 + i].extra2 = &port->probe_info[i]; } - port_name_len = strnlen(port->name, PARPORT_NAME_MAX_LEN); - /* - * Allocate a buffer for two paths: dev/parport/PORT and dev/parport/PORT/devices. - * We calculate for the second as that will give us enough for the first. - */ - tmp_path_len = PARPORT_BASE_DEVICES_PATH_SIZE + port_name_len; - tmp_dir_path = kzalloc(tmp_path_len, GFP_KERNEL); + tmp_dir_path = kasprintf(GFP_KERNEL, "dev/parport/%s/devices", port->name); if (!tmp_dir_path) { err = -ENOMEM; goto exit_free_t; } - bytes_written = snprintf(tmp_dir_path, tmp_path_len, - "dev/parport/%s/devices", port->name); - if (tmp_path_len <= bytes_written) { - err = -ENOENT; - goto exit_free_tmp_dir_path; - } t->devices_header = register_sysctl(tmp_dir_path, t->device_dir); if (t->devices_header == NULL) { err = -ENOENT; goto exit_free_tmp_dir_path; } - tmp_path_len = PARPORT_BASE_PATH_SIZE + port_name_len; - bytes_written = snprintf(tmp_dir_path, tmp_path_len, - "dev/parport/%s", port->name); - if (tmp_path_len <= bytes_written) { - err = -ENOENT; + kfree(tmp_dir_path); + + tmp_dir_path = kasprintf(GFP_KERNEL, "dev/parport/%s", port->name); + if (!tmp_dir_path) { + err = -ENOMEM; goto unregister_devices_h; } @@ -514,34 +494,22 @@ int parport_proc_unregister(struct parport *port) int parport_device_proc_register(struct pardevice *device) { - int bytes_written, err = 0; struct parport_device_sysctl_table *t; struct parport * port = device->port; - size_t port_name_len, device_name_len, tmp_dir_path_len; char *tmp_dir_path; + int err = 0; t = kmemdup(&parport_device_sysctl_template, sizeof(*t), GFP_KERNEL); if (t == NULL) return -ENOMEM; - port_name_len = strnlen(port->name, PARPORT_NAME_MAX_LEN); - device_name_len = strnlen(device->name, PATH_MAX); - /* Allocate a buffer for two paths: dev/parport/PORT/devices/DEVICE. */ - tmp_dir_path_len = PARPORT_BASE_DEVICES_PATH_SIZE + port_name_len + device_name_len; - tmp_dir_path = kzalloc(tmp_dir_path_len, GFP_KERNEL); + tmp_dir_path = kasprintf(GFP_KERNEL, "dev/parport/%s/devices/%s", port->name, device->name); if (!tmp_dir_path) { err = -ENOMEM; goto exit_free_t; } - bytes_written = snprintf(tmp_dir_path, tmp_dir_path_len, "dev/parport/%s/devices/%s", - port->name, device->name); - if (tmp_dir_path_len <= bytes_written) { - err = -ENOENT; - goto exit_free_path; - } - t->vars[0].data = &device->timeslice; t->sysctl_header = register_sysctl(tmp_dir_path, t->vars); @@ -554,9 +522,6 @@ int parport_device_proc_register(struct pardevice *device) kfree(tmp_dir_path); return 0; -exit_free_path: - kfree(tmp_dir_path); - exit_free_t: kfree(t); diff --git a/drivers/parport/share.c b/drivers/parport/share.c index 2d46b1d4fd69..8037bcd07bcf 100644 --- a/drivers/parport/share.c +++ b/drivers/parport/share.c @@ -438,7 +438,6 @@ struct parport *parport_register_port(unsigned long base, int irq, int dma, struct parport *tmp; int num; int device; - char *name; int ret; tmp = kzalloc(sizeof(struct parport), GFP_KERNEL); @@ -467,11 +466,6 @@ struct parport *parport_register_port(unsigned long base, int irq, int dma, atomic_set(&tmp->ref_count, 1); INIT_LIST_HEAD(&tmp->full_list); - name = kmalloc(PARPORT_NAME_MAX_LEN, GFP_KERNEL); - if (!name) { - kfree(tmp); - return NULL; - } /* Search for the lowest free parport number. */ spin_lock(&full_list_lock); @@ -487,11 +481,14 @@ struct parport *parport_register_port(unsigned long base, int irq, int dma, /* * Now that the portnum is known finish doing the Init. */ - sprintf(name, "parport%d", tmp->portnum = tmp->number); - tmp->name = name; + tmp->name = kasprintf(GFP_KERNEL, "parport%d", tmp->portnum); + if (!tmp->name) { + kfree(tmp); + return NULL; + } + dev_set_name(&tmp->bus_dev, tmp->name); tmp->bus_dev.bus = &parport_bus_type; tmp->bus_dev.release = free_port; - dev_set_name(&tmp->bus_dev, name); tmp->bus_dev.type = &parport_device_type; for (device = 0; device < 5; device++) -- cgit v1.2.3 From b8cb855d1b2ee6669e46c54a132024bd1dd0dcbb Mon Sep 17 00:00:00 2001 From: Andy Shevchenko Date: Mon, 16 Oct 2023 16:31:34 +0300 Subject: parport: Use list_for_each() helper Convert hard to read custom code to list_for_each(). No functional changes intended. Note, we may not use list_for_each_entry() as at the end of the list the iterator will point to an invalid entry and may not be dereferenced. Signed-off-by: Andy Shevchenko Link: https://lore.kernel.org/r/20231016133135.1203643-3-andriy.shevchenko@linux.intel.com Signed-off-by: Greg Kroah-Hartman --- drivers/parport/share.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'drivers/parport') diff --git a/drivers/parport/share.c b/drivers/parport/share.c index 8037bcd07bcf..38780f6a9119 100644 --- a/drivers/parport/share.c +++ b/drivers/parport/share.c @@ -469,9 +469,11 @@ struct parport *parport_register_port(unsigned long base, int irq, int dma, /* Search for the lowest free parport number. */ spin_lock(&full_list_lock); - for (l = all_ports.next, num = 0; l != &all_ports; l = l->next, num++) { + num = 0; + list_for_each(l, &all_ports) { struct parport *p = list_entry(l, struct parport, full_list); - if (p->number != num) + + if (p->number != num++) break; } tmp->portnum = tmp->number = num; -- cgit v1.2.3 From f7572e93d5f6bc4f659d2a9b603574cd126482d8 Mon Sep 17 00:00:00 2001 From: Andy Shevchenko Date: Mon, 16 Oct 2023 16:31:35 +0300 Subject: parport: Drop unneeded NULL or 0 assignments kzalloc() gives us a zeroed memory, no need to explicitly assing 0 or NULL or similar to the members of the data structure that has been allocated with the above mentioned API. Signed-off-by: Andy Shevchenko Link: https://lore.kernel.org/r/20231016133135.1203643-4-andriy.shevchenko@linux.intel.com Signed-off-by: Greg Kroah-Hartman --- drivers/parport/share.c | 4 ---- 1 file changed, 4 deletions(-) (limited to 'drivers/parport') diff --git a/drivers/parport/share.c b/drivers/parport/share.c index 38780f6a9119..a9a9cb0477ea 100644 --- a/drivers/parport/share.c +++ b/drivers/parport/share.c @@ -449,13 +449,9 @@ struct parport *parport_register_port(unsigned long base, int irq, int dma, tmp->irq = irq; tmp->dma = dma; tmp->muxport = tmp->daisy = tmp->muxsel = -1; - tmp->modes = 0; INIT_LIST_HEAD(&tmp->list); - tmp->devices = tmp->cad = NULL; - tmp->flags = 0; tmp->ops = ops; tmp->physport = tmp; - memset(tmp->probe_info, 0, 5 * sizeof(struct parport_device_info)); rwlock_init(&tmp->cad_lock); spin_lock_init(&tmp->waitlist_lock); spin_lock_init(&tmp->pardevice_lock); -- cgit v1.2.3 From 12280cc708f28357e7ad85bab2bcc3715e59a8f7 Mon Sep 17 00:00:00 2001 From: Andy Shevchenko Date: Wed, 18 Oct 2023 17:59:47 +0300 Subject: parport: Clean up resources correctly when parport_register_port() fails The smatch warns about uncleaned resources in case the parport_register_port() fails: parport_register_port() warn: '&tmp->full_list' not removed from list This is indeed an issue introduced when converting code to use kasprintf(). However, the whole kasprintf() dance in this case is not needed as dev_set_name() can handle the formatted input and produces the same result. So, the solution is to delegate name forming to the dev_set_name() and make device_register() error path to deal with error handling (via put_device() call). Fixes: 8d8ae17eb0de ("parport: Use kasprintf() instead of fixed buffer formatting") Reported-by: kernel test robot Reported-by: Dan Carpenter Closes: https://lore.kernel.org/r/202310180809.hepZB9k6-lkp@intel.com/ Signed-off-by: Andy Shevchenko Link: https://lore.kernel.org/r/20231018145948.1367648-1-andriy.shevchenko@linux.intel.com Signed-off-by: Greg Kroah-Hartman --- drivers/parport/share.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'drivers/parport') diff --git a/drivers/parport/share.c b/drivers/parport/share.c index a9a9cb0477ea..048a459ce4b0 100644 --- a/drivers/parport/share.c +++ b/drivers/parport/share.c @@ -361,7 +361,6 @@ static void free_port(struct device *dev) kfree(port->probe_info[d].description); } - kfree(port->name); kfree(port); } @@ -479,16 +478,13 @@ struct parport *parport_register_port(unsigned long base, int irq, int dma, /* * Now that the portnum is known finish doing the Init. */ - tmp->name = kasprintf(GFP_KERNEL, "parport%d", tmp->portnum); - if (!tmp->name) { - kfree(tmp); - return NULL; - } - dev_set_name(&tmp->bus_dev, tmp->name); + dev_set_name(&tmp->bus_dev, "parport%d", tmp->portnum); tmp->bus_dev.bus = &parport_bus_type; tmp->bus_dev.release = free_port; tmp->bus_dev.type = &parport_device_type; + tmp->name = dev_name(&tmp->bus_dev); + for (device = 0; device < 5; device++) /* assume the worst */ tmp->probe_info[device].class = PARPORT_CLASS_LEGACY; -- cgit v1.2.3 From 421359cbdbdcaea93b83e1e6b61eb261b75b1998 Mon Sep 17 00:00:00 2001 From: Andy Shevchenko Date: Wed, 18 Oct 2023 17:59:48 +0300 Subject: parport: Drop even more unneeded NULL or 0 assignments kzalloc() gives us a zeroed memory, no need to explicitly assing 0 or NULL or similar to the members of the data structure that has been allocated with the above mentioned API. Note, the initializstion of full_list member is not needed anymore as list_add_tail will rewrite the contents of the prev and next pointers. Signed-off-by: Andy Shevchenko Link: https://lore.kernel.org/r/20231018145948.1367648-2-andriy.shevchenko@linux.intel.com Signed-off-by: Greg Kroah-Hartman --- drivers/parport/share.c | 3 --- 1 file changed, 3 deletions(-) (limited to 'drivers/parport') diff --git a/drivers/parport/share.c b/drivers/parport/share.c index 048a459ce4b0..e21831d93305 100644 --- a/drivers/parport/share.c +++ b/drivers/parport/share.c @@ -459,7 +459,6 @@ struct parport *parport_register_port(unsigned long base, int irq, int dma, sema_init(&tmp->ieee1284.irq, 0); tmp->spintime = parport_default_spintime; atomic_set(&tmp->ref_count, 1); - INIT_LIST_HEAD(&tmp->full_list); /* Search for the lowest free parport number. */ @@ -489,8 +488,6 @@ struct parport *parport_register_port(unsigned long base, int irq, int dma, /* assume the worst */ tmp->probe_info[device].class = PARPORT_CLASS_LEGACY; - tmp->waithead = tmp->waittail = NULL; - ret = device_register(&tmp->bus_dev); if (ret) { put_device(&tmp->bus_dev); -- cgit v1.2.3