Skip to contentSkip to conversation

hugetlb_cgroup: fix illegal access to memory

PATCH Applied Mina Almasry 3 participants 5 messages Mar 13, 2020 – Mar 24, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hugetlb_cgroup: fix illegal access to memory

This appears to be a mistake in commit faced7e0806cf ("mm: hugetlb controller for cgroups v2"). Essentially that commit does
a hugetlb_cgroup_from_counter assuming that page_counter_try_charge has initialized counter, but if page_counter_try_charge has failed then it seems it does not initialize counter, so hugetlb_cgroup_from_counter(counter) ends up pointing to random memory, causing kasan to complain.

Solution, simply use h_cg, instead of hugetlb_cgroup_from_counter(counter), since that is a reference to the hugetlb_cgroup anyway. After this change kasan ceases to complain.

Reported-by [email protected]
Fixes commit faced7e0806cf ("mm: hugetlb controller for cgroups v2")
Cc Andrew Morton <[email protected]>
Cc Giuseppe Scrivano <[email protected]>
Cc Tejun Heo <[email protected]>
---
 mm/hugetlb_cgroup.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mm/hugetlb_cgroup.c +1 -2
index 7994eb8a2a0b4..aabf65d4d91ba 100644
@@ -259,8 +259,7 @@ static int __hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
 		    __hugetlb_cgroup_counter_from_cgroup(h_cg, idx, rsvd),
 		    nr_pages, &counter)) {
 		ret = -ENOMEM;
-		hugetlb_event(hugetlb_cgroup_from_counter(counter, idx), idx,
-			      HUGETLB_MAX);
+		hugetlb_event(h_cg, idx, HUGETLB_MAX);
 		css_put(&h_cg->css);
 		goto done;
 	}

--
2.25.1.481.gfbce0eb801-goog

#1 lore raw
Quoted 43 lines · Mina Almasry

This appears to be a mistake in commit faced7e0806cf ("mm: hugetlb controller for cgroups v2"). Essentially that commit does
a hugetlb_cgroup_from_counter assuming that page_counter_try_charge has initialized counter, but if page_counter_try_charge has failed then it seems it does not initialize counter, so hugetlb_cgroup_from_counter(counter) ends up pointing to random memory, causing kasan to complain.

Solution, simply use h_cg, instead of hugetlb_cgroup_from_counter(counter), since that is a reference to the hugetlb_cgroup anyway. After this change kasan ceases to complain.

Reported-by [email protected]
Fixes commit faced7e0806cf ("mm: hugetlb controller for cgroups v2")
Cc Andrew Morton <[email protected]>
Cc Giuseppe Scrivano <[email protected]>
Cc Tejun Heo <[email protected]>
---
 mm/hugetlb_cgroup.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mm/hugetlb_cgroup.c +1 -3
index 7994eb8a2a0b4..aabf65d4d91ba 100644
@@ -259,8 +259,7 @@ static int __hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
                    __hugetlb_cgroup_counter_from_cgroup(h_cg, idx, rsvd),
                    nr_pages, &counter)) {
                ret = -ENOMEM;
-               hugetlb_event(hugetlb_cgroup_from_counter(counter, idx), idx,
-                             HUGETLB_MAX);
+               hugetlb_event(h_cg, idx, HUGETLB_MAX);
                css_put(&h_cg->css);
                goto done;
        }
--
Quoted 1 line

2.25.1.481.gfbce0eb801-goog

The patch this fixes is in linus's tree, but isn't in 5.5 stable yet.

#2 lore raw
Quoted 29 lines · Mina Almasry
Quoted 28 lines · Mina Almasry

This appears to be a mistake in commit faced7e0806cf ("mm: hugetlb controller for cgroups v2"). Essentially that commit does
a hugetlb_cgroup_from_counter assuming that page_counter_try_charge has initialized counter, but if page_counter_try_charge has failed then it seems it does not initialize counter, so hugetlb_cgroup_from_counter(counter) ends up pointing to random memory, causing kasan to complain.

