mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] nvme: work around -Wformat-security warning
@ 2026-09-16 11:44 Arnd Bergmann
  2026-09-17  3:40 ` Damien Le Moal
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Arnd Bergmann @ 2026-09-16 11:44 UTC (permalink / raw)
  To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
	Maurizio Lombardi
  Cc: Arnd Bergmann, Hannes Reinecke, Nilay Shroff, John Garry,
	Martin K. Petersen, linux-nvme, linux-kernel

From: Arnd Bergmann <arnd@arndb.de>

Passing a string variable into dev_set_name() causes a warning
when building with -Wformat-security enabled:

drivers/nvme/host/core.c: In function 'nvme_cdev_add':
drivers/nvme/host/core.c:3911:9: error: format not a string literal and no format arguments [-Werror=format-security]
 3911 |         ret = dev_set_name(cdev_device, name);

Remove the temporary strings and let dev_set_name() do the
same thing internally.

Fixes: 26acdaa357cd ("nvme: fix crash and memory leak during invalid cdev teardown")
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
 drivers/nvme/host/core.c      | 21 +++++++++------------
 drivers/nvme/host/multipath.c | 12 +++++-------
 drivers/nvme/host/nvme.h      |  6 +++---
 3 files changed, 17 insertions(+), 22 deletions(-)

diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index beea23d04a70..79f6a2c7923e 100644
--- a/drivers/nvme/host/core.c
+++ b/drivers/nvme/host/core.c
@@ -3931,9 +3931,9 @@ void nvme_cdev_del(struct cdev *cdev, struct device *cdev_device)
 	put_device(cdev_device);
 }
 
-int nvme_cdev_add(const char *name, struct cdev *cdev,
-		struct device *cdev_device,
-		const struct file_operations *fops, struct module *owner)
+int nvme_cdev_add(struct cdev *cdev, struct device *cdev_device,
+		const struct file_operations *fops, struct module *owner,
+		int ctrl, int head)
 {
 	int minor, ret;
 
@@ -3941,7 +3941,7 @@ int nvme_cdev_add(const char *name, struct cdev *cdev,
 	if (minor < 0)
 		return minor;
 
-	ret = dev_set_name(cdev_device, name);
+	ret = dev_set_name(cdev_device, "ng%dn%d", ctrl, head);
 	if (ret) {
 		ida_free(&nvme_ns_chr_minor_ida, minor);
 		return ret;
@@ -3982,17 +3982,14 @@ static const struct file_operations nvme_ns_chr_fops = {
 
 static void nvme_add_ns_cdev(struct nvme_ns *ns)
 {
-	char name[32];
-
 	ns->cdev_device.parent = ns->ctrl->device;
-	snprintf(name, sizeof(name), "ng%dn%d", ns->ctrl->instance,
-		 ns->head->instance);
 
 	nvme_get_ns(ns); /* Undone in nvme_cdev_rel() */
-	if (nvme_cdev_add(name, &ns->cdev, &ns->cdev_device,
-			&nvme_ns_chr_fops, ns->ctrl->ops->module)) {
-		dev_err(ns->ctrl->device, "Unable to create the %s device\n",
-			name);
+	if (nvme_cdev_add(&ns->cdev, &ns->cdev_device,
+			&nvme_ns_chr_fops, ns->ctrl->ops->module,
+			ns->ctrl->instance, ns->head->instance)) {
+		dev_err(ns->ctrl->device, "Unable to create the ng%dn%d device\n",
+			ns->ctrl->instance, ns->head->instance);
 		nvme_put_ns(ns);
 		return;
 	}
diff --git a/drivers/nvme/host/multipath.c b/drivers/nvme/host/multipath.c
index 75dbb58286a3..3d46c4f28a47 100644
--- a/drivers/nvme/host/multipath.c
+++ b/drivers/nvme/host/multipath.c
@@ -646,17 +646,15 @@ static const struct file_operations nvme_ns_head_chr_fops = {
 
 static void nvme_add_ns_head_cdev(struct nvme_ns_head *head)
 {
-	char name[32];
-
 	head->cdev_device.parent = &head->subsys->dev;
-	snprintf(name, sizeof(name), "ng%dn%d", head->subsys->instance,
-		 head->instance);
 
 	nvme_get_ns_head(head); /* Undone in nvme_cdev_rel() */
-	if (nvme_cdev_add(name, &head->cdev, &head->cdev_device,
-			&nvme_ns_head_chr_fops, THIS_MODULE)) {
+	if (nvme_cdev_add(&head->cdev, &head->cdev_device,
+			&nvme_ns_head_chr_fops, THIS_MODULE,
+			head->subsys->instance, head->instance)) {
 		dev_err(disk_to_dev(head->disk),
-			"Unable to create the %s device\n", name);
+			"Unable to create the ng%dn%d device\n",
+			head->subsys->instance, head->instance);
 		nvme_put_ns_head(head);
 		return;
 	}
diff --git a/drivers/nvme/host/nvme.h b/drivers/nvme/host/nvme.h
index 2cff9fcbf740..e0260f4d24fd 100644
--- a/drivers/nvme/host/nvme.h
+++ b/drivers/nvme/host/nvme.h
@@ -1013,9 +1013,9 @@ int nvme_get_log(struct nvme_ctrl *ctrl, u32 nsid, u8 log_page, u8 lsp, u8 csi,
 void nvme_get_ns_head(struct nvme_ns_head *head);
 bool nvme_tryget_ns_head(struct nvme_ns_head *head);
 void nvme_put_ns_head(struct nvme_ns_head *head);
-int nvme_cdev_add(const char *name, struct cdev *cdev,
-		struct device *cdev_device,
-		const struct file_operations *fops, struct module *owner);
+int nvme_cdev_add(struct cdev *cdev, struct device *cdev_device,
+		const struct file_operations *fops, struct module *owner,
+		int ctrl, int head);
 void nvme_cdev_del(struct cdev *cdev, struct device *cdev_device);
 int nvme_ioctl(struct block_device *bdev, blk_mode_t mode,
 		unsigned int cmd, unsigned long arg);
-- 
2.53.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] nvme: work around -Wformat-security warning
  2026-09-16 11:44 [PATCH] nvme: work around -Wformat-security warning Arnd Bergmann
@ 2026-09-17  3:40 ` Damien Le Moal
  2026-09-17  4:14 ` Nilay Shroff
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Damien Le Moal @ 2026-09-17  3:40 UTC (permalink / raw)
  To: Arnd Bergmann, Keith Busch, Jens Axboe, Christoph Hellwig,
	Sagi Grimberg, Maurizio Lombardi
  Cc: Arnd Bergmann, Hannes Reinecke, Nilay Shroff, John Garry,
	Martin K. Petersen, linux-nvme, linux-kernel

On 2026/09/16 18:44, Arnd Bergmann wrote:
> From: Arnd Bergmann <arnd@arndb.de>
> 
> Passing a string variable into dev_set_name() causes a warning
> when building with -Wformat-security enabled:
> 
> drivers/nvme/host/core.c: In function 'nvme_cdev_add':
> drivers/nvme/host/core.c:3911:9: error: format not a string literal and no format arguments [-Werror=format-security]
>  3911 |         ret = dev_set_name(cdev_device, name);
> 
> Remove the temporary strings and let dev_set_name() do the
> same thing internally.
> 
> Fixes: 26acdaa357cd ("nvme: fix crash and memory leak during invalid cdev teardown")
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>

Looks OK to me.

Reviewed-by: Damien Le Moal <dlemoal@kernel.org>

-- 
Damien Le Moal
Western Digital Research

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] nvme: work around -Wformat-security warning
  2026-09-16 11:44 [PATCH] nvme: work around -Wformat-security warning Arnd Bergmann
  2026-09-17  3:40 ` Damien Le Moal
