Conversation

Takashi Sakamoto (坂本 貴史)

[GIT PULL] devm_kmalloc_obj conversion for v7.4-rc1 https://lore.kernel.org/lkml/202610301154.4B5EBBF13@keescook/

It contains uneven ways to handle the second argument, hmm…

commit 7e9903bd1909100fdc647c110353f6daaaecbf72
Author: Kees Cook <kees+treewide@kernel.org>
Date:   Wed Oct 28 23:16:33 2026 -0700

    treewide: Convert devm_kmalloc() family to the devm_kmalloc_obj() family
    
    Mirroring the earlier kmalloc_obj() conversion, convert the
    device-managed allocators over to the type-based API using the now
    updated scripts/coccinelle/api/kmalloc_objs.cocci. The size is derived
    from the type rather than written out as a sizeof(), and the result is
    a pointer to that type rather than void *, so a mismatch between the
    size requested and the variable assigned becomes a build error instead
    of being silently accepted.
    
    The conversions break down as:
    
            6699    devm_kzalloc_obj()
            1749    devm_kzalloc_objs()
             303    devm_kzalloc_flex()
              91    devm_kmalloc_objs()
              82    devm_kmalloc_obj()
               2    devm_kmalloc_flex()
    
    The rules deliberately leave alone allocations sized by a string or a
    byte-sized type, or assigned to a pointer to a byte-sized type, since
    those are byte buffers rather than objects. Allocations sized by a
    multi-byte integer type are only converted when they are assigned to a
    pointer to that same type, and allocations of pointers to integers or of
    void pointers are left alone too. After this change, 528 files still
    call devm_kmalloc(), devm_kzalloc(), devm_kcalloc(), or
    devm_kmalloc_array().
    
    No functional change intended: every conversion allocates the same number
    of bytes as before, and GFP_KERNEL is the macro default where it was
    dropped.
    
    Build tested with allmodconfig on x86_64, arm64, arm, riscv, s390, m68k,
    mips, parisc, sparc, loongarch, arc, xtensa, and openrisc with GCC
    16.2.0 with no new warnings.
    
    Signed-off-by: Kees Cook <kees+treewide@kernel.org>

diff --git a/drivers/firewire/ohci.c b/drivers/firewire/ohci.c
index c06e8d872..20e561ed3 100644
--- a/drivers/firewire/ohci.c
+++ b/drivers/firewire/ohci.c
@@ -3788,7 +3788,8 @@ static int pci_probe(struct pci_dev *dev,
 	reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, ~0);
 	ohci->ir_context_mask = ohci->ir_context_support;
 	ohci->n_ir = hweight32(ohci->ir_context_mask);
-	ohci->ir_context_list = devm_kcalloc(&dev->dev, ohci->n_ir, sizeof(struct iso_context), GFP_KERNEL);
+	ohci->ir_context_list = devm_kzalloc_objs(&dev->dev, struct iso_context,
+						  ohci->n_ir);
 	if (!ohci->ir_context_list)
 		return -ENOMEM;
 
@@ -3802,7 +3803,8 @@ static int pci_probe(struct pci_dev *dev,
 	reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, ~0);
 	ohci->it_context_mask = ohci->it_context_support;
 	ohci->n_it = hweight32(ohci->it_context_mask);
-	ohci->it_context_list = devm_kcalloc(&dev->dev, ohci->n_it, sizeof(struct iso_context), GFP_KERNEL);
+	ohci->it_context_list = devm_kzalloc_objs(&dev->dev, struct iso_context,
+						  ohci->n_it);
 	if (!ohci->it_context_list)
 		return -ENOMEM;
 
diff --git a/sound/firewire/bebob/bebob_maudio.c b/sound/firewire/bebob/bebob_maudio.c
index 8079a938a..4ca5e0421 100644
--- a/sound/firewire/bebob/bebob_maudio.c
+++ b/sound/firewire/bebob/bebob_maudio.c
@@ -260,8 +260,7 @@ snd_bebob_maudio_special_discover(struct snd_bebob *bebob, bool is1814)
 	struct special_params *params;
 	int err;
 
