From nobody Mon Apr 6 17:25:46 2026 Received: from mail-oi1-f180.google.com (mail-oi1-f180.google.com [209.85.167.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 584C23803D9 for ; Wed, 18 Mar 2026 22:30:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773873019; cv=none; b=umh9oBLadaNNdElfpCM8w6P9/5wLoZNkrKzyTZ5NJ4rXgwSWpPOVEHlQUIydg+RbtDOV7g0U9C2b7mRA2aGU80x9i40t01XyPJVMqmBTr7VVXgHHw5a4j1+i05eE5pGVln5Acp6XDg+HhKlZVE2Ga/zuOj53TaAmlUKK5Lhpvd8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773873019; c=relaxed/simple; bh=Kbbs1P/Z7sYIeQrUTAJw3MhoUM3kuDJXf1v0JtacBaA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=j4Cnml1jyfZiv6nXI8Hd50UCYOX1kiVGESjcEVMMwcxOPv5xVDoZ3YxKzc9BW/FJCV6suadsIFIggBhfxcckaTVzx/iZlaASrj0oJfZLrbUXnBLqKdRzoq6G39cigVLM6vMAzHyuBq++PySew/h4GhKYXG3CMbSjux8MC4xfWe8= 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=A6poJPQp; arc=none smtp.client-ip=209.85.167.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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="A6poJPQp" Received: by mail-oi1-f180.google.com with SMTP id 5614622812f47-4670676ba03so37555b6e.1 for ; Wed, 18 Mar 2026 15:30:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1773873015; x=1774477815; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/r4lbyJWNRb4OPU+FwoTkjRVnxoVbk6U7PxvuQ43o+8=; b=A6poJPQpgu2xTuuVUub/pH2kfF93zOBi+zsCj4cz356IJTImTLwxY1v22ZGD5Zns/m UDvZjMYGIiFhwAu2X6cdxw0j91wM9Zl2nUNsvHf+SVkEpad2K+B/WHjXcNyprI0qnGai dH/yIqpB0XCH9AZW5jngS089MVgqxw2taY2w/5FQddFQqJwwuMlNPz0Is1Rc1EJW9PqT 4tt2WXC6Tt3ss6aP4glowTtsFqhG0NGhDXdU3Dl9Ly78fKjVyr3TliNi+B8UjvLfVzXv OMKIahgtJ9UZ4MH8Vqc0rlgWWJdJXwqeTUuG3NcxIyGLh87+ybJpUGJQo01Dvl926IMx L7uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773873015; x=1774477815; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=/r4lbyJWNRb4OPU+FwoTkjRVnxoVbk6U7PxvuQ43o+8=; b=SeRbRqqU3M8YZ9WwzVUAGz3oqFGUFfWmmAqXvm/4fs571azTQpS8sKQE24VOLxKp+L WBhYqx7mr9/RIqRoqNZSLCr+3H5L736ZtXv/Ljn/TKmzLqwqIep1sUCOGEFZn/Uo6WxP h4DNXNhYC/Mm06RDfuqAJ63VVt338Xno5x91EddGTWHGw5sy5zmRqFYssHhMXtZLs+Nw W9tIBqnTvZ/VgjzyQoQwetamUeGhhvx+nf3f7AQCnLPZy0X5PCtrz4GdpbPwonAB1GWM bb/YoQeyz25Kf0I/V4WkdMkdutcYa45a1stCSMmFba1ezGNICNtHkRypTXvON2f1s5bw 5oDg== X-Forwarded-Encrypted: i=1; AJvYcCWewN8ab5/fzbeVU9uoJJTGqtTyDKc2Bt63XMwBERvp9FDdnu4mbhp0W8bctEHZI6wJTRKp8smCIaVx1Tg=@vger.kernel.org X-Gm-Message-State: AOJu0YyHzEH3+EtFgqbTrIVDov2mH9R7NFA7KkhmCQcc9rit6exSHF2R nEGQ7jbh6bL0AfaMCIntRD5pbk4ZOUPdL3EUrruaAFsfZvh5w6mtMckH X-Gm-Gg: ATEYQzwRiHFhZkKJ6oSNnrAXjWZoWgEGsFW1TwX4/XxuBd1bZN9xsIGWhXWDkGYYsOU DIYMPcCL4p6cdwLHvC86+uMrfgAvm7UtJfmgzagfkXBkkU+YQljeYlNeNsLh2MardRZKzBMdL+Y vU/LKadr/pP3LD2FJl1ykLboKnYL7ECLrpI0ZL4R9De/C1b87CQ3iE7WjkRMjXb+LS2JO8iJUrM GC+/nbc6e5yLrJ5lkZ3hcin9zTFpAFbtcvsE2YCcZaRRmssqllaxWe3h2fKuNey+Tj2EaX6Oj0m fxqPIdbP4/aeaIQJRGLhnNRZI1RTWqT6HgT38+A/Rd81NotqAq+ekTr/WAYDSAD/YoKwN8e8kkZ Xr1Kr7iA8MCWsd6mbIFzyWpekqx0RtqvXH09/SBttHFvOf+KqqtayUZwDZlZoWIIB4gA9ujVgzF wmNOmUmDv3r8Pez4qGC+yHbGgjwi3e8rOUg4Jcb7W50Gv2Sg== X-Received: by 2002:a05:6808:16a7:b0:467:db5:8b21 with SMTP id 5614622812f47-467b9f4cf4bmr3062816b6e.0.1773873014996; Wed, 18 Mar 2026 15:30:14 -0700 (PDT) Received: from localhost ([2a03:2880:10ff:71::]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-41bd2c2d51esm4194088fac.11.2026.03.18.15.30.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Mar 2026 15:30:14 -0700 (PDT) From: Nhat Pham To: kasong@tencent.com Cc: Liam.Howlett@oracle.com, akpm@linux-foundation.org, apopple@nvidia.com, axelrasmussen@google.com, baohua@kernel.org, baolin.wang@linux.alibaba.com, bhe@redhat.com, byungchul@sk.com, cgroups@vger.kernel.org, chengming.zhou@linux.dev, chrisl@kernel.org, corbet@lwn.net, david@kernel.org, dev.jain@arm.com, gourry@gourry.net, hannes@cmpxchg.org, hughd@google.com, jannh@google.com, joshua.hahnjy@gmail.com, lance.yang@linux.dev, lenb@kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-pm@vger.kernel.org, lorenzo.stoakes@oracle.com, matthew.brost@intel.com, mhocko@suse.com, muchun.song@linux.dev, npache@redhat.com, nphamcs@gmail.com, pavel@kernel.org, peterx@redhat.com, peterz@infradead.org, pfalcato@suse.de, rafael@kernel.org, rakie.kim@sk.com, roman.gushchin@linux.dev, rppt@kernel.org, ryan.roberts@arm.com, shakeel.butt@linux.dev, shikemeng@huaweicloud.com, surenb@google.com, tglx@kernel.org, vbabka@suse.cz, weixugc@google.com, ying.huang@linux.alibaba.com, yosry.ahmed@linux.dev, yuanchu@google.com, zhengqi.arch@bytedance.com, ziy@nvidia.com, kernel-team@meta.com, riel@surriel.com Subject: [PATCH v4 12/21] swap: implement the swap_cgroup API using virtual swap Date: Wed, 18 Mar 2026 15:29:43 -0700 Message-ID: <20260318222953.441758-13-nphamcs@gmail.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260318222953.441758-1-nphamcs@gmail.com> References: <20260318222953.441758-1-nphamcs@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Once we decouple a swap entry from its backing store via the virtual swap, we can no longer statically allocate an array to store the swap entries' cgroup information. Move it to the swap descriptor. Note that the memory overhead for swap cgroup information is now on demand, i.e dynamically incurred when the virtual swap cluster is allocated. This help reduces the memory overhead in a huge but sparsely used swap space. For instance, a 2 TB swapfile consists of 2147483648 swap slots, each incurring 2 bytes of overhead for swap cgroup, for a total of 1 GB. If we only utilize 10% of the swapfile, we will save 900 MB. Signed-off-by: Nhat Pham --- include/linux/swap_cgroup.h | 13 --- mm/Makefile | 3 - mm/swap_cgroup.c | 174 ------------------------------------ mm/swapfile.c | 7 -- mm/vswap.c | 95 ++++++++++++++++++++ 5 files changed, 95 insertions(+), 197 deletions(-) delete mode 100644 mm/swap_cgroup.c diff --git a/include/linux/swap_cgroup.h b/include/linux/swap_cgroup.h index 91cdf12190a03..a2abb4d6fa085 100644 --- a/include/linux/swap_cgroup.h +++ b/include/linux/swap_cgroup.h @@ -9,8 +9,6 @@ extern void swap_cgroup_record(struct folio *folio, unsigned short id, swp= _entry_t ent); extern unsigned short swap_cgroup_clear(swp_entry_t ent, unsigned int nr_e= nts); extern unsigned short lookup_swap_cgroup_id(swp_entry_t ent); -extern int swap_cgroup_swapon(int type, unsigned long max_pages); -extern void swap_cgroup_swapoff(int type); =20 #else =20 @@ -31,17 +29,6 @@ unsigned short lookup_swap_cgroup_id(swp_entry_t ent) return 0; } =20 -static inline int -swap_cgroup_swapon(int type, unsigned long max_pages) -{ - return 0; -} - -static inline void swap_cgroup_swapoff(int type) -{ - return; -} - #endif =20 #endif /* __LINUX_SWAP_CGROUP_H */ diff --git a/mm/Makefile b/mm/Makefile index 67fa4586e7e18..a7538784191bf 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -103,9 +103,6 @@ obj-$(CONFIG_PAGE_COUNTER) +=3D page_counter.o obj-$(CONFIG_LIVEUPDATE) +=3D memfd_luo.o obj-$(CONFIG_MEMCG_V1) +=3D memcontrol-v1.o obj-$(CONFIG_MEMCG) +=3D memcontrol.o vmpressure.o -ifdef CONFIG_SWAP -obj-$(CONFIG_MEMCG) +=3D swap_cgroup.o -endif obj-$(CONFIG_CGROUP_HUGETLB) +=3D hugetlb_cgroup.o obj-$(CONFIG_GUP_TEST) +=3D gup_test.o obj-$(CONFIG_DMAPOOL_TEST) +=3D dmapool_test.o diff --git a/mm/swap_cgroup.c b/mm/swap_cgroup.c deleted file mode 100644 index 77ce1d66c318d..0000000000000 --- a/mm/swap_cgroup.c +++ /dev/null @@ -1,174 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -#include -#include -#include - -#include /* depends on mm.h include */ - -static DEFINE_MUTEX(swap_cgroup_mutex); - -/* Pack two cgroup id (short) of two entries in one swap_cgroup (atomic_t)= */ -#define ID_PER_SC (sizeof(struct swap_cgroup) / sizeof(unsigned short)) -#define ID_SHIFT (BITS_PER_TYPE(unsigned short)) -#define ID_MASK (BIT(ID_SHIFT) - 1) -struct swap_cgroup { - atomic_t ids; -}; - -struct swap_cgroup_ctrl { - struct swap_cgroup *map; -}; - -static struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES]; - -static unsigned short __swap_cgroup_id_lookup(struct swap_cgroup *map, - pgoff_t offset) -{ - unsigned int shift =3D (offset % ID_PER_SC) * ID_SHIFT; - unsigned int old_ids =3D atomic_read(&map[offset / ID_PER_SC].ids); - - BUILD_BUG_ON(!is_power_of_2(ID_PER_SC)); - BUILD_BUG_ON(sizeof(struct swap_cgroup) !=3D sizeof(atomic_t)); - - return (old_ids >> shift) & ID_MASK; -} - -static unsigned short __swap_cgroup_id_xchg(struct swap_cgroup *map, - pgoff_t offset, - unsigned short new_id) -{ - unsigned short old_id; - struct swap_cgroup *sc =3D &map[offset / ID_PER_SC]; - unsigned int shift =3D (offset % ID_PER_SC) * ID_SHIFT; - unsigned int new_ids, old_ids =3D atomic_read(&sc->ids); - - do { - old_id =3D (old_ids >> shift) & ID_MASK; - new_ids =3D (old_ids & ~(ID_MASK << shift)); - new_ids |=3D ((unsigned int)new_id) << shift; - } while (!atomic_try_cmpxchg(&sc->ids, &old_ids, new_ids)); - - return old_id; -} - -/** - * swap_cgroup_record - record mem_cgroup for a set of swap entries. - * These entries must belong to one single folio, and that folio - * must be being charged for swap space (swap out), and these - * entries must not have been charged - * - * @folio: the folio that the swap entry belongs to - * @id: mem_cgroup ID to be recorded - * @ent: the first swap entry to be recorded - */ -void swap_cgroup_record(struct folio *folio, unsigned short id, - swp_entry_t ent) -{ - unsigned int nr_ents =3D folio_nr_pages(folio); - swp_slot_t slot =3D swp_entry_to_swp_slot(ent); - struct swap_cgroup *map; - pgoff_t offset, end; - unsigned short old; - - offset =3D swp_slot_offset(slot); - end =3D offset + nr_ents; - map =3D swap_cgroup_ctrl[swp_slot_type(slot)].map; - - do { - old =3D __swap_cgroup_id_xchg(map, offset, id); - VM_BUG_ON(old); - } while (++offset !=3D end); -} - -/** - * swap_cgroup_clear - clear mem_cgroup for a set of swap entries. - * These entries must be being uncharged from swap. They either - * belongs to one single folio in the swap cache (swap in for - * cgroup v1), or no longer have any users (slot freeing). - * - * @ent: the first swap entry to be recorded into - * @nr_ents: number of swap entries to be recorded - * - * Returns the existing old value. - */ -unsigned short swap_cgroup_clear(swp_entry_t ent, unsigned int nr_ents) -{ - swp_slot_t slot =3D swp_entry_to_swp_slot(ent); - pgoff_t offset =3D swp_slot_offset(slot); - pgoff_t end =3D offset + nr_ents; - struct swap_cgroup *map; - unsigned short old, iter =3D 0; - - map =3D swap_cgroup_ctrl[swp_slot_type(slot)].map; - - do { - old =3D __swap_cgroup_id_xchg(map, offset, 0); - if (!iter) - iter =3D old; - VM_BUG_ON(iter !=3D old); - } while (++offset !=3D end); - - return old; -} - -/** - * lookup_swap_cgroup_id - lookup mem_cgroup id tied to swap entry - * @ent: swap entry to be looked up. - * - * Returns ID of mem_cgroup at success. 0 at failure. (0 is invalid ID) - */ -unsigned short lookup_swap_cgroup_id(swp_entry_t ent) -{ - struct swap_cgroup_ctrl *ctrl; - swp_slot_t slot =3D swp_entry_to_swp_slot(ent); - - if (mem_cgroup_disabled()) - return 0; - - ctrl =3D &swap_cgroup_ctrl[swp_slot_type(slot)]; - return __swap_cgroup_id_lookup(ctrl->map, swp_slot_offset(slot)); -} - -int swap_cgroup_swapon(int type, unsigned long max_pages) -{ - struct swap_cgroup *map; - struct swap_cgroup_ctrl *ctrl; - - if (mem_cgroup_disabled()) - return 0; - - BUILD_BUG_ON(sizeof(unsigned short) * ID_PER_SC !=3D - sizeof(struct swap_cgroup)); - map =3D vzalloc(DIV_ROUND_UP(max_pages, ID_PER_SC) * - sizeof(struct swap_cgroup)); - if (!map) - goto nomem; - - ctrl =3D &swap_cgroup_ctrl[type]; - mutex_lock(&swap_cgroup_mutex); - ctrl->map =3D map; - mutex_unlock(&swap_cgroup_mutex); - - return 0; -nomem: - pr_info("couldn't allocate enough memory for swap_cgroup\n"); - pr_info("swap_cgroup can be disabled by swapaccount=3D0 boot option\n"); - return -ENOMEM; -} - -void swap_cgroup_swapoff(int type) -{ - struct swap_cgroup *map; - struct swap_cgroup_ctrl *ctrl; - - if (mem_cgroup_disabled()) - return; - - mutex_lock(&swap_cgroup_mutex); - ctrl =3D &swap_cgroup_ctrl[type]; - map =3D ctrl->map; - ctrl->map =3D NULL; - mutex_unlock(&swap_cgroup_mutex); - - vfree(map); -} diff --git a/mm/swapfile.c b/mm/swapfile.c index a47e024f2152c..adfcce286258f 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -2934,8 +2934,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, special= file) vfree(swap_map); kvfree(zeromap); free_cluster_info(cluster_info, maxpages); - /* Destroy swap account information */ - swap_cgroup_swapoff(p->type); =20 inode =3D mapping->host; =20 @@ -3500,10 +3498,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, special= file, int, swap_flags) goto bad_swap_unlock_inode; } =20 - error =3D swap_cgroup_swapon(si->type, maxpages); - if (error) - goto bad_swap_unlock_inode; - error =3D setup_swap_map(si, swap_header, swap_map, maxpages); if (error) goto bad_swap_unlock_inode; @@ -3608,7 +3602,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialf= ile, int, swap_flags) si->global_cluster =3D NULL; inode =3D NULL; destroy_swap_extents(si); - swap_cgroup_swapoff(si->type); spin_lock(&swap_lock); si->swap_file =3D NULL; si->flags =3D 0; diff --git a/mm/vswap.c b/mm/vswap.c index f2ebd79854572..95d683cf4505e 100644 --- a/mm/vswap.c +++ b/mm/vswap.c @@ -41,6 +41,7 @@ * @zswap_entry: The zswap entry associated with this swap slot. * @swap_cache: The folio in swap cache. * @shadow: The shadow entry. + * @memcgid: The memcg id of the owning memcg, if any. */ struct swp_desc { swp_slot_t slot; @@ -49,6 +50,9 @@ struct swp_desc { struct folio *swap_cache; void *shadow; }; +#ifdef CONFIG_MEMCG + unsigned short memcgid; +#endif }; =20 #define VSWAP_CLUSTER_SHIFT HPAGE_PMD_ORDER @@ -242,6 +246,9 @@ static void __vswap_alloc_from_cluster(struct vswap_clu= ster *cluster, int start) desc =3D &cluster->descriptors[start + i]; desc->slot.val =3D 0; desc->zswap_entry =3D NULL; +#ifdef CONFIG_MEMCG + desc->memcgid =3D 0; +#endif } cluster->count +=3D nr; } @@ -1109,6 +1116,94 @@ bool zswap_empty(swp_entry_t swpentry) } #endif /* CONFIG_ZSWAP */ =20 +#ifdef CONFIG_MEMCG +static unsigned short vswap_cgroup_record(swp_entry_t entry, + unsigned short memcgid, unsigned int nr_ents) +{ + struct vswap_cluster *cluster =3D NULL; + struct swp_desc *desc; + unsigned short oldid, iter =3D 0; + int i; + + rcu_read_lock(); + for (i =3D 0; i < nr_ents; i++) { + desc =3D vswap_iter(&cluster, entry.val + i); + VM_WARN_ON(!desc); + oldid =3D desc->memcgid; + desc->memcgid =3D memcgid; + if (!iter) + iter =3D oldid; + VM_WARN_ON(iter !=3D oldid); + } + spin_unlock(&cluster->lock); + rcu_read_unlock(); + + return oldid; +} + +/** + * swap_cgroup_record - record mem_cgroup for a set of swap entries. + * These entries must belong to one single folio, and that folio + * must be being charged for swap space (swap out), and these + * entries must not have been charged + * + * @folio: the folio that the swap entry belongs to + * @memcgid: mem_cgroup ID to be recorded + * @entry: the first swap entry to be recorded + */ +void swap_cgroup_record(struct folio *folio, unsigned short memcgid, + swp_entry_t entry) +{ + unsigned short oldid =3D + vswap_cgroup_record(entry, memcgid, folio_nr_pages(folio)); + + VM_WARN_ON(oldid); +} + +/** + * swap_cgroup_clear - clear mem_cgroup for a set of swap entries. + * These entries must be being uncharged from swap. They either + * belongs to one single folio in the swap cache (swap in for + * cgroup v1), or no longer have any users (slot freeing). + * + * @entry: the first swap entry to be recorded into + * @nr_ents: number of swap entries to be recorded + * + * Returns the existing old value. + */ +unsigned short swap_cgroup_clear(swp_entry_t entry, unsigned int nr_ents) +{ + return vswap_cgroup_record(entry, 0, nr_ents); +} + +/** + * lookup_swap_cgroup_id - lookup mem_cgroup id tied to swap entry + * @entry: swap entry to be looked up. + * + * Returns ID of mem_cgroup at success. 0 at failure. (0 is invalid ID) + */ +unsigned short lookup_swap_cgroup_id(swp_entry_t entry) +{ + struct vswap_cluster *cluster =3D NULL; + struct swp_desc *desc; + unsigned short ret; + + /* + * Note that the virtual swap slot can be freed under us, for instance in + * the invocation of mem_cgroup_swapin_charge_folio. We need to wrap the + * entire lookup in RCU read-side critical section, and double check the + * existence of the swap descriptor. + */ + rcu_read_lock(); + desc =3D vswap_iter(&cluster, entry.val); + ret =3D desc ? desc->memcgid : 0; + if (cluster) + spin_unlock(&cluster->lock); + rcu_read_unlock(); + return ret; +} +#endif /* CONFIG_MEMCG */ + int vswap_init(void) { int i; --=20 2.52.0