From patchwork Fri Jan 15 12:49: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: 12022567 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 69692C433DB for ; Fri, 15 Jan 2021 12:51:27 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id ED61E221F7 for ; Fri, 15 Jan 2021 12:51:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org ED61E221F7 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 9250A6B0137; Fri, 15 Jan 2021 07:51:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8FD2E8D0163; Fri, 15 Jan 2021 07:51:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8121B8D0162; Fri, 15 Jan 2021 07:51:26 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0165.hostedemail.com [216.40.44.165]) by kanga.kvack.org (Postfix) with ESMTP id 6A6496B0137 for ; Fri, 15 Jan 2021 07:51:26 -0500 (EST) Received: from smtpin07.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 3689C180AD804 for ; Fri, 15 Jan 2021 12:51:26 +0000 (UTC) X-FDA: 77707995372.07.beef98_500e00b2752f Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin07.hostedemail.com (Postfix) with ESMTP id 163D01803F9A3 for ; Fri, 15 Jan 2021 12:51:26 +0000 (UTC) X-HE-Tag: beef98_500e00b2752f X-Filterd-Recvd-Size: 7174 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) by imf36.hostedemail.com (Postfix) with ESMTP for ; Fri, 15 Jan 2021 12:51:25 +0000 (UTC) Received: by mail-pf1-f182.google.com with SMTP id c79so5443832pfc.2 for ; Fri, 15 Jan 2021 04:51:25 -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=SURxKip/mYhGMjUhZ4z0ytkBtEFScnH+ZvTCaUzxn2g=; b=MAimcbY5VsTKL/RaGaw4hIl8R9xImx+LUTRLJVzSdfL7kG+q7uWjOLYNRnOA1NKImV 9CvO995egFhsKUKUSwvsjXXNCEjAgQGuO/bM9Lvssk/40vSBgY83ISDADN2s9zXvoMFM krNsjbH+OH3PZij2ap32x7TcQrhN5aOGFympAuk+/pPGSUy/2CQPC1w+fBxxK2U9B1FF oivOaww9QNpmKkjgIGQAMZLGi0IitEq3Q8rleu/VDxkel1aOzLBwXZBHVdC24m9qWrO/ LVUZ6ma90SGca09LcCN2UHAx/h1xMoQsSR16ZH+12gIyptgn+ZHtOUuBwdVldlWAmYNH kKjA== 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=SURxKip/mYhGMjUhZ4z0ytkBtEFScnH+ZvTCaUzxn2g=; b=Ran5+MhIz050ULmLUgyuWyMo4DsKm4BeH9kcoWA0lBsXJ+3tt2bVjRTBMHIcjFjx5Z sa4pFibdGF0w86nxH7+m9zn+dwJogqg1X98JexbqTodq6RB7OMk2jMyNZUzbFJKk7UzN ABJCOuOz7WWQbyoruqvhq6vsffKsRa8Y4QbVKHUTsxHHuiAPct0+nfIYvwGcBMqtG11p oVQ+OhsOoVp3YWQOZK0nY9XCw79N/ias5VoGts2fzIFhzRcqiVwNIPE1lgzSqR5VEjA6 6uUO3GBTKO1gxIVjZoNIX+dvDoWYn7xXUnAdO0LU4+rBklL7SSHMPrIHHNoRaojiAq8a hSmw== X-Gm-Message-State: AOAM533pWOSHl2Mooj1yci/melMqU2hl2A6Qj3xMykd0crgAszk2m6Yg 5NJHPj5Zg/2y6S+sWhHR5fCTzg== X-Google-Smtp-Source: ABdhPJxxYQDcef8kwkMh6J61KL1JdqF9D4h57D3AcxgVwSS/v61gjpqzyebdv++OOEWmh63fWsTZeQ== X-Received: by 2002:a62:864e:0:b029:1ab:e82c:d724 with SMTP id x75-20020a62864e0000b02901abe82cd724mr12462709pfd.9.1610715084805; Fri, 15 Jan 2021 04:51:24 -0800 (PST) Received: from localhost.localdomain ([139.177.225.247]) by smtp.gmail.com with ESMTPSA id c5sm8193600pjo.4.2021.01.15.04.51.20 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 15 Jan 2021 04:51:23 -0800 (PST) From: Muchun Song To: mike.kravetz@oracle.com, akpm@linux-foundation.org Cc: n-horiguchi@ah.jp.nec.com, ak@linux.intel.com, mhocko@suse.cz, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song , stable@vger.kernel.org Subject: [PATCH v6 3/5] mm: hugetlb: fix a race between freeing and dissolving the page Date: Fri, 15 Jan 2021 20:49:40 +0800 Message-Id: <20210115124942.46403-4-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20210115124942.46403-1-songmuchun@bytedance.com> References: <20210115124942.46403-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 dissolve_free_huge_page(). We should make sure that the page is already on the free list when it is dissolved. As a result __free_huge_page would corrupt page(s) already in the buddy allocator. Fixes: c8721bbbdd36 ("mm: memory-hotplug: enable memory hotplug to handle hugepage") Signed-off-by: Muchun Song Reviewed-by: Mike Kravetz Cc: stable@vger.kernel.org Reviewed-by: Oscar Salvador Acked-by: Michal Hocko --- mm/hugetlb.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 4741d60f8955..b99fe4a2b435 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 + 4) == -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); } @@ -1754,6 +1772,7 @@ int dissolve_free_huge_page(struct page *page) { int rc = -EBUSY; +retry: /* Not to disrupt normal path by vainly holding hugetlb_lock */ if (!PageHuge(page)) return 0; @@ -1770,6 +1789,26 @@ 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; + + /* + * We should make sure that the page is already on the free list + * when it is dissolved. + */ + if (unlikely(!PageHugeFreed(head))) { + spin_unlock(&hugetlb_lock); + cond_resched(); + + /* + * Theoretically, we should return -EBUSY when we + * encounter this race. In fact, we have a chance + * to successfully dissolve the page if we do a + * retry. Because the race window is quite small. + * If we seize this opportunity, it is an optimization + * for increasing the success rate of dissolving page. + */ + goto retry; + } + /* * Move PageHWPoison flag from head page to the raw error page, * which makes any subpages rather than the error page reusable.