From patchwork Mon Jan 4 06:58:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11996223 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.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,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 5033BC433DB for ; Mon, 4 Jan 2021 07:02:36 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B70CB21D79 for ; Mon, 4 Jan 2021 07:02:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B70CB21D79 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 2F3E58D0001; Mon, 4 Jan 2021 02:02:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2CA476B00AD; Mon, 4 Jan 2021 02:02:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1DFE28D0001; Mon, 4 Jan 2021 02:02:35 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0064.hostedemail.com [216.40.44.64]) by kanga.kvack.org (Postfix) with ESMTP id 08AAA6B00AB for ; Mon, 4 Jan 2021 02:02:35 -0500 (EST) Received: from smtpin06.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id AFFD9362C for ; Mon, 4 Jan 2021 07:02:34 +0000 (UTC) X-FDA: 77667199428.06.arch83_19004a9274ce Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin06.hostedemail.com (Postfix) with ESMTP id 954C010043ABD for ; Mon, 4 Jan 2021 07:02:34 +0000 (UTC) X-HE-Tag: arch83_19004a9274ce X-Filterd-Recvd-Size: 7364 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) by imf33.hostedemail.com (Postfix) with ESMTP for ; Mon, 4 Jan 2021 07:02:34 +0000 (UTC) Received: by mail-pl1-f179.google.com with SMTP id v3so14009597plz.13 for ; Sun, 03 Jan 2021 23:02:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Ecn176xO4zQ8Y5+qvJO+NXfQmQmbZVPueFRQXY2zG70=; b=V1viqRcLBhAjMI4s5hkLwNzsHo44mZ1JVGTUfwxULDQ4QljgyDmKFC7FWmn5TTH6Sx OCeUI59t+P/ZcDGB60unp9LOjdx5uwLi8PxK6IuO7n5b1teoQDHDUq8OXtp7K7WySchi 5r8YZA+2YSFbwY1YdjU1RoPe1YGiEJSVH4Lc2tIVDFxCTJDL6iKXy9WgAFNsygxk/izN N+Pcs5HnmXZxLZwHBB7IDlFDZ8RWKR0aXdVYHtHjPv+jcqvqS2S4Czad3Z00iGBc3Set pbHT3VLWs30Kk3+NDES94Y4BM5SIDCw1xtAB4eVrFsJjVs00HWyFm0/q/aUESg6fL6B9 C7kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Ecn176xO4zQ8Y5+qvJO+NXfQmQmbZVPueFRQXY2zG70=; b=I3/TYp0fftnGK3P3alk5xR2AMScCVe3v+Ov8ecUhj28KvkgnRjKxUa1ZjdV9vVRAQy DmcxGM/45bTv8HQ2IY3f453lJyerfpnFURQimpe9tBRdHXQIeU//9822Z4q03BSDTNJG CDL8YVOjGzoGJPXXCzfsnqAzdKots3nzrdAETBmLHgCbv01LH4Qj2YXhFX7P/L0Z4TXt aJezgCPCUm080/kzx+IJaDrsDvwfD5jyHKL0/iraXSTw3abIWfekITk+qYzD/dAvuKdK aLBPVPIwUgSVzfC6Fc+DP1WXiuDNjLbfwzo0lyHyXwT5BcP5Ep77yO1AYE0V8uGB8zg5 HttQ== X-Gm-Message-State: AOAM5331sSKQcmIobNaPoysE0iP7jRbjqyvZEqTrNpWYI1/zYPk5xKyj bEy3gNQbzy0knYFg07Ml8GZiLA== X-Google-Smtp-Source: ABdhPJxoUVmQraBDL53NIFbrMpNmkyyk7mkatnVzcuLbK4z/W+93TnPT0tjGcq72noGrgnbp0oofLg== X-Received: by 2002:a17:902:b948:b029:dc:102c:b4a with SMTP id h8-20020a170902b948b02900dc102c0b4amr62777604pls.3.1609743753038; Sun, 03 Jan 2021 23:02:33 -0800 (PST) Received: from localhost.localdomain ([240e:b1:e401:3::6]) by smtp.gmail.com with ESMTPSA id v1sm36306648pga.63.2021.01.03.23.02.05 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 03 Jan 2021 23:02:32 -0800 (PST) From: Muchun Song To: mike.kravetz@oracle.com, akpm@linux-foundation.org Cc: hillf.zj@alibaba-inc.com, n-horiguchi@ah.jp.nec.com, ak@linux.intel.com, yongjun_wei@trendmicro.com.cn, mhocko@suse.cz, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song Subject: [PATCH 3/6] mm: hugetlb: fix a race between freeing and dissolving the page Date: Mon, 4 Jan 2021 14:58:40 +0800 Message-Id: <20210104065843.5658-3-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20210104065843.5658-1-songmuchun@bytedance.com> References: <20210104065843.5658-1-songmuchun@bytedance.com> MIME-Version: 1.0 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: There is a race condition between __free_huge_page() and dissolve_free_huge_page(). CPU0: CPU1: // page_count(page) == 1 put_page(page) __free_huge_page(page) dissolve_free_huge_page(page) spin_lock(&hugetlb_lock) // PageHuge(page) && !page_count(page) update_and_free_page(page) // page is freed to the buddy spin_unlock(&hugetlb_lock) spin_lock(&hugetlb_lock) clear_page_huge_active(page) enqueue_huge_page(page) // It is wrong, the page is already freed spin_unlock(&hugetlb_lock) The race windows is between put_page() and spin_lock() which is in the __free_huge_page(). We should make sure that the page is already on the free list when it is dissolved. Fixes: c8721bbbdd36 ("mm: memory-hotplug: enable memory hotplug to handle hugepage") Signed-off-by: Muchun Song --- mm/hugetlb.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 1f3bf1710b66..72608008f8b4 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -79,6 +79,21 @@ DEFINE_SPINLOCK(hugetlb_lock); static int num_fault_mutexes; struct mutex *hugetlb_fault_mutex_table ____cacheline_aligned_in_smp; +static inline bool PageHugeFreed(struct page *head) +{ + return page_private(head) == -1UL; +} + +static inline void SetPageHugeFreed(struct page *head) +{ + set_page_private(head + 4, -1UL); +} + +static inline void ClearPageHugeFreed(struct page *head) +{ + set_page_private(head + 4, 0); +} + /* Forward declaration */ static int hugetlb_acct_memory(struct hstate *h, long delta); @@ -1028,6 +1043,7 @@ static void enqueue_huge_page(struct hstate *h, struct page *page) list_move(&page->lru, &h->hugepage_freelists[nid]); h->free_huge_pages++; h->free_huge_pages_node[nid]++; + SetPageHugeFreed(page); } static struct page *dequeue_huge_page_node_exact(struct hstate *h, int nid) @@ -1044,6 +1060,7 @@ static struct page *dequeue_huge_page_node_exact(struct hstate *h, int nid) list_move(&page->lru, &h->hugepage_activelist); set_page_refcounted(page); + ClearPageHugeFreed(page); h->free_huge_pages--; h->free_huge_pages_node[nid]--; return page; @@ -1504,6 +1521,7 @@ static void prep_new_huge_page(struct hstate *h, struct page *page, int nid) spin_lock(&hugetlb_lock); h->nr_huge_pages++; h->nr_huge_pages_node[nid]++; + ClearPageHugeFreed(page); spin_unlock(&hugetlb_lock); } @@ -1770,6 +1788,36 @@ int dissolve_free_huge_page(struct page *page) int nid = page_to_nid(head); if (h->free_huge_pages - h->resv_huge_pages == 0) goto out; + + /* + * There is a race condition between __free_huge_page() + * and dissolve_free_huge_page(). + * + * CPU0: CPU1: + * + * // page_count(page) == 1 + * put_page(page) + * __free_huge_page(page) + * dissolve_free_huge_page(page) + * spin_lock(&hugetlb_lock) + * // PageHuge(page) && !page_count(page) + * update_and_free_page(page) + * // page is freed to the buddy + * spin_unlock(&hugetlb_lock) + * spin_lock(&hugetlb_lock) + * enqueue_huge_page(page) + * // It is wrong, the page is already freed + * spin_unlock(&hugetlb_lock) + * + * The race window is between put_page() and spin_lock() which + * is in the __free_huge_page(). + * + * We should make sure that the page is already on the free list + * when it is dissolved. + */ + if (unlikely(!PageHugeFreed(head))) + goto out; + /* * Move PageHWPoison flag from head page to the raw error page, * which makes any subpages rather than the error page reusable.