Fix u32's systematic failure to free IDR entries for hnodes.
Subsystems (3)
PATCH 1/1 Fix u32's systematic failure to free IDR entries for hnodes.
To generate hnode handles (in gen_new_htid()), u32 uses IDR and encodes the returned small integer into a structured 32-bit word. Unfortunately, at disposal time, the needed decoding is not done. As a result, idr_remove() fails, and the IDR fills up. Since its size is 2048, the following script ends up with "Filter already exists":
tc filter add dev myve $FILTER1
tc filter add dev myve $FILTER2
for i in {1..2048}
do
echo $i
tc filter del dev myve $FILTER2
tc filter add dev myve $FILTER2
done
This patch adds the missing decoding logic for handles that deserve it.
--- net/sched/cls_u32.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)
net/sched/cls_u32.c +14 -4
@@ -41,6 +41,16 @@#include <linux/idr.h> #include <net/tc_wrapper.h> +static inline unsigned int handle2id (unsigned int h) +{ +return ((h &0x80000000 ) ? ((h >>20 ) &0x7FF ) : h); +} + +static inline unsigned int id2handle (unsigned int id) +{ +return (id |0x800U ) <<20 ; +} +struct tc_u_knode {struct tc_u_knode __rcu *next;u32 handle;
@@ -310,7 +320,7 @@ static u32 gen_new_htid(struct tc_u_common *tp_c, struct tc_u_hnode *ptr)int id =idr_alloc_cyclic (&tp_c->handle_idr, ptr,1 ,0x7FF ,GFP_KERNEL );if (id <0 )return 0 ; -return (id |0x800U ) <<20 ; +return id2handle (id); }static struct hlist_head *tc_u_common_hash;
@@ -360,7 +370,7 @@ static int u32_init(struct tcf_proto *tp)return -ENOBUFS ;refcount_set (&root_ht->refcnt,1 ); - root_ht->handle = tp_c ?gen_new_htid (tp_c, root_ht) :0x80000000 ; + root_ht->handle = tp_c ?gen_new_htid (tp_c, root_ht) :id2handle (0 ); root_ht->prio = tp->prio; root_ht->is_root = true;idr_init (&root_ht->handle_idr);
@@ -612,7 +622,7 @@ static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,if (phn == ht) {u32_clear_hw_hnode (tp, ht, extack);idr_destroy (&ht->handle_idr); -idr_remove (&tp_c->handle_idr, ht->handle); +idr_remove (&tp_c->handle_idr,handle2id (ht->handle));RCU_INIT_POINTER (*hn, ht->next);kfree_rcu (ht, rcu);return 0 ;
@@ -989,7 +999,7 @@ static int u32_change(struct net *net, struct sk_buff *in_skb, err =u32_replace_hw_hnode (tp, ht, userflags, extack);if (err) { -idr_remove (&tp_c->handle_idr, handle); +idr_remove (&tp_c->handle_idr,handle2id (handle));kfree (ht);return err; }
Signature · 1 line
2.30.2
Quoted 16 lines · Alexandre Ferrieux
To generate hnode handles (in
gen_new_htid()), u32 uses IDR and encodes the returned small integer into a structured 32-bit word. Unfortunately, at disposal time, the needed decoding is not done. As a result,idr_remove()fails, and the IDR fills up. Since its size is 2048, the following script ends up with "Filter already exists":tc filter add dev myve $FILTER1
tc filter add dev myve $FILTER2
for i in {1..2048}
do
echo $i
tc filter del dev myve $FILTER2
tc filter add dev myve $FILTER2
done
Quoted 4 lines
This patch adds the missing decoding logic for handles that deserve it.
Signed-off-by Alexandre Ferrieux <[email protected]>
SoB does not match sender, probably missing 'From:' tag Also, this seems to deserve a 'Fixes:' tag as well
Quoted 12 lines
--- net/sched/cls_u32.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)net/sched/cls_u32.c +0
-0@@ -41,6 +41,16 @@#include <linux/idr.h> #include <net/tc_wrapper.h>
net/sched/cls_u32.c line 53
Quoted 10 lines
net/sched/cls_u32.c +10
-0+static inline unsigned int handle2id (unsigned int h) +{ +return ((h &0x80000000 ) ? ((h >>20 ) &0x7FF ) : h); +} + +static inline unsigned int id2handle (unsigned int id) +{ +return (id |0x800U ) <<20 ; +} +
'static inline' is discouraged in .c files
Quoted 39 lines
net/sched/cls_u32.c +4
-4struct tc_u_knode {struct tc_u_knode __rcu *next;u32 handle;@@ -310,7 +320,7 @@ static u32 gen_new_htid(struct tc_u_common *tp_c, struct tc_u_hnode *ptr)int id =idr_alloc_cyclic (&tp_c->handle_idr, ptr,1 ,0x7FF ,GFP_KERNEL );if (id <0 )return 0 ; -return (id |0x800U ) <<20 ; +return id2handle (id); }static struct hlist_head *tc_u_common_hash;@@ -360,7 +370,7 @@ static int u32_init(struct tcf_proto *tp)return -ENOBUFS ;refcount_set (&root_ht->refcnt,1 ); - root_ht->handle = tp_c ?gen_new_htid (tp_c, root_ht) :0x80000000 ; + root_ht->handle = tp_c ?gen_new_htid (tp_c, root_ht) :id2handle (0 ); root_ht->prio = tp->prio; root_ht->is_root = true;idr_init (&root_ht->handle_idr);@@ -612,7 +622,7 @@ static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,if (phn == ht) {u32_clear_hw_hnode (tp, ht, extack);idr_destroy (&ht->handle_idr); -idr_remove (&tp_c->handle_idr, ht->handle); +idr_remove (&tp_c->handle_idr,handle2id (ht->handle));RCU_INIT_POINTER (*hn, ht->next);kfree_rcu (ht, rcu);return 0 ;@@ -989,7 +999,7 @@ static int u32_change(struct net *net, struct sk_buff *in_skb, err =u32_replace_hw_hnode (tp, ht, userflags, extack);if (err) { -idr_remove (&tp_c->handle_idr, handle); +idr_remove (&tp_c->handle_idr,handle2id (handle));kfree (ht);return err; }
Quoted 4 lines · Pedro Tammela
Quoted 2 lines
Signed-off-by Alexandre Ferrieux <[email protected]>SoB does not match sender, probably missing 'From:' tag
Due to dumb administrativia at my organization, I am compelled to post from my personal gmail accout in order for my posts to be acceptable on this mailing list; while I'd like to keep my official address in commit logs. Is it possible ?
Quoted 1 line
Also, this seems to deserve a 'Fixes:' tag as well
This would be the initial commit:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 19)
Is that what you mean ?
Quoted 1 line
'static inline' is discouraged in .c files
Why ?
It could have been a local macro, but an inline has (a bit) better type checking. And I didn't want to add it to a .h that is included by many other unrelated components, as it makes no sense to them. So, what is the recommendation ?
Quoted 9 lines · Alexandre Ferrieux
Quoted 4 lines · Pedro Tammela
Quoted 2 lines
Signed-off-by Alexandre Ferrieux <[email protected]>SoB does not match sender, probably missing 'From:' tag
Due to dumb administrativia at my organization, I am compelled to post from my personal gmail accout in order for my posts to be acceptable on this mailing list; while I'd like to keep my official address in commit logs. Is it possible ?
Yes, it's possible, the author of commit in your local git should use email account of company, then git format-patch will generate proper header.
Quoted 8 lines
Quoted 1 line
Also, this seems to deserve a 'Fixes:' tag as well
This would be the initial commit:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 19)
Is that what you mean ?
you can add
Quoted 7 lines
Quoted 1 line
'static inline' is discouraged in .c files
Why ?
It could have been a local macro, but an inline has (a bit) better type checking. And I didn't want to add it to a .h that is included by many other unrelated components, as it makes no sense to them. So, what is the recommendation ?
Either move it to some local header file, or use 'static u32 handle2id(u32 h)' and let compiler decide whether to include it or not. But in either cases use u32 as types to be consistent with other types in the functions you modify.
Quoted 5 lines · Vadim Fedorenko
Quoted 4 lines · Alexandre Ferrieux
Quoted 3 lines · Pedro Tammela
Quoted 2 lines
Signed-off-by Alexandre Ferrieux <[email protected]>
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
SoB does not match sender, probably missing 'From:' tag
Due to dumb administrativia at my organization, I am compelled to post from my personal gmail accout in order for my posts to be acceptable on this mailing list; while I'd like to keep my official address in commit logs. Is it possible ?
Yes, it's possible, the author of commit in your local git should use email account of company, then git format-patch will generate proper header.
That's exactly what I did, and the file generated by format-patch does have the proper From:, but it gets overridden by Gmail when sending. That's why, as a last resort, I tried Signed-off-by... Any hope ?
Quoted 2 lines
you can add
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Ok.
Quoted 11 lines
Quoted 7 lines
Quoted 1 line
'static inline' is discouraged in .c files
Why ?
It could have been a local macro, but an inline has (a bit) better type checking. And I didn't want to add it to a .h that is included by many other unrelated components, as it makes no sense to them. So, what is the recommendation ?
Either move it to some local header file, or use 'static u32 handle2id(u32 h)'
and let compiler decide whether to include it or not.
I believe you mean "let the compiler decide whether to _inline_ it or not". Sure, with a sufficiently modern Gcc this will do. However, what about more exotic environments ? Wouldn't it risk a perf regression for style reasons ?
And speaking of style, what about the dozens of instances of "static inline" in net/sched/*.c alone ? Why is it a concern suddenly ?
Quoted 3 lines
But in either
cases use u32 as types to be consistent with other types in the functions you modify.
Ok.
Quoted 12 lines · Alexandre Ferrieux
Quoted 11 lines · Vadim Fedorenko
Quoted 9 lines · Alexandre Ferrieux
Quoted 4 lines · Pedro Tammela
Quoted 2 lines
Signed-off-by Alexandre Ferrieux <[email protected]>SoB does not match sender, probably missing 'From:' tag
Due to dumb administrativia at my organization, I am compelled to post from my personal gmail accout in order for my posts to be acceptable on this mailing list; while I'd like to keep my official address in commit logs. Is it possible ?
Quoted 6 lines
Quoted 2 lines
Yes, it's possible, the author of commit in your local git should use email account of company, then git format-patch will generate proper header.
That's exactly what I did, and the file generated by format-patch does have the proper From:, but it gets overridden by Gmail when sending. That's why, as a last resort, I tried Signed-off-by... Any hope ?
You might try b4 send and see if that helps: https://b4.docs.kernel.org/en/latest/contributor/send.html
Signature · 1 line
Florian
Quoted 14 lines · Vadim Fedorenko
Quoted 5 lines
Quoted 5 lines · Pedro Tammela
Quoted 1 line
'static inline' is discouraged in .c files
Why ?
Quoted 7 lines
Quoted 3 lines
It could have been a local macro, but an inline has (a bit) better type checking. And I didn't want to add it to a .h that is included by many other unrelated components, as it makes no sense to them. So, what is the recommendation ?
Either move it to some local header file, or use 'static u32 handle2id(u32 h)'
and let compiler decide whether to include it or not.
Quoted 6 lines
I believe you mean "let the compiler decide whether to _inline_ it or not". Sure, with a sufficiently modern Gcc this will do. However, what about more exotic environments ? Wouldn't it risk a perf regression for style reasons ?
And speaking of style, what about the dozens of instances of "static inline" in
net/sched/*.calone ? Why is it a concern suddenly ?
Can you please explain *why* in the first place you're saying "'static inline' is discouraged in .c files" ? I see no trace if this in coding-style.rst, and the kernel contains hundreds of counter-examples.
Quoted 18 lines · Alexandre Ferrieux
Quoted 18 lines · Vadim Fedorenko
Quoted 5 lines
Quoted 5 lines · Pedro Tammela
Quoted 1 line
'static inline' is discouraged in .c files
Why ?
Quoted 7 lines
Quoted 3 lines
It could have been a local macro, but an inline has (a bit) better type checking. And I didn't want to add it to a .h that is included by many other unrelated components, as it makes no sense to them. So, what is the recommendation ?
Either move it to some local header file, or use 'static u32 handle2id(u32 h)'
and let compiler decide whether to include it or not.I believe you mean "let the compiler decide whether to _inline_ it or not". Sure, with a sufficiently modern Gcc this will do. However, what about more exotic environments ? Wouldn't it risk a perf regression for style reasons ?
Quoted 6 lines
Quoted 2 lines
And speaking of style, what about the dozens of instances of "static inline" in
net/sched/*.calone ? Why is it a concern suddenly ?Can you please explain *why* in the first place you're saying "'static inline' is discouraged in .c files" ? I see no trace if this in
coding-style.rst, and the kernel contains hundreds of counter-examples.
The biggest reason is because it will mask unused function warnings when "static inline" function will not be used because of some future patches. There is no big reason to refactor old code that's why there are counter-examples in the kernel, but the new code shouldn't have it.
I don't really understand what kind of exotic environment you are thinking about, but modern kernel has proper gcc/clang version dependency, both of these compilers have good heuristics to identify which function to inline.
Quoted 10 lines · Vadim Fedorenko
Quoted 4 lines · Alexandre Ferrieux
Can you please explain *why* in the first place you're saying "'static inline' is discouraged in .c files" ? I see no trace if this in
coding-style.rst, and the kernel contains hundreds of counter-examples.The biggest reason is because it will mask unused function warnings when "static inline" function will not be used because of some future patches. There is no big reason to refactor old code that's why there are counter-examples in the kernel, but the new code shouldn't have it.
A macro doesn't elicit unused function warnings either, so this looks like a very weak motivation. While coding-style.rst explicitly encourages to use static inline instead of macros, as they have better type checking and syntaxic isolation.
Regarding old vs new code, below are the last two month's fresh commits of "static inline" in *.c. So it looks like the motivation is not shared by other maintainers. Do we expect to see "local styles" emerge ?
$ git log --pretty='%h %as %ae' -p | gawk
'/^[0-9a-f]{12}/{c=$0;next}/^diff/{f=$NF;next}/^[+].*static.inline/{if
(f~/[.]c$/){print c "\t"gensub(/.*\//,"","1",f)}}'baa802d2aa5c 2024-10-21 [email protected] verifier_const.cbaa802d2aa5c 2024-10-21 [email protected] verifier_const.cd1744a4c975b 2024-10-21 [email protected] amd.cd1744a4c975b 2024-10-21 [email protected] amd.ca6e0ceb7bf48 2024-10-11 [email protected] maple.c78f636e82b22 2024-09-25 [email protected] ap_queue.c
19773ec99720 2024-10-07 [email protected] disk_accounting.c 9b23fdbd5d29 2024-09-29 [email protected] inode.c 9b23fdbd5d29 2024-09-29 [email protected] inode.c
3d5854d75e31 2024-09-30 [email protected] kcore.c 3d5854d75e31 2024-09-30 [email protected] kcore.c38864eccf78b 2024-09-30 [email protected] fsck.cd278a9de5e18 2024-10-02 [email protected] init.cf811b83879fb 2024-10-02 [email protected] dm-verity-target.c4c411cca33cf 2024-09-13 [email protected] intel_idle.c42268ad0eb41 2024-09-24 [email protected] ext.c56bcd0f07fdb 2024-09-05 [email protected] localio.c
1b11c4d36548 2024-09-01 [email protected] ec.c 7a51608d0125 2024-09-04 [email protected] btree_cache.c 7a51608d0125 2024-09-04 [email protected] btree_cache.c 691f2cba2291 2024-09-05 [email protected] btree_cache.c
Quoted 7 lines · Alexandre Ferrieux
Quoted 6 lines · Vadim Fedorenko
Quoted 4 lines · Alexandre Ferrieux
Can you please explain *why* in the first place you're saying "'static inline' is discouraged in .c files" ? I see no trace if this in
coding-style.rst, and the kernel contains hundreds of counter-examples.
Quoted 8 lines
Quoted 4 lines
The biggest reason is because it will mask unused function warnings when "static inline" function will not be used because of some future patches. There is no big reason to refactor old code that's why there are counter-examples in the kernel, but the new code shouldn't have it.
A macro doesn't elicit unused function warnings either, so this looks like a very weak motivation. While
coding-style.rstexplicitly encourages to use static inline instead of macros, as they have better type checking and syntaxic isolation.
Unused macro will not generate any code and will not make build time longer. But don't get me wrong, I'm not encouraging you to use macro in this case.
Quoted 3 lines
Regarding old vs new code, below are the last two month's fresh commits of "static inline" in *.c. So it looks like the motivation is not shared by other maintainers. Do we expect to see "local styles" emerge ?
There are some "local styles" differences in different subsystems. If you filter out netdev related diffs from awk-magic below, you will find that is was mostly refactoring of already existing code.
Anyways, you can ignore this suggestion, it's always up to submitter how to use review feedback given by others.
Quoted 27 lines
$ git log --pretty='%h %as %ae' -p | gawk '/^[0-9a-f]{12}/{c=$0;next}/^diff/{f=$NF;next}/^[+].*static.inline/{if (f~/[.]c$/){print c "\t"gensub(/.*\//,"","1",f)}}'
baa802d2aa5c2024-10-21 [email protected]verifier_const.cbaa802d2aa5c2024-10-21 [email protected]verifier_const.cd1744a4c975b2024-10-21 [email protected]amd.cd1744a4c975b2024-10-21 [email protected]amd.ca6e0ceb7bf482024-10-11 [email protected]maple.c78f636e82b222024-09-25 [email protected]ap_queue.c19773ec99720 2024-10-07 [email protected] disk_accounting.c 9b23fdbd5d29 2024-09-29 [email protected] inode.c 9b23fdbd5d29 2024-09-29 [email protected] inode.c
3d5854d75e312024-09-30 [email protected]kcore.c3d5854d75e312024-09-30 [email protected]kcore.c38864eccf78b2024-09-30 [email protected]fsck.cd278a9de5e182024-10-02 [email protected]init.cf811b83879fb2024-10-02 [email protected]dm-verity-target.c4c411cca33cf2024-09-13 [email protected]intel_idle.c42268ad0eb412024-09-24 [email protected]ext.c56bcd0f07fdb2024-09-05 [email protected]localio.c1b11c4d36548 2024-09-01 [email protected] ec.c 7a51608d0125 2024-09-04 [email protected] btree_cache.c 7a51608d0125 2024-09-04 [email protected] btree_cache.c 691f2cba2291 2024-09-05 [email protected] btree_cache.c
Simon Horman
reviewerQuoted 36 lines · Alexandre Ferrieux
Quoted 5 lines · Vadim Fedorenko
Quoted 4 lines · Alexandre Ferrieux
Quoted 3 lines · Pedro Tammela
Quoted 2 lines
Signed-off-by Alexandre Ferrieux <[email protected]>Quoted 8 lines
Quoted 5 lines
Quoted 1 line
SoB does not match sender, probably missing 'From:' tag
Due to dumb administrativia at my organization, I am compelled to post from my personal gmail accout in order for my posts to be acceptable on this mailing list; while I'd like to keep my official address in commit logs. Is it possible ?
Yes, it's possible, the author of commit in your local git should use email account of company, then git format-patch will generate proper header.
That's exactly what I did, and the file generated by format-patch does have the proper From:, but it gets overridden by Gmail when sending. That's why, as a last resort, I tried Signed-off-by... Any hope ?
Quoted 2 lines
you can add
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Ok.
Quoted 11 lines
Quoted 7 lines
Quoted 1 line
'static inline' is discouraged in .c files
Why ?
It could have been a local macro, but an inline has (a bit) better type checking. And I didn't want to add it to a .h that is included by many other unrelated components, as it makes no sense to them. So, what is the recommendation ?
Either move it to some local header file, or use 'static u32 handle2id(u32 h)'
and let compiler decide whether to include it or not.
Quoted 6 lines
I believe you mean "let the compiler decide whether to _inline_ it or not". Sure, with a sufficiently modern Gcc this will do. However, what about more exotic environments ? Wouldn't it risk a perf regression for style reasons ?
And speaking of style, what about the dozens of instances of "static inline" in
net/sched/*.calone ? Why is it a concern suddenly ?
Hi Alexandre,
It's not suddenly a concern. It is a long standing style guideline for Networking code, even if not always followed. Possibly some of the code you have found in net/sched/*.c is even longer standing than the guideline.
Please don't add new instances of inline to .c files unless there is a demonstrable - usually performance - reason to do so.
Thanks!
Quoted 11 lines · Simon Horman
Quoted 7 lines · Alexandre Ferrieux
I believe you mean "let the compiler decide whether to _inline_ it or not". Sure, with a sufficiently modern Gcc this will do. However, what about more exotic environments ? Wouldn't it risk a perf regression for style reasons ?
And speaking of style, what about the dozens of instances of "static inline" in
net/sched/*.calone ? Why is it a concern suddenly ?Hi Alexandre,
Quoted 7 lines
It's not suddenly a concern. It is a long standing style guideline for Networking code, even if not always followed. Possibly some of the code you have found in
net/sched/*.cis even longer standing than the guideline.Please don't add new instances of inline to .c files unless there is a demonstrable - usually performance - reason to do so.
Sure, I will abide in the next version :)
That said, please note it is hard to understand why such a rule would be enforced both locally and tacitly. Things would be entirely different if it were listed in coding-style.rst, advertising both consensus and wide applicability.
Simon Horman
reviewerQuoted 17 lines · Alexandre Ferrieux
Quoted 16 lines · Simon Horman
Quoted 7 lines · Alexandre Ferrieux
I believe you mean "let the compiler decide whether to _inline_ it or not". Sure, with a sufficiently modern Gcc this will do. However, what about more exotic environments ? Wouldn't it risk a perf regression for style reasons ?
And speaking of style, what about the dozens of instances of "static inline" in
net/sched/*.calone ? Why is it a concern suddenly ?Hi Alexandre,
It's not suddenly a concern. It is a long standing style guideline for Networking code, even if not always followed. Possibly some of the code you have found in
net/sched/*.cis even longer standing than the guideline.
Quoted 8 lines
Quoted 2 lines
Please don't add new instances of inline to .c files unless there is a demonstrable - usually performance - reason to do so.
Sure, I will abide in the next version :)
That said, please note it is hard to understand why such a rule would be enforced both locally and tacitly. Things would be entirely different if it were listed in
coding-style.rst, advertising both consensus and wide applicability.
Thanks,
I completely agree that it would be better if it was documented somewhere. I will add that to my TODO list.