linux/kernel.h: fix overflow for DIV_ROUND_UP_ULL
PATCH 1/1 linux/kernel.h: fix overflow for DIV_ROUND_UP_ULL
DIV_ROUND_UP_ULL adds the two arguments and then invokes DIV_ROUND_DOWN_ULL. But on a 32bit system the addition of two 32 bit values can overflow. DIV_ROUND_DOWN_ULL does it correctly and stashes the addition into a unsigned long long so cast the result to unsigned long long here to avoid the overflow condition.
--- include/linux/kernel.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/kernel.h +2 -1
@@ -93,7 +93,8 @@#define DIV_ROUND_DOWN_ULL (ll, d) \ ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; }) -#define DIV_ROUND_UP_ULL (ll, d) DIV_ROUND_DOWN_ULL((ll) + (d) - 1, (d)) +#define DIV_ROUND_UP_ULL (ll, d) \ + ({ DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d)) })#if BITS_PER_LONG ==32 # define DIV_ROUND_UP_SECTOR_T (ll,d) DIV_ROUND_UP_ULL(ll, d)
Signature · 1 line
2.20.1
Quoted 11 lines · On Tue, 25 Jun 2019 15:35:18 +0530 Vinod Koul <[email protected]> wrote:
DIV_ROUND_UP_ULLadds the two arguments and then invokesDIV_ROUND_DOWN_ULL. But on a 32bit system the addition of two 32 bit values can overflow.DIV_ROUND_DOWN_ULLdoes it correctly and stashes the addition into a unsigned long long so cast the result to unsigned long long here to avoid the overflow condition....
include/linux/kernel.h line 98
Quoted 7 lines
include/linux/kernel.h +2
-1@@ -93,7 +93,8 @@#define DIV_ROUND_DOWN_ULL (ll, d) \ ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; }) -#define DIV_ROUND_UP_ULL (ll, d) DIV_ROUND_DOWN_ULL((ll) + (d) - 1, (d)) +#define DIV_ROUND_UP_ULL (ll, d) \ + ({ DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d)) })
This clearly wasn't tested :(
fs/fs-writeback.c: In function wb_split_bdi_pages:./include/linux/kernel.h:97:65: error: expected ; before } token
({ DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d)) })
^
fs/fs-writeback.c:811:10: note: in expansion of macro DIV_ROUND_UP_ULL
return DIV_ROUND_UP_ULL((u64)nr_pages * this_bw, tot_bw);
From: Andrew Morton <[email protected]>
Subject: linux-kernelh-fix-overflow-for-div_round_up_ull-fix
DIV_ROUND_UP_ULL must be an rval
---
include/linux/kernel.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
include/linux/kernel.h renamed from include/linux/kernel.h~linux-kernelh-fix-overflow-for-div_round_up_ull-fix +4 -2
@@ -93,8 +93,10 @@#define DIV_ROUND_DOWN_ULL (ll, d) \ ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; }) -#define DIV_ROUND_UP_ULL (ll, d) \ - ({ DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d)) }) +#define DIV_ROUND_UP_ULL (ll, d) ({ \ + unsigned long long _tmp; \ + _tmp = DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d)); \ + _tmp; })#if BITS_PER_LONG ==32 # define DIV_ROUND_UP_SECTOR_T (ll,d) DIV_ROUND_UP_ULL(ll, d)
_
Quoted 48 lines · On Tue, 25 Jun 2019 15:29:38 -0700 Andrew Morton <[email protected]>…
Quoted 11 lines · On Tue, 25 Jun 2019 15:35:18 +0530 Vinod Koul <[email protected]> wrote:
DIV_ROUND_UP_ULLadds the two arguments and then invokesDIV_ROUND_DOWN_ULL. But on a 32bit system the addition of two 32 bit values can overflow.DIV_ROUND_DOWN_ULLdoes it correctly and stashes the addition into a unsigned long long so cast the result to unsigned long long here to avoid the overflow condition....
include/linux/kernel.h line 98
Quoted 7 lines
include/linux/kernel.h +2
-1@@ -93,7 +93,8 @@#define DIV_ROUND_DOWN_ULL (ll, d) \ ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; }) -#define DIV_ROUND_UP_ULL (ll, d) DIV_ROUND_DOWN_ULL((ll) + (d) - 1, (d)) +#define DIV_ROUND_UP_ULL (ll, d) \ + ({ DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d)) })This clearly wasn't tested :(
fs/fs-writeback.c: In functionwb_split_bdi_pages:./include/linux/kernel.h:97:65: error: expected ; before } token
({DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d))})^
fs/fs-writeback.c:811:10: note: in expansion of macroDIV_ROUND_UP_ULL
returnDIV_ROUND_UP_ULL((u64)nr_pages * this_bw, tot_bw);From: Andrew Morton <[email protected]>
Subject: linux-kernelh-fix-overflow-for-div_round_up_ull-fix
DIV_ROUND_UP_ULLmust be an rvalCc Bjorn Andersson <[email protected]>Cc Randy Dunlap <[email protected]>Cc Vinod Koul <[email protected]>Signed-off-by Andrew Morton <[email protected]>---
include/linux/kernel.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
include/linux/kernel.h line 99
Quoted 9 lines
include/linux/kernel.h renamed from include/linux/kernel.h~linux-kernelh-fix-overflow-for-div_round_up_ull-fix +4
-2@@ -93,8 +93,10 @@#define DIV_ROUND_DOWN_ULL (ll, d) \ ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; }) -#define DIV_ROUND_UP_ULL (ll, d) \ - ({ DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d)) }) +#define DIV_ROUND_UP_ULL (ll, d) ({ \ + unsigned long long _tmp; \ + _tmp = DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d)); \ + _tmp; })
Simpler:
include/linux/kernel.h renamed from include/linux/kernel.h~linux-kernelh-fix-overflow-for-div_round_up_ull-fix +1 -1
@@ -94,7 +94,7 @@ ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })#define DIV_ROUND_UP_ULL (ll, d) \ - ({ DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d)) }) + DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d))#if BITS_PER_LONG ==32 # define DIV_ROUND_UP_SECTOR_T (ll,d) DIV_ROUND_UP_ULL(ll, d)
_
Quoted 18 lines · Andrew Morton
Quoted 16 lines · On Tue, 25 Jun 2019 15:29:38 -0700 Andrew Morton <[email protected]>…
Quoted 14 lines · On Tue, 25 Jun 2019 15:35:18 +0530 Vinod Koul <[email protected]> wrote:
DIV_ROUND_UP_ULLadds the two arguments and then invokesDIV_ROUND_DOWN_ULL. But on a 32bit system the addition of two 32 bit values can overflow.DIV_ROUND_DOWN_ULLdoes it correctly and stashes the addition into a unsigned long long so cast the result to unsigned long long here to avoid the overflow condition....
include/linux/kernel.h +0
-0@@ -93,7 +93,8 @@#define DIV_ROUND_DOWN_ULL (ll, d) \ ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
Quoted 6 lines
Quoted 6 lines
include/linux/kernel.h line 98
Quoted 4 lines
include/linux/kernel.h +2
-1-#define DIV_ROUND_UP_ULL (ll, d) DIV_ROUND_DOWN_ULL((ll) + (d) - 1, (d)) +#define DIV_ROUND_UP_ULL (ll, d) \ + ({ DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d)) })This clearly wasn't tested :(
Apologies for that, I did test and stash, but failed to amend the commit. I should have noticed while sending but :(
Anyway I had the same conclusion as yous, so all is good.
Thanks for fixing this
Quoted 50 lines
Quoted 26 lines
fs/fs-writeback.c: In functionwb_split_bdi_pages:./include/linux/kernel.h:97:65: error: expected ; before } token
({DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d))})^
fs/fs-writeback.c:811:10: note: in expansion of macroDIV_ROUND_UP_ULL
returnDIV_ROUND_UP_ULL((u64)nr_pages * this_bw, tot_bw);From: Andrew Morton <[email protected]>
Subject: linux-kernelh-fix-overflow-for-div_round_up_ull-fix
DIV_ROUND_UP_ULLmust be an rvalCc Bjorn Andersson <[email protected]>Cc Randy Dunlap <[email protected]>Cc Vinod Koul <[email protected]>Signed-off-by Andrew Morton <[email protected]>---
include/linux/kernel.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)include/linux/kernel.h line 99
Quoted 9 lines
include/linux/kernel.h renamed from include/linux/kernel.h~linux-kernelh-fix-overflow-for-div_round_up_ull-fix +4
-2@@ -93,8 +93,10 @@#define DIV_ROUND_DOWN_ULL (ll, d) \ ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; }) -#define DIV_ROUND_UP_ULL (ll, d) \ - ({ DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d)) }) +#define DIV_ROUND_UP_ULL (ll, d) ({ \ + unsigned long long _tmp; \ + _tmp = DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d)); \ + _tmp; })Simpler:
include/linux/kernel.h renamed from include/linux/kernel.h~linux-kernelh-fix-overflow-for-div_round_up_ull-fix +1
-1@@ -94,7 +94,7 @@ ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })#define DIV_ROUND_UP_ULL (ll, d) \ - ({ DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d)) }) + DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d))#if BITS_PER_LONG ==32 # define DIV_ROUND_UP_SECTOR_T (ll,d) DIV_ROUND_UP_ULL(ll, d)_
Signature · 1 line
~Vinod