From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A4DB9C52D7C for ; Mon, 19 Aug 2024 21:35:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 05B386B0082; Mon, 19 Aug 2024 17:35:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 00A446B0083; Mon, 19 Aug 2024 17:35:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DEF166B0085; Mon, 19 Aug 2024 17:35:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id C15796B0082 for ; Mon, 19 Aug 2024 17:35:12 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 3C504A8DF2 for ; Mon, 19 Aug 2024 21:35:12 +0000 (UTC) X-FDA: 82470300864.21.DAE1F9C Received: from mail-yb1-f177.google.com (mail-yb1-f177.google.com [209.85.219.177]) by imf06.hostedemail.com (Postfix) with ESMTP id 7600F18000F for ; Mon, 19 Aug 2024 21:35:10 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=none; spf=pass (imf06.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.219.177 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=kernel.org (policy=none) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724103232; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=v2Fa97VOpmKXu2KcqUq3KAv9vO209RmoS1OhIQtYunY=; b=tou2N5hJW0xCuaJooSlNVrV54FrPryaXxQbzgbl3WXytYMtg8wMTzf4Yhix9gABLhuhqo1 UnJpFlJMNbioq2JmZuOD7bczCU5TjM+mxU6NZXGlIRwffh5jg02QjCCsoacERhld/22rk9 TLQ4N/hSfnKdlNewsWlTWvPpa9HBUnQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724103232; a=rsa-sha256; cv=none; b=dG16JqzKWBRP2/7n7Jx3+H8gzOYqwWIMHdsQwe2VoomkRULWihzPBBp7S2p/yRF2J2MrT7 7JpCOrdWK0jdPcpI8AUIfrp7whVzZ162j51JGdNZyIu1K8yyBMQSm4uwnR/4lXzqwqFjrK kCm5518xesmqoltluj2VY2gyJUmLcJA= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=none; spf=pass (imf06.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.219.177 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=kernel.org (policy=none) Received: by mail-yb1-f177.google.com with SMTP id 3f1490d57ef6-dff1ccdc17bso5356952276.0 for ; Mon, 19 Aug 2024 14:35:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724103309; x=1724708109; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=v2Fa97VOpmKXu2KcqUq3KAv9vO209RmoS1OhIQtYunY=; b=AASC0Mwl3f3+hBJ5SO3w9JluopA+IuZcM97QkC5xwRYM3THB+amJy91eT600aiI9Cj Z/MbG4FGuDv9k3dVbq7fhUapI8PqSW5+A55EF/9MaXCytUYI8g3dtbWGzJ6bDUVtfKa0 zT+9DUxRvbpPARl+V71C8yRvk4IQ80CEq7IQoph7z9B82YVybtGr9J9NPR3/sURG+KLc sjdMd7QWCOutQ4fqThc5I7CEOssiiZpIWjORmgzyZDvbO27MsXEoiCXv1PuAuOsoNliD ABmiPqubg1/500z+4jnSsve+2X/P59qRqyHycCf40cGRQTKzvqTEMt/cV9DUoJxPD22R 7ImA== X-Forwarded-Encrypted: i=1; AJvYcCUNiLsZGXn1J/aP+g0tbm/bhrtrgJ8oFhh7kJ/iivmJdJKkQdcm2ONcwaB/4EkBiYMZrxKYj9YaA5QBVbWxaG1uuzc= X-Gm-Message-State: AOJu0Yx39yi27E6AGQk0ujy0DQCnugO9pyAH5lSMa3yjqkAvePEfsllg vQ8wu/2beE0xsQBwlQ4ndeWL6DJySQZtia8kENWoE8E1fLMYYFySzJZLMznQOvSCd/3LMxdQmhN uLrmAiSq+xS5AXcYX10KUq2vigsA= X-Google-Smtp-Source: AGHT+IFucDWUvZCR2f1GfJJq3VCGE/0nUAGOVSRdrDd7MzMnspllL+9vskjBcsXpDj9ABN1M3/nOBY9RTn8iHF+Wbz0= X-Received: by 2002:a05:6902:2612:b0:e11:7f99:f75b with SMTP id 3f1490d57ef6-e16513bf2e8mr449851276.42.1724103309362; Mon, 19 Aug 2024 14:35:09 -0700 (PDT) MIME-Version: 1.0 References: <20240819023145.2415299-1-usamaarif642@gmail.com> <20240819023145.2415299-5-usamaarif642@gmail.com> <9a58e794-2156-4a9f-a383-1cdfc07eee5e@gmail.com> In-Reply-To: <9a58e794-2156-4a9f-a383-1cdfc07eee5e@gmail.com> From: Barry Song Date: Tue, 20 Aug 2024 09:34:53 +1200 Message-ID: Subject: Re: [PATCH v4 4/6] mm: Introduce a pageflag for partially mapped folios To: Usama Arif Cc: akpm@linux-foundation.org, linux-mm@kvack.org, hannes@cmpxchg.org, riel@surriel.com, shakeel.butt@linux.dev, roman.gushchin@linux.dev, yuzhao@google.com, david@redhat.com, ryan.roberts@arm.com, rppt@kernel.org, willy@infradead.org, cerasuolodomenico@gmail.com, ryncsn@gmail.com, corbet@lwn.net, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kernel-team@meta.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 7600F18000F X-Stat-Signature: z3statmrw9pqkhcuyqmet6tsdorpqsid X-HE-Tag: 1724103310-949174 X-HE-Meta: U2FsdGVkX18WI+9tlIoVacs3umRxMiAKt1zuNMnkK4bPcnloVdfyINgiPTanzA6W2MtQAgDI/21UhoDRpcSJS2yOmi/PEd8qEZwSp2nf1aF1M88HggaXiilfjRRWpTWcHj20YqBddOvumpVRo4KMG7Te93yg/Dx3whAXdNufyCqNBxab3qOoT9FBqcJqIETXcGGGhEvOKaLOEjJuFE4zJGPW5iETTGOvrZv8KIvHaR+RKAsHLd33IatpwKyKuABTtvzkt66WayJmXA9O0j7fF/EJ2hlrTDkihJyduQHPMCJ2AJd585+bQumbIHxxnwN4JKa2tN7GX9krDCgLTkYFXTjvrow3lZCJj2WTGFXkAbBe69tDej8HIf/doxySoDmIvqZebijNcdiruOU4qVBMkT0wMpsSn3EoNTkbISOO377TQsNUCuLta3icVMHog0M4Gl4ae4woi+eZ94QeiM/OEDxV/SPrWFJwT4ZNW1RCS6+4sHcLSyvC/SJ0eCx5uG9Q7iBCxCrWyzzjX02uE3Gj3z+pc2R/2EUvfrxtGu6jH3bTyLXgn/0b4gSB3r0mwHWQDepGbIpn1htohTPqZI6AuTytNhKHq0bdLZjxdOzU1KOSv224tX9Tux5fqufhAZFzlKLaKescknPzwETtkWPsW/FzC3gRT9QVYyMWUO2R1ermRgtOY5UWsvfuGqB0JOMVVi77cm8C5xnu9XIMO33x9qez37+PvexKPdvIaTCGOaYiDPQorlEBUJbcZsny+6UyjqXNeBsRpRbjeDM4+a7Xmw0xRIViVfxea3ApUUHaPwdYxE8QtGLl1jA/bBpmPHz7i8hmbITLbtpm5z5I04kldKTq+9xhFfMAMTgoG9tEroQn3Kyje1XV0OITFYNaxdEGGiTuEaBWQTxL0GDpF3T5Q8xEzXmecY0PvKiObXbh5H/tE/ZUWOrSq1AFTvgBuMpgdWovPjZvA3PGdIhfCF3 txguZWLx cwe/75ciCo0S2zKdRoXL1HOjfu6vB6tOF1egUfin7LSpUaRxShQ3u6rinHvc17ONXimgnmzPlY4Zf1YiHbVkbBrfP2FpWO2ndjnUzJa24ooR+e419w2TJx5/UxcKSNdj7AiZD+GBlDNzJ33q2fD21RTZQcKMyzshEeJCx+6KeF+mTWCY72OI3WQfgM6ezcv3VM8omguFzN0hM+kqLjxKnSVKonA/AhZNa7YUp0vAgjGYpCDZHAFb6lTs2shVJXxpbYk1hUFOJkaFjTx63pf4MnXQJTUdFLIWDsSbY85nnl1ELITU= 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: List-Subscribe: List-Unsubscribe: On Tue, Aug 20, 2024 at 8:16=E2=80=AFAM Usama Arif = wrote: > > > > On 19/08/2024 20:00, Barry Song wrote: > > On Tue, Aug 20, 2024 at 2:17=E2=80=AFAM Usama Arif wrote: > >> > >> > >> > >> On 19/08/2024 09:29, Barry Song wrote: > >>> Hi Usama, > >>> > >>> I feel it is much better now! thanks! > >>> > >>> On Mon, Aug 19, 2024 at 2:31=E2=80=AFPM Usama Arif wrote: > >>>> > >>>> Currently folio->_deferred_list is used to keep track of > >>>> partially_mapped folios that are going to be split under memory > >>>> pressure. In the next patch, all THPs that are faulted in and collap= sed > >>>> by khugepaged are also going to be tracked using _deferred_list. > >>>> > >>>> This patch introduces a pageflag to be able to distinguish between > >>>> partially mapped folios and others in the deferred_list at split tim= e in > >>>> deferred_split_scan. Its needed as __folio_remove_rmap decrements > >>>> _mapcount, _large_mapcount and _entire_mapcount, hence it won't be > >>>> possible to distinguish between partially mapped folios and others i= n > >>>> deferred_split_scan. > >>>> > >>>> Eventhough it introduces an extra flag to track if the folio is > >>>> partially mapped, there is no functional change intended with this > >>>> patch and the flag is not useful in this patch itself, it will > >>>> become useful in the next patch when _deferred_list has non partiall= y > >>>> mapped folios. > >>>> > >>>> Signed-off-by: Usama Arif > >>>> --- > >>>> include/linux/huge_mm.h | 4 ++-- > >>>> include/linux/page-flags.h | 11 +++++++++++ > >>>> mm/huge_memory.c | 23 ++++++++++++++++------- > >>>> mm/internal.h | 4 +++- > >>>> mm/memcontrol.c | 3 ++- > >>>> mm/migrate.c | 3 ++- > >>>> mm/page_alloc.c | 5 +++-- > >>>> mm/rmap.c | 5 +++-- > >>>> mm/vmscan.c | 3 ++- > >>>> 9 files changed, 44 insertions(+), 17 deletions(-) > >>>> > >>>> diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h > >>>> index 4c32058cacfe..969f11f360d2 100644 > >>>> --- a/include/linux/huge_mm.h > >>>> +++ b/include/linux/huge_mm.h > >>>> @@ -321,7 +321,7 @@ static inline int split_huge_page(struct page *p= age) > >>>> { > >>>> return split_huge_page_to_list_to_order(page, NULL, 0); > >>>> } > >>>> -void deferred_split_folio(struct folio *folio); > >>>> +void deferred_split_folio(struct folio *folio, bool partially_mappe= d); > >>>> > >>>> void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, > >>>> unsigned long address, bool freeze, struct folio *fo= lio); > >>>> @@ -495,7 +495,7 @@ static inline int split_huge_page(struct page *p= age) > >>>> { > >>>> return 0; > >>>> } > >>>> -static inline void deferred_split_folio(struct folio *folio) {} > >>>> +static inline void deferred_split_folio(struct folio *folio, bool p= artially_mapped) {} > >>>> #define split_huge_pmd(__vma, __pmd, __address) \ > >>>> do { } while (0) > >>>> > >>>> diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h > >>>> index a0a29bd092f8..c3bb0e0da581 100644 > >>>> --- a/include/linux/page-flags.h > >>>> +++ b/include/linux/page-flags.h > >>>> @@ -182,6 +182,7 @@ enum pageflags { > >>>> /* At least one page in this folio has the hwpoison flag set= */ > >>>> PG_has_hwpoisoned =3D PG_active, > >>>> PG_large_rmappable =3D PG_workingset, /* anon or file-backed= */ > >>>> + PG_partially_mapped =3D PG_reclaim, /* was identified to be = partially mapped */ > >>>> }; > >>>> > >>>> #define PAGEFLAGS_MASK ((1UL << NR_PAGEFLAGS) - 1) > >>>> @@ -861,8 +862,18 @@ static inline void ClearPageCompound(struct pag= e *page) > >>>> ClearPageHead(page); > >>>> } > >>>> FOLIO_FLAG(large_rmappable, FOLIO_SECOND_PAGE) > >>>> +FOLIO_TEST_FLAG(partially_mapped, FOLIO_SECOND_PAGE) > >>>> +/* > >>>> + * PG_partially_mapped is protected by deferred_split split_queue_l= ock, > >>>> + * so its safe to use non-atomic set/clear. > >>>> + */ > >>>> +__FOLIO_SET_FLAG(partially_mapped, FOLIO_SECOND_PAGE) > >>>> +__FOLIO_CLEAR_FLAG(partially_mapped, FOLIO_SECOND_PAGE) > >>>> #else > >>>> FOLIO_FLAG_FALSE(large_rmappable) > >>>> +FOLIO_TEST_FLAG_FALSE(partially_mapped) > >>>> +__FOLIO_SET_FLAG_NOOP(partially_mapped) > >>>> +__FOLIO_CLEAR_FLAG_NOOP(partially_mapped) > >>>> #endif > >>>> > >>>> #define PG_head_mask ((1UL << PG_head)) > >>>> diff --git a/mm/huge_memory.c b/mm/huge_memory.c > >>>> index 2d77b5d2291e..70ee49dfeaad 100644 > >>>> --- a/mm/huge_memory.c > >>>> +++ b/mm/huge_memory.c > >>>> @@ -3398,6 +3398,7 @@ int split_huge_page_to_list_to_order(struct pa= ge *page, struct list_head *list, > >>>> * page_deferred_list. > >>>> */ > >>>> list_del_init(&folio->_deferred_list); > >>>> + __folio_clear_partially_mapped(folio); > >>>> } > >>>> spin_unlock(&ds_queue->split_queue_lock); > >>>> if (mapping) { > >>>> @@ -3454,11 +3455,13 @@ void __folio_undo_large_rmappable(struct fol= io *folio) > >>>> if (!list_empty(&folio->_deferred_list)) { > >>>> ds_queue->split_queue_len--; > >>>> list_del_init(&folio->_deferred_list); > >>>> + __folio_clear_partially_mapped(folio); > >>> > >>> is it possible to make things clearer by > >>> > >>> if (folio_clear_partially_mapped) > >>> __folio_clear_partially_mapped(folio); > >>> > >>> While writing without conditions isn't necessarily wrong, adding a co= ndition > >>> will improve the readability of the code and enhance the clarity of m= y mTHP > >>> counters series. also help decrease smp cache sync if we can avoid > >>> unnecessary writing? > >>> > >> > >> Do you mean if(folio_test_partially_mapped(folio))? > >> > >> I don't like this idea. I think it makes the readability worse? If I w= as looking at if (test) -> clear for the first time, I would become confuse= d why its being tested if its going to be clear at the end anyways? > > > > In the pmd-order case, the majority of folios are not partially mapped. > > Unconditional writes will trigger cache synchronization across all > > CPUs (related to the MESI protocol), making them more costly. By > > using conditional writes, such as "if(test) write," we can avoid > > most unnecessary writes, which is much more efficient. Additionally, > > we only need to manage nr_split_deferred when the condition > > is met. We are carefully evaluating all scenarios to determine > > if modifications to the partially_mapped flag are necessary. > > > > > Hmm okay, as you said its needed for nr_split_deferred anyways. Something= like below is ok to fold in? > > commit 4ae9e2067346effd902b342296987b97dee29018 (HEAD) > Author: Usama Arif > Date: Mon Aug 19 21:07:16 2024 +0100 > > mm: Introduce a pageflag for partially mapped folios fix > > Test partially_mapped flag before clearing it. This should > avoid unnecessary writes and will be needed in the nr_split_deferred > series. > > Signed-off-by: Usama Arif > > diff --git a/mm/huge_memory.c b/mm/huge_memory.c > index 5d67d3b3c1b2..ccde60aaaa0f 100644 > --- a/mm/huge_memory.c > +++ b/mm/huge_memory.c > @@ -3479,7 +3479,8 @@ void __folio_undo_large_rmappable(struct folio *fol= io) > if (!list_empty(&folio->_deferred_list)) { > ds_queue->split_queue_len--; > list_del_init(&folio->_deferred_list); > - __folio_clear_partially_mapped(folio); > + if (folio_test_partially_mapped(folio)) > + __folio_clear_partially_mapped(folio); > } > spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags); > } > @@ -3610,7 +3611,8 @@ static unsigned long deferred_split_scan(struct shr= inker *shrink, > } else { > /* We lost race with folio_put() */ > list_del_init(&folio->_deferred_list); > - __folio_clear_partially_mapped(folio); > + if (folio_test_partially_mapped(folio)) > + __folio_clear_partially_mapped(folio); > ds_queue->split_queue_len--; > } > if (!--sc->nr_to_scan) > Do we also need if (folio_test_partially_mapped(folio)) in split_huge_page_to_list_to_order()? I recall that in Yu Zhao's TAO, there=E2=80=99s a chance of splitting (shat= tering) non-partially-mapped folios. To be future-proof, we might want to handle both cases equally. By the way, we might not need to clear the flag for a new folio. This diffe= rs from the init_list, which is necessary. If a new folio has the partially_ma= pped flag, it indicates that we failed to clear it when freeing the folio to the buddy system, which is a bug we need to fix in the free path. Thanks Barry