Skip to contentSkip to conversation

bpf: fix order of args in call to bpf_map_kvcalloc

PATCH RESEND In review Mohammad Shehar Yaar Tausif 7 participants 10 messages Jun 12, 2024 – Jul 10, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 bpf: fix order of args in call to bpf_map_kvcalloc

The original function call passed size of smap->bucket before the number of buckets which raises the error 'calloc-transposed-args' on compilation.

Fixes 62827d612ae5 ("bpf: Remove __bpf_local_storage_map_alloc")

---
- already merged in linux-next
- [1] suggested sending as a fix for 6.10 cycle

[1] https://lore.kernel.org/all/[email protected]/

---
 kernel/bpf/bpf_local_storage.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/bpf/bpf_local_storage.c +2 -2
index 976cb258a0ed..c938dea5ddbf 100644
@@ -782,8 +782,8 @@ bpf_local_storage_map_alloc(union bpf_attr *attr,
 	nbuckets = max_t(u32, 2, nbuckets);
 	smap->bucket_log = ilog2(nbuckets);
 
-	smap->buckets = bpf_map_kvcalloc(&smap->map, sizeof(*smap->buckets),
-					 nbuckets, GFP_USER | __GFP_NOWARN);
+	smap->buckets = bpf_map_kvcalloc(&smap->map, nbuckets,
+					 sizeof(*smap->buckets), GFP_USER | __GFP_NOWARN);
 	if (!smap->buckets) {
 		err = -ENOMEM;
 		goto free_smap;

---
base-commit: 2ef5971ff345d3c000873725db555085e0131961
change-id: 20240612-master-fe9e63ab5c95

Best regards,

Signature · 1 line
Mohammad Shehar Yaar Tausif <[email protected]>
#1 lore raw
Quoted 10 lines · Mohammad Shehar Yaar Tausif

The original function call passed size of smap->bucket before the number of buckets which raises the error 'calloc-transposed-args' on compilation.

Fixes 62827d612ae5 ("bpf: Remove __bpf_local_storage_map_alloc")

---
- already merged in linux-next
- [1] suggested sending as a fix for 6.10 cycle

No. It's not a fix.

pw-bot: cr

Quoted 30 lines

[1] https://lore.kernel.org/all/[email protected]/

---
 kernel/bpf/bpf_local_storage.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/bpf/bpf_local_storage.c +2 -3
index 976cb258a0ed..c938dea5ddbf 100644
@@ -782,8 +782,8 @@ bpf_local_storage_map_alloc(union bpf_attr *attr,
        nbuckets = max_t(u32, 2, nbuckets);
        smap->bucket_log = ilog2(nbuckets);
 
-       smap->buckets = bpf_map_kvcalloc(&smap->map, sizeof(*smap->buckets),
-                                        nbuckets, GFP_USER | __GFP_NOWARN);
+       smap->buckets = bpf_map_kvcalloc(&smap->map, nbuckets,
+                                        sizeof(*smap->buckets), GFP_USER | __GFP_NOWARN);
        if (!smap->buckets) {
                err = -ENOMEM;
                goto free_smap;
 
---

base-commit: 2ef5971ff345d3c000873725db555085e0131961
change-id: 20240612-master-fe9e63ab5c95

Best regards,
--
Mohammad Shehar Yaar Tausif <[email protected]>

#2 lore raw

[CCing the regressions list and people mentioned below]

Quoted 6 lines · Alexei Starovoitov
Quoted 4 lines · Mohammad Shehar Yaar Tausif

The original function call passed size of smap->bucket before the number of buckets which raises the error 'calloc-transposed-args' on compilation.

Quoted 8 lines
Quoted 6 lines
Fixes 62827d612ae5 ("bpf: Remove __bpf_local_storage_map_alloc")

---
- already merged in linux-next
- [1] suggested sending as a fix for 6.10 cycle

No. It's not a fix.

If you have a minute, could you please explain why that is? From what I can see a quite a few people run into build problems with 6.10-rc recently that are fixed by the patch:

* Péter Ujfalusi
https://lore.kernel.org/bpf/[email protected]/

* Christian Kujau
https://lore.kernel.org/bpf/[email protected]/
https://lore.kernel.org/lkml/[email protected]/

* Lorenzo Stoakes
https://fosstodon.org/@[email protected]/112734050799590482

At the same time I see that the culprit mentioned above is from 6.4-rc1, so I guess it there must be some other reason why a few people seem to tun into this now. Did some other change expose this problem? Or are updated compilers causing this?

Ciao, Thorsten

Quoted 29 lines
Quoted 29 lines

[1] https://lore.kernel.org/all/[email protected]/

---
 kernel/bpf/bpf_local_storage.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/bpf/bpf_local_storage.c +2 -3
index 976cb258a0ed..c938dea5ddbf 100644
@@ -782,8 +782,8 @@ bpf_local_storage_map_alloc(union bpf_attr *attr,
        nbuckets = max_t(u32, 2, nbuckets);
        smap->bucket_log = ilog2(nbuckets);
 
-       smap->buckets = bpf_map_kvcalloc(&smap->map, sizeof(*smap->buckets),
-                                        nbuckets, GFP_USER | __GFP_NOWARN);
+       smap->buckets = bpf_map_kvcalloc(&smap->map, nbuckets,
+                                        sizeof(*smap->buckets), GFP_USER | __GFP_NOWARN);
        if (!smap->buckets) {
                err = -ENOMEM;
                goto free_smap;
 
---

base-commit: 2ef5971ff345d3c000873725db555085e0131961
change-id: 20240612-master-fe9e63ab5c95

Best regards,
--
Mohammad Shehar Yaar Tausif <[email protected]>

#3 lore raw
Quoted 16 lines · Linux regression tracking (Thorsten Leemhuis)

[CCing the regressions list and people mentioned below]

Quoted 13 lines · Alexei Starovoitov
Quoted 10 lines · Mohammad Shehar Yaar Tausif

The original function call passed size of smap->bucket before the number of buckets which raises the error 'calloc-transposed-args' on compilation.

Fixes 62827d612ae5 ("bpf: Remove __bpf_local_storage_map_alloc")

---
- already merged in linux-next
- [1] suggested sending as a fix for 6.10 cycle

Quoted 5 lines
Quoted 1 line

No. It's not a fix.

If you have a minute, could you please explain why that is? From what I can see a quite a few people run into build problems with 6.10-rc recently that are fixed by the patch:

This is explicitly breaking my build in Linus's kernel (and subsequently mm-unstable where I hit it first).

I have gcc 14.1.1, and can easily repro this with a defconfig on x86-64 with:

make mrproper && make defconfig && scripts/config --enable bpf_syscall && \
make olddefconfig && make -j $(nproc)

kernel/bpf/bpf_local_storage.c:785:60: error: ‘kvmalloc_array_node_noprof’ sizes specified with ‘sizeof’ in the earlier argument and not in the laterargument [-Werror=calloc-transposed-args]
785 | smap->buckets = bpf_map_kvcalloc(&smap->map, sizeof(*smap->buckets),

It's kind of surprising no build bot caught this (maybe somebody needs to look into that), but it's proactively causing problems right now, I have to keep the kernel patched in order for it to build.

So a fix of some kind is needed, urgently.

Quoted 8 lines

* Péter Ujfalusi
https://lore.kernel.org/bpf/[email protected]/

* Christian Kujau
https://lore.kernel.org/bpf/[email protected]/
https://lore.kernel.org/lkml/[email protected]/

Quoted 7 lines

* Lorenzo Stoakes
https://fosstodon.org/@[email protected]/112734050799590482

At the same time I see that the culprit mentioned above is from 6.4-rc1, so I guess it there must be some other reason why a few people seem to tun into this now. Did some other change expose this problem? Or are updated compilers causing this?

I suspect the latter. It seems x86-64 defconfig unables CONFIG_WERROR by default.

Quoted 32 lines

Ciao, Thorsten

Quoted 29 lines
Quoted 29 lines

[1] https://lore.kernel.org/all/[email protected]/

---
 kernel/bpf/bpf_local_storage.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/bpf/bpf_local_storage.c +2 -3
index 976cb258a0ed..c938dea5ddbf 100644
@@ -782,8 +782,8 @@ bpf_local_storage_map_alloc(union bpf_attr *attr,
        nbuckets = max_t(u32, 2, nbuckets);
        smap->bucket_log = ilog2(nbuckets);
 
-       smap->buckets = bpf_map_kvcalloc(&smap->map, sizeof(*smap->buckets),
-                                        nbuckets, GFP_USER | __GFP_NOWARN);
+       smap->buckets = bpf_map_kvcalloc(&smap->map, nbuckets,
+                                        sizeof(*smap->buckets), GFP_USER | __GFP_NOWARN);
        if (!smap->buckets) {
                err = -ENOMEM;
                goto free_smap;
 
---

base-commit: 2ef5971ff345d3c000873725db555085e0131961
change-id: 20240612-master-fe9e63ab5c95

Best regards,
--
Mohammad Shehar Yaar Tausif <[email protected]>

#4 lore raw
In reply to Linux regression tracking (Thorsten Leemhuis)
Quoted 25 lines · Linux regression tracking (Thorsten Leemhuis)

[CCing the regressions list and people mentioned below]

Quoted 6 lines · Alexei Starovoitov
Quoted 4 lines · Mohammad Shehar Yaar Tausif

The original function call passed size of smap->bucket before the number of buckets which raises the error 'calloc-transposed-args' on compilation.

Quoted 8 lines
Quoted 6 lines
Fixes 62827d612ae5 ("bpf: Remove __bpf_local_storage_map_alloc")

---
- already merged in linux-next
- [1] suggested sending as a fix for 6.10 cycle

No. It's not a fix.

If you have a minute, could you please explain why that is? From what I can see a quite a few people run into build problems with 6.10-rc recently that are fixed by the patch:

* Péter Ujfalusi
https://lore.kernel.org/bpf/[email protected]/

Quoted 8 lines

* Christian Kujau
https://lore.kernel.org/bpf/[email protected]/
https://lore.kernel.org/lkml/[email protected]/

* Lorenzo Stoakes
https://fosstodon.org/@[email protected]/112734050799590482

At the same time I see that the culprit mentioned above is from 6.4-rc1,

IIUC the order was wrong even before, but see below.

Quoted 3 lines

so I guess it there must be some other reason why a few people seem to tun into this now. Did some other change expose this problem? Or are updated compilers causing this?

I think it's because of 2c321f3f70bc ("mm: change inlined allocation helpers to account at the call site"), which was added in 6.10-rc1 and thus makes this technically a 6.10 regression after all. So what triggers the bug is AFAICS the following together:

- gcc-14 (didn't see it with gcc-13)
- commit 2c321f3f70bc that makes bpf_map_kvcalloc a macro that does kvcalloc() directly instead of static inline function wrapping it for !CONFIG_MEMCG
- CONFIG_MEMCG=n in .config

The fix is so trivial, it's better to include it in 6.10 even this late.

Quoted 31 lines

Ciao, Thorsten

Quoted 29 lines
Quoted 29 lines

[1] https://lore.kernel.org/all/[email protected]/

---
 kernel/bpf/bpf_local_storage.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/bpf/bpf_local_storage.c +2 -3
index 976cb258a0ed..c938dea5ddbf 100644
@@ -782,8 +782,8 @@ bpf_local_storage_map_alloc(union bpf_attr *attr,
        nbuckets = max_t(u32, 2, nbuckets);
        smap->bucket_log = ilog2(nbuckets);
 
-       smap->buckets = bpf_map_kvcalloc(&smap->map, sizeof(*smap->buckets),
-                                        nbuckets, GFP_USER | __GFP_NOWARN);
+       smap->buckets = bpf_map_kvcalloc(&smap->map, nbuckets,
+                                        sizeof(*smap->buckets), GFP_USER | __GFP_NOWARN);
        if (!smap->buckets) {
                err = -ENOMEM;
                goto free_smap;
 
---

base-commit: 2ef5971ff345d3c000873725db555085e0131961
change-id: 20240612-master-fe9e63ab5c95

Best regards,
--
Mohammad Shehar Yaar Tausif <[email protected]>

#5 lore raw
Quoted 38 lines · Vlastimil Babka
Quoted 25 lines · Linux regression tracking (Thorsten Leemhuis)

[CCing the regressions list and people mentioned below]

Quoted 6 lines · Alexei Starovoitov
Quoted 4 lines · Mohammad Shehar Yaar Tausif

The original function call passed size of smap->bucket before the number of buckets which raises the error 'calloc-transposed-args' on compilation.

Quoted 8 lines
Quoted 6 lines
Fixes 62827d612ae5 ("bpf: Remove __bpf_local_storage_map_alloc")

---
- already merged in linux-next
- [1] suggested sending as a fix for 6.10 cycle

No. It's not a fix.

If you have a minute, could you please explain why that is? From what I can see a quite a few people run into build problems with 6.10-rc recently that are fixed by the patch:

* Péter Ujfalusi
https://lore.kernel.org/bpf/[email protected]/

Quoted 8 lines

* Christian Kujau
https://lore.kernel.org/bpf/[email protected]/
https://lore.kernel.org/lkml/[email protected]/

* Lorenzo Stoakes
https://fosstodon.org/@[email protected]/112734050799590482

At the same time I see that the culprit mentioned above is from 6.4-rc1,

IIUC the order was wrong even before, but see below.

Quoted 7 lines
Quoted 3 lines

so I guess it there must be some other reason why a few people seem to tun into this now. Did some other change expose this problem? Or are updated compilers causing this?

I think it's because of 2c321f3f70bc ("mm: change inlined allocation helpers to account at the call site"), which was added in 6.10-rc1 and thus makes this technically a 6.10 regression after all.

IIUC the above mentioned change reveals a problem that was there before the change. So, it's a build regression in 6.10 because the bug got exposed but the bug was introduced much earlier. The fix should be marked as:

Fixes ddef81b5fd1d ("bpf: use bpf_map_kvcalloc in bpf_local_storage")
Quoted 43 lines

So what triggers the bug is
AFAICS the following together:

- gcc-14 (didn't see it with gcc-13)
- commit 2c321f3f70bc that makes bpf_map_kvcalloc a macro that does kvcalloc() directly instead of static inline function wrapping it for !CONFIG_MEMCG
- CONFIG_MEMCG=n in .config

The fix is so trivial, it's better to include it in 6.10 even this late.

Quoted 31 lines

Ciao, Thorsten

Quoted 29 lines
Quoted 29 lines

[1] https://lore.kernel.org/all/[email protected]/

---
 kernel/bpf/bpf_local_storage.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/bpf/bpf_local_storage.c +2 -3
index 976cb258a0ed..c938dea5ddbf 100644
@@ -782,8 +782,8 @@ bpf_local_storage_map_alloc(union bpf_attr *attr,
        nbuckets = max_t(u32, 2, nbuckets);
        smap->bucket_log = ilog2(nbuckets);
 
-       smap->buckets = bpf_map_kvcalloc(&smap->map, sizeof(*smap->buckets),
-                                        nbuckets, GFP_USER | __GFP_NOWARN);
+       smap->buckets = bpf_map_kvcalloc(&smap->map, nbuckets,
+                                        sizeof(*smap->buckets), GFP_USER | __GFP_NOWARN);
        if (!smap->buckets) {
                err = -ENOMEM;
                goto free_smap;
 
---

base-commit: 2ef5971ff345d3c000873725db555085e0131961
change-id: 20240612-master-fe9e63ab5c95

Best regards,
--
Mohammad Shehar Yaar Tausif <[email protected]>

#6 lore raw
Quoted 48 lines · Suren Baghdasaryan
Quoted 38 lines · Vlastimil Babka
Quoted 25 lines · Linux regression tracking (Thorsten Leemhuis)

[CCing the regressions list and people mentioned below]

Quoted 6 lines · Alexei Starovoitov
Quoted 4 lines · Mohammad Shehar Yaar Tausif

The original function call passed size of smap->bucket before the number of buckets which raises the error 'calloc-transposed-args' on compilation.

Quoted 8 lines
Quoted 6 lines
Fixes 62827d612ae5 ("bpf: Remove __bpf_local_storage_map_alloc")

---
- already merged in linux-next
- [1] suggested sending as a fix for 6.10 cycle

No. It's not a fix.

If you have a minute, could you please explain why that is? From what I can see a quite a few people run into build problems with 6.10-rc recently that are fixed by the patch:

* Péter Ujfalusi
https://lore.kernel.org/bpf/[email protected]/

Quoted 8 lines

* Christian Kujau
https://lore.kernel.org/bpf/[email protected]/
https://lore.kernel.org/lkml/[email protected]/

* Lorenzo Stoakes
https://fosstodon.org/@[email protected]/112734050799590482

At the same time I see that the culprit mentioned above is from 6.4-rc1,

IIUC the order was wrong even before, but see below.

Quoted 7 lines
Quoted 3 lines

so I guess it there must be some other reason why a few people seem to tun into this now. Did some other change expose this problem? Or are updated compilers causing this?

I think it's because of 2c321f3f70bc ("mm: change inlined allocation helpers to account at the call site"), which was added in 6.10-rc1 and thus makes this technically a 6.10 regression after all.

Quoted 6 lines

IIUC the above mentioned change reveals a problem that was there before the change. So, it's a build regression in 6.10 because the bug got exposed but the bug was introduced much earlier. The fix should be marked as:

Fixes ddef81b5fd1d ("bpf: use bpf_map_kvcalloc in bpf_local_storage")

Not really. The order was flipped before that patch.

Quoted 8 lines
Quoted 8 lines

So what triggers the bug is
AFAICS the following together:

- gcc-14 (didn't see it with gcc-13)
- commit 2c321f3f70bc that makes bpf_map_kvcalloc a macro that does kvcalloc() directly instead of static inline function wrapping it for !CONFIG_MEMCG
- CONFIG_MEMCG=n in .config

Can somebody respin the patch with above details?

tbh I don't think it qualifies as a "bug". Plenty of code places mix up size/n arguments to calloc. Erroring the build in such cases is imo wrong. Not sure what makes gcc-14 produce such warn/error.

But since the patch is trivial we can get that in quickly. Pls respin with all details.

#7 lore raw

PATCH 1/1 bpf: fix order of args in call to bpf_map_kvcalloc

From: Mohammad Shehar Yaar Tausif <[email protected]>

The original function call passed size of smap->bucket before the number of buckets which raises the error 'calloc-transposed-args' on compilation.

Vlastimil Babka added:

The order of parameters can be traced back all the way to 6ac99e8f23d4 ("bpf: Introduce bpf sk local storage") accross several refactorings, and that's why the commit is used as a Fixes: tag.

In v6.10-rc1, a different commit 2c321f3f70bc ("mm: change inlined allocation helpers to account at the call site") however exposed the order of args in a way that gcc-14 has enough visibility to start warning about it, because (in !CONFIG_MEMCG case) bpf_map_kvcalloc is then a macro alias for kvcalloc instead of a static inline wrapper.

To sum up the warning happens when the following conditions are all met:

- gcc-14 is used (didn't see it with gcc-13)
- commit 2c321f3f70bc is present
- CONFIG_MEMCG is not enabled in .config
- CONFIG_WERROR turns this from a compiler warning to error

Fixes 6ac99e8f23d4 ("bpf: Introduce bpf sk local storage")
---
 kernel/bpf/bpf_local_storage.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/bpf/bpf_local_storage.c +2 -2
index 976cb258a0ed..c938dea5ddbf 100644
@@ -782,8 +782,8 @@ bpf_local_storage_map_alloc(union bpf_attr *attr,
 	nbuckets = max_t(u32, 2, nbuckets);
 	smap->bucket_log = ilog2(nbuckets);
 
-	smap->buckets = bpf_map_kvcalloc(&smap->map, sizeof(*smap->buckets),
-					 nbuckets, GFP_USER | __GFP_NOWARN);
+	smap->buckets = bpf_map_kvcalloc(&smap->map, nbuckets,
+					 sizeof(*smap->buckets), GFP_USER | __GFP_NOWARN);
 	if (!smap->buckets) {
 		err = -ENOMEM;
 		goto free_smap;
Signature · 1 line
2.45.2
Tested-by on v1 1/1
#8 lore raw
Quoted 1 line · Vlastimil Babka
Fixes 6ac99e8f23d4 ("bpf: Introduce bpf sk local storage")

Thanks for not forgetting about this! If this matters, just tested this against today's mainline:

C.

Signature · 3 lines
BOFH excuse #418:

Sysadmins busy fighting SPAM.
#9 lore raw
Quoted 8 lines · Christian Kujau
Quoted 1 line · Vlastimil Babka
Fixes 6ac99e8f23d4 ("bpf: Introduce bpf sk local storage")

Thanks for not forgetting about this! If this matters, just tested this against today's mainline:

Thanks everyone. Applied to bpf tree.