Solution, simply use h_cg, instead of hugetlb_cgroup_from_counter(counter), since that is a reference to the hugetlb_cgroup anyway. After this change kasan ceases to complain.

Reported-by [email protected]
Fixes commit faced7e0806cf ("mm: hugetlb controller for cgroups v2")
Cc Andrew Morton <[email protected]>
Cc Giuseppe Scrivano <[email protected]>
Cc Tejun Heo <[email protected]>
---
 mm/hugetlb_cgroup.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
Quoted 16 lines
Quoted 15 lines
mm/hugetlb_cgroup.c +1 -3
index 7994eb8a2a0b4..aabf65d4d91ba 100644
@@ -259,8 +259,7 @@ static int __hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
                    __hugetlb_cgroup_counter_from_cgroup(h_cg, idx, rsvd),
                    nr_pages, &counter)) {
                ret = -ENOMEM;
-               hugetlb_event(hugetlb_cgroup_from_counter(counter, idx), idx,
-                             HUGETLB_MAX);
+               hugetlb_event(h_cg, idx, HUGETLB_MAX);
                css_put(&h_cg->css);
                goto done;
        }
--
Quoted 1 line

2.25.1.481.gfbce0eb801-goog

Thanks,
Giuseppe

#3 lore raw

Tejun/Mike you want to also give this a quick look?

Quoted 54 lines · Giuseppe Scrivano
Quoted 29 lines · Mina Almasry
Quoted 28 lines · Mina Almasry

This appears to be a mistake in commit faced7e0806cf ("mm: hugetlb controller for cgroups v2"). Essentially that commit does
a hugetlb_cgroup_from_counter assuming that page_counter_try_charge has initialized counter, but if page_counter_try_charge has failed then it seems it does not initialize counter, so hugetlb_cgroup_from_counter(counter) ends up pointing to random memory, causing kasan to complain.

Solution, simply use h_cg, instead of hugetlb_cgroup_from_counter(counter), since that is a reference to the hugetlb_cgroup anyway. After this change kasan ceases to complain.

Reported-by [email protected]
Fixes commit faced7e0806cf ("mm: hugetlb controller for cgroups v2")
Cc Andrew Morton <[email protected]>
Cc Giuseppe Scrivano <[email protected]>
Cc Tejun Heo <[email protected]>
---
 mm/hugetlb_cgroup.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
Quoted 16 lines
Quoted 15 lines
mm/hugetlb_cgroup.c +1 -3
index 7994eb8a2a0b4..aabf65d4d91ba 100644
@@ -259,8 +259,7 @@ static int __hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
                    __hugetlb_cgroup_counter_from_cgroup(h_cg, idx, rsvd),
                    nr_pages, &counter)) {
                ret = -ENOMEM;
-               hugetlb_event(hugetlb_cgroup_from_counter(counter, idx), idx,
-                             HUGETLB_MAX);
+               hugetlb_event(h_cg, idx, HUGETLB_MAX);
                css_put(&h_cg->css);
                goto done;
        }
--
Quoted 1 line

2.25.1.481.gfbce0eb801-goog

Thanks,
Giuseppe

Acked-by on v1 1/1
#4 lore raw
In reply to Mina Almasry
Quoted 12 lines · Mina Almasry

This appears to be a mistake in commit faced7e0806cf ("mm: hugetlb controller for cgroups v2"). Essentially that commit does
a hugetlb_cgroup_from_counter assuming that page_counter_try_charge has initialized counter, but if page_counter_try_charge has failed then it seems it does not initialize counter, so hugetlb_cgroup_from_counter(counter) ends up pointing to random memory, causing kasan to complain.

Solution, simply use h_cg, instead of hugetlb_cgroup_from_counter(counter), since that is a reference to the hugetlb_cgroup anyway. After this change kasan ceases to complain.

Quoted 10 lines
Reported-by [email protected]
Fixes commit faced7e0806cf ("mm: hugetlb controller for cgroups v2")
Cc Andrew Morton <[email protected]>
Cc Giuseppe Scrivano <[email protected]>
Cc Tejun Heo <[email protected]>

Thanks.

Signature · 1 line
tejun