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 617BDC48BF6 for ; Sat, 24 Feb 2024 03:54:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E3AAE6B0072; Fri, 23 Feb 2024 22:54:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DEB0F6B0074; Fri, 23 Feb 2024 22:54:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C65286B0075; Fri, 23 Feb 2024 22:54:36 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id B29A46B0072 for ; Fri, 23 Feb 2024 22:54:36 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 75E1E80F7F for ; Sat, 24 Feb 2024 03:54:36 +0000 (UTC) X-FDA: 81825330552.04.B9E5D3B Received: from mail-yw1-f172.google.com (mail-yw1-f172.google.com [209.85.128.172]) by imf11.hostedemail.com (Postfix) with ESMTP id C6C3D40012 for ; Sat, 24 Feb 2024 03:54:34 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mx4gCAQm; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.128.172 as permitted sender) smtp.mailfrom=ioworker0@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708746874; a=rsa-sha256; cv=none; b=3CZAJUHpzhA1XmZ0ADjoAssZxU9LTtWwMDuGszXc/TOdZLsqa5d9pKXrRpFCow92/AWM7u p6JD3ZgVrWwAaSz79KjeByVR24VoAwE4oLhkkoD3SDmv2DUUk7sH7uKD5mXr/W1gC0r8z0 gi+uSZI1u2yhmFbTySVIl6FYOKnjb2g= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mx4gCAQm; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.128.172 as permitted sender) smtp.mailfrom=ioworker0@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708746874; 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=sl2hbMYkg/YhUEOi4LJCwvnP+BbK5NoXlEwkB02OZsE=; b=X71hVbRtSQ2LuNJ1LpMJYssjww7XAhDUAPADH7hC74m5VeYesmXHD8yw0JNtmg3brerr0D 4IWMbrDLzFiIzYeImg0eKLFvd5+WAOXCFOznf1dtIBy2arLScDxdNFZM0t6bBE+E67CklM rcEvBt2l6y1caKYFbsEb5DzVzL7kl6Q= Received: by mail-yw1-f172.google.com with SMTP id 00721157ae682-607cd210962so14394127b3.2 for ; Fri, 23 Feb 2024 19:54:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708746874; x=1709351674; 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=sl2hbMYkg/YhUEOi4LJCwvnP+BbK5NoXlEwkB02OZsE=; b=mx4gCAQmyFm7TcK8YBKJYrEO68h6rX5Wz9Bt6sabTwOI54rg4CDSv88/b2xpeBGX5M 25OhS6EaXkLIuek85tS8SRON5XtAGeZ1OXdMD3Cm4DRQahC/OsvZnI4f1zIbgmeRr7DQ YbtDFk0WpSGP1bGRUpgbiHogQw5vYsareS2nzEvDD8SCgetje2yumd8B2EIdgwyxCivO 5yTJNTJ1r0K8GHQXfroTGdfYitNisuWyiUUVXC08DVKOdemDimThf4/wXQUU1+lxBjW6 ePNNpEphK/EPYDBYbs7xts8DVuRXvnlvnTp6l+auZkbdYOoyMuIVgd+wRTnvNK/derka 5JAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708746874; x=1709351674; 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=sl2hbMYkg/YhUEOi4LJCwvnP+BbK5NoXlEwkB02OZsE=; b=Jm3Tm51U8tAR8DNDOJaWFvgCgyqUF+7fgKaYlvhSaBWzjqlL0DiQO1xFYHXqg6wfqj ZSODQtv9GxnQ1hfnAXqdH+Gb7LPhHUDNVihxqEGMV4M5aEZUSLD2Vn21FsLrJpOWD4DN Jx9HwVOjQS+EGXeqK6yeZa2jVEBnTPwTlXKJCQhzu8e6Q6PZXksLCKt6M7XMhulw4m1x bw1UZHmCQy/XU+U0uzuB5KhEvuDm3G30b8v9dMGEkVGcH4nqUufIfhjukNOq+gbiDLtg BLL8muq6Aem7G9xM/jE5pzVF2Krioeum6AQKmPLWQvLTWQGcyNFh5CnhCLU1DYl3R7RS wRRw== X-Forwarded-Encrypted: i=1; AJvYcCX1aG0eHUe3syucg7U9x/WBpK2+WP+zoAC23bkIAHhYWqAVW1MEOEpKgvqm/JKXztr8YuQ0Q8QRvLl0YWvHJHoPDGI= X-Gm-Message-State: AOJu0Ywczi0CiNn3rEfhEbrrFfYp35KS4aU/pNqAMGql9jTCtDmjAjW2 9rZGh5b+VNt/DVwBBW1hQQ6rya/JBeJvvvLI4/w2iadG15SRgS9mS45KaDVNMwqDrIuGcoDBvYt VHzqSYvBE8/azz6faSm4W2UDG7eo= X-Google-Smtp-Source: AGHT+IH5UKUXVOYaKs9FkZP/1YIEVqspxasf2/Gfa43SYTduZraIUOcvqIgGYZwI2zy0mezLTtoOyykHyInv73A0lhc= X-Received: by 2002:a81:4946:0:b0:604:a9c2:2a17 with SMTP id w67-20020a814946000000b00604a9c22a17mr1663459ywa.45.1708746873846; Fri, 23 Feb 2024 19:54:33 -0800 (PST) MIME-Version: 1.0 References: <20240129054551.57728-1-ioworker0@gmail.com> In-Reply-To: From: Lance Yang Date: Sat, 24 Feb 2024 11:54:20 +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-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: C6C3D40012 X-Stat-Signature: o7owb8wt6qnzzqob4om7kw7iz8ah6g3w X-HE-Tag: 1708746874-481768 X-HE-Meta: U2FsdGVkX1/2iwT+6vpEuiCHclrgF3ROjYMzgK5TzlggJnMh5u2nDUyLU3C53MObI2j1MSMcBZlFfj69L3qB2AplFs3S83ztGcaLC5jekZKHro8EPwa+yNqW351V8N93QIXAiOtcSrvSL88Otyb3kqcD4v2o2q89pfHKE/KPBwvwoEVvf4bVRulc3xzoIKDd6uB1Df7tTvDlhpJBSASvxBG27znE6HWkhLUlwhdPi8QUVPCvkwBq/dvpfPntTcgXqgfpnRUkMpyucZPd/nbntQUnlBPTXzFGyzD9fh+IVFLSm3Mdp6nAZc9j+fZ4LUAO1+sld6xx32SUb9iCOuM2LCV6wE/1o1bBjzgQCE/qBYRcKJIOBXnTom/y7i0qdqhyvXqaie1EJ102/g8GqrkIM8LP/BEzvuGFJKT6bt71Z+qGyWaaHzInJ/UW2QR8ByELexMVlFs2gZRnXplNGJnhm7CGFi7+F68hqk6dOc7q4bLNgvmfohoZroh1i24I/shW4Hx3bD+b9acYTn88/3N2QpwYgnrpjxoPXMxD2FgVVCV0Uswz+qQq26O8bcoqkUhE7JdjLhnhHu+AC0ZIO1olB8gJpLw/4HRC5q+bZsi6X3N5KXgSjBTReQI0N5ACBBPxAtyUZRSxRQjTAqaZiGt7V+rkEppVL0208IQ1Uv7kWVkExcmZ4uwdSJZkxl32Xc+tgvxo5AgZqxSDal5nyXcajf4PBgnvbTvtsMi4114UmgyL0vMyQ/RbaxyQ2ryaauvbIfUVsoyn2j+GfmO40UJuiBRQJCvdyabgwkrRQZmKtT66W5kX+vPcAGyS3UrOTI4i0+q1HJRd2zCYfwoOsZVhdVfOcCdmZMT/L+uSHplI5hxaMMmZ5BR3C1kb+Cw4OEPRWUlvYKrQ3YUFI7cT1z/CHQQtCvJJ3C/UC/wjK5NhecuuMUUZJwlS6MkG6sSNQVvRdbOuo85TzfpcS8iQYl9 qBV5q+UW 422ef4ft97YxN+BoyRqNqumVTGLkMVfXnqmdagOquFVRcPmSFwIh+vF8krVwHAotsbvyXvtOktYd7JpCyB0wDa96Y4A== 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! On Sat, Feb 24, 2024 at 9:46=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)) { > > I'm not quite sure whether we should remove the mm from mm_slot and > drop mm_count or not since clearing MMF_THP_DISABLE flag doesn't add > the mm back. Creating new vma may add the mm back, but I don't think > the behavior should depend on this. Thanks for bringing this up! I agree with your point. I'll remove the MMF_THP_DISABLE flag check in collect_mm_slot(). > > > /* 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))) = { > > 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) { > > Same like the above comment. I=E2=80=98ll also remove the MMF_THP_DISABLE flag check here. > > In addition, didn't you need to change hpage_collapse_test_exit() to > hpage_collapse_test_exit_or_disable() in hugepage_vma_revalidate()? Thanks for your suggestion! It makes sense to change hpage_collapse_test_exit() to hpage_collapse_test_exit_or_disable() in hugepage_vma_revalidate(). Thanks again for your time! Lance > > > /* > > * Make sure that if mm_users is reaching zero while > > * khugepaged runs here, khugepaged_exit will find > > -- > > 2.33.1 > >