From patchwork Wed Jan 6 08:47:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 12001135 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 D14E1C433DB for ; Wed, 6 Jan 2021 08:49:54 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 69C8B23107 for ; Wed, 6 Jan 2021 08:49:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 69C8B23107 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 DDF8A8D00F6; Wed, 6 Jan 2021 03:49:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D67D28D0090; Wed, 6 Jan 2021 03:49:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A0DDA8D00F8; Wed, 6 Jan 2021 03:49:52 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0031.hostedemail.com [216.40.44.31]) by kanga.kvack.org (Postfix) with ESMTP id 7B8198D00F6 for ; Wed, 6 Jan 2021 03:49:52 -0500 (EST) Received: from smtpin21.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 3E5F6181AEF15 for ; Wed, 6 Jan 2021 08:49:52 +0000 (UTC) X-FDA: 77674727424.21.leg53_2b16795274e0 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin21.hostedemail.com (Postfix) with ESMTP id 2061D180442C0 for ; Wed, 6 Jan 2021 08:49:52 +0000 (UTC) X-HE-Tag: leg53_2b16795274e0 X-Filterd-Recvd-Size: 7328 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by imf27.hostedemail.com (Postfix) with ESMTP for ; Wed, 6 Jan 2021 08:49:51 +0000 (UTC) Received: by mail-pl1-f172.google.com with SMTP id x18so1219631pln.6 for ; Wed, 06 Jan 2021 00:49:51 -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=lJ6hRAKquF055Y1OoIKyzC/uqe2sOAj6aTfGxl8Ov5o=; b=lwRKGn/TMdWvGDwbh+I3PhFr+hVFNVubFO6mDlXQmjWeArVJAjaxvGB7GjgPg/Xl7Q Tr+8YP3+L1Qvii6CxDgJPvhJTSlQDOZXvOhEN+S1aqTHX/EsTK4EdwYBXhxz7Ag5RrOE GCce0HX0TIONUeffpf/dD9VmexumkkMbQ7AlUtgKxOu9i7Q7Qe0DuVJ8i5hF2+XD3r7F yY4OyX7HFREnzFvY/PJoENi/WP3Pzt4MPLmPYQNy8CElxM5KuVEdiiUJYGbnIEMdrg70 1qRYZf8tfpYydljgGoDAY/CToBQjp5wK8ihPmgDkA9eR9/h9ZsHVlmIS0aN1ZrfSX0Bq sP1Q== 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=lJ6hRAKquF055Y1OoIKyzC/uqe2sOAj6aTfGxl8Ov5o=; b=R5TvG5/KaBkSCxBv1mb2NfKZWSTErdqthSw/PtxqbYRK9g5h43z9IRP3F/yd+34Zv1 jsmUG5yRbUtNk8GWbUfpkPTc1zDuc6fgM6pq/7kRLcX4cZQ50FmoCIZmrMsBzI3VtYmq 4WdwmnB0iZ3lGl4CSHJzXbPjZvC9phzNl+UGjPI7zK5v/+u6PG/UFGRnD6+pztdvLFxV UxEYfpkiU7CxVK+oKfnFpw+m8x/H0b2ZshOOj1vjyKjfvp4fjvoNx41eYR/fuCXIN1HZ vHYb8OiphkSMnEIxdULG72RUTxQgo74cDhXb9k4/fuAIh1ZS14stfX1Xjryo2J+E3yy9 SG4w== X-Gm-Message-State: AOAM532sR+Qyxnn9FMihinXWCchNa81GCWsBRGB6D4aLf2j41EbAwspL EM3wIxAF0j/+jM3W453dtjKGrA== X-Google-Smtp-Source: ABdhPJzoLjhNr+suJcN6OMvbPP4mPjlFSjx8Cv3PSxcwuIgyqps+XldiscytJCAnQg0qL+Ae0XhHog== X-Received: by 2002:a17:902:ed45:b029:da:c274:d7ac with SMTP id y5-20020a170902ed45b02900dac274d7acmr3281653plb.69.1609922990839; Wed, 06 Jan 2021 00:49:50 -0800 (PST) Received: from localhost.localdomain ([139.177.225.232]) by smtp.gmail.com with ESMTPSA id h8sm1851806pjc.2.2021.01.06.00.49.47 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 06 Jan 2021 00:49:50 -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 Subject: [PATCH v2 3/6] mm: hugetlb: fix a race between freeing and dissolving the page Date: Wed, 6 Jan 2021 16:47:36 +0800 Message-Id: <20210106084739.63318-4-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20210106084739.63318-1-songmuchun@bytedance.com> References: <20210106084739.63318-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 | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 4741d60f8955..8ff138c17129 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 (unsigned long)head[3].mapping == -1U; +} + +static inline void SetPageHugeFreed(struct page *head) +{ + head[3].mapping = (void *)-1U; +} + +static inline void ClearPageHugeFreed(struct page *head) +{ + head[3].mapping = NULL; +} + /* 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; @@ -1291,6 +1308,17 @@ static inline void destroy_compound_gigantic_page(struct page *page, unsigned int order) { } #endif +/* + * Because we reuse the mapping field of some tail page structs, we should + * reset those mapping to initial value before @head is freed to the buddy + * allocator. The invalid value will be checked in the free_tail_pages_check(). + */ +static inline void reset_tail_page_mapping(struct hstate *h, struct page *head) +{ + if (!hstate_is_gigantic(h)) + head[3].mapping = TAIL_MAPPING; +} + static void update_and_free_page(struct hstate *h, struct page *page) { int i; @@ -1298,6 +1326,7 @@ static void update_and_free_page(struct hstate *h, struct page *page) if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported()) return; + reset_tail_page_mapping(h, page); h->nr_huge_pages--; h->nr_huge_pages_node[page_to_nid(page)]--; for (i = 0; i < pages_per_huge_page(h); i++) { @@ -1504,6 +1533,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 +1800,14 @@ 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))) + goto out; + /* * Move PageHWPoison flag from head page to the raw error page, * which makes any subpages rather than the error page reusable.