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 D70F4C47DDB for ; Mon, 29 Jan 2024 18:53:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 572726B0095; Mon, 29 Jan 2024 13:53:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 522016B0098; Mon, 29 Jan 2024 13:53:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3E9A56B0099; Mon, 29 Jan 2024 13:53:23 -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 307F86B0095 for ; Mon, 29 Jan 2024 13:53:23 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id ED1F3A1F04 for ; Mon, 29 Jan 2024 18:53:22 +0000 (UTC) X-FDA: 81733246644.24.C7B7729 Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) by imf02.hostedemail.com (Postfix) with ESMTP id 2B7E180013 for ; Mon, 29 Jan 2024 18:53:20 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=AHTKrhoZ; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf02.hostedemail.com: domain of shy828301@gmail.com designates 209.85.216.42 as permitted sender) smtp.mailfrom=shy828301@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706554401; 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=+YwmkIaURVZ8vPjIbvRHrGqiyqFCmwDrlPD7H+kz8UE=; b=dH/ygtrcVfUwOQ90tES/xVTx30BzT7scemZOgwrUOZZpuk54mpUhcANmgFhPZCTuzrL5Yx RflCIucxIDwR/UO3WKgaFXVukQJaiuudTgyyny/H741qOq5boaGB/wwzznP2wOeCbil3Re MXRavejrEkxvXG6wi9UI8C3Sl6ctAKI= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=AHTKrhoZ; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf02.hostedemail.com: domain of shy828301@gmail.com designates 209.85.216.42 as permitted sender) smtp.mailfrom=shy828301@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706554401; a=rsa-sha256; cv=none; b=MgKFw5onbd8APMGK9LEYxX0Ocqp0xP8Sgov3qEyb/TOMJerDbD7LXqyk75oyGC+xDZblhT a2JhR7UnrPVxryCh0R6a+k7YOnfj5Bzrfzg/EwwCJrljODOqR9UMhaD1/XMqWJLDx2TcL/ h8CZiS/Cn97v06tCCzXdKY8Vcpuqfh8= Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-2903498ae21so1657464a91.1 for ; Mon, 29 Jan 2024 10:53:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706554400; x=1707159200; 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=+YwmkIaURVZ8vPjIbvRHrGqiyqFCmwDrlPD7H+kz8UE=; b=AHTKrhoZbA3LLX8HtkGsJ6zee4kCQbzwiB/aFO5NxUuAYe2V5isD9qSGNKj8LaY8Ni 0BnDZhWZ37lTNj415iX6GUWWah/xR7K9qARepm4eivZV13pa2IOhxkEh7gIigMuhgqRs 4R8aUpGUXGBXXuJDrn9b+VqlpC1nWk+NQlXFSo3KLIPG2GkJIrjEqGWWjwtgJTqbfAhd bUT6qMe7FhlECRymske1FSLo5ZdFAjyKrWvKkBVzRgQvhA4OYq9+CQtZUxe5b2TG932c nLN4R1xCkFymx0m/lp5B3IuN8xKvIWWlHXI9ud1429LLqM+ywfkmVSaw0DrEyHEA3O0s 1xFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706554400; x=1707159200; 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=+YwmkIaURVZ8vPjIbvRHrGqiyqFCmwDrlPD7H+kz8UE=; b=n+jql3JVGtRBXKEWJ3ysyaArUpngelr3GdLoETaq/rv55TEdXVPG50P/pKiLQsIv3k 5z9rAYOeCubxuKCAjtIgbOHPTvbZ9sA3Bi84w7jPXIqP6Xd1PLNwPBbfckpQIJhjCVRx x1nAZbiQlS7hSRz2B30d+rWCoAuQFS2VRuj794PKvQlrRsjrSSFryGV4++vMhWeWJSma JWMGFN2sHeHTRPcIdygK8cBNZCMDZs9q5/t51W7myQbbDNng9KVS/qO7P7SRb7qOgFAb NOg/SO9LBG6yxNwbTmeexXWBjuzeMT/63n4uI+VmRM+hYVy2vP+QPSexYw9u8sbOcN9W lhAQ== X-Gm-Message-State: AOJu0YxZ3uW9xst5PAJno8pp2lXGZYXJzB5YvZpGCYrnwxqjl0XycFT+ CE3LqRBTKD8DEOcUXLWz8C2d6fJK3eTEc9E8TGy6+hs0EcSMWdylO8s65TGrtgbS2tjOQSWBeym 1ZTIK1jLG+bubmHfyIRX7LPjpU/w= X-Google-Smtp-Source: AGHT+IFIzCfOdhT8ezi11yOAP4WRj9BoQB4VbdwxVvfa4oJDrxmfXQeNnWop+4JG9vj2143VUB2qaLHZ0+W6RMHzfF8= X-Received: by 2002:a17:90a:ad06:b0:295:891f:aa19 with SMTP id r6-20020a17090aad0600b00295891faa19mr1479785pjq.16.1706554399900; Mon, 29 Jan 2024 10:53:19 -0800 (PST) MIME-Version: 1.0 References: <20240129054551.57728-1-ioworker0@gmail.com> In-Reply-To: <20240129054551.57728-1-ioworker0@gmail.com> From: Yang Shi Date: Mon, 29 Jan 2024 10:53:07 -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-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 2B7E180013 X-Stat-Signature: offuud7agz41irn744y7yg9ki9uoj8zr X-HE-Tag: 1706554400-549325 X-HE-Meta: U2FsdGVkX19AzIO4O+RSe6xEVd5CBm7eifceMbh1HAyODK6wlnvNME39Xh+ulugx7pIq9xEhUE6FL4Ww1i2+kfs0PooYb6MbKWzYqTKkKuvQ5PSggBmSzc5QDgS5nML/76BUan9IASr/UCM7humNByiCTOfPmA1VNS/c6kjLgOBzXR2UjbqLKb4HRDqgR9bIF6rRPSdqiAlHPb0oAyjBukgbpNT+LyhvPPpkJ0aeqLgXLDfTbbB05KI115e3T8b0x8eV62qL/ZWNsRSgmnMzMU9iPqAQSwaSNrXtHeoJBxUEr6K50os4rApE7sLGWNo0OBl0QAxVX71wj7A0RynHNWnzIq0PS3uOE1hVHNWffHGTrxfX9TAFBr4cJLHk1RrT7d9qgMz+U9KT9p5VzeSAgyTni0DVMpPabxbnQ31csMSsgd3BPG39B18dA4O9Gd7d+Sq3fzvILvfHcunsGjNbF+zpZL4uXD2PxqTosWs20MGizce7I8E5THuwcXQjkzGRp91tk8D72kH3/CWCjkyCkm5S6FopDc92P2KJf1BgYQH5Qf+K6hBXGFNfE8HK/Rp1TrNKLadXEI9IpBMltBmUhW3blKrrONQ5IrIUu2DnKEhfXJzLEynJY2iBbgPxaNqUmntCeonq6z2Im16/wcaEqe60uUELbGIa3tD1SaLYsqHcOhG8VRQEq9KFfoUSkQRuOpp65uVIa7/YhR/0dJQu6r//cPKgl1e2HnHvH7wF4R5Q01N7mHBJanHWNPfiHsQlVX0DbUwXx0LlHIfqNBzMkRASrrBsPqwz73GRCA4weNLh1sCw3XqPbDITwEUWPSUFG4z1rqo4YwFXFL0XvpDCxUYWUUOruUV+GMHymyFkkcuJMX9gd7FpXThJ1HhTmZBN2mpyiwvKN/Oxv9ZHl2hLqqMxtDUOtM+vwsn///CxMdNUYmlNarZzWQoqnJ1PobPfvC1HjJJhjn6Xz5vlD2G imAOce1m TBzVdCb+Bi6rvLi0wwtx62VkuIT96NKkk1BezAiqUTZBfJJDct0VpoN1E9XUp7qHWRKmiGsPHa4pu9Hp5Kz8+9K95LpXDF9sHTspG68j49Wn3VORWoNeKHmQbja+YjHI4eq5A0YWdQIJ5DcFCCxsMnW2RM5++arWQsnsGVlrvrsuUmJ0KMJ8jXRiincUpMQizFIk3CJOaWOEpgbjwjctD9nKZdEzDhMlCsjJHFx8fVO8b8k+PRfc0YRfZMYw+fat2CbZjz5VCsmHOvAIgH0uNA2xR02wVvUl4ZKEN3W4pPrqeWKfp2TSpuLfOrnDL/R0zzi5d5UzC3IEhfQxiZgxqXbWU+iis3jm1Lf87A95H5Ya66Gsvm7rnnepDXWPM2v8xuYYaLCx6nSqVTHgfaVRVC5NLIjqt1KCSpyDmh0Mo4ITBYuI= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, 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 Sun, Jan 28, 2024 at 9: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))) { 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(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 >