cgroup/misc: Fix an overflow
PATCH 1/1 cgroup/misc: Fix an overflow
The variable 'new_usage' in misc_cg_try_charge() may overflow if it becomes above INT_MAX. This was observed when I implement the new SGX EPC cgroup[1] as a misc cgroup and test on a platform with large SGX EPC sizes.
Change type of new_usage to long from int and check overflow.
[1] https://lore.kernel.org/linux-sgx/[email protected]/
--- kernel/cgroup/misc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/cgroup/misc.c +3 -3
@@ -143,7 +143,7 @@ int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg,struct misc_cg *i, *j;int ret;struct misc_res *res; -int new_usage; +long new_usage;if (!(valid_type (type) && cg &&READ_ONCE (misc_res_capacity[type])))return -EINVAL ;
@@ -153,10 +153,10 @@ int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg,for (i = cg; i; i =parent_misc (i)) { res = &i->res[type]; - new_usage =atomic_long_add_return (amount, &res->usage);if (new_usage >READ_ONCE (res->max) || - new_usage >READ_ONCE (misc_res_capacity[type])) { + new_usage >READ_ONCE (misc_res_capacity[type]) || + new_usage <0 ) { ret = -EBUSY ; goto err_charge; }
Signature · 1 line
2.25.1
Tejun Heo
maintainerQuoted 12 lines · Haitao Huang
The variable '
new_usage' inmisc_cg_try_charge()may overflow if it becomes aboveINT_MAX. This was observed when I implement the new SGX EPC cgroup[1] as a misc cgroup and test on a platform with large SGX EPC sizes.Change type of
new_usageto long from int and check overflow.kernel/cgroup/misc.c +1
-1-int new_usage; +long new_usage;if (!(valid_type (type) && cg &&READ_ONCE (misc_res_capacity[type])))return -EINVAL ;
kernel/cgroup/misc.c int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg, · line 159
Quoted 9 lines
kernel/cgroup/misc.c +2
-2@@ -153,10 +153,10 @@ int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg,for (i = cg; i; i =parent_misc (i)) { res = &i->res[type]; - new_usage =atomic_long_add_return (amount, &res->usage);if (new_usage >READ_ONCE (res->max) || - new_usage >READ_ONCE (misc_res_capacity[type])) { + new_usage >READ_ONCE (misc_res_capacity[type]) || + new_usage <0 ) {
Applying to cgroup/for-6.6 (as none of the current users are affected by this) but I think the right thing to do here is using explicit 64bit types (s64 or u64) for the resource counters rather than depending on the long width.
Thanks.
Signature · 1 line
tejun
Jarkko Sakkinen
maintainerQuoted 29 lines · Haitao Huang
The variable '
new_usage' inmisc_cg_try_charge()may overflow if it becomes aboveINT_MAX. This was observed when I implement the new SGX EPC cgroup[1] as a misc cgroup and test on a platform with large SGX EPC sizes.Change type of
new_usageto long from int and check overflow.Fixes a72232eabdfcf ("cgroup: Add misc cgroup controller")Signed-off-by Haitao Huang <[email protected]>[1] https://lore.kernel.org/linux-sgx/[email protected]/
--- kernel/cgroup/misc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)kernel/cgroup/misc.c +1
-1@@ -143,7 +143,7 @@ int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg,struct misc_cg *i, *j;int ret;struct misc_res *res; -int new_usage; +long new_usage;if (!(valid_type (type) && cg &&READ_ONCE (misc_res_capacity[type])))return -EINVAL ;
kernel/cgroup/misc.c int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg, · line 156
Quoted 4 lines
kernel/cgroup/misc.c +0
-1@@ -153,10 +153,10 @@ int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg,for (i = cg; i; i =parent_misc (i)) { res = &i->res[type]; -
This is extra noise in the patch, please remove the change.
Quoted 10 lines
kernel/cgroup/misc.c +2
-1new_usage =atomic_long_add_return (amount, &res->usage);if (new_usage >READ_ONCE (res->max) || - new_usage >READ_ONCE (misc_res_capacity[type])) { + new_usage >READ_ONCE (misc_res_capacity[type]) || + new_usage <0 ) { ret = -EBUSY ; goto err_charge; }Signature · 1 line
2.25.1
BR, Jarkko
Tejun Heo
maintainerQuoted 31 lines · Jarkko Sakkinen
Quoted 30 lines · Haitao Huang
The variable '
new_usage' inmisc_cg_try_charge()may overflow if it becomes aboveINT_MAX. This was observed when I implement the new SGX EPC cgroup[1] as a misc cgroup and test on a platform with large SGX EPC sizes.Change type of
new_usageto long from int and check overflow.Fixes a72232eabdfcf ("cgroup: Add misc cgroup controller")--- kernel/cgroup/misc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)kernel/cgroup/misc.c +1
-1@@ -143,7 +143,7 @@ int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg,struct misc_cg *i, *j;int ret;struct misc_res *res; -int new_usage; +long new_usage;if (!(valid_type (type) && cg &&READ_ONCE (misc_res_capacity[type])))return -EINVAL ;@@ -153,10 +153,10 @@ int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg,
Quoted 5 lines
kernel/cgroup/misc.c int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg, · line 156
Quoted 3 lines
kernel/cgroup/misc.c +0
-1for (i = cg; i; i =parent_misc (i)) { res = &i->res[type]; -This is extra noise in the patch, please remove the change.
Lemme just revert it. Haitao, can you instead make the resource counters and all related variables explicit 64bit instead?
Thanks.
Signature · 1 line
tejun
Quoted 36 lines · Tejun Heo
Quoted 35 lines · Jarkko Sakkinen
Quoted 3 lines · Haitao Huang
The variable '
new_usage' inmisc_cg_try_charge()may overflow if it becomes aboveINT_MAX. This was observed when I implement the new SGX EPC cgroup[1] as a misc cgroup and test on a platform with large SGXEPC
Quoted 8 lines
sizes.
Change type of
new_usageto long from int and check overflow.Fixes a72232eabdfcf ("cgroup: Add misc cgroup controller")[1]
kernel/cgroup/misc.c int misc_cg_try_charge(enum misc_res_type type,
Quoted 9 lines
--- kernel/cgroup/misc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)kernel/cgroup/misc.c +0
-0@@ -143,7 +143,7 @@ int misc_cg_try_charge(enum misc_res_type type,struct
misc_cg*cg,kernel/cgroup/misc.c int misc_cg_try_charge(enum misc_res_type type,
Quoted 9 lines
kernel/cgroup/misc.c +1
-1struct misc_cg *i, *j;int ret;struct misc_res *res; -int new_usage; +long new_usage;if (!(valid_type (type) && cg &&READ_ONCE (misc_res_capacity[type])))return -EINVAL ;@@ -153,10 +153,10 @@ int misc_cg_try_charge(enum misc_res_type type,struct
misc_cg*cg,Quoted 1 line
Quoted 9 lines
Quoted 5 lines
kernel/cgroup/misc.c int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg, · line 156
Quoted 3 lines
kernel/cgroup/misc.c +0
-1for (i = cg; i; i =parent_misc (i)) { res = &i->res[type]; -This is extra noise in the patch, please remove the change.
Lemme just revert it. Haitao, can you instead make the resource counters and all related variables explicit 64bit instead?
Will do.
Thanks
Haitao
Quoted 43 lines · Haitao Huang
Quoted 41 lines · Tejun Heo
Quoted 40 lines · Jarkko Sakkinen
Quoted 3 lines · Haitao Huang
The variable '
new_usage' inmisc_cg_try_charge()may overflow if it becomes aboveINT_MAX. This was observed when I implement the new SGX EPC cgroup[1] as a misc cgroup and test on a platform with large SGXEPC
Quoted 8 lines
sizes.
Change type of
new_usageto long from int and check overflow.Fixes a72232eabdfcf ("cgroup: Add misc cgroup controller")[1]
kernel/cgroup/misc.c int misc_cg_try_charge(enum misc_res_type type,
Quoted 9 lines
--- kernel/cgroup/misc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)kernel/cgroup/misc.c +0
-0@@ -143,7 +143,7 @@ int misc_cg_try_charge(enum misc_res_type type,struct
misc_cg*cg,kernel/cgroup/misc.c int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg,
Quoted 7 lines
kernel/cgroup/misc.c +1
-1struct misc_cg *i, *j;int ret;struct misc_res *res; -int new_usage; +long new_usage; if (!(valid_type(type) && cg &&
READ_ONCE(misc_res_capacity[type])))kernel/cgroup/misc.c int misc_cg_try_charge(enum misc_res_type
Quoted 2 lines
kernel/cgroup/misc.c +0
-0return -EINVAL ;@@ -153,10 +153,10 @@ int misc_cg_try_charge(enum misc_res_typetype, struct
misc_cg*cg,Quoted 4 lines
kernel/cgroup/misc.c +0
-1for (i = cg; i; i =parent_misc (i)) { res = &i->res[type]; -
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
This is extra noise in the patch, please remove the change.
Lemme just revert it. Haitao, can you instead make the resource counters and all related variables explicit 64bit instead?
Will do.
Actually, we are using atomic_long_t for 'current' which is the same width as long defined by arch/compiler. So new_usage should be long to be consistent?
ditto for event counter. Only max is plain unsigned long but I think it is also OK as it only compared with 'current' without any arithmetic ops involved. Did I miss something here?
Thanks
Haitao
Tejun Heo
maintainerHello,
Quoted 3 lines · Haitao Huang
Actually, we are using
atomic_long_tfor 'current' which is the same width as long defined byarch/compiler. Sonew_usageshould be long to be consistent?
We can use atomic64_t, right? It's slower on 32bit machines but I think it'd be better to guarantee resource counter range than micro-optimizing charge operations. None of the current users are hot enough for this to matter and if somebody becomes that hot, the difference between atomic_t and atomic64_t isn't gonna matter that much. We'd need to batch allocations per-cpu and so on.
Quoted 4 lines
ditto for event counter. Only max is plain unsigned long but I think it is also OK as it only compared with 'current' without any arithmetic ops involved.
Did I miss something here?
I'm saying that it'd be better to make everything explicitly 64bit.
Thanks.
Signature · 1 line
tejun
PATCH 1/2 cgroup/misc: Fix an overflow
The variable 'new_usage' in misc_cg_try_charge() may overflow if it becomes above INT_MAX. This was observed when I implement the new SGX EPC cgroup[1] as a misc cgroup and test on a platform with large SGX EPC sizes.
Change type of new_usage to long from int and check overflow.
[1] https://lore.kernel.org/linux-sgx/[email protected]/
--- kernel/cgroup/misc.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/cgroup/misc.c +3 -2
@@ -143,7 +143,7 @@ int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg,struct misc_cg *i, *j;int ret;struct misc_res *res; -int new_usage; +long new_usage;if (!(valid_type (type) && cg &&READ_ONCE (misc_res_capacity[type])))return -EINVAL ;
@@ -156,7 +156,8 @@ int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg, new_usage =atomic_long_add_return (amount, &res->usage);if (new_usage >READ_ONCE (res->max) || - new_usage >READ_ONCE (misc_res_capacity[type])) { + new_usage >READ_ONCE (misc_res_capacity[type]) || + new_usage <0 ) { ret = -EBUSY ; goto err_charge; }
Signature · 1 line
2.25.1
PATCH 2/2 cgroup/misc: Change counters to be explicit 64bit types
So the variables can account for resources of huge quantities even on 32-bit machines.
--- include/linux/misc_cgroup.h | 22 +++++++-------- kernel/cgroup/misc.c | 53 +++++++++++++++++++------------------ 2 files changed, 38 insertions(+), 37 deletions(-)
include/linux/misc_cgroup.h +11 -11
@@ -34,9 +34,9 @@ struct misc_cg;* @failed: True if charged failed for the resource in a cgroup. */ struct misc_res { -unsigned long max; -atomic_long_t usage; -atomic_long_t events; +u64 max; +atomic64_t usage; +atomic64_t events; };/**
@@ -53,12 +53,12 @@ struct misc_cg {struct misc_res res[MISC_CG_RES_TYPES ]; }; -unsigned long misc_cg_res_total_usage (enum misc_res_type type); -int misc_cg_set_capacity (enum misc_res_type type,unsigned long capacity); +u64 misc_cg_res_total_usage (enum misc_res_type type); +int misc_cg_set_capacity (enum misc_res_type type,u64 capacity);int misc_cg_try_charge (enum misc_res_type type,struct misc_cg *cg, -unsigned long amount); +u64 amount);void misc_cg_uncharge (enum misc_res_type type,struct misc_cg *cg, -unsigned long amount); +u64 amount);/** * css_misc() - Get misc cgroup from the css.
@@ -99,27 +99,27 @@ static inline void put_misc_cg(struct misc_cg *cg)#else /* !CONFIG_CGROUP_MISC */ -static inline unsigned long misc_cg_res_total_usage (enum misc_res_type type) +static inline u64 misc_cg_res_total_usage (enum misc_res_type type) {return 0 ; }static inline int misc_cg_set_capacity (enum misc_res_type type, -unsigned long capacity) +u64 capacity) {return 0 ; }static inline int misc_cg_try_charge (enum misc_res_type type,struct misc_cg *cg, -unsigned long amount) +u64 amount) {return 0 ; }static inline void misc_cg_uncharge (enum misc_res_type type,struct misc_cg *cg, -unsigned long amount) +u64 amount) { }
kernel/cgroup/misc.c +27 -26
178 lines not on this page · raw message
Signature · 1 line
2.25.1
Tejun Heo
maintainerRe: [PATCH 2/2] cgroup/misc: Change counters to be explicit 64bit types
Quoted 4 lines · Haitao Huang
So the variables can account for resources of huge quantities even on 32-bit machines.
Applied to cgroup/for-6.6 with some whitespace adjustments. I think the code is broken when we cross the signed boundary but that's not a new problem caused by your patch. I think what we should do is to treat atomic64_t reads as u64 instead of putting it in s64.
Thanks.
Signature · 1 line
tejun
Re: [PATCH 2/2] cgroup/misc: Change counters to be explicit 64bit types
Hi
Quoted 11 lines · Tejun Heo
Quoted 4 lines · Haitao Huang
So the variables can account for resources of huge quantities even on 32-bit machines.
Applied to cgroup/for-6.6 with some whitespace adjustments. I think the code is broken when we cross the signed boundary but that's not a new problem caused by your patch. I think what we should do is to treat
atomic64_treads as u64 instead of putting it in s64.
Thanks. I think you meant the 'new_usage' in try_charge. I'll send a patch.
BR
Haitao
PATCH 1/1 cgroup/misc: Store atomic64_t reads to u64
In reply to Tejun HeoChange 'new_usage' type to u64 so it can be compared with unsigned 'max' and 'capacity' properly even if the value crosses the signed boundary.
--- kernel/cgroup/misc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/cgroup/misc.c +1 -1
@@ -142,7 +142,7 @@ int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg, u64 amount)struct misc_cg *i, *j;int ret;struct misc_res *res; -s64 new_usage; +u64 new_usage;if (!(valid_type (type) && cg &&READ_ONCE (misc_res_capacity[type])))return -EINVAL ;
base-commit: 32bf85c60ca3584a7ba3bef19da2779b73b2e7d6
Signature · 1 line
2.25.1
Re: [PATCH] cgroup/misc: Store atomic64_t reads to u64
Quoted 4 lines · Haitao Huang
Change '
new_usage' type to u64 so it can be compared with unsigned 'max' and 'capacity' properly even if the value crosses the signed boundary.
Applied to cgroup/for-6.6.
Thanks.
Signature · 1 line
tejun
Hi
Quoted 14 lines · Tejun Heo
Hello,
Quoted 3 lines · Haitao Huang
Actually, we are using
atomic_long_tfor 'current' which is the same width as long defined byarch/compiler. Sonew_usageshould be long to be consistent?We can use
atomic64_t, right? It's slower on 32bit machines but I think it'd be better to guarantee resource counter range than micro-optimizing charge operations. None of the current users are hot enough for this to matter and if somebody becomes that hot, the difference betweenatomic_tandatomic64_tisn't gonna matter that much. We'd need to batch allocations per-cpu and so on.
Quoted 7 lines
Quoted 4 lines
ditto for event counter. Only max is plain unsigned long but I think it is also OK as it only compared with 'current' without any arithmetic ops involved.
Did I miss something here?I'm saying that it'd be better to make everything explicitly 64bit.
Thanks for the explanation. I think I got it and sent it as a separate patch now just to be sure. BR
Haitao
Jarkko Sakkinen
maintainerQuoted 7 lines · Tejun Heo
Hello,
Quoted 3 lines · Haitao Huang
Actually, we are using
atomic_long_tfor 'current' which is the same width as long defined byarch/compiler. Sonew_usageshould be long to be consistent?
Quoted 6 lines
We can use
atomic64_t, right? It's slower on 32bit machines but I think it'd be better to guarantee resource counter range than micro-optimizing charge operations. None of the current users are hot enough for this to matter and if somebody becomes that hot, the difference betweenatomic_tandatomic64_tisn't gonna matter that much. We'd need to batch allocations per-cpu and so on.
In our context, the microcode of SGX could support 32-bit but by design we only support 64-bit. So at least with the current implementation this would not be an issue for SGX.
BR, Jarkko