@ 2026-09-17  4:14 ` Nilay Shroff
  2026-09-17  5:59   ` Arnd Bergmann
  2026-09-17  8:01 ` John Garry
  2026-09-17  8:18 ` Nilay Shroff
  3 siblings, 1 reply; 6+ messages in thread
From: Nilay Shroff @ 2026-09-17  4:14 UTC (permalink / raw)
  To: Arnd Bergmann, Keith Busch, Jens Axboe, Christoph Hellwig,
	Sagi Grimberg, Maurizio Lombardi
  Cc: Arnd Bergmann, Hannes Reinecke, John Garry, Martin K. Petersen,
	linux-nvme, linux-kernel

On 9/16/26 5:14 PM, Arnd Bergmann wrote:
> From: Arnd Bergmann<arnd@arndb.de>
> 
> Passing a string variable into dev_set_name() causes a warning
> when building with -Wformat-security enabled:
> 
> drivers/nvme/host/core.c: In function 'nvme_cdev_add':
> drivers/nvme/host/core.c:3911:9: error: format not a string literal and no format arguments [-Werror=format-security]
>   3911 |         ret = dev_set_name(cdev_device, name);
> 
> Remove the temporary strings and let dev_set_name() do the
> same thing internally.
> 
> Fixes: 26acdaa357cd ("nvme: fix crash and memory leak during invalid cdev teardown")
> Signed-off-by: Arnd Bergmann<arnd@arndb.de>

