From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-170.mta0.migadu.com (out-170.mta0.migadu.com [91.218.175.170]) (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 A8ABA380FE6 for ; Sat, 11 Jul 2026 09:12:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783761146; cv=none; b=iUkJ6BOcrdGR2qSKoEMCRG7wzqiLhqvE5GhUvqi2xyK7MJP8z2o7VLytyDRk5N9eH0UgBPO6D5MHq5gieu73BaNYdjVtIDTL0m0vIr+jpP/Nm0uLJ0PGOB3zJHa+SvgkRTJek0sdrgMHSfAsgxMItAdM4R0/GwWfhQquZy5nnYE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783761146; c=relaxed/simple; bh=8L0x0W6q7vI59X5feYF/Qatglgd2wv+e7L5bV2y2tjw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ThnyYLpKDCAdsIuymMJr7K5iUo7P+niFWVq5pxG/kSPJKJcUWzjLDbGw0c4dcFB4R15BYmBsl5+OPwsZYoR33Q7VmHZEFoWlM7IkabpTGqVU9h1fP/CLOTifn3mdUyUTymf1L5fS6YF1eHgYjlt2tXbl07dI5YtjggYfREOQARY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=tzY++Vc1; arc=none smtp.client-ip=91.218.175.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="tzY++Vc1" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1783761142; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=n/fmkbFM82bplCCU3tUfwRgpNFIhXBSaqcx3fDjOrsA=; b=tzY++Vc1F1viUG6vvFRtxLtjS5oZ1uSR8/uLyMekaroZ51SNwiZhPb96dgQWtb6UizyMX6 sKQxrJ9aCw+DjKlWLNg+1m5YIaj5GYUGgtPo3uImTLeKgFWmuNFEAVXSJhYgL93yRfpKh6 PrYELeCcMbq2aLwWAb6bdiEqVAya9GU= From: Ridong Chen To: Andrew Morton , Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Chris Li , Kairui Song , David Hildenbrand , Barry Song , Yuanchu Xie Cc: linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, ridong.chen@linux.dev, Ridong Chen Subject: [PATCH 1/2] memcg: move mem_cgroup_swappiness to memcontrol.h Date: Sat, 11 Jul 2026 17:11:56 +0800 Message-ID: <20260711091157.306070-2-ridong.chen@linux.dev> In-Reply-To: <20260711091157.306070-1-ridong.chen@linux.dev> References: <20260711091157.306070-1-ridong.chen@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT From: Ridong Chen The per-memcg swappiness knob is v1-only; v2 always uses global vm_swappiness and ignores the per-cgroup field. Guard memcg->swappiness with CONFIG_MEMCG_V1, and move the helper to memcontrol.h where it belongs. No functional change for v1; v2-only kernels drop the unused field. Signed-off-by: Ridong Chen Acked-by: Johannes Weiner --- include/linux/memcontrol.h | 25 +++++++++++++++++++++++-- include/linux/swap.h | 19 ------------------- mm/memcontrol.c | 3 +-- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index e1f46a0016fc..f59614956f96 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -239,8 +239,6 @@ struct mem_cgroup { */ bool oom_group; - int swappiness; - /* memory.events and memory.events.local */ struct cgroup_file events_file; struct cgroup_file events_local_file; @@ -318,6 +316,9 @@ struct mem_cgroup { /* List of events which userspace want to receive */ struct list_head event_list; spinlock_t event_list_lock; + + int swappiness; + #endif /* CONFIG_MEMCG_V1 */ struct mem_cgroup_per_node *nodeinfo[]; @@ -365,6 +366,9 @@ enum objext_flags { #define OBJEXTS_FLAGS_MASK (__NR_OBJEXTS_FLAGS - 1) +/* Defined in mm/vmscan.c; used by mem_cgroup_swappiness(). */ +extern int vm_swappiness; + #ifdef CONFIG_MEMCG /* * After the initialization objcg->memcg is always pointing at @@ -1440,6 +1444,23 @@ static inline void mem_cgroup_flush_workqueue(void) { } static inline int mem_cgroup_init(void) { return 0; } #endif /* CONFIG_MEMCG */ +static inline int mem_cgroup_swappiness(struct mem_cgroup *memcg) +{ +#ifdef CONFIG_MEMCG_V1 + /* Cgroup2 doesn't have per-cgroup swappiness */ + if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) + return READ_ONCE(vm_swappiness); + + /* root ? */ + if (mem_cgroup_disabled() || mem_cgroup_is_root(memcg)) + return READ_ONCE(vm_swappiness); + + return READ_ONCE(memcg->swappiness); +#else + return READ_ONCE(vm_swappiness); +#endif +} + /* * Extended information for slab objects stored as an array in page->memcg_data * if MEMCG_DATA_OBJEXTS is set. diff --git a/include/linux/swap.h b/include/linux/swap.h index 8f0f68e245ba..2cfe364d66dc 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -372,7 +372,6 @@ extern unsigned long mem_cgroup_shrink_node(struct mem_cgroup *mem, pg_data_t *pgdat, unsigned long *nr_scanned); extern unsigned long shrink_all_memory(unsigned long nr_pages); -extern int vm_swappiness; long remove_mapping(struct address_space *mapping, struct folio *folio); #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA) @@ -537,25 +536,7 @@ static inline int add_swap_extent(struct swap_info_struct *sis, } #endif /* CONFIG_SWAP */ #ifdef CONFIG_MEMCG -static inline int mem_cgroup_swappiness(struct mem_cgroup *memcg) -{ - /* Cgroup2 doesn't have per-cgroup swappiness */ - if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) - return READ_ONCE(vm_swappiness); - - /* root ? */ - if (mem_cgroup_disabled() || mem_cgroup_is_root(memcg)) - return READ_ONCE(vm_swappiness); - - return READ_ONCE(memcg->swappiness); -} - void lru_reparent_memcg(struct mem_cgroup *memcg, struct mem_cgroup *parent, int nid); -#else -static inline int mem_cgroup_swappiness(struct mem_cgroup *mem) -{ - return READ_ONCE(vm_swappiness); -} #endif #if defined(CONFIG_SWAP) && defined(CONFIG_MEMCG) && defined(CONFIG_BLK_CGROUP) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 6dc4888a90f3..b61def56c41a 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -4174,11 +4174,10 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) #endif page_counter_set_high(&memcg->swap, PAGE_COUNTER_MAX); if (parent) { - WRITE_ONCE(memcg->swappiness, mem_cgroup_swappiness(parent)); - page_counter_init(&memcg->memory, &parent->memory, memcg_on_dfl); page_counter_init(&memcg->swap, &parent->swap, false); #ifdef CONFIG_MEMCG_V1 + WRITE_ONCE(memcg->swappiness, mem_cgroup_swappiness(parent)); memcg->memory.track_failcnt = !memcg_on_dfl; WRITE_ONCE(memcg->oom_kill_disable, READ_ONCE(parent->oom_kill_disable)); page_counter_init(&memcg->kmem, &parent->kmem, false); -- 2.43.0