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 B7B6AC47258 for ; Wed, 31 Jan 2024 20:06:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B9E4D6B006E; Wed, 31 Jan 2024 15:06:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B4EB56B0071; Wed, 31 Jan 2024 15:06:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A16896B0072; Wed, 31 Jan 2024 15:06:47 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 920BB6B006E for ; Wed, 31 Jan 2024 15:06:47 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 6A86C808F3 for ; Wed, 31 Jan 2024 20:06:47 +0000 (UTC) X-FDA: 81740689254.03.19F7476 Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) by imf12.hostedemail.com (Postfix) with ESMTP id 9F6EC40007 for ; Wed, 31 Jan 2024 20:06:45 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=H4OvIGOj; spf=pass (imf12.hostedemail.com: domain of shy828301@gmail.com designates 209.85.216.46 as permitted sender) smtp.mailfrom=shy828301@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=1706731605; 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=XBzDdvRsLLKEceNS+LZmwQIkmEXrkHn77clVBzNk9Io=; b=BtTuDCrlHThxV12ynM37Ad4eZ9ch5PXcQspflfxFBQAzsm6ESg55lCNo7gVcWAirujoOeV uBbGf8oLCwTrp050u4pLsto1j5R08kYJ/CGYF5rIPRY6qzhQB2QFo0dLVT2wt4GYsP+0HZ +oDGoHjjtUZaOIRZoFxn6szhZA8xw4k= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706731605; a=rsa-sha256; cv=none; b=1oK2pc/4aeCDzao+V4/bLYCmFI7yqiEOlhJVBBDc+wZS62qyoJ24+TC2tHG+ds5NKfNZ8S Mz4hYvZYMQ9Auckq6n3oLAa5vhg8vMcvpvwkSCiTfILq3EVb2SRBwWcKb3EPavLD2gtpgw pMPygwNkm/76zy5ALo2lQKl6REO5K+g= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=H4OvIGOj; spf=pass (imf12.hostedemail.com: domain of shy828301@gmail.com designates 209.85.216.46 as permitted sender) smtp.mailfrom=shy828301@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pj1-f46.google.com with SMTP id 98e67ed59e1d1-295d22bd625so77279a91.1 for ; Wed, 31 Jan 2024 12:06:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706731604; x=1707336404; 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=XBzDdvRsLLKEceNS+LZmwQIkmEXrkHn77clVBzNk9Io=; b=H4OvIGOji9NNS1eELL9iAyPZ9+ZkbEqux7kM7oEfIHUqV+y5mqdKWrV/vtzzYHxVZ+ QSJuSj0FIxXcbF3I/Ov7PljTmRJyVCnxmwz6yTqsqO/mK3owhkchUjPucXGxzAytS3G8 Fia4xA3oyH3R4fprsx4r3mKZyIdGEWzYH3RD9Hg1/yszPBncqZQD32Wi2WixpD4Zw3QA XVphgprUficjU3jwKJ7zkBuA4HKdAcChxePexvMeg1+PZqLERkTLTyKXX4wLA/lF6N+G TE9kSPHsQ3koSdAlAzyh4zfvxiMIKTCoQo1GZvC/q1V6VNL/8QvpSbbCuoTtqzSt1gKe 73Vw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706731604; x=1707336404; 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=XBzDdvRsLLKEceNS+LZmwQIkmEXrkHn77clVBzNk9Io=; b=DVwo6Yfs1RK3BKDaFIJbX6HoOqRUsIt5fV/o9Gkp1uBvx6ca/M3PfmvUAKxj6sQ1rW FairiuErqkCZGX9DiQ9Oq+fumHY85H6wegvXpLZt5OB6XZ0L0/29pprPuEr/FZQwtuWD 4WjFLCf1iim3Af6s8Ljqzey+4h1JKxleTU6CP9OCvxHogiVT6Y4mI0CMndjWnxVMuX2b U5x/oGIyIBwS4cJltKQvSSsSoBgUudbGAjt9H+AH4l3yzHPA3NHvlybLplwLWdz7JGhE 8ttOF7FlNQgqP0HtMA//PpvSlvoM/2AFx4TJlc+T+yQFo0TUB2xBTDawGLKMplzaZzGF R/og== X-Gm-Message-State: AOJu0Yz+diMIWQqD4RxgU/l3BN8RwujvJl+7hxWqRweixmGHes56fKGo H8ApDiOr6MU18o7x1nljUzFRyk1aiYVSL0FLg8OBkns0ldxZ2JuX4nNm102hUce7T/qzqaHyZHU xMYKi2hvddTIIujgK+WmvhTnuPV8= X-Google-Smtp-Source: AGHT+IESjf9/FSyS/yTRw9eFrkUbAOV0D3eR7ZN7x9oleGmjKJmzH+RoL9cs0qRYohW8MnQnTYhDHsYNe1UIBkFxRwk= X-Received: by 2002:a17:90b:a0d:b0:295:b985:5323 with SMTP id gg13-20020a17090b0a0d00b00295b9855323mr2831805pjb.7.1706731604371; Wed, 31 Jan 2024 12:06:44 -0800 (PST) MIME-Version: 1.0 References: <20240129054551.57728-1-ioworker0@gmail.com> In-Reply-To: From: Yang Shi Date: Wed, 31 Jan 2024 12:06:32 -0800 Message-ID: Subject: Re: [PATCH 1/1] mm/khugepaged: bypassing unnecessary scans with MMF_DISABLE_THP check To: Lance Yang 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: 9F6EC40007 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: gaxmzokpq9j149b7ripkexj4mydasyeq X-HE-Tag: 1706731605-721130 X-HE-Meta: U2FsdGVkX19Gom9sfHezseIrGrc5sBMLIpT/J83ToE2Rp/gylTn/jqsOf7HVM0NSDNS65K30og5MdWyaxr05Egwti4RdejjoQ/yWsHICPHo5WVKK5qigzLtz7GQ65W7mhfVKoViMAnQei3DKEGBc833+iF0tDnfD1UoZ66do4GDeAKhFZHU9JcXLhN4asG6j51+FLcRvjJRUGge68HvA9baN3oJ/bEEVZKH3y+PkHq0Y4xuxrJw+iLGLrhFuiPxNit2h146+UyEfBPaE6Ljcpqy1O/+Gkuy2nDB1gcxRlZFFLu9V7rwd5psP90OmC5Wt6hD2HQgZIiVirdik7vVNCDUc/X7c4JWOTQwIQJDJGyetH4O7yAaAqTGSluIeAoThiyhLZh+sqmkr+9Lt0jqQbitESd4XWBBxhdHTuEmlnIwHmdf5fURdfFvW8/8JB1hAKdE/3uGbfPiE3L75EvIcfzigufHqb+vDPl1dR2MMU6Rx1sr45E8vzI6olsjHWgtxCwsOAnEkSnshgh+k2UwFjVKoT57XEQoqnLIx3zJgvAa5gSYmFfcsuCiylSRRe25lXuo7KAYgAciF/dlDyY59Gg1K+zQxYowqZ1YThkOHYN1T/9I0KLU+cZB+28gYR3oM9S2eUjavLHdMNXp1FyGSPJeRMH/ZjWaO1VU6EOhDTn+k+bluohOmpW6edEJkNoLIq7vCG+86UkzXzCpWwHmB415N8Ms3k9K0tq9MaY809IhmECip6cvIFYK5LRvqn6dZkNpmnRhApsxQb5ALwT0B3N6wgNA3phzO1oK8uaErRvfoVCu2ScsFuuVoOxJcdMwsHupj4pGGWxpUmNhAnyVp2CAVxT3u9NYAildGJlrcclbTkmD/tqfZCaMe4IkQ5WZxihu6i07SHlDAWavufcmqjC0TOJ7a+4oEcfjLGJT6iJyXDsx9GZiXs1tgkh7XJizrpVwKKNKqfyt7X+hoy9n w1cu7d6C 6mYBgJ40qYiYo3GNEIL4edyA8nK/xWAAE+szqGaUnfMgkpzrFmmALXIuCp1rxCCM1wuHYSrNesdtcaASRxx2KrTcOIRVdYnHeJ+VkU/hkaLEJcsviwS+SMDm3m5AxgHWzNAp3FANlkGc2Juh40wD/3YxCIT7BoHJFP1Ea5ekVe45iX85eYu3t4JcBZmdDP9PLoC4CbzH70whsO1EDPnQqzXgvF5Kx7PgeaDtLxJSmI5ypMqayo09+irO0xwEF+zHBB41bBWl2Kp3C+8hkSLSLMwXbpAwHP1LFECoVE9oOn2j+SlFfAETyXiFG8Udmh78MfCouHMjxu0NFmzVWWfTPD8uKc7AmDhPrNA5Ig72EbWNRxwj9pY/F7tx8MF11ewWbVLEMts2pBXkLjAOxnOX4TAULBlz7Zb4FU1Ins6W7Cc7I7/M= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000012, 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 Wed, Jan 31, 2024 at 1:30=E2=80=AFAM Lance Yang wr= ote: > > 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 help jus= tify. > > 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(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))) = { > > 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 > >