The changes look good. However, I applied the patch and built
drivers/nvme/ with -Wformat-security, and I still see a couple of
additional warnings:

$ make M=drivers/nvme/ KCFLAGS="-Wformat-security"
make[1]: Entering directory '/home/nilay/Development/linux-x86/drivers/nvme'
[...]
host/sysfs.c: In function ‘cntrltype_show’:
host/sysfs.c:682:9: error: format not a string literal and no format arguments [-Werror=format-security]
   682 |         return sysfs_emit(buf, type[ctrl->cntrltype]);
       |         ^~~~~~
host/sysfs.c: In function ‘dctype_show’:
host/sysfs.c:699:9: error: format not a string literal and no format arguments [-Werror=format-security]
   699 |         return sysfs_emit(buf, type[ctrl->dctype]);
       |         ^~~~~~

I think that if we're fixing -Wformat-security warnings in the NVMe
driver, it would be useful to address above warnings as part of this
patch as well.

Thanks,
--Nilay

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] nvme: work around -Wformat-security warning
  2026-09-17  4:14 ` Nilay Shroff
@ 2026-09-17  5:59   ` Arnd Bergmann
  0 siblings, 0 replies; 6+ messages in thread
From: Arnd Bergmann @ 2026-09-17  5:59 UTC (permalink / raw)
  To: Nilay Shroff, Arnd Bergmann, Keith Busch, Jens Axboe,
	Christoph Hellwig, Sagi Grimberg, Maurizio Lombardi
  Cc: Hannes Reinecke, John Garry, Martin K. Petersen, linux-nvme,
	linux-kernel

On Thu, Sep 17, 2026, at 06:14, Nilay Shroff wrote:
> On 9/16/26 5:14 PM, Arnd Bergmann wrote:
>
> The changes look good. However, I applied the patch and built
> drivers/nvme/ with -Wformat-security, and I still see a couple of
> additional warnings:
>
> $ make M=drivers/nvme/ KCFLAGS="-Wformat-security"
> make[1]: Entering directory 
> '/home/nilay/Development/linux-x86/drivers/nvme'
> [...]
> host/sysfs.c: In function ‘cntrltype_show’:
> host/sysfs.c:682:9: error: format not a string literal and no format 
> arguments [-Werror=format-security]
>    682 |         return sysfs_emit(buf, type[ctrl->cntrltype]);
>        |         ^~~~~~
> host/sysfs.c: In function ‘dctype_show’:
> host/sysfs.c:699:9: error: format not a string literal and no format 
> arguments [-Werror=format-security]
>    699 |         return sysfs_emit(buf, type[ctrl->dctype]);
>        |         ^~~~~~
>
> I think that if we're fixing -Wformat-security warnings in the NVMe
> driver, it would be useful to address above warnings as part of this
> patch as well.

Good point, I forgot to check for the patches I already had fixed
in my randconfig branch and only addressed the new one here. I'll
send a separate patch to fix the two instance above.

        Arnd

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] nvme: work around -Wformat-security warning
  2026-09-16 11:44 [PATCH] nvme: work around -Wformat-security warning Arnd Bergmann
  2026-09-17  3:40 ` Damien Le Moal
  2026-09-17  4:14 ` Nilay Shroff
@ 2026-09-17  8:01 ` John Garry
  2026-09-17  8:18 ` Nilay Shroff
  3 siblings, 0 replies; 6+ messages in thread
From: John Garry @ 2026-09-17  8:01 UTC (permalink / raw)
  To: Arnd Bergmann, Keith Busch, Jens Axboe, Christoph Hellwig,
	Sagi Grimberg, Maurizio Lombardi
  Cc: Arnd Bergmann, Hannes Reinecke, Nilay Shroff, John Garry,
	Martin K. Petersen, linux-nvme, linux-kernel

