Fix clamp() of ip_vs_conn_tab on small memory systems.
Subsystems (5)
PATCH 1/1 Fix clamp() of ip_vs_conn_tab on small memory systems.
The intention of the code seems to be that the minimum table size should be 256 (1 << min).
However the code uses max = clamp(20, 5, max_avail) which implies the author thought max_avail could be less than 5.
But clamp(val, min, max) is only well defined for max >= min. If max < min whether is returns min or max depends on the order of the comparisons.
Change to clamp(max_avail, 5, 20) which has the expected behaviour.
Replace the clamp_val() on the line below with clamp(). clamp_val() is just 'an accident waiting to happen' and not needed here.
(Although I actually doubt the code is used on small memory systems.)
Detected by compile time checks added to clamp(), specifically:minmax.h: use BUILD_BUG_ON_MSG() for the lo < hi test in clamp()
--- net/netfilter/ipvs/ip_vs_conn.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/netfilter/ipvs/ip_vs_conn.c +2 -2
@@ -1495,8 +1495,8 @@ int __init ip_vs_conn_init(void) max_avail -=2 ;/* ~4 in hash row */ max_avail -=1 ;/* IPVS up to 1/2 of mem */ max_avail -=order_base_2 (sizeof (struct ip_vs_conn )); - max =clamp (max, min, max_avail); - ip_vs_conn_tab_bits =clamp_val (ip_vs_conn_tab_bits, min, max); + max =clamp (max_avail, min, max); + ip_vs_conn_tab_bits =clamp (ip_vs_conn_tab_bits, min, max); ip_vs_conn_tab_size =1 << ip_vs_conn_tab_bits; ip_vs_conn_tab_mask = ip_vs_conn_tab_size -1 ;
Signature · 5 lines
2.17.1 - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Julian Anastasov
maintainerHello,
Quoted 3 lines · David Laight
The intention of the code seems to be that the minimum table size should be 256 (1 << min).
However the code uses max =clamp(20, 5, max_avail)which implies
Actually, it tries to reduce max=20 (max possible) belowmax_avail: [8 .. max_avail]. Not sure what 5 is here...
Quoted 4 lines
the author thought
max_availcould be less than 5.
Butclamp(val, min, max)is only well defined for max >= min. If max < min whether is returns min or max depends on the order of the comparisons.
Looks like max_avail goes below 8 ? What value you see
for such small system?
Quoted 1 line
Change to
clamp(max_avail, 5, 20)which has the expected behaviour.
It should be clamp(max_avail, 8, 20)
Quoted 3 lines
Replace the
clamp_val()on the line below withclamp().clamp_val()is just 'an accident waiting to happen' and not needed here.
OK
Quoted 5 lines
Fixes 4f325e26277b6(Although I actually doubt the code is used on small memory systems.)
Detected by compile time checks added to
clamp(), specifically:minmax.h: useBUILD_BUG_ON_MSG()for the lo < hi test inclamp()
Existing or new check? Does it happen that max_avail
is a constant, so that a compile check triggers?
Quoted 2 lines
Signed-off-by David Laight <[email protected]>
The code below looks ok to me but can you change the
comments above to more correctly specify the values and if the problem is that max_avail goes below 8 (min).
net/netfilter/ipvs/ip_vs_conn.c int __init ip_vs_conn_init(void) · line 1497
Quoted 12 lines
--- net/netfilter/ipvs/ip_vs_conn.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)net/netfilter/ipvs/ip_vs_conn.c +0
-0@@ -1495,8 +1495,8 @@ int __init ip_vs_conn_init(void) max_avail -=2 ;/* ~4 in hash row */ max_avail -=1 ;/* IPVS up to 1/2 of mem */ max_avail -=order_base_2 (sizeof (struct ip_vs_conn ));
More likely we can additionally clamp max_avail here:
max_avail = max(min, max_avail);
But your solution solves the problem with less lines.
Quoted 9 lines
net/netfilter/ipvs/ip_vs_conn.c +2
-2- max =clamp (max, min, max_avail); - ip_vs_conn_tab_bits =clamp_val (ip_vs_conn_tab_bits, min, max); + max =clamp (max_avail, min, max); + ip_vs_conn_tab_bits =clamp (ip_vs_conn_tab_bits, min, max); ip_vs_conn_tab_size =1 << ip_vs_conn_tab_bits; ip_vs_conn_tab_mask = ip_vs_conn_tab_size -1 ;Signature · 1 line
2.17.1
Regards
--
Julian Anastasov <[email protected]>
From: Julian Anastasov
Quoted 10 lines
Sent: 06 December 2024 12:19
Quoted 3 lines · David Laight
The intention of the code seems to be that the minimum table size should be 256 (1 << min).
However the code uses max =clamp(20, 5, max_avail)which impliesActually, it tries to reduce max=20 (max possible) below
max_avail: [8 ..max_avail]. Not sure what 5 is here...
Me mistyping values between two windows :-)
Well min(max, max_avail) would be the reduced upper limit. But you'd still fall foul of the compiler propagating the 'n > 1' check in order_base_2() further down the function.
Quoted 7 lines
Quoted 4 lines
the author thought
max_availcould be less than 5.
Butclamp(val, min, max)is only well defined for max >= min. If max < min whether is returns min or max depends on the order of the comparisons.Looks like
max_availgoes below 8 ? What value you see
for such small system?
I'm not, but clearly you thought the value could be small otherwise the code would only have a 'max' limit.
(Apart from a 'sanity' min of maybe 2 to stop the code breaking.)
Quoted 11 lines
Quoted 2 lines
Change to
clamp(max_avail, 5, 20)which has the expected behaviour.It should be
clamp(max_avail, 8, 20)Quoted 3 lines
Replace the
clamp_val()on the line below withclamp().clamp_val()is just 'an accident waiting to happen' and not needed here.OK
Quoted 8 lines
Quoted 5 lines
Fixes 4f325e26277b6(Although I actually doubt the code is used on small memory systems.)
Detected by compile time checks added to
clamp(), specifically:minmax.h: useBUILD_BUG_ON_MSG()for the lo < hi test inclamp()Existing or new check? Does it happen that
max_avail
is a constant, so that a compile check triggers?
Is all stems from order_base_2(totalram_pages()). order_base_2(n) is 'n > 1 ? ilog2(n - 1) + 1 : 0'. And the compiler generates two copies of the code that follows for the 'constant zero' and ilog2() values.
And the 'zero' case compiles clamp(20, 8, 0) which is errored. Note that it is only executed if totalram_pages() is zero, but it is always compiled 'just in case'.
Quoted 23 lines
Quoted 3 lines
Signed-off-by David Laight <[email protected]>The code below looks ok to me but can you change the
comments above to more correctly specify the values and if the problem is thatmax_availgoes below 8 (min).net/netfilter/ipvs/ip_vs_conn.c int __init ip_vs_conn_init(void) · line 1497
Quoted 12 lines
--- net/netfilter/ipvs/ip_vs_conn.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)net/netfilter/ipvs/ip_vs_conn.c +0
-0@@ -1495,8 +1495,8 @@ int __init ip_vs_conn_init(void) max_avail -=2 ;/* ~4 in hash row */ max_avail -=1 ;/* IPVS up to 1/2 of mem */ max_avail -=order_base_2 (sizeof (struct ip_vs_conn ));More likely we can additionally clamp
max_availhere:
Quoted 3 lines
max_avail=max(min, max_avail);But your solution solves the problem with less lines.
And less code in the path that is actually executed.
David
Quoted 8 lines
Quoted 8 lines
net/netfilter/ipvs/ip_vs_conn.c +2
-2- max =clamp (max, min, max_avail); - ip_vs_conn_tab_bits =clamp_val (ip_vs_conn_tab_bits, min, max); + max =clamp (max_avail, min, max); + ip_vs_conn_tab_bits =clamp (ip_vs_conn_tab_bits, min, max); ip_vs_conn_tab_size =1 << ip_vs_conn_tab_bits; ip_vs_conn_tab_mask = ip_vs_conn_tab_size -1 ;
Quoted 7 lines
Quoted 2 lines
--
2.17.1Regards
--
Julian Anastasov <[email protected]>
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Julian Anastasov
maintainerHello,
Quoted 23 lines · David Laight
From: Julian Anastasov
Quoted 10 lines
Sent: 06 December 2024 12:19
Quoted 3 lines · David Laight
The intention of the code seems to be that the minimum table size should be 256 (1 << min).
However the code uses max =clamp(20, 5, max_avail)which impliesActually, it tries to reduce max=20 (max possible) below
max_avail: [8 ..max_avail]. Not sure what 5 is here...Me mistyping values between two windows :-)
Well
min(max, max_avail)would be the reduced upper limit. But you'd still fall foul of the compiler propagating the 'n > 1' check inorder_base_2()further down the function.Quoted 5 lines
Quoted 4 lines
the author thought
max_availcould be less than 5.
Butclamp(val, min, max)is only well defined for max >= min. If max < min whether is returns min or max depends on the order of the comparisons.
Quoted 6 lines
Quoted 2 lines
Looks like
max_availgoes below 8 ? What value you see
for such small system?I'm not, but clearly you thought the value could be small otherwise the code would only have a 'max' limit.
(Apart from a 'sanity' min of maybe 2 to stop the code breaking.)
I'm not sure how much memory we can see in small system,
IMHO, problem should not be possible in practice:
- nobody expects 0 from totalram_pages() in the code
- order_base_2(sizeof(struct ip_vs_conn)) is probably 8 on 32-bit
- PAGE_SHIFT: 12 (for 4KB) or more?
So, if totalram_pages() returns below 128 pages (4KB each)max_avail will be below 19 (7 + 12), then 19 is reduced with 2 + 1 and becomes 16, finally with 8 (from the 2nd order_base_2) to reach 16-8=8. You need a system with less than 512KB (19 bits) to trigger problem in clamp() that will lead to max below 8. Further, without checks, for ip_vs_conn_tab_bits=1 we need totalram_pages() to return 0 pages.
Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Detected by compile time checks added to
clamp(), specifically:minmax.h: useBUILD_BUG_ON_MSG()for the lo < hi test inclamp()Existing or new check? Does it happen that
max_avail
is a constant, so that a compile check triggers?
Quoted 7 lines
Is all stems from order_base_2(totalram_pages()).
order_base_2(n)is 'n > 1 ? ilog2(n - 1) + 1 : 0'. And the compiler generates two copies of the code that follows for the 'constant zero' andilog2()values.
And the 'zero' case compilesclamp(20, 8, 0)which is errored. Note that it is only executed iftotalram_pages()is zero, but it is always compiled 'just in case'.
I'm confused with these compiler issues, if you
think we should go with the patch just decide if it is a net or net-next material. Your change is safer for bad max_avail values but I don't expect to see problem while running without the change, except the building bugs.
Also, please use nf/nf-next tag to avoid any
confusion with upstreaming...
Regards
--
Julian Anastasov <[email protected]>
From: Julian Anastasov
Quoted 1 line
Sent: 06 December 2024 16:23
...
Quoted 6 lines
I'm not sure how much memory we can see in small system,
IMHO, problem should not be possible in practice:- nobody expects 0 from
totalram_pages()in the code-
order_base_2(sizeof(struct ip_vs_conn))is probably 8 on 32-bit
It is 0x120 bytes on 64bit, so 8 could well be right.
Quoted 7 lines
-
PAGE_SHIFT: 12 (for 4KB) or more?So, if
totalram_pages()returns below 128 pages (4KB each)max_availwill be below 19 (7 + 12), then 19 is reduced with 2 + 1 and becomes 16, finally with 8 (from the 2ndorder_base_2) to reach 16-8=8. You need a system with less than 512KB (19 bits) to trigger problem inclamp()that will lead to max below 8.
Which pretty much won't happen, I think my (dead) sun3 has more than that.
Quoted 18 lines
Further, without
checks, for ip_vs_conn_tab_bits=1 we needtotalram_pages()to return 0 pages.Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Detected by compile time checks added to
clamp(), specifically:minmax.h: useBUILD_BUG_ON_MSG()for the lo < hi test inclamp()Existing or new check? Does it happen that
max_avail
is a constant, so that a compile check triggers?Quoted 7 lines
Is all stems from order_base_2(totalram_pages()).
order_base_2(n)is 'n > 1 ? ilog2(n - 1) + 1 : 0'. And the compiler generates two copies of the code that follows for the 'constant zero' andilog2()values.
And the 'zero' case compilesclamp(20, 8, 0)which is errored. Note that it is only executed iftotalram_pages()is zero, but it is always compiled 'just in case'.
Quoted 1 line
I'm confused with these compiler issues,
The compiler is just doing its job.
Consider this expression:
(x >= 1 ? 2 * x : 1) - 1 It is likely to get converted to: (x >= 1 ? 2 * x - 1 : 0)
to avoid the subtract when x < 1.
The same thing is happening here.order_base_2() has a (condition ? fn() : 0) in it.
All the +/- constants get moved inside, on 64bit that is +12 -2 -1 -9 = 0. Then the clamp() with constants gets moved inside:
(condition ? clamp(27, 8, fn() + 0) : clamp(27, 8, 0 + 0))
Now, at runtime, we know that 'condition' is true and (fn() >= 8) so the first clamp() is valid and the second one never used. But this isn't known by the compiler and clamp() detects the invalid call and generates a warning.
Quoted 8 lines
if you
think we should go with the patch just decide if it is a net or net-next material. Your change is safer for badmax_availvalues but I don't expect to see problem while running without the change, except the building bugs.Also, please use nf/nf-next tag to avoid any
confusion with upstreaming...
I've copied Andrew M - he's taken the minmax.h change into his mm tree. This is one of the build breakages.
It probably only needs to go into next for now (via some route). But I can image the minmax.h changes getting backported a bit.
David
Quoted 5 lines
Regards
--
Julian Anastasov <[email protected]>
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Julian Anastasov
maintainerHello,
Quoted 11 lines · David Laight
From: Julian Anastasov
Quoted 1 line
Sent: 06 December 2024 16:23
...
Quoted 6 lines
I'm not sure how much memory we can see in small system,
IMHO, problem should not be possible in practice:- nobody expects 0 from
totalram_pages()in the code-
order_base_2(sizeof(struct ip_vs_conn))is probably 8 on 32-bitIt is
0x120bytes on 64bit, so 8 could well be right.
That is already for 9 :)
Quoted 21 lines
Quoted 9 lines
Quoted 7 lines
Is all stems from order_base_2(totalram_pages()).
order_base_2(n)is 'n > 1 ? ilog2(n - 1) + 1 : 0'. And the compiler generates two copies of the code that follows for the 'constant zero' andilog2()values.
And the 'zero' case compilesclamp(20, 8, 0)which is errored. Note that it is only executed iftotalram_pages()is zero, but it is always compiled 'just in case'.I'm confused with these compiler issues,
The compiler is just doing its job.
Consider this expression:(x >= 1 ? 2 * x : 1) - 1 It is likely to get converted to: (x >= 1 ? 2 * x - 1 : 0)to avoid the subtract when x < 1.
The same thing is happening here.
order_base_2()has a (condition ?fn(): 0) in it.
All the +/- constants get moved inside, on 64bit that is +12 -2 -1 -9 = 0. Then theclamp()with constants gets moved inside:
Quoted 5 lines
(condition ? clamp(27, 8, fn() + 0) : clamp(27, 8, 0 + 0))Now, at runtime, we know that 'condition' is true and (
fn()>= 8) so the firstclamp()is valid and the second one never used. But this isn't known by the compiler andclamp()detects the invalid call and generates a warning.
I see, such optimizations are beyond my expectations,
I used max_avail var to separate the operations between different macro calls but in the end they are mixed together...
Quoted 6 lines
Quoted 6 lines
if you
think we should go with the patch just decide if it is a net or net-next material. Your change is safer for badmax_availvalues but I don't expect to see problem while running without the change, except the building bugs.
Quoted 8 lines
Quoted 2 lines
Also, please use nf/nf-next tag to avoid any
confusion with upstreaming...I've copied Andrew M - he's taken the
minmax.hchange into his mm tree. This is one of the build breakages.It probably only needs to go into next for now (via some route). But I can image the
minmax.hchanges getting backported a bit.
OK, then can you send v2 with Fixes header, precised comments
and nf tag, it fixes a recent commit, so it can be backported easily...
Regards
--
Julian Anastasov <[email protected]>