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 882B7C47DB3 for ; Tue, 30 Jan 2024 02:21:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 07F896B00AF; Mon, 29 Jan 2024 21:21:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 02F336B00B1; Mon, 29 Jan 2024 21:21:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DEB586B00B5; Mon, 29 Jan 2024 21:21:16 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id CD8E96B00AF for ; Mon, 29 Jan 2024 21:21:16 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 9B06340286 for ; Tue, 30 Jan 2024 02:21:16 +0000 (UTC) X-FDA: 81734375352.21.C43F077 Received: from mail-yb1-f169.google.com (mail-yb1-f169.google.com [209.85.219.169]) by imf15.hostedemail.com (Postfix) with ESMTP id E0FAAA0008 for ; Tue, 30 Jan 2024 02:21:14 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=i4f7VonO; spf=pass (imf15.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.219.169 as permitted sender) smtp.mailfrom=ioworker0@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706581274; 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:dkim-signature; bh=SqvvkgNcia++P3J4vObWaZUkkmBWvOpjfl4ds6Ix2cQ=; b=K+NM1dVIvQVA3Nf1kjKaqNIMkN2M/7SQqQ/iHVc6gx9ZFasPDdvPPi4H8MdEk4oQwt1obK LiIfY21iHWhFKWeWi5UH8ID8L7W2G4MTSNYYlGAefqlSVhZ32XjzmORCI59TsP0tTE6MkF IipOW0VLoPexZ6sLmabMYQtljkgX6Io= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706581274; a=rsa-sha256; cv=none; b=cUxsQ0vrOIo6eLMq7o2uH6mI+XXjRGgltlSmIFWGRiKKtTsgYjUcC+ZUlQh8QZzHRtfhL1 +zXhoDbzt8ibZB7uoAlGqrcRhnALmo6ADXH41J1Iw9yTq2qQUJU2DK7GcaduWhApI/wFZL z1f3KOXKF/UOEr4Pdbja8uvdVFE+O7k= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=i4f7VonO; spf=pass (imf15.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.219.169 as permitted sender) smtp.mailfrom=ioworker0@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-yb1-f169.google.com with SMTP id 3f1490d57ef6-dc642cd955fso2610534276.0 for ; Mon, 29 Jan 2024 18:21:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706581274; x=1707186074; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=SqvvkgNcia++P3J4vObWaZUkkmBWvOpjfl4ds6Ix2cQ=; b=i4f7VonOseU+D7j8gdaD0auXoQsNeHeXHenkmUf2RUFa7Uu84IdALsB95LKpXjb7qW mcpl3vVQU1E7K3WPrbqF90dwVpNIhUZsNuTaful7Bf9Sy0sWbla18Oc0P6bXWZT1L86d k9f0x0munAPib7ixkokFCJXJyDnkTVvTKa0kNUGs8ibXSL5bP44k9c/Kb7AlHfjOROws 3CmJ1XHkgGII+gXj+90io1PwH1+mR1u8aflD8d65NblkYb/rQYZK09vv0y80D7C0H1+O wOel6nqM8rH/6LLVqZW1DIdMSs42/2GPJksYjaDrjvrOv+nqXV3aEPThpwiRPBoq1sJp mY+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706581274; x=1707186074; 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=SqvvkgNcia++P3J4vObWaZUkkmBWvOpjfl4ds6Ix2cQ=; b=Fazo+8RvFTU2NkHkPK9uXpZA0zybIvHgX1Vn47Z8qtdHe1zltLNOHtP+HT74o2WTud u6soBgEZD3LkVBorQH88KxHjQ6UJ7gj4OCm4m1Xe416xcwS7YT7brra5KWT8DEUqV60q 7ameD6onxqSP9x7wL/yoyvylJ/U3c1h8jZOuNK1FdbtEp/AGeOICZITiadGbcdtlLB9r K+Ti1wKPQgLaTdYWwe+Le5vxRNVG7U7dF0CJAQWv5Ng9GnsrFcBkcpNgku6gALOzfs2U E+uNzdfHCOqc/iviL8CPZwRuy34hy1dN3m0uKyH+NUbmNpZbICIRg068CIQlRW/hOoc+ eRsw== X-Gm-Message-State: AOJu0Yw9Kb/HMxaCsDuLKJWqNywXQxHEyTXj6htjhw7Um2M9g2UvEhUa yDljzDmhWmz+1ayh4V8FkjXh/9wxVwrPcbvdtEykD2IlMV5YYhju7tPTBqrEUT+X2VZcpHtTety H8BgZGB34u0yfZTYuoHiN8lTWcK0= X-Google-Smtp-Source: AGHT+IGPUm2g3mX53rCNExa6/35IeYTMYeLrmrsnYIT9kHR/YEO91YNEXMJovx3BPc4Dn2EWGw+mQS97dvaKkzoOGFg= X-Received: by 2002:a05:6902:218a:b0:dc3:696f:167e with SMTP id dl10-20020a056902218a00b00dc3696f167emr3985908ybb.64.1706581273948; Mon, 29 Jan 2024 18:21:13 -0800 (PST) MIME-Version: 1.0 References: <20240129054551.57728-1-ioworker0@gmail.com> In-Reply-To: From: Lance Yang Date: Tue, 30 Jan 2024 10:21:00 +0800 Message-ID: Subject: Re: [PATCH 1/1] mm/khugepaged: bypassing unnecessary scans with MMF_DISABLE_THP check To: Yang Shi Cc: akpm@linux-foundation.org, mhocko@suse.com, zokeefe@google.com, david@redhat.com, songmuchun@bytedance.com, peterx@redhat.com, minchan@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: E0FAAA0008 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: nt68q79pk6hg65u89apxs44pkb94b7n3 X-HE-Tag: 1706581274-428316 X-HE-Meta: U2FsdGVkX1/oFdi1VY7BsVhTWcerlYTDPMj3PEZO+ozMNBNOjcrqUBGVUCUZmaENymgCUUPO9YX0Bum5k46PVBhb4eQN5J2EcHMFNMfCDxcSaBYF6R0h7NlDA3jRy24vDTyf5LNFGxZydFYtpl56q2xNz6mWkfED2ax22hUypk2PkTzUCrBYQ+L6uN2oVSwXINoxhUyi+p/jNICbqRN9Dbl+hfYaBKlEtsui2hZT4KKJNCR0ZzmyQRsVsAbisZWQRKZomJHQVmauJLazLr9B2xxw2c6C1gTpyZVvRGGPnlc/3/osKJuI0GlYiXcfqlp0XxCoFUKMSQ7tQUOAqJliKOSSqG3apDr1wzQoqGI5wXVygPFtOLIvKgyIk72vxTWooAMOs7pnmJKVNptRFYyf09NQ6pNyXSTB4kzX1iBVibBzAemB3Z4wQAz25kuK/G1okxdx3csJbDgV1IVVa2VSoZ/aJA22vtgyq9FcEd4+FS+Ax9KIPLWDSYKTevPYjWoWkMibpz1ENM9JnfIi99Se53oQpolhKZuQfuqvZeCHR0oL7AU0m8TFIs2DQDCHlwQTP5POfnP2GlR57/J471aedidQxVHkCdEbMxlb3E8gsoS+Ji5kl4nRyrRJCsnrS5GjT/gGTY6EaxA3GXqKu7ZlE08lP9HTXA8LP7OVs+s2UoenqrTyDzeXPHWb7bBQj79YS2IDYXPpD1Z3v4sBbnH3AuoQ2xrc98juWvGeTctcOOK0COrMokmPoQmwSVK6AxnjvXHv0P/0fO4+WkpkK7A8/LwJlzbReWdkpwtx0pJVj9fjd9iyrGHpvdBFs1L5Rx31GGU1fvjzT+T7ocOnQr3psx/zKKJnGKnjGVhwyJqYCTsMt2udi0IcvAlxmOMPCMhCf/i5j4lCzoTZDUht6Cy814/gvO9x3KrLGCoyFTP6MqI6PJnqQmQ+kKuuozh+A01kUIdXBvIicg3sdj7Tp3N +q46wcwT V9MgGxgHZwS3VjB4yyf1/o5NphQsshcauJSZmlEKPZhyGAEkZmkaTGbzlufX7TWOi+hFUgKVi4A167i06Vg2dsUkToB+ljV9kxTZh9nWAxNBIfaisCLSBzSWMuJOlFJAyhBggi42AT0crBRkSjKVotYMWV+ZM0x/8WXTBL5nNDacfexPUdwi1ktPII2amPpgw5fNLRMMU4XPAazRn6hRSRoxRkgkqhszygDPCzeKk5mYzhvDetJhsGHw/fIw4hHEJ3vTjjSRFrxgzpJaYhRVLH5nOj8j87rfTA22VTKHgB7JR4twkGo8fa31RW8M/4RsqIRGKnno6n96TP0I= 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: Hey Yang, Thanks for taking time to review! thp_vma_allowable_order and hugepage_vma_revalidate do check whether MMF_DISABLE_THP is set. IIRC, even if MMF_DISABLE_THP is set, khugepaged will still continue to scan the address space. BR, Lance On Tue, Jan 30, 2024 at 2:53=E2=80=AFAM Yang Shi wrot= e: > > On Sun, Jan 28, 2024 at 9:46=E2=80=AFPM Lance Yang = wrote: > > > > khugepaged scans the entire address space in the > > background for each given mm, looking for > > opportunities to merge sequences of basic pages > > into huge pages. However, when an mm is inserted > > to the mm_slots list, and the MMF_DISABLE_THP flag > > is set later, this scanning process becomes > > unnecessary for that mm and can be skipped to avoid > > redundant operations, especially in scenarios with > > a large address space. > > > > This commit introduces a check before each scanning > > process to test the MMF_DISABLE_THP flag for the > > given mm; if the flag is set, the scanning process > > is bypassed, thereby improving the efficiency of > > khugepaged. > > > > Signed-off-by: Lance Yang > > --- > > mm/khugepaged.c | 18 ++++++++++++------ > > 1 file changed, 12 insertions(+), 6 deletions(-) > > > > diff --git a/mm/khugepaged.c b/mm/khugepaged.c > > index 2b219acb528e..d6a700834edc 100644 > > --- a/mm/khugepaged.c > > +++ b/mm/khugepaged.c > > @@ -410,6 +410,12 @@ static inline int hpage_collapse_test_exit(struct = mm_struct *mm) > > return atomic_read(&mm->mm_users) =3D=3D 0; > > } > > > > +static inline int hpage_collapse_test_exit_or_disable(struct mm_struct= *mm) > > +{ > > + return hpage_collapse_test_exit(mm) || > > + test_bit(MMF_DISABLE_THP, &mm->flags); > > +} > > + > > void __khugepaged_enter(struct mm_struct *mm) > > { > > struct khugepaged_mm_slot *mm_slot; > > @@ -1422,7 +1428,7 @@ static void collect_mm_slot(struct khugepaged_mm_= slot *mm_slot) > > > > lockdep_assert_held(&khugepaged_mm_lock); > > > > - if (hpage_collapse_test_exit(mm)) { > > + if (hpage_collapse_test_exit_or_disable(mm)) { > > /* free mm_slot */ > > hash_del(&slot->hash); > > list_del(&slot->mm_node); > > @@ -2360,7 +2366,7 @@ static unsigned int khugepaged_scan_mm_slot(unsig= ned int pages, int *result, > > goto breakouterloop_mmap_lock; > > > > progress++; > > - if (unlikely(hpage_collapse_test_exit(mm))) > > + if (unlikely(hpage_collapse_test_exit_or_disable(mm))) > > goto breakouterloop; > > > > vma_iter_init(&vmi, mm, khugepaged_scan.address); > > @@ -2368,7 +2374,7 @@ static unsigned int khugepaged_scan_mm_slot(unsig= ned int pages, int *result, > > unsigned long hstart, hend; > > > > cond_resched(); > > - if (unlikely(hpage_collapse_test_exit(mm))) { > > + if (unlikely(hpage_collapse_test_exit_or_disable(mm))) = { > > The later thp_vma_allowable_order() does check whether MMF_DISABLE_THP > is set or not. And the hugepage_vma_revalidate() after re-acquiring > mmap_lock does the same check too. The checking in khugepaged should > be already serialized with prctl, which takes mmap_lock in write. > > > progress++; > > break; > > } > > @@ -2390,7 +2396,7 @@ static unsigned int khugepaged_scan_mm_slot(unsig= ned int pages, int *result, > > bool mmap_locked =3D true; > > > > cond_resched(); > > - if (unlikely(hpage_collapse_test_exit(mm))) > > + if (unlikely(hpage_collapse_test_exit_or_disabl= e(mm))) > > goto breakouterloop; > > > > VM_BUG_ON(khugepaged_scan.address < hstart || > > @@ -2408,7 +2414,7 @@ static unsigned int khugepaged_scan_mm_slot(unsig= ned int pages, int *result, > > fput(file); > > if (*result =3D=3D SCAN_PTE_MAPPED_HUGE= PAGE) { > > mmap_read_lock(mm); > > - if (hpage_collapse_test_exit(mm= )) > > + if (hpage_collapse_test_exit_or= _disable(mm)) > > goto breakouterloop; > > *result =3D collapse_pte_mapped= _thp(mm, > > khugepaged_scan.address= , false); > > @@ -2450,7 +2456,7 @@ static unsigned int khugepaged_scan_mm_slot(unsig= ned int pages, int *result, > > * Release the current mm_slot if this mm is about to die, or > > * if we scanned all vmas of this mm. > > */ > > - if (hpage_collapse_test_exit(mm) || !vma) { > > + if (hpage_collapse_test_exit_or_disable(mm) || !vma) { > > /* > > * Make sure that if mm_users is reaching zero while > > * khugepaged runs here, khugepaged_exit will find > > -- > > 2.33.1 > >