On 9/16/26 12:44, Arnd Bergmann wrote:
> From: Arnd Bergmann<arnd@arndb.de>
> 
> Passing a string variable into dev_set_name() causes a warning
> when building with -Wformat-security enabled:
> 
> drivers/nvme/host/core.c: In function 'nvme_cdev_add':
> drivers/nvme/host/core.c:3911:9: error: format not a string literal and no format arguments [-Werror=format-security]
>   3911 |         ret = dev_set_name(cdev_device, name);
> 
> Remove the temporary strings and let dev_set_name() do the
> same thing internally.
> 

Prior to 26acdaa357cd, we had the caller of nvme_cdev_add() also calling 
dev_set_name(). Maybe it is better to revert to that (but keep the 
functionality to set NVME_NSHEAD_CDEV_LIVE). But then put_device() call 
for errors becomes a bit messy.

...

>   	}
> diff --git a/drivers/nvme/host/nvme.h b/drivers/nvme/host/nvme.h
> index 2cff9fcbf740..e0260f4d24fd 100644
> --- a/drivers/nvme/host/nvme.h
> +++ b/drivers/nvme/host/nvme.h
> @@ -1013,9 +1013,9 @@ int nvme_get_log(struct nvme_ctrl *ctrl, u32 nsid, u8 log_page, u8 lsp, u8 csi,
>   void nvme_get_ns_head(struct nvme_ns_head *head);
>   bool nvme_tryget_ns_head(struct nvme_ns_head *head);
>   void nvme_put_ns_head(struct nvme_ns_head *head);
> -int nvme_cdev_add(const char *name, struct cdev *cdev,
> -		struct device *cdev_device,
> -		const struct file_operations *fops, struct module *owner);
> +int nvme_cdev_add(struct cdev *cdev, struct device *cdev_device,
> +		const struct file_operations *fops, struct module *owner,
> +		int ctrl, int head);


nit: maybe @head arg would be better called "ns", as it is the "ns" index.

>   void nvme_cdev_del(struct cdev *cdev, struct device *cdev_device);
>   int nvme_ioctl(struct block_device *bdev, blk_mode_t mode,
>   		unsigned int cmd, unsigned long arg);
> -- 2.53.0

Regardless of comments:
Reviewed-by: John Garry <john.garry@linux.dev>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] nvme: work around -Wformat-security warning
  2026-09-16 11:44 [PATCH] nvme: work around -Wformat-security warning Arnd Bergmann
                   ` (2 preceding siblings ...)
  2026-09-17  8:01 ` John Garry
@ 2026-09-17  8:18 ` Nilay Shroff
  3 siblings, 0 replies; 6+ messages in thread
From: Nilay Shroff @ 2026-09-17  8:18 UTC (permalink / raw)
  To: Arnd Bergmann, Keith Busch, Jens Axboe, Christoph Hellwig,
	Sagi Grimberg, Maurizio Lombardi
  Cc: Arnd Bergmann, Hannes Reinecke, John Garry, Martin K. Petersen,
	linux-nvme, linux-kernel

On 9/16/26 5:14 PM, Arnd Bergmann wrote:
> From: Arnd Bergmann<arnd@arndb.de>
> 
> Passing a string variable into dev_set_name() causes a warning
> when building with -Wformat-security enabled:
> 
> drivers/nvme/host/core.c: In function 'nvme_cdev_add':
> drivers/nvme/host/core.c:3911:9: error: format not a string literal and no format arguments [-Werror=format-security]
>   3911 |         ret = dev_set_name(cdev_device, name);
> 
> Remove the temporary strings and let dev_set_name() do the
> same thing internally.
> 
> Fixes: 26acdaa357cd ("nvme: fix crash and memory leak during invalid cdev teardown")
> Signed-off-by: Arnd Bergmann<arnd@arndb.de>

Looks good to me.
Reviewed-by: Nilay Shroff <nilay@linux.ibm.com>

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-09-17  8:18 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-16 11:44 [PATCH] nvme: work around -Wformat-security warning Arnd Bergmann
2026-09-17  3:40 ` Damien Le Moal
2026-09-17  4:14 ` Nilay Shroff
2026-09-17  5:59   ` Arnd Bergmann
2026-09-17  8:01 ` John Garry
2026-09-17  8:18 ` Nilay Shroff

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®