From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8542CC433F5 for ; Sat, 20 Nov 2021 00:48:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C7D436B0080; Fri, 19 Nov 2021 19:43:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C2C376B0081; Fri, 19 Nov 2021 19:43:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B1BE86B0082; Fri, 19 Nov 2021 19:43:52 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0201.hostedemail.com [216.40.44.201]) by kanga.kvack.org (Postfix) with ESMTP id A46566B0080 for ; Fri, 19 Nov 2021 19:43:52 -0500 (EST) Received: from smtpin10.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 6FA89181CAC43 for ; Sat, 20 Nov 2021 00:43:42 +0000 (UTC) X-FDA: 78827460726.10.46CE1AE Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf09.hostedemail.com (Postfix) with ESMTP id D3CDF300011E for ; Sat, 20 Nov 2021 00:43:39 +0000 (UTC) Received: by mail.kernel.org (Postfix) with ESMTPSA id EA0F961A86; Sat, 20 Nov 2021 00:43:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1637369021; bh=O/JdrIesE0G8FKH3RyIHlFgd93mrEsm4ABmBxynphyc=; h=Date:From:To:Subject:In-Reply-To:From; b=Cl66eE4fBES2VmTsmX/0RQ0vri64kTgW1OiFbVN7ktfhwEvnXP+fOlpsPSb4G1bza ySzLTMN74T22TH94pP5xeMwMNiclr8HsWP81dpxiEyBpcodFFMtj4wYFtyWxTyY6bN jOkKw0As/ysYrwYGDjHfa/y0ov/7ERFbApIAotIc= Date: Fri, 19 Nov 2021 16:43:40 -0800 From: Andrew Morton To: akpm@linux-foundation.org, almasrymina@google.com, linmiaohe@huawei.com, linux-mm@kvack.org, mhocko@suse.com, mike.kravetz@oracle.com, minhquangbui99@gmail.com, mm-commits@vger.kernel.org, songmuchun@bytedance.com, torvalds@linux-foundation.org Subject: [patch 09/15] hugetlb: fix hugetlb cgroup refcounting during mremap Message-ID: <20211120004340.S1hmWsXgy%akpm@linux-foundation.org> In-Reply-To: <20211119164248.50feee07c5d2cc6cc4addf97@linux-foundation.org> User-Agent: s-nail v14.8.16 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: D3CDF300011E X-Stat-Signature: 4xwd93f9qtb7isusg16cqi8t76k3ea7b Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=Cl66eE4f; dmarc=none; spf=pass (imf09.hostedemail.com: domain of akpm@linux-foundation.org designates 198.145.29.99 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org X-HE-Tag: 1637369019-54160 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Bui Quang Minh Subject: 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: 550a7d60bd5e35 ("mm, hugepages: add mremap() support for hugepage backed vma") Signed-off-by: Bui Quang Minh Reviewed-by: Mike Kravetz Reviewed-by: Mina Almasry Cc: Miaohe Lin Cc: Michal Hocko Cc: Muchun Song Signed-off-by: Andrew Morton --- include/linux/hugetlb_cgroup.h | 12 ++++++++++++ mm/hugetlb.c | 4 +++- 2 files changed, 15 insertions(+), 1 deletion(-) --- a/include/linux/hugetlb_cgroup.h~hugetlb-fix-hugetlb-cgroup-refcounting-during-mremap +++ a/include/linux/hugetlb_cgroup.h @@ -128,6 +128,13 @@ static inline void resv_map_dup_hugetlb_ css_get(resv_map->css); } +static inline void resv_map_put_hugetlb_cgroup_uncharge_info( + struct resv_map *resv_map) +{ + if (resv_map->css) + css_put(resv_map->css); +} + extern int hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages, struct hugetlb_cgroup **ptr); extern int hugetlb_cgroup_charge_cgroup_rsvd(int idx, unsigned long nr_pages, @@ -210,6 +217,11 @@ static inline void resv_map_dup_hugetlb_ struct resv_map *resv_map) { } + +static inline void resv_map_put_hugetlb_cgroup_uncharge_info( + struct resv_map *resv_map) +{ +} static inline int hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages, struct hugetlb_cgroup **ptr) --- a/mm/hugetlb.c~hugetlb-fix-hugetlb-cgroup-refcounting-during-mremap +++ a/mm/hugetlb.c @@ -1037,8 +1037,10 @@ void clear_vma_resv_huge_pages(struct vm */ struct resv_map *reservations = vma_resv_map(vma); - if (reservations && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) + if (reservations && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) { + resv_map_put_hugetlb_cgroup_uncharge_info(reservations); kref_put(&reservations->refs, resv_map_release); + } reset_vma_resv_huge_pages(vma); } _