Skip to content

Commit afe041c

Browse files
minhbq-99torvalds
authored andcommitted
hugetlb: fix hugetlb cgroup refcounting during mremap
When hugetlb_vm_op_open() is called during copy_vma(), we may take the reference to resv_map->css. Later, when clearing the reservation pointer of old_vma after transferring it to new_vma, we forget to drop the reference to resv_map->css. This leads to a reference leak of css. Fixes this by adding a check to drop reservation css reference in clear_vma_resv_huge_pages() Link: https://lkml.kernel.org/r/20211113154412.91134-1-minhquangbui99@gmail.com Fixes: 550a7d6 ("mm, hugepages: add mremap() support for hugepage backed vma") Signed-off-by: Bui Quang Minh <minhquangbui99@gmail.com> Reviewed-by: Mike Kravetz <mike.kravetz@oracle.com> Reviewed-by: Mina Almasry <almasrymina@google.com> Cc: Miaohe Lin <linmiaohe@huawei.com> Cc: Michal Hocko <mhocko@suse.com> Cc: Muchun Song <songmuchun@bytedance.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 34dbc3a commit afe041c

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

include/linux/hugetlb_cgroup.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ static inline void resv_map_dup_hugetlb_cgroup_uncharge_info(
128128
css_get(resv_map->css);
129129
}
130130

131+
static inline void resv_map_put_hugetlb_cgroup_uncharge_info(
132+
struct resv_map *resv_map)
133+
{
134+
if (resv_map->css)
135+
css_put(resv_map->css);
136+
}
137+
131138
extern int hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
132139
struct hugetlb_cgroup **ptr);
133140
extern int hugetlb_cgroup_charge_cgroup_rsvd(int idx, unsigned long nr_pages,
@@ -211,6 +218,11 @@ static inline void resv_map_dup_hugetlb_cgroup_uncharge_info(
211218
{
212219
}
213220

221+
static inline void resv_map_put_hugetlb_cgroup_uncharge_info(
222+
struct resv_map *resv_map)
223+
{
224+
}
225+
214226
static inline int hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
215227
struct hugetlb_cgroup **ptr)
216228
{

mm/hugetlb.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1037,8 +1037,10 @@ void clear_vma_resv_huge_pages(struct vm_area_struct *vma)
10371037
*/
10381038
struct resv_map *reservations = vma_resv_map(vma);
10391039

1040-
if (reservations && is_vma_resv_set(vma, HPAGE_RESV_OWNER))
1040+
if (reservations && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
1041+
resv_map_put_hugetlb_cgroup_uncharge_info(reservations);
10411042
kref_put(&reservations->refs, resv_map_release);
1043+
}
10421044

10431045
reset_vma_resv_huge_pages(vma);
10441046
}

0 commit comments

Comments
 (0)