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 E7FCBC4828F for ; Sat, 3 Feb 2024 03:21:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 334FA6B006E; Fri, 2 Feb 2024 22:21:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2E4DB6B0071; Fri, 2 Feb 2024 22:21:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1AC476B0072; Fri, 2 Feb 2024 22:21:14 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 06DDF6B006E for ; Fri, 2 Feb 2024 22:21:14 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 6E89B140838 for ; Sat, 3 Feb 2024 03:21:13 +0000 (UTC) X-FDA: 81749041626.25.FBEA630 Received: from mail-yb1-f176.google.com (mail-yb1-f176.google.com [209.85.219.176]) by imf14.hostedemail.com (Postfix) with ESMTP id AFCB9100006 for ; Sat, 3 Feb 2024 03:21:10 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ZgL6Losd; spf=pass (imf14.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.219.176 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=1706930470; 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=W699dhQrIRx2hl06FjgorWvkEI0rXLNSwHMVMLrlHT0=; b=UAKw9bERdatXaNhhpRfo1fFkg/IjTWxfSypfsaQ/a3bV9VJ0Z0phb+/Dv1+xiObY6Rqj2r QEeqrR4iZ/gXB3XVASLck9iwi2M4uUanwIFFcvz39LTE0MZal+rnQoRd24t/XGJrq/yBgI wgFRfXC9bPvH/1GorQEY4eeDLIewXz0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706930470; a=rsa-sha256; cv=none; b=N9NyeGCf/6Yb6q90exNnH9ga97wuxXZqiBTTiZe1PVHXJDT+BfYKMWfBiLh1oWdLV7AZC2 sgD/ImHdG8qtZiVLLxVOVfoZAH/DdzP0Rf4wd0TS9/3O5ebaDwU2meK7rnz/279GCKCda3 65XR2p0xI3DJn5YORVJYAB8po8QAD6A= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ZgL6Losd; spf=pass (imf14.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.219.176 as permitted sender) smtp.mailfrom=ioworker0@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-yb1-f176.google.com with SMTP id 3f1490d57ef6-dc6da01f092so2301803276.0 for ; Fri, 02 Feb 2024 19:21:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706930470; x=1707535270; 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=W699dhQrIRx2hl06FjgorWvkEI0rXLNSwHMVMLrlHT0=; b=ZgL6LosdRPjumGhap7hm+wsTG1g9rINJD9APcjv4mau+GhSP/dYOGTW8UqRrfKXIOu hmQ/cJY3x48pkr6lOGSoR82C3rr4LjDcc26ugtA9/tWl14YJ1KTlkxKEq1a9gxZoSTYg cTbzVvHh9HAXWV6ozMr9QAeSz1uonEejVGOWBN4YYVrUGh+v6pf9NXj3rPnSdyY1zig2 kCKjoJ7TUxq8I1YbQsWb2O0vDMAGDuxIFGnTXerVp33ZfS5L9TDpLFDigVvoQBZA3/Cd 7a+xXJnz7+e+qgKY9Qfen/z9uGUIYWLMSWcrivVw4dtLMOE29BzYv5h+85EH6edCensL XWFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706930470; x=1707535270; 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=W699dhQrIRx2hl06FjgorWvkEI0rXLNSwHMVMLrlHT0=; b=SFWtCcH4Md0AackXd8V1hS+RGTv8RkOtMbyk3TZvfHCcXkU23ezFWLcIKA6GCu4h3n Z2aGoVGwnyu8g1M0Pqizu6spB+VSVIXmtzXN6rS0h5yrj3SRw1PzZgfDwDd4O+7KqT5s gFSvEc7DrHiuIHM38/hx7O0NZM3L8Fmz50r3m++I1NkvBlFW9TTcmwus/0o8hwC1NxBi Gy2VEBmhLI37geiPeVukkH4AJ8T7S7k3w/MxFyvx1Y9xOUooE3S6iTGjE57KWBgULCFH cVP/n6Uo92+3yzReeOGGM6hNbpGSLILUd+tlOtq8cgA3QZgIVTb/cyad8ShTDdjA1KzE 098A== X-Gm-Message-State: AOJu0YzbPUIo8ZgCHaIyw8IQYONQS+zWA2rZGffEKim/Qzj6xCajAyCe Pe17GH1/3kHkbeA8+1O3yqGghP/Am10hnF92CiCpIZ9nTNroponSVm3RGkBA9fpavXOjerV3J4h 8zG8AucP49BnA8ftjlRLVS6+z9bU= X-Google-Smtp-Source: AGHT+IGal0zu142ko8bnWqOPYyWjrK1GRtwWKns3Z9ARyOW+iDlA4UpYue30PMH5R5Ovdsl8l6k0oc9URByYj9g6VJc= X-Received: by 2002:a25:eb07:0:b0:dc2:3ec3:95d4 with SMTP id d7-20020a25eb07000000b00dc23ec395d4mr3957148ybs.47.1706930469673; Fri, 02 Feb 2024 19:21:09 -0800 (PST) MIME-Version: 1.0 References: <20240129054551.57728-1-ioworker0@gmail.com> In-Reply-To: From: Lance Yang Date: Sat, 3 Feb 2024 11:20:58 +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-Stat-Signature: 49rfrtbueiqto3cqizjdoekefsnhz3qn X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: AFCB9100006 X-Rspam-User: X-HE-Tag: 1706930470-33965 X-HE-Meta: U2FsdGVkX186YZQsj/ZIMncZa8znW1UU4ibQ2FvhP44kyf8JHmbFkOVj2n+8qTN+lKUJGbV5K2Sx7ePD3yqAjixZbx30Xy1VQ2916dTu+ZPTfhbAocIc9GsaVDGMSL86u4ZIfIPxCd9L49B9Li4qQMbBxATvcjg6QmxDCjGJlxTqC36x+bDvoqV175dIu8WqtQUlEiz/r863/6X6VSE6D3LIfgddyOaiqBX6DQ1W6no8n1rnZPE5tGDq79CuhhYbb1kL3BEjEuDhk2jr+8+5tngqODNv9Xw/49cmLTb/S/fo7QjDWg7iQOu86FRdIEFh7FT9Ec52f5RNaSbRnssR04BP33eALbsaxNZCLT5v5HAat0WQXaEpie8wjaz6mXrDB9BXl9+pjNUUo1BzA2Xr6K6Xvwye80iFhr33wZX/LYXC6C2UYzmZ4WdTamlL6t175p1wqU+4ZhwbyrSBf82hjFx3Y9ZzSeVcGPhrxVoN1MBggpRHZbWSto5jPjQAmKBmDsaSG5UCtOtg0F9oUPlLDNmIQVllrGslIqIULDY5Ljkui07M9OC8X+E9k7iG2FdLKORJEfDw/F7PsENYRgfDzDh6iMDfjP2ykIVyITD91XYhVA9RVt85UBzEx8zo6v0yRgbw0n5fQcm15bEiaVyVWhJlA1dP/uJ1cG+N0saXDUVj1udRUO2N10UCuGUwb0R2oRGwVhfagRxRWOwAVsGLT5KJy6IBqd8AIj3mQCg3Q10NWrjlSk0iuPRpOPEH86cJEmDsBl9lMG+AUQTZiu10quphHRsq0LwCxY1M3G2NIQkIvRAZ5D+2jV1qQzubI4tq0yk/MD4ePqmrp9KbzMKWfvCsqEPh1/WBbmwd4phAH3MuqnkhX+YCBYrXqtDomioxagbpbayl+sX+sO/HI5xSQQXqW3FGtTMN1KeV0hL/g8nVRPoUvAqWYkN0mQRen/OP8nxjvxeIRsITqG6XQot +AZTnt+e Rrh1PYOalR1WwgRdr13ZUQ9yVUW7j6VoVSAgPDeHRxY6RW5GMPMBL0kLzQfisO/zCU3JCEIm3OiYr+pbdhfnDY3HK/3XkOQTPbQ7HhH3td+vc1PDyxwY1QdbNY/Tnjhu+2qCDnSfwFNXSDtcEZKNj8A6dOM7tW47lXQMN/MZkZ4Ov+zg45YWXexpT1NHCjH/dB6fJWjQKUngvHSkmln7yT05u1/Xvt8jqTjPXgIkXHFhyl0MzFgVEkRolFXoI+UZXhFyzLmYzwAHyXAvGUA76/UqlprATbgv67K0DZ0p7LAmMOEa/UrFKmWFPblU7k6RD2htnrlViyq3dfHQ39W2cDdsyuCDPHh4CqXuw0B5mDVFCh7WNgv+4Px3cUWsx3CNTnADTVQ5BSVNq+FsInA29zvDEbbXo4O6jtQ5JeLX2HMM/XPKXdik+WAlZLG3+vZuKJSOUiMGbEUyYFytUxPgWOL+jaA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000015, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: I asked the maintainer of the daemon. Currently, the daemon is not optimized for huge pages. It has over 200 VMAs, with the majority being file-mapped. On Fri, Feb 2, 2024 at 2:56=E2=80=AFAM Yang Shi wrote= : > > On Wed, Jan 31, 2024 at 5:13=E2=80=AFPM Lance Yang = wrote: > > > > Hey Yang, > > > > Thank you for the clarification. > > > > You're correct. If the daemon calls prctl with > > MMF_DISABLE_THP before fork, the child > > mm won't be on the hash list. > > > > What I meant is that the daemon mm might > > already be on the hash list before fork. > > Therefore, khugepaged might still scan the > > address space for the daemon. > > OK, I thought you don't care about the daemon since you mentioned the > daemon would call prctl to disable THP or enable THP for different > children, so the daemon's THP preference may be constantly changed or > doesn't matter at all. > > So the actual cost is actually traversing the maple tree for the > daemon. Does the daemon have excessive vmas? I'm not sure whether the > improvement is noticeable or not. > > > > > Thanks, > > Lance > > > > On Thu, Feb 1, 2024 at 4:06=E2=80=AFAM Yang Shi w= rote: > > > > > > On Wed, Jan 31, 2024 at 1:30=E2=80=AFAM Lance Yang wrote: > > > > > > > > Updating the change log. > > > > > > > > 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. > > > > > > > > This optimization is not a correctness issue but rather an > > > > enhancement to save expensive checks on each VMA > > > > when userspace cannot prctl itself before spawning > > > > into the new process. > > > > > > If this is an optimization, you'd better show some real numbers to he= lp justify. > > > > > > > > > > > On some servers within our company, we deploy a > > > > daemon responsible for monitoring and updating local > > > > applications. Some applications prefer not to use THP, > > > > so the daemon calls prctl to disable THP before fork/exec. > > > > Conversely, for other applications, the daemon calls prctl > > > > to enable THP before fork/exec. > > > > > > If your daemon calls prctl with MMF_DISABLE_THP before fork, then you > > > end up having the child mm on the hash list in the first place, I > > > think it should be a bug in khugepaged_fork() IIUC. khugepaged_fork() > > > should check this flag and bail out if it is set. Did I miss > > > something? > > > > > > > > > > > Ideally, the daemon should invoke prctl after the fork, > > > > but its current implementation follows the described > > > > approach. In the Go standard library, there is no direct > > > > encapsulation of the fork system call; instead, fork and > > > > execve are combined into one through syscall.ForkExec. > > > > > > > > Thanks, > > > > Lance > > > > > > > > On Mon, Jan 29, 2024 at 1: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(s= truct 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 khugepag= ed_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= (unsigned 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= (unsigned 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= (unsigned 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_= disable(mm))) > > > > > goto breakouterloop; > > > > > > > > > > VM_BUG_ON(khugepaged_scan.address < hstar= t || > > > > > @@ -2408,7 +2414,7 @@ static unsigned int khugepaged_scan_mm_slot= (unsigned int pages, int *result, > > > > > fput(file); > > > > > if (*result =3D=3D SCAN_PTE_MAPPE= D_HUGEPAGE) { > > > > > mmap_read_lock(mm); > > > > > - if (hpage_collapse_test_e= xit(mm)) > > > > > + if (hpage_collapse_test_e= xit_or_disable(mm)) > > > > > goto breakouterlo= op; > > > > > *result =3D collapse_pte_= mapped_thp(mm, > > > > > khugepaged_scan.a= ddress, false); > > > > > @@ -2450,7 +2456,7 @@ static unsigned int khugepaged_scan_mm_slot= (unsigned 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 wh= ile > > > > > * khugepaged runs here, khugepaged_exit will fin= d > > > > > -- > > > > > 2.33.1 > > > > >