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 1E8A11F4CA9 for ; Sun, 8 Mar 2026 21:05:31 +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=1773003932; cv=none; b=lXZEDeGv/frs7ZuhZPn8kGYxdeZ5IjiAETkvGslLEJyD9S+tDOsa4hbezD9E/4BhAEvrHySCj28Y66sEzq7IUWNMm9YMk7imfLPZKXaDBKKDGKuIp9AoS66rV5cDdXr45YYFWdF+/AIiFhluZ4eqhmU0XUXYFHoKEKdOMewh9eM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773003932; c=relaxed/simple; bh=X2iWXWJ4ssDw2wUAjr6v8vk50icl4TKzNNwYwDBMJps=; h=Date:From:To:Cc:Subject:Message-Id:In-Reply-To:References: Mime-Version:Content-Type; b=nB4BDdvbLaWSiSgnsTNRuPqlfqFAqWupkBw6gxOw8Kml1TQ3RooLA1F0791hfPcV60SFh6gFYFeAbqNRDrBEKYG9sNA+ZwjJ7hdf1RWtzxHTqqPnxPvns5FKocJKueNrcRCgctkPjDO/aIOXKVIrySvx+osuksuOS2UBAX56kF0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b=rch3nReA; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b="rch3nReA" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 02DD7C116C6; Sun, 8 Mar 2026 21:05:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1773003931; bh=X2iWXWJ4ssDw2wUAjr6v8vk50icl4TKzNNwYwDBMJps=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=rch3nReATYwOo0Ok5ueUob1xAdZakSjEwbCaa8ER1STIb9ndzWpdaRn8qQqK88g5o AcEba50+wuoLwQ0oAUZCc4FgouCvFbP5biVJlXYb54HVAs70U+CC7gHMILy8wjdOa5 DKW9TYM2wZg9y6PXkw/4CxYpMuLV/E8yc0RNlLWc= Date: Sun, 8 Mar 2026 14:05:30 -0700 From: Andrew Morton To: Breno Leitao Cc: 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 , Mike Rapoport , linux-mm@kvack.org, linux-kernel@vger.kernel.org, usamaarif642@gmail.com, kas@kernel.org, kernel-team@meta.com, "Lorenzo Stoakes (Oracle)" Subject: Re: [PATCH v3 2/4] mm: huge_memory: refactor anon_enabled_store() with change_anon_orders() Message-Id: <20260308140530.9ab6d1445d0936467eab4aef@linux-foundation.org> In-Reply-To: <20260307-thp_logs-v3-2-a45d2c8f3685@debian.org> References: <20260307-thp_logs-v3-0-a45d2c8f3685@debian.org> <20260307-thp_logs-v3-2-a45d2c8f3685@debian.org> X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) 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-Transfer-Encoding: 7bit On Sat, 07 Mar 2026 08:08:06 -0800 Breno Leitao wrote: > Consolidate the repeated spin_lock/set_bit/clear_bit pattern in > anon_enabled_store() into a new change_anon_orders() helper that > loops over an orders[] array, setting the bit for the selected mode > and clearing the others. > > Introduce enum anon_enabled_mode and anon_enabled_mode_strings[] > for the per-order anon THP setting. > > Use sysfs_match_string() with the anon_enabled_mode_strings[] table > to replace the if/else chain of sysfs_streq() calls. > > The helper uses test_and_set_bit()/test_and_clear_bit() to track > whether the state actually changed, so start_stop_khugepaged() is > only called when needed. When the mode is unchanged, > set_recommended_min_free_kbytes() is called directly to preserve > the watermark recalculation behavior of the original code. > > ... Somme nitlets, feel free to ignore: > --- a/mm/huge_memory.c > +++ b/mm/huge_memory.c > @@ -316,6 +316,20 @@ static ssize_t enabled_show(struct kobject *kobj, > return sysfs_emit(buf, "%s\n", output); > } > > +enum anon_enabled_mode { > + ANON_ENABLED_ALWAYS, > + ANON_ENABLED_MADVISE, > + ANON_ENABLED_INHERIT, > + ANON_ENABLED_NEVER, > +}; If we're feeling fancy we could use ANON_ENABLED_ALWAYS = 0, ... here, just to make it clear that we iterate over the enum values with an integer and we *require* those values. > +static bool change_anon_orders(int order, enum anon_enabled_mode mode) > +{ > + static unsigned long *orders[] = { > + &huge_anon_orders_always, > + &huge_anon_orders_madvise, > + &huge_anon_orders_inherit, > + }; > + bool changed = false; > + int i; enum anon_enabled_mode m; might be clearer here? > + > + spin_lock(&huge_anon_orders_lock); > + for (i = 0; i < ARRAY_SIZE(orders); i++) { > + if (i == mode) > + changed |= !test_and_set_bit(order, orders[i]); > + else > + changed |= test_and_clear_bit(order, orders[i]); > + } > + spin_unlock(&huge_anon_orders_lock); > + > + return changed; > +}