From patchwork Tue Jan 26 11:55:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miaohe Lin X-Patchwork-Id: 12046189 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 24A27C433E0 for ; Tue, 26 Jan 2021 11:55:34 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id A257B2311D for ; Tue, 26 Jan 2021 11:55:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A257B2311D Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=huawei.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id D252C8D00C6; Tue, 26 Jan 2021 06:55:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C81D18D00B0; Tue, 26 Jan 2021 06:55:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B96688D00C6; Tue, 26 Jan 2021 06:55:32 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0076.hostedemail.com [216.40.44.76]) by kanga.kvack.org (Postfix) with ESMTP id A69608D00B0 for ; Tue, 26 Jan 2021 06:55:32 -0500 (EST) Received: from smtpin05.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 69A49181AEF3F for ; Tue, 26 Jan 2021 11:55:32 +0000 (UTC) X-FDA: 77747771304.05.match19_0f16dc22758e Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin05.hostedemail.com (Postfix) with ESMTP id 4FFCE1802B4B1 for ; Tue, 26 Jan 2021 11:55:32 +0000 (UTC) X-HE-Tag: match19_0f16dc22758e X-Filterd-Recvd-Size: 2858 Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) by imf38.hostedemail.com (Postfix) with ESMTP for ; Tue, 26 Jan 2021 11:55:30 +0000 (UTC) Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.58]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4DQ4rg1t7Yzl2wG; Tue, 26 Jan 2021 19:53:55 +0800 (CST) Received: from huawei.com (10.175.104.175) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.498.0; Tue, 26 Jan 2021 19:55:17 +0800 From: Miaohe Lin To: , CC: , , Subject: [PATCH] mm/hugetlb: Fix use after free when subpool max_hpages accounting is not enabled Date: Tue, 26 Jan 2021 06:55:10 -0500 Message-ID: <20210126115510.53374-1-linmiaohe@huawei.com> X-Mailer: git-send-email 2.19.1 MIME-Version: 1.0 X-Originating-IP: [10.175.104.175] X-CFilter-Loop: Reflected 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: When subpool max_hpages accounting is not enabled, used_hpages is always 0 and might lead to release subpool prematurely because it indicates no pages are used now while there might be. In order to fix this issue, we should check used_hpages == 0 iff max_hpages accounting is enabled. As max_hpages accounting should be enabled in most common case, this is not worth a Cc stable. Signed-off-by: Hongxiang Lou Signed-off-by: Miaohe Lin Reviewed-by: Mike Kravetz --- mm/hugetlb.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 777bc0e45bf3..53ea65d1c5ab 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -97,16 +97,26 @@ static inline void ClearPageHugeFreed(struct page *head) /* Forward declaration */ static int hugetlb_acct_memory(struct hstate *h, long delta); -static inline void unlock_or_release_subpool(struct hugepage_subpool *spool) +static inline bool subpool_is_free(struct hugepage_subpool *spool) { - bool free = (spool->count == 0) && (spool->used_hpages == 0); + if (spool->count) + return false; + if (spool->max_hpages != -1) + return spool->used_hpages == 0; + if (spool->min_hpages != -1) + return spool->rsv_hpages == spool->min_hpages; + return true; +} + +static inline void unlock_or_release_subpool(struct hugepage_subpool *spool) +{ spin_unlock(&spool->lock); /* If no pages are used, and no other handles to the subpool * remain, give up any reservations based on minimum size and * free the subpool */ - if (free) { + if (subpool_is_free(spool)) { if (spool->min_hpages != -1) hugetlb_acct_memory(spool->hstate, -spool->min_hpages);