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 D2AD0C47258 for ; Wed, 31 Jan 2024 09:30:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0EA556B007D; Wed, 31 Jan 2024 04:30:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 09B886B007E; Wed, 31 Jan 2024 04:30:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EA43F6B0080; Wed, 31 Jan 2024 04:30:25 -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 D875E6B007D for ; Wed, 31 Jan 2024 04:30:25 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id A1AF7120C55 for ; Wed, 31 Jan 2024 09:30:25 +0000 (UTC) X-FDA: 81739085610.28.A5051A0 Received: from mail-yb1-f169.google.com (mail-yb1-f169.google.com [209.85.219.169]) by imf28.hostedemail.com (Postfix) with ESMTP id C8EAEC0006 for ; Wed, 31 Jan 2024 09:30:23 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Eb76OjyK; spf=pass (imf28.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=1706693423; 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=n1hVXwdyUh5O4q5jCGS4m9GkNrPafUP16G5xoPdeqV0=; b=GlK7xPEepucn8X0+PV2p4KRFKJdrl1hiSYsOpBmdQSVw2JMiHemzS/v9fiHLKVut+thEnI IlEx/K1E8rkQTm9oxoCZXKMcZu8S06w0IMg9w6209/aafZxfnMvrtihl0cFU2cFdjvDFWT vNV5DfebbMnL0SFiaKX/9BkJmRM2y3w= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706693423; a=rsa-sha256; cv=none; b=SOoO8stVg9+mU9f2HqP9ys9H16oQno0XgONjDvCJZ4IoKiyUvbzw65nF//41Ke4nW/Qb1W TcRrkNbiAMi+FWURkTclDIQAm/O5joj21X2rRP82HMxkmAzTBQmWo7bydZVKi3+Yd/0I2K +qvTLuE8Zl4CHg6RuuHG8QjSRoWUtBo= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Eb76OjyK; spf=pass (imf28.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-dc236729a2bso4894132276.0 for ; Wed, 31 Jan 2024 01:30:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706693423; x=1707298223; 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=n1hVXwdyUh5O4q5jCGS4m9GkNrPafUP16G5xoPdeqV0=; b=Eb76OjyKUJuIq7hAjdeVfIWUj8yZHFwLx6wbHY9DqPKdIe2gliD4cFyFTCHwaiFCna XHoOTi5q3arYjcwKo1NIsfNwbigXmINfrWjtYuycBDLIBlRWe8V5O3sy2fMy502YI16b SD4atq0d0iVX6ISJB6e1ypHDFbZ9JOi/bU3qnxTkiQ6MRDBLry67f4Y+N9IHJXs17rk1 nx3ud8PeK2VmXUXkL8WKRTryvi783voE5B9dl+lRS8M3n5JkHXM0yWhE5h9vtr84HvN8 UUUbGfTkKK0poyGHdTIMzZmmsH0VrPk7zU3qWUWT7mS9y6tp0iX2Vl0uTFFhG5EJGec8 N9Zg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706693423; x=1707298223; 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=n1hVXwdyUh5O4q5jCGS4m9GkNrPafUP16G5xoPdeqV0=; b=UNNY2cPnjUeUf0hekG2v1uG8/8upveYlo58p578S7nxqZMkIW+tRzkocV5jn5L8OBH cC77sVXbySS2Di1IPNDAKNX/xZf59K84zavmvD2ti3v8c0Zab2XeL/7AGQmdrEoUvqHD S4VQHrOZFEYZ45ryrXnPe7CKZDg+BTVaMCmN36Gw9Rxsx+hoSIUtVnbrJfF3fCPLaUA2 1x8NoJNoSFSwmkbpJB0VkwzbSXW9f9Tl96DMmfv6Ajz9WSXHUWK1UEOa88cm10+DRN6s rhzbQlzIoJrPxGVwdPGr/IxT0+xzYEAdcXaaAcqBwVK0xzlxAeFlyzditbPFdnq0V47d BuaA== X-Gm-Message-State: AOJu0YxtH+tBUYW/Ts5Su0w3QxOXjzjyMSBvwDisG5Qpmz+cSHO9nGyO t6cbmPZiDog1Qi8OX33k14d6b2WIzITz2426V0yBGm5DH99M742qWhV6ZDZu5TLsdW5yTWtDQMX 4DehqJyPF4hlX+Drvw9v2ziTARHM= X-Google-Smtp-Source: AGHT+IGh2bSmh+uIzdUeEPJ0o5bZ1WTxUmBY9WeH8Yyf8ltmNtPPrRirIzXstNlEqmjWLu9W4uzjyOnWnzzyimD7/9Q= X-Received: by 2002:a25:ce90:0:b0:dc2:271e:51ec with SMTP id x138-20020a25ce90000000b00dc2271e51ecmr1071886ybe.11.1706693422887; Wed, 31 Jan 2024 01:30:22 -0800 (PST) MIME-Version: 1.0 References: <20240129054551.57728-1-ioworker0@gmail.com> In-Reply-To: <20240129054551.57728-1-ioworker0@gmail.com> From: Lance Yang Date: Wed, 31 Jan 2024 17:30:11 +0800 Message-ID: Subject: Re: [PATCH 1/1] mm/khugepaged: bypassing unnecessary scans with MMF_DISABLE_THP check To: akpm@linux-foundation.org Cc: mhocko@suse.com, zokeefe@google.com, david@redhat.com, songmuchun@bytedance.com, shy828301@gmail.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: C8EAEC0006 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: acfn4t8wt9xyrs4adq67bdgbjw8hbs7k X-HE-Tag: 1706693423-867419 X-HE-Meta: U2FsdGVkX19EUfj92miKukdoHQi9V18EO7x2alJDxzdBpHIN2EppqMmrN6D7vf3a0YiqrXB8jn3BmZo9kwX4TgApiwhKT0NCfQJ5dnqRw+YPqrlKPzBEBTaM0gHOzrE1XZXzEZcFafexCcSe4/Ho15fTa/CvZfS1CqMaJnt+hzIXHZP7MAtI3udcCF5rpmeW9aNY+z3bi7DwG1ZzFQdF1/MpvwtA7p5Rexk//iTBTGR3hsy37JEr+tD/WdAKOXnSWC68eSxF0b0aTj1ZamskSpTR9iIxAHqS1FFvzzVOTKQ3IRZw1WgBR0VH8scLL9a8hltrqSEUtAX/5FZ8eml4G++9iv5QcIMWuMDNgr4srN6jxEz73jPflmcGtZSN9sOt8kxiAdNtZqZCuQ1LU9g5DdCfKWNS7C4uyRIQeIsIu5kEQIxHRDMA5ssOG8QfPJ2eSai9IDop8oei+WNXyumvyTojbk180jn0J/UPIbBI2fxvmsl/zkrrcL+AuOATz/tl8ePyVZqC08md+QJvfw57zSdXYuM+cSVvDZx8Nm9jKedPeblQxNe1kOwz3s1Glk5GC9BqPPcp/C2vXZJlXTjjn/HAI4bZYS2+aaKdhthmulUeA3VFMAPyPgELIS2NMGh1LchUda9OTcIEk6y/b3stizVKsLzEErA6LhH0N4TZ0BE02445lxFCQU8lf5ieDXP+K1pKExyGf+JEK5fuOD74QgUc9HczVy3UzQSgrD48fcjwidKO9L34sxtT4O1JrZKlq+zee841SYZjRiAII4dLevVSKIosDKh7ufjuzxEbKr/PyxJHxVR3Zs9ADMhpokOeax6ka67DmGsUCszPH+FXPTDdHR7gzS/Di8C7FGknMgEllcb4V/7cA3qd5WRVQ3Rd9WuSjWB29TEm88h0kDqBnsVr8+hN0Y0+MuUcrmNonVPldWDlqnb6fUd/r9jdVLKlcAVcif6CV0AYSqu5CYG bKqgFFx2 GLxvGMpzna9ewNXC0gtDOE0OVBJWu1OBltYOo4vR5xavC1b9RK6iNC2oIByu0GiB1waAxK4n2+bOLr96zyXTirkaxbv6XwM5NPe5ygvqPN121By4LIzVk2opgfUjfwReJMGW6VDXmejkuREJZFwnEz1qtK47Hdf4gETM/bVffsv1VB2PaKrY4IKmriDFglufQOKQX29PsSnHGkNHj5Te3SyieOVvfEsliULmqajtOyltr1WBTvnhdYPp+X7okK3ELJVeSqfbNClGeFiXtJFgR9MY//ZB/GrJvcYdly+T21oic8TVlukPPcVnkz2hGOQmrw1Z2CkQM8/HqfSE= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000004, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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. 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. 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 wr= ote: > > 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_sl= ot *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(unsigne= d 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(unsigne= d 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(unsigne= d 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 < hstart || > @@ -2408,7 +2414,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigne= d int pages, int *result, > fput(file); > if (*result =3D=3D SCAN_PTE_MAPPED_HUGEPA= GE) { > mmap_read_lock(mm); > - if (hpage_collapse_test_exit(mm)) > + if (hpage_collapse_test_exit_or_d= isable(mm)) > goto breakouterloop; > *result =3D collapse_pte_mapped_t= hp(mm, > khugepaged_scan.address, = false); > @@ -2450,7 +2456,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigne= d 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 >