From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 58B203537E5 for ; Wed, 4 Mar 2026 16:40:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772642426; cv=none; b=RVctUcOkSfBCf/ysbtpPuMIRgIL2e05AOKLIv03OEriWzGauFp26htNmIkobuTjIuU/7ebPzhiz4zIWxmRijgEHLu9gBKFBpKoUryq69oIDcYHqGZ/CSOUtinmvfMn5v2rjx1+Jf3FqrgTmf8L7BXPKw2xw5p+KAzOe+X//jxAQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772642426; c=relaxed/simple; bh=zjtjHKRlpOdJAFXpFqfNvOxmBTyKuh8J7CIZSN6zpFA=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=iAiKSh2kRiCVaFh7O7k1/EUqhyiYq1LltH6sq5MqoPdliftF0fGwyv4k5lxIfFVH5IfhnUsG4JOsSzQBUgRQyIlH988Gtozd3NQfbZxQMpNWr1AuA098x6p4KvVbLeCWosR7ZMsBjTB0vSRe6eXu3xGGgK2cdI416zy/c3B404M= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=YhG7OC2U; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="YhG7OC2U" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8B457C4CEF7; Wed, 4 Mar 2026 16:40:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1772642426; bh=zjtjHKRlpOdJAFXpFqfNvOxmBTyKuh8J7CIZSN6zpFA=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=YhG7OC2UUjtKf3MfSWO435HdWtoax6lg7pnL2C1GXR4/F0LxFuwqT4PIsJsBQ0TQM fNzje4N34+/VU5O2dcI/JL//+O+qr+IfKoYmbthoy5CAIWwr57kigb9xGcWKBmZTEn KDCTnBYBUWIDLVOUJJc27wVnuhu3VnHbxM9zReNu8cxnt7aCn3dK811FnsCd0Clzpq wY0gUuaBUWK4e+FWZaqiBvAyR6C9HRTn++NM8JU+IzZNJO24KxeE2rl9Tv209dmFjr UVYfGsWKQGJQYi2YaR/4sNlukvmLj3ji+R2ItJgQmRm4cp876bRBUHVN30OpSIxnSi ytM5NECv+4TiA== Date: Wed, 4 Mar 2026 16:40:22 +0000 From: "Lorenzo Stoakes (Oracle)" To: Breno Leitao Cc: Andrew Morton , David Hildenbrand , Lorenzo Stoakes , Zi Yan , Baolin Wang , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Vlastimil Babka , Suren Baghdasaryan , Michal Hocko , Brendan Jackman , Johannes Weiner , linux-mm@kvack.org, linux-kernel@vger.kernel.org, usamaarif642@gmail.com, kas@kernel.org, kernel-team@meta.com Subject: Re: [PATCH 1/2] mm: thp: avoid calling start_stop_khugepaged() in anon_enabled_store() Message-ID: References: <20260304-thp_logs-v1-0-59038218a253@debian.org> <20260304-thp_logs-v1-1-59038218a253@debian.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260304-thp_logs-v1-1-59038218a253@debian.org> On Wed, Mar 04, 2026 at 02:22:33AM -0800, Breno Leitao wrote: > Writing "never" (or any other value) multiple times to > /sys/kernel/mm/transparent_hugepage/hugepages-*/enabled calls > start_stop_khugepaged() each time, even when nothing actually changed. > This causes set_recommended_min_free_kbytes() to run unconditionally, > which is unnecessary and floods the printk buffer with "raising > min_free_kbytes" messages. Example: > > # for i in $(seq 100); do > # echo never > /sys/kernel/mm/transparent_hugepage/enabled > # done > > # dmesg | grep "min_free_kbytes is not updated" | wc -l > 100 > > Use test_and_set_bit()/test_and_clear_bit() instead of the plain > variants to detect whether any bit actually flipped, and skip the > start_stop_khugepaged() call entirely when the configuration is > unchanged. > > With this patch, redoing the same operation becomes a no-op. > > Signed-off-by: Breno Leitao General concept is sensible, but let's improve this code please. > --- > mm/huge_memory.c | 27 ++++++++++++++------------- > 1 file changed, 14 insertions(+), 13 deletions(-) > > diff --git a/mm/huge_memory.c b/mm/huge_memory.c > index 8e2746ea74adf..9abfb115e9329 100644 > --- a/mm/huge_memory.c > +++ b/mm/huge_memory.c > @@ -520,36 +520,37 @@ static ssize_t anon_enabled_store(struct kobject *kobj, > const char *buf, size_t count) > { > int order = to_thpsize(kobj)->order; > + bool changed = false; > ssize_t ret = count; > > if (sysfs_streq(buf, "always")) { > spin_lock(&huge_anon_orders_lock); > - clear_bit(order, &huge_anon_orders_inherit); > - clear_bit(order, &huge_anon_orders_madvise); > - set_bit(order, &huge_anon_orders_always); > + changed = test_and_clear_bit(order, &huge_anon_orders_inherit); > + changed |= test_and_clear_bit(order, &huge_anon_orders_madvise); > + changed |= !test_and_set_bit(order, &huge_anon_orders_always); > spin_unlock(&huge_anon_orders_lock); > } else if (sysfs_streq(buf, "inherit")) { > spin_lock(&huge_anon_orders_lock); > - clear_bit(order, &huge_anon_orders_always); > - clear_bit(order, &huge_anon_orders_madvise); > - set_bit(order, &huge_anon_orders_inherit); > + changed = test_and_clear_bit(order, &huge_anon_orders_always); > + changed |= test_and_clear_bit(order, &huge_anon_orders_madvise); > + changed |= !test_and_set_bit(order, &huge_anon_orders_inherit); > spin_unlock(&huge_anon_orders_lock); > } else if (sysfs_streq(buf, "madvise")) { > spin_lock(&huge_anon_orders_lock); > - clear_bit(order, &huge_anon_orders_always); > - clear_bit(order, &huge_anon_orders_inherit); > - set_bit(order, &huge_anon_orders_madvise); > + changed = test_and_clear_bit(order, &huge_anon_orders_always); > + changed |= test_and_clear_bit(order, &huge_anon_orders_inherit); > + changed |= !test_and_set_bit(order, &huge_anon_orders_madvise); > spin_unlock(&huge_anon_orders_lock); > } else if (sysfs_streq(buf, "never")) { > spin_lock(&huge_anon_orders_lock); > - clear_bit(order, &huge_anon_orders_always); > - clear_bit(order, &huge_anon_orders_inherit); > - clear_bit(order, &huge_anon_orders_madvise); > + changed = test_and_clear_bit(order, &huge_anon_orders_always); > + changed |= test_and_clear_bit(order, &huge_anon_orders_inherit); > + changed |= test_and_clear_bit(order, &huge_anon_orders_madvise); This is badly implemented already (sigh) so a little tricky as to how to abstract. Yes the existing logic duplicated, doesn't mean we have to keep doing so :) To put money where my mouth is attached a (totally untested, in line with Kiryl's :P) patch to give a sense of how one might achieve this. As to this vs. Kiryl's... I mean it might be nice to fix this crap up here to be honest. Maybe David can have deciding vote ;) But see below for a caveat... > spin_unlock(&huge_anon_orders_lock); > } else > ret = -EINVAL; > > - if (ret > 0) { > + if (ret > 0 && changed) { > int err; > > err = start_stop_khugepaged(); There's a caveat here as mentioned in reply to Kiryl - I'm concerned users might rely on the set recommended min kbytes even when things don't change. Not sure how likely that is, but it's a user-visible change in how this behaves. Cheers, Lorenzo > > -- > 2.47.3 > ----8<---- >From cb2c4c8bf183ef0d10068cfd12c12d19cb17a241 Mon Sep 17 00:00:00 2001 From: "Lorenzo Stoakes (Oracle)" Date: Wed, 4 Mar 2026 16:37:20 +0000 Subject: [PATCH] idea Signed-off-by: Lorenzo Stoakes (Oracle) --- mm/huge_memory.c | 74 ++++++++++++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 28 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 0df1f4a17430..97dabbeb9112 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -515,46 +515,64 @@ static ssize_t anon_enabled_show(struct kobject *kobj, return sysfs_emit(buf, "%s\n", output); } +enum huge_mode { + HUGE_ALWAYS, + HUGE_INHERIT, + HUGE_MADVISE, + HUGE_NUM_MODES, + HUGE_NEVER, +}; + +static bool change_anon_orders(int order, enum huge_mode mode) +{ + static unsigned long *orders[] = { + &huge_anon_orders_always, + &huge_anon_orders_inherit, + &huge_anon_orders_madvise, + }; + bool changed = false; + int i; + + spin_lock(&huge_anon_orders_lock); + for (i = 0; i < HUGE_NUM_MODES; i++) { + if (i == mode) + changed |= !test_and_set_bit(order, orders[mode]); + else + changed |= test_and_clear_bit(order, orders[mode]); + } + spin_unlock(&huge_anon_orders_lock); + + return changed; +} + static ssize_t anon_enabled_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { int order = to_thpsize(kobj)->order; ssize_t ret = count; + bool changed; + + if (sysfs_streq(buf, "always")) + changed = change_anon_orders(order, HUGE_ALWAYS); + else if (sysfs_streq(buf, "inherit")) + changed = change_anon_orders(order, HUGE_INHERIT); + else if (sysfs_streq(buf, "madvise")) + changed = change_anon_orders(order, HUGE_MADVISE); + else if (sysfs_streq(buf, "never")) + changed = change_anon_orders(order, HUGE_NEVER); + else + return -EINVAL; - if (sysfs_streq(buf, "always")) { - spin_lock(&huge_anon_orders_lock); - clear_bit(order, &huge_anon_orders_inherit); - clear_bit(order, &huge_anon_orders_madvise); - set_bit(order, &huge_anon_orders_always); - spin_unlock(&huge_anon_orders_lock); - } else if (sysfs_streq(buf, "inherit")) { - spin_lock(&huge_anon_orders_lock); - clear_bit(order, &huge_anon_orders_always); - clear_bit(order, &huge_anon_orders_madvise); - set_bit(order, &huge_anon_orders_inherit); - spin_unlock(&huge_anon_orders_lock); - } else if (sysfs_streq(buf, "madvise")) { - spin_lock(&huge_anon_orders_lock); - clear_bit(order, &huge_anon_orders_always); - clear_bit(order, &huge_anon_orders_inherit); - set_bit(order, &huge_anon_orders_madvise); - spin_unlock(&huge_anon_orders_lock); - } else if (sysfs_streq(buf, "never")) { - spin_lock(&huge_anon_orders_lock); - clear_bit(order, &huge_anon_orders_always); - clear_bit(order, &huge_anon_orders_inherit); - clear_bit(order, &huge_anon_orders_madvise); - spin_unlock(&huge_anon_orders_lock); - } else - ret = -EINVAL; - - if (ret > 0) { + if (changed) { int err; err = start_stop_khugepaged(); if (err) ret = err; + } else { + /* Users expect this even if unchanged. TODO: Put in header... */ + //set_recommended_min_free_kbytes(); } return ret; } -- 2.53.0