2010-11-09 01:16:33

by Greg Thelen

[permalink] [raw]
Subject: [PATCH] memcg: avoid overflow in memcg_hierarchical_free_pages()

Use page counts rather than byte counts to avoid overflowing
unsigned long local variables.

Signed-off-by: Greg Thelen <[email protected]>
---
mm/memcontrol.c | 10 +++++-----
1 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 6c7115d..b287afd 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -1345,17 +1345,17 @@ memcg_hierarchical_free_pages(struct mem_cgroup *mem)
{
unsigned long free, min_free;

- min_free = global_page_state(NR_FREE_PAGES) << PAGE_SHIFT;
+ min_free = global_page_state(NR_FREE_PAGES);

while (mem) {
- free = res_counter_read_u64(&mem->res, RES_LIMIT) -
- res_counter_read_u64(&mem->res, RES_USAGE);
+ free = (res_counter_read_u64(&mem->res, RES_LIMIT) -
+ res_counter_read_u64(&mem->res, RES_USAGE)) >>
+ PAGE_SHIFT;
min_free = min(min_free, free);
mem = parent_mem_cgroup(mem);
}

- /* Translate free memory in pages */
- return min_free >> PAGE_SHIFT;
+ return min_free;
}

/*
--
1.7.3.1


2010-11-09 01:28:10

by Minchan Kim

[permalink] [raw]
Subject: Re: [PATCH] memcg: avoid overflow in memcg_hierarchical_free_pages()

On Tue, Nov 9, 2010 at 10:15 AM, Greg Thelen <[email protected]> wrote:
> Use page counts rather than byte counts to avoid overflowing
> unsigned long local variables.
>
> Signed-off-by: Greg Thelen <[email protected]>
Reviewed-by: Minchan Kim <[email protected]>
--
Kind regards,
Minchan Kim

2010-11-09 01:48:14

by Johannes Weiner

[permalink] [raw]
Subject: Re: [PATCH] memcg: avoid overflow in memcg_hierarchical_free_pages()

On Mon, Nov 08, 2010 at 05:15:20PM -0800, Greg Thelen wrote:
> Use page counts rather than byte counts to avoid overflowing
> unsigned long local variables.
>
> Signed-off-by: Greg Thelen <[email protected]>

Reviewed-by: Johannes Weiner <[email protected]>

2010-11-09 03:46:41

by Daisuke Nishimura

[permalink] [raw]
Subject: Re: [PATCH] memcg: avoid overflow in memcg_hierarchical_free_pages()

On Mon, 8 Nov 2010 17:15:20 -0800
Greg Thelen <[email protected]> wrote:

> Use page counts rather than byte counts to avoid overflowing
> unsigned long local variables.
>
> Signed-off-by: Greg Thelen <[email protected]>
> ---
> mm/memcontrol.c | 10 +++++-----
> 1 files changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index 6c7115d..b287afd 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -1345,17 +1345,17 @@ memcg_hierarchical_free_pages(struct mem_cgroup *mem)
> {
> unsigned long free, min_free;
>
hmm, the default value of RES_LIMIT is LLONG_MAX, so I think we must declare
"free" as unsinged long long to avoid overflow.

Thanks,
Daisuke Nishimura.

> - min_free = global_page_state(NR_FREE_PAGES) << PAGE_SHIFT;
> + min_free = global_page_state(NR_FREE_PAGES);
>
> while (mem) {
> - free = res_counter_read_u64(&mem->res, RES_LIMIT) -
> - res_counter_read_u64(&mem->res, RES_USAGE);
> + free = (res_counter_read_u64(&mem->res, RES_LIMIT) -
> + res_counter_read_u64(&mem->res, RES_USAGE)) >>
> + PAGE_SHIFT;
> min_free = min(min_free, free);
> mem = parent_mem_cgroup(mem);
> }
>
> - /* Translate free memory in pages */
> - return min_free >> PAGE_SHIFT;
> + return min_free;
> }
>
> /*
> --
> 1.7.3.1
>

2010-11-09 03:52:45

by Greg Thelen

[permalink] [raw]
Subject: Re: [PATCH] memcg: avoid overflow in memcg_hierarchical_free_pages()

On Mon, Nov 8, 2010 at 7:44 PM, Daisuke Nishimura
<[email protected]> wrote:
> On Mon, ?8 Nov 2010 17:15:20 -0800
> Greg Thelen <[email protected]> wrote:
>
>> Use page counts rather than byte counts to avoid overflowing
>> unsigned long local variables.
>>
>> Signed-off-by: Greg Thelen <[email protected]>
>> ---
>> ?mm/memcontrol.c | ? 10 +++++-----
>> ?1 files changed, 5 insertions(+), 5 deletions(-)
>>
>> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
>> index 6c7115d..b287afd 100644
>> --- a/mm/memcontrol.c
>> +++ b/mm/memcontrol.c
>> @@ -1345,17 +1345,17 @@ memcg_hierarchical_free_pages(struct mem_cgroup *mem)
>> ?{
>> ? ? ? unsigned long free, min_free;
>>
> hmm, the default value of RES_LIMIT is LLONG_MAX, so I think we must declare
> "free" as unsinged long long to avoid overflow.

Agreed. I am testing a fix for that issue now. I do not want
complicate this patch with the RES_LIMIT issue you mention. The fix
will be in a separate patch.

> Thanks,
> Daisuke Nishimura.
>
>> - ? ? min_free = global_page_state(NR_FREE_PAGES) << PAGE_SHIFT;
>> + ? ? min_free = global_page_state(NR_FREE_PAGES);
>>
>> ? ? ? while (mem) {
>> - ? ? ? ? ? ? free = res_counter_read_u64(&mem->res, RES_LIMIT) -
>> - ? ? ? ? ? ? ? ? ? ? res_counter_read_u64(&mem->res, RES_USAGE);
>> + ? ? ? ? ? ? free = (res_counter_read_u64(&mem->res, RES_LIMIT) -
>> + ? ? ? ? ? ? ? ? ? ? res_counter_read_u64(&mem->res, RES_USAGE)) >>
>> + ? ? ? ? ? ? ? ? ? ? PAGE_SHIFT;
>> ? ? ? ? ? ? ? min_free = min(min_free, free);
>> ? ? ? ? ? ? ? mem = parent_mem_cgroup(mem);
>> ? ? ? }
>>
>> - ? ? /* Translate free memory in pages */
>> - ? ? return min_free >> PAGE_SHIFT;
>> + ? ? return min_free;
>> ?}
>>
>> ?/*
>> --
>> 1.7.3.1
>>
>

2010-11-16 03:51:10

by Kamezawa Hiroyuki

[permalink] [raw]
Subject: Re: [PATCH] memcg: avoid overflow in memcg_hierarchical_free_pages()

On Mon, 8 Nov 2010 17:15:20 -0800
Greg Thelen <[email protected]> wrote:

> Use page counts rather than byte counts to avoid overflowing
> unsigned long local variables.
>
> Signed-off-by: Greg Thelen <[email protected]>

Acked-by: KAMEZAWA Hiroyuki <[email protected]>