-	params = devm_kzalloc(&bebob->card->card_dev,
-			      sizeof(struct special_params), GFP_KERNEL);
+	params = devm_kzalloc_obj(&bebob->card->card_dev, struct special_params);
 	if (!params)
 		return -ENOMEM;
 
diff --git a/sound/firewire/motu/motu-command-dsp-message-parser.c b/sound/firewire/motu/motu-command-dsp-message-parser.c
index c6440e6e3..8caeb1019 100644
--- a/sound/firewire/motu/motu-command-dsp-message-parser.c
+++ b/sound/firewire/motu/motu-command-dsp-message-parser.c
@@ -37,7 +37,7 @@ int snd_motu_command_dsp_message_parser_new(struct snd_motu *motu)
 {
 	struct msg_parser *parser;
 
-	parser = devm_kzalloc(&motu->card->card_dev, sizeof(*parser), GFP_KERNEL);
+	parser = devm_kzalloc_obj(&motu->card->card_dev, *parser);
 	if (!parser)
 		return -ENOMEM;
 	spin_lock_init(&parser->lock);
diff --git a/sound/firewire/motu/motu-register-dsp-message-parser.c b/sound/firewire/motu/motu-register-dsp-message-parser.c
index 4ec23e688..3541d540b 100644
--- a/sound/firewire/motu/motu-register-dsp-message-parser.c
+++ b/sound/firewire/motu/motu-register-dsp-message-parser.c
@@ -101,7 +101,7 @@ struct msg_parser {
 int snd_motu_register_dsp_message_parser_new(struct snd_motu *motu)
 {
 	struct msg_parser *parser;
-	parser = devm_kzalloc(&motu->card->card_dev, sizeof(*parser), GFP_KERNEL);
+	parser = devm_kzalloc_obj(&motu->card->card_dev, *parser);
 	if (!parser)
 		return -ENOMEM;
 	spin_lock_init(&parser->lock);
diff --git a/sound/firewire/oxfw/oxfw-scs1x.c b/sound/firewire/oxfw/oxfw-scs1x.c
index 21412a3ca..9a6e572af 100644
--- a/sound/firewire/oxfw/oxfw-scs1x.c
+++ b/sound/firewire/oxfw/oxfw-scs1x.c
@@ -371,8 +371,7 @@ int snd_oxfw_scs1x_add(struct snd_oxfw *oxfw)
 	struct fw_scs1x *scs;
 	int err;
 
-	scs = devm_kzalloc(&oxfw->card->card_dev, sizeof(struct fw_scs1x),
-			   GFP_KERNEL);
+	scs = devm_kzalloc_obj(&oxfw->card->card_dev, struct fw_scs1x);
 	if (!scs)
 		return -ENOMEM;
 	scs->fw_dev = fw_parent_device(oxfw->unit);
diff --git a/sound/firewire/oxfw/oxfw-spkr.c b/sound/firewire/oxfw/oxfw-spkr.c
index f2767fb19..1bf19310f 100644
--- a/sound/firewire/oxfw/oxfw-spkr.c
+++ b/sound/firewire/oxfw/oxfw-spkr.c
@@ -270,8 +270,7 @@ int snd_oxfw_add_spkr(struct snd_oxfw *oxfw, bool is_lacie)
 	unsigned int i, first_ch;
 	int err;
 
-	spkr = devm_kzalloc(&oxfw->card->card_dev, sizeof(struct fw_spkr),
-			    GFP_KERNEL);
+	spkr = devm_kzalloc_obj(&oxfw->card->card_dev, struct fw_spkr);
 	if (!spkr)
 		return -ENOMEM;
 	oxfw->spec = spkr;
0
0
0