Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp1540515rdb; Wed, 31 Jan 2024 01:30:45 -0800 (PST) X-Google-Smtp-Source: AGHT+IG8eQH+hbiSVLR9VUMDzKjRlLvmh1M3NBMyErpVl+2Kjp0eupRUzOSyZ6nfRW75ccRNCfZK X-Received: by 2002:a17:906:b248:b0:a36:8b31:5ca2 with SMTP id ce8-20020a170906b24800b00a368b315ca2mr291834ejb.75.1706693445267; Wed, 31 Jan 2024 01:30:45 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706693445; cv=pass; d=google.com; s=arc-20160816; b=pKqh/amKYk5QIEKWnlZzyQZ99ZZ8pp+I15p72W9Z2ys3jQiT7eCQqIACjdj5IKDtAz hCGJ6WQ9t+dHvuEKwWKlXRh1oCLmgMSjeJhP4jvyN3L+G7KZBdoTQMs1t8PzernboztB dBjYa+60olPVSFzcm23FluCk6zHvNDp5zRPeHgIfzPD7/8pHoDc6+4sZUg3tgns4yIMU mnjbu/2qEZWMd7/Aj9Sdwm/kTd5Moe8sn3xyPStErar6fZOf1dEz1JJvAXna51uQJVmL vx6RrvDMNyksOuuR5mVt16qOzRIjsmJBYGl88GfP8kJxl/R2MCSc7iXWhKRCmFW1TJqq pGew== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=n1hVXwdyUh5O4q5jCGS4m9GkNrPafUP16G5xoPdeqV0=; fh=We01PRZqsslLjIuFZKKoRBv5o11LglmRyxzZRgVVa6s=; b=RC4ArJadBkEHwLVat2bVpAmAh0IQb+SOApcIDW9/oBQkAQ02v+5jrQYauT7UN0UpGd KZGkk6EmO5fGcoJTN61u8jdafFaBEzP9jC925GA8Pmh9I3VVPE1Ozh7QZX1VxHP8ZztI 77NPY6Uedkz2dXi79REzKnIeQ5PRtkd5YVJQsgOW7ydLWYZYdvJB/WOTgxrpMQUvQMe2 o/ol5TW7yOBU0Ssnek+wuEQiufEYjr1cZUdfZkvDuLnAqNsJT5AF89SeONYv6/MVzqz0 eKr05VkyROwGYRbsZ9SmlW/a8VIj1I2xIKxYDrCMArJDA+twCKHSEvsUDtGfd2BrkCSK G6AA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=J1pFQoza; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-46140-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46140-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com X-Forwarded-Encrypted: i=1; AJvYcCXCsQALaNGEWWjs7CJA+e/cLYj6+UTS3oxXcfkn1LLTGVca4hb7MutNMIsBvClpmT4iIu/UTOZiyA9n0rsLMzj5s8BmyWSFeEDy0DwFDA== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id s18-20020a170906285200b00a3064ce6062si5088318ejc.388.2024.01.31.01.30.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 01:30:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-46140-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=J1pFQoza; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-46140-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46140-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id D99621F2C54D for ; Wed, 31 Jan 2024 09:30:44 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7B0A967E8E; Wed, 31 Jan 2024 09:30:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="J1pFQoza" Received: from mail-yb1-f180.google.com (mail-yb1-f180.google.com [209.85.219.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2280A67A1F for ; Wed, 31 Jan 2024 09:30:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706693426; cv=none; b=dDtz3WtczTbJyjoY0Le/K4z2Y699GoUfb2OSJbBm4dvK2E1AOfMs9DJQ7rGFVh7Ww2jyljs7PpjLGQBEhl6/EoQT9gGcAKSCZl+NkPYb5vPNBStXotjOGqriDhguKQFtmW7mXCToCKamILcKDPDVz7gZw4fvDKjSgEu+EZzLMqE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706693426; c=relaxed/simple; bh=TVJL02G0kimP27mlX3sjX/UsIEhPEDElPeP+WCA5DWE=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=MgSlddLsvgOzoQOzpQfFTGglyjZvd0OCJHmICLqUTnY1h7F626PJJg8GhnWqwuoY2g/PIEaAX9+5QhwP6mq6Mh1veAg3nfsAEGtZTLHIc8LSR4S7X3vfJsH0lqbt/Y6F+MsaZGH/JqvVXRIrGyrLJ1D7mcYLeFFwRN5f1TQ1c3E= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=J1pFQoza; arc=none smtp.client-ip=209.85.219.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-yb1-f180.google.com with SMTP id 3f1490d57ef6-dc261316b0dso4229530276.3 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=vger.kernel.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=J1pFQozaAr6L+UELeK4QmpdAyag6ZVBS/ULmcVXi6w0jbYu2cn7ED1iaJbMJRcaGov MbzkMjLrJwfPxknixpxCp7zox/FWcipl6JGwWY+2ARWUgK/NkTsH8+7dGplL5Mfcc31x E1Yo+ojdZPrKSNnSoLjbFjqKHV0ft87ncDPJthqGQsU5Q6XtM3iHjA19naBKpRCPVmb9 /ImIJnzcasOaDBVvMm2iuM1O+Z/xI6GjGcKanvgz8s+JJyyWLOYKx9vVZWO+Tck06mFD 2FqYHlW7gPMKrlWUECEPusTLFGE1LS5LQSyBYQwXhUd89gWuV3XZLyfzZW1xu/8uqrwr XGLA== 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=TDfXR4FHccIP+243gzO9izuOkd7qDy8LMYrnvflZDFp9k9UCt6D83GHTq5hd0TGxuu yjJZVcClcx/LMMQavjIiCsCX2CoxdhsLQX0HnwtukvwEi+g2dvdkgZTFMqQQyaMk3uYa bwW+GP2eO9Wm49fisJ8Z14AgkWFWs3QmsC1Mpv5ymmdS34OlF5QyUQfWi2nQrbJ4bvj9 v2mX9h8OjJYWYdTemD6VbCx9pcp9fy5ZG03EBcuOy4nRU6EdAMzcV7stqULKjJAxWU5e Yejgtgi8P06zA3f7HVV2yR4lIZ9OS9lP2a8GbusXPaTWkTbVlWYJtOjxGIc/vwgecjU7 8h2Q== X-Gm-Message-State: AOJu0YzU3PGs5oQ4EeBXkbhIfbJEmQQNeKrDVu2gYSUOYEynvlCQ6pBi Jg+nSBbIHbWl/mjUuKJb5aZtYfV23w0cgreaYRR+OVCnOcGWMKd+sJ3dNQjH9bqHtCtTKZ/P9Ix 3oMszv6S2RToqoe72Do3NYT6teVk= 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) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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 >