From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 03A54633 for ; Wed, 2 Nov 2022 03:45:15 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 10A62C433D6; Wed, 2 Nov 2022 03:45:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1667360715; bh=nfibOujb8CDCpAAkqQoNIZTWN/+WR4g+z4XJFx+EaS0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UuOrFOWkKS7wYTjTGqRaAxwW1y5O5uxAADUH4IDA4hpgqtXUSG3YJ/x8KLRaf63PA GnowmmPTr1bzVSZ7RS3cUFUlSzRHJBDcFj5V5eW7bqJKHMDIv423k1sdROwt5G6zbw 8gzgJ+MbGV5cuD6SrOuXgWCSgLk/A1o1mXU6AB6o= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Rik van Riel , Mike Kravetz , Naoya Horiguchi , Glen McCready , Muchun Song , Andrew Morton Subject: [PATCH 4.9 24/44] mm,hugetlb: take hugetlb_lock before decrementing h->resv_huge_pages Date: Wed, 2 Nov 2022 03:35:10 +0100 Message-Id: <20221102022049.910274368@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221102022049.017479464@linuxfoundation.org> References: <20221102022049.017479464@linuxfoundation.org> User-Agent: quilt/0.67 Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Rik van Riel commit 12df140f0bdfae5dcfc81800970dd7f6f632e00c upstream. The h->*_huge_pages counters are protected by the hugetlb_lock, but alloc_huge_page has a corner case where it can decrement the counter outside of the lock. This could lead to a corrupted value of h->resv_huge_pages, which we have observed on our systems. Take the hugetlb_lock before decrementing h->resv_huge_pages to avoid a potential race. Link: https://lkml.kernel.org/r/20221017202505.0e6a4fcd@imladris.surriel.com Fixes: a88c76954804 ("mm: hugetlb: fix hugepage memory leak caused by wrong reserve count") Signed-off-by: Rik van Riel Reviewed-by: Mike Kravetz Cc: Naoya Horiguchi Cc: Glen McCready Cc: Mike Kravetz Cc: Muchun Song Cc: Signed-off-by: Andrew Morton Signed-off-by: Mike Kravetz Signed-off-by: Greg Kroah-Hartman --- mm/hugetlb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -2104,11 +2104,11 @@ struct page *alloc_huge_page(struct vm_a page = __alloc_buddy_huge_page_with_mpol(h, vma, addr); if (!page) goto out_uncharge_cgroup; + spin_lock(&hugetlb_lock); if (!avoid_reserve && vma_has_reserves(vma, gbl_chg)) { SetPagePrivate(page); h->resv_huge_pages--; } - spin_lock(&hugetlb_lock); list_move(&page->lru, &h->hugepage_activelist); /* Fall through */ }