Skip to contentSkip to conversation

zynq: clkc: Add kmalloc allocation flag

PATCH In review Li zeming 3 participants 4 messages Feb 21, 2023 – Feb 27, 2023

Subsystems (2)
lore
Share & tools
lore

Li zeming

author
#0 lore raw

PATCH 1/1 zynq: clkc: Add kmalloc allocation flag

The kmalloc could crash if allocation fails. Add the __GFP_NOFAIL flag to ensure that allocation succeeds every time.

---
 drivers/clk/zynq/clkc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clk/zynq/clkc.c +1 -1
index 7bdeaff2bfd6..7621c2f00468 100644
@@ -427,7 +427,7 @@ static void __init zynq_clk_setup(struct device_node *np)
 			SLCR_GEM1_CLK_CTRL, 0, 0, &gem1clk_lock);
 
 	tmp = strlen("mio_clk_00x");
-	clk_name = kmalloc(tmp, GFP_KERNEL);
+	clk_name = kmalloc(tmp, GFP_KERNEL | __GFP_NOFAIL);
 	for (i = 0; i < NUM_MIO_PINS; i++) {
 		int idx;
 
Signature · 1 line
2.18.2
maintainer
#1 lore raw

Quoting Li zeming (2023-02-22 13:58:34)

Quoted 13 lines

The kmalloc could crash if allocation fails. Add the __GFP_NOFAIL flag to ensure that allocation succeeds every time.

---
 drivers/clk/zynq/clkc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clk/zynq/clkc.c +0 -0
index 7bdeaff2bfd6..7621c2f00468 100644

drivers/clk/zynq/clkc.c static void __init zynq_clk_setup(struct device_node *np) · line 430

Quoted 5 lines
drivers/clk/zynq/clkc.c +1 -1
@@ -427,7 +427,7 @@ static void __init zynq_clk_setup(struct device_node *np)
                        SLCR_GEM1_CLK_CTRL, 0, 0, &gem1clk_lock);
 
        tmp = strlen("mio_clk_00x");
-       clk_name = kmalloc(tmp, GFP_KERNEL);
+       clk_name = kmalloc(tmp, GFP_KERNEL | __GFP_NOFAIL);

There are so many more allocations happening in this function and they aren't marked nofail. Why is this one special?

I could see a patch moving mio_clk_00x to the stack and then printing to it. But it is also fine like this, so I don't see any reason to change this.

author
#2 lore raw

hello senior:
I observed that some other variable assignments in this function are basically judged by the if statement, while clk_name does not make an if branch statement, and I think clk_name is also relatively important, increasing __GFP_NOFAIL flag ensures that the assignment can succeed under any circumstances.

maintainer
#3 lore raw

Hi,

Quoted 4 lines · Li zeming

hello senior:
I observed that some other variable assignments in this function are basically judged by the if statement, while clk_name does not make an if branch statement, and I think clk_name is also relatively important, increasing __GFP_NOFAIL flag ensures that the assignment can succeed under any circumstances.

I think that solution with array on stack would be better choice. It will be faster and you can completely skip the whole allocation code for it.

Thanks,
Michal