From patchwork Thu Sep 16 13:47:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 12499129 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,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 96417C433EF for ; Thu, 16 Sep 2021 13:52:46 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4880F60EB4 for ; Thu, 16 Sep 2021 13:52:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 4880F60EB4 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id E4EF76B007B; Thu, 16 Sep 2021 09:52:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DFDF76B007D; Thu, 16 Sep 2021 09:52:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C9F2F6B007E; Thu, 16 Sep 2021 09:52:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0147.hostedemail.com [216.40.44.147]) by kanga.kvack.org (Postfix) with ESMTP id BBD0B6B007B for ; Thu, 16 Sep 2021 09:52:45 -0400 (EDT) Received: from smtpin29.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 7DBE6267D7 for ; Thu, 16 Sep 2021 13:52:45 +0000 (UTC) X-FDA: 78593577090.29.DDB9FF7 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) by imf04.hostedemail.com (Postfix) with ESMTP id 383E350000B3 for ; Thu, 16 Sep 2021 13:52:45 +0000 (UTC) Received: by mail-pf1-f180.google.com with SMTP id j6so5981334pfa.4 for ; Thu, 16 Sep 2021 06:52:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Czw+lJoI8c+vqpLarmySj3yLnTuDl0dY5o95pqOIb5c=; b=ZgWK155sZkXvFxsdPVc9idbYRDUwcU0WSFJ7BHU4VE5Qi6Ormq9uEOWap7v+NHFnyz DiMm0dZqdTbYfmWz6m0Ux0MsIiyl7kOvaheG+HLrfxHKYEkDRIn7S5Zj4eGGfFke4zug O4l4wY1TpAcuGNCjqXEQnhjHVrgdAEw5PRK1zXJifY0hSOphTKYuX85mhkp3yTT0BVHq AFD5Bgb2XpK4E7T3X/zsUkFze5d5kmqo3/6dP3K3GSrlYElplKsBMF+ClyZrOdKTGQKa 3dzi/NtNJE0pucQigJVwo6dEZhqZDrkW/fwoOnBnaYMQNdw3OUy7mXkJP5npnJtoxlcf PfQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Czw+lJoI8c+vqpLarmySj3yLnTuDl0dY5o95pqOIb5c=; b=ULesY9Zd09xRSgTnHsI13q7f5/ZhoLKDITnCmJu+xjhk4IVZq5yu4l+prt1m56vWx6 6SKEkJ+MRyf+0XwUl3Nse3jH2vc0VjFYke6AVB3SuBwuytW6Y0KptdyBGKSkyDrrqkSy CDSYTUOFlUnj1gnNm3oIP11MzxpDitEJ7fODKJ4YqIxp/FeV5t/epZEvkguR4XR8kYjy 9RZ22+fgU2XnjKRgm8gXys29ajVMvDJfwnnZbTHlA0AW4Te6vZF4tnbKFE+S/Y079OaD BsclFmIqiWj5tIJlvA7BMhEkVIrCRTpA2Xm8e63duNFIxJzQV3ROAnohfESodr9N0EO9 5ifg== X-Gm-Message-State: AOAM533JA5YdvkN33qgRltkT4qtq9AZUrp5JkOFeZavrS9eZIcsTIgwN WouF/+qCASA8MNv24xTEGst3WA== X-Google-Smtp-Source: ABdhPJxjMl5ioCWxKY3IDwBaGXdistBIZIAxOdUzGGox92RGW/Jf0bHF4//MwnZaB7sanWqnuQQ77Q== X-Received: by 2002:a63:f050:: with SMTP id s16mr5058345pgj.258.1631800364394; Thu, 16 Sep 2021 06:52:44 -0700 (PDT) Received: from localhost.localdomain ([139.177.225.226]) by smtp.gmail.com with ESMTPSA id o9sm3617443pfh.217.2021.09.16.06.52.38 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 16 Sep 2021 06:52:44 -0700 (PDT) From: Muchun Song To: guro@fb.com, hannes@cmpxchg.org, mhocko@kernel.org, akpm@linux-foundation.org, shakeelb@google.com, vdavydov.dev@gmail.com Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, duanxiongchun@bytedance.com, fam.zheng@bytedance.com, bsingharora@gmail.com, shy828301@gmail.com, alexs@kernel.org, smuchun@gmail.com, zhengqi.arch@bytedance.com, Muchun Song Subject: [PATCH v2 06/13] mm: thp: introduce split_queue_lock/unlock{_irqsave}() Date: Thu, 16 Sep 2021 21:47:41 +0800 Message-Id: <20210916134748.67712-7-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20210916134748.67712-1-songmuchun@bytedance.com> References: <20210916134748.67712-1-songmuchun@bytedance.com> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 383E350000B3 X-Stat-Signature: 68ak4o6joty4bddpjcipmdrfr9i6qg1u Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=ZgWK155s; spf=pass (imf04.hostedemail.com: domain of songmuchun@bytedance.com designates 209.85.210.180 as permitted sender) smtp.mailfrom=songmuchun@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com X-HE-Tag: 1631800365-240014 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: We should make thp deferred split queue lock safe when LRU pages reparented. Similar to lock_page_lruvec{_irqsave, _irq}(), we introduce split_queue_lock/unlock{_irqsave}() to make the deferred split queue lock easier to be reparented. And in the next patch, we can use a similar approach (just like lruvec lock did) to make thp deferred split queue lock safe when the LRU pages reparented. Signed-off-by: Muchun Song Reported-by: kernel test robot Reported-by: kernel test robot --- mm/huge_memory.c | 90 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 67 insertions(+), 23 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 5e9ef0fc261e..9d8dfa82991a 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -499,25 +499,70 @@ pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) } #ifdef CONFIG_MEMCG -static inline struct deferred_split *get_deferred_split_queue(struct page *page) +static inline struct mem_cgroup *split_queue_memcg(struct deferred_split *queue) { - struct mem_cgroup *memcg = page_memcg(compound_head(page)); - struct pglist_data *pgdat = NODE_DATA(page_to_nid(page)); + if (mem_cgroup_disabled()) + return NULL; + return container_of(queue, struct mem_cgroup, deferred_split_queue); +} - if (memcg) - return &memcg->deferred_split_queue; - else - return &pgdat->deferred_split_queue; +static inline struct deferred_split *page_memcg_split_queue(struct page *head) +{ + struct mem_cgroup *memcg = page_memcg(head); + + return memcg ? &memcg->deferred_split_queue : NULL; } #else -static inline struct deferred_split *get_deferred_split_queue(struct page *page) ++static inline struct mem_cgroup *split_queue_memcg(struct deferred_split *queue) { - struct pglist_data *pgdat = NODE_DATA(page_to_nid(page)); + return NULL; +} - return &pgdat->deferred_split_queue; +static inline struct deferred_split *page_memcg_split_queue(struct page *head) +{ + return NULL; } #endif +static struct deferred_split *page_split_queue(struct page *head) +{ + struct deferred_split *queue = page_memcg_split_queue(head); + + return queue ? : &NODE_DATA(page_to_nid(head))->deferred_split_queue; +} + +static struct deferred_split *split_queue_lock(struct page *head) +{ + struct deferred_split *queue; + + queue = page_split_queue(head); + spin_lock(&queue->split_queue_lock); + + return queue; +} + +static struct deferred_split * +split_queue_lock_irqsave(struct page *head, unsigned long *flags) +{ + struct deferred_split *queue; + + queue = page_split_queue(head); + spin_lock_irqsave(&queue->split_queue_lock, *flags); + + return queue; +} + +static inline void split_queue_unlock(struct deferred_split *queue) +{ + spin_unlock(&queue->split_queue_lock); +} + +static inline void split_queue_unlock_irqrestore(struct deferred_split *queue, + unsigned long flags) +{ + spin_unlock_irqrestore(&queue->split_queue_lock, flags); +} + void prep_transhuge_page(struct page *page) { /* @@ -2610,7 +2655,7 @@ bool can_split_huge_page(struct page *page, int *pextra_pins) int split_huge_page_to_list(struct page *page, struct list_head *list) { struct page *head = compound_head(page); - struct deferred_split *ds_queue = get_deferred_split_queue(head); + struct deferred_split *ds_queue; struct anon_vma *anon_vma = NULL; struct address_space *mapping = NULL; int extra_pins, ret; @@ -2690,13 +2735,13 @@ int split_huge_page_to_list(struct page *page, struct list_head *list) } /* Prevent deferred_split_scan() touching ->_refcount */ - spin_lock(&ds_queue->split_queue_lock); + ds_queue = split_queue_lock(head); if (page_ref_freeze(head, 1 + extra_pins)) { if (!list_empty(page_deferred_list(head))) { ds_queue->split_queue_len--; list_del(page_deferred_list(head)); } - spin_unlock(&ds_queue->split_queue_lock); + split_queue_unlock(ds_queue); if (mapping) { int nr = thp_nr_pages(head); @@ -2711,7 +2756,7 @@ int split_huge_page_to_list(struct page *page, struct list_head *list) __split_huge_page(page, list, end); ret = 0; } else { - spin_unlock(&ds_queue->split_queue_lock); + split_queue_unlock(ds_queue); fail: if (mapping) xa_unlock(&mapping->i_pages); @@ -2734,24 +2779,22 @@ int split_huge_page_to_list(struct page *page, struct list_head *list) void free_transhuge_page(struct page *page) { - struct deferred_split *ds_queue = get_deferred_split_queue(page); + struct deferred_split *ds_queue; unsigned long flags; - spin_lock_irqsave(&ds_queue->split_queue_lock, flags); + ds_queue = split_queue_lock_irqsave(page, &flags); if (!list_empty(page_deferred_list(page))) { ds_queue->split_queue_len--; list_del(page_deferred_list(page)); } - spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags); + split_queue_unlock_irqrestore(ds_queue, flags); free_compound_page(page); } void deferred_split_huge_page(struct page *page) { - struct deferred_split *ds_queue = get_deferred_split_queue(page); -#ifdef CONFIG_MEMCG - struct mem_cgroup *memcg = page_memcg(compound_head(page)); -#endif + struct deferred_split *ds_queue; + struct mem_cgroup __maybe_unused *memcg; unsigned long flags; VM_BUG_ON_PAGE(!PageTransHuge(page), page); @@ -2769,7 +2812,8 @@ void deferred_split_huge_page(struct page *page) if (PageSwapCache(page)) return; - spin_lock_irqsave(&ds_queue->split_queue_lock, flags); + ds_queue = split_queue_lock_irqsave(page, &flags); + memcg = split_queue_memcg(ds_queue); if (list_empty(page_deferred_list(page))) { count_vm_event(THP_DEFERRED_SPLIT_PAGE); list_add_tail(page_deferred_list(page), &ds_queue->split_queue); @@ -2780,7 +2824,7 @@ void deferred_split_huge_page(struct page *page) deferred_split_shrinker.id); #endif } - spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags); + split_queue_unlock_irqrestore(ds_queue, flags); } static unsigned long deferred_split_count(struct shrinker *shrink,