From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.50 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796103455; b=4M/dxZaStwAcfUEHW9nbhNsZ0AcosZH7V0v9SxazxUd2R+C8JI5dFvk1f72p04XUnnev qVoSP4IcAzxre51IxGthbZV8OI9phSM8k5uHhMNVMeUyC1gjM4OPrbpPrsqHbaNvZDg43 NgNlcjqdPfHPK9ZsYK8aJv6CPgCK3gDsg7S1DC0GyJZJDxeEsHOH2gV/en6Ie7A/kqoZD wvm0EbWIBixs9RsCpntk9oh48ZnMv1yHubBMgvbZSKeeKlNWP2UhBhf2C3TFRS/TQe7H4 UZYj4qGbqWEUzo1PBpfo5jc1saB+ilpZvA2JwsfIbv6+KDzUGLQQrnQl0EwbKOuUHtQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796103455; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-Id:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=K8wwDzsy4+DZZM2MO3YAVzE8XPV9XgrQlTiNaPItwsg=; b=BHm/Vlw2duw8C05XejzUU5B+cR0K3EULEh2FJCxCnVxo97VbzDPUHOfEPLRA6Z4/pj1d myHNmuzlRp/E0jFfu5PDKJpLYCudXFcXSXECJjLNIVy7djAgkaJ5mSCqPBbTTIYBnMF4E W6OjXg7uBNhexfwaWFUHkj99iWJ7WejJOO+LxQi2Zhr0B72JZ8Ts0/DtxU4bDbypTsOtZ D9zEboj6k3RjTGxntkr7Tl3R/KVlmaJzdwdKSPXey7e0J7nO3ztdhuYPMNKvE/b4raTTl kzySO6t6i0jtnb/OBcZJpIM4ECOpAIca4A4/bHAL6mywbV7Llu8Tngk0QLBpG9/5AUg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=MntwY5gD; arc=none smtp.remote-ip=74.125.82.50 Received: from mail-dl1-f50.google.com (mail-dl1-f50.google.com [74.125.82.50]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CA7C8606A688 for ; Tue, 1 Dec 2026 05:37:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org CA7C8606A688 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.a=rsa-sha256 header.s=20260707 header.b=MntwY5gD Received: by mail-dl1-f50.google.com with SMTP id a92af1059eb24-15dd124bbe7so19211428c88.1 for ; Mon, 30 Nov 2026 21:37:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796103454; x=1796708254; 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:content-type; bh=K8wwDzsy4+DZZM2MO3YAVzE8XPV9XgrQlTiNaPItwsg=; b=MntwY5gD203fYzdnOvg18sC4utaiqOoW2Kk0AM9CPwW9srKocQxxMO71sxKC7h0Uni vKLVCYyb4Y3Io0h5yD5G6jZMOHU4qgE+6IJAbxRNYSxMDjZbznjP+Qq07Srn9DdlCq5X e+CL5JMY5uy0+fUInrSozZeb+dom18g3YIlQIAyG4O8bQkLiAwiafnSyuICin8x4BwSl AuLOXgD1M/kU8hv8IlfIu1UqDLr5sPsIszyxs6Da5e7PLxkz5PmEwU7jbMOV6Ctf74GE VP7gVbwZp93CGJOqqsnrPsyiLoxJbcKqh2BoPg08yT2DqZ8zTvvnQz+N1Lh/jWQO+KVY CqTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796103454; x=1796708254; 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:content-type; bh=K8wwDzsy4+DZZM2MO3YAVzE8XPV9XgrQlTiNaPItwsg=; b=2GdfWDlWdNXejgsDPVEnBJgA3o8EeIS+Xi+FEEldAwmFVB2JclfFuabVUSQtyF7Iv/ sPl37n02wGY7PMNyQ+mDX47n4+cSlp0piS8eTKIgnDK70k5qI+/3hYgQdhewV6FJ+L1f gg+Ry3HaPGpCsYX3pMf8dl6JMwDJv+sIwGAMgmspAmKFc1yG/2jJTpbOVTUi6a2GHToA LKRNfxSHG2NlSc+H8Z1r2TbINJ1Es7LPYZyKoopl4zO0ZF2LLEZbt9I3qjUKBPtBNTKu 4xvy1y/3A3odLrvW6TS0KXOedbq0ixJqbGjpAW/LBAT0yx2pZu3AIKT1NELUwvikZQ1T XvRg== X-Forwarded-Encrypted: i=1; AL+hErGDy5JMlSZydFAYel25k7EuGr2geA3zf/wxogdOdIxuDPfaGMNj2d/z2isHNymU02IlotiqjZ3wQDzWWlo=@vger.kernel.org X-Gm-Message-State: APuU4Z+YyB2yXXN7cAiwGPU13ox+5vC5lZkRpwvuuLjydDogKu4sAfOB UuOARUYK+eHTuJ0qnN01h44zXrn07kurwztrzyiUQpPVJMJWzXqEdPze X-Gm-Gg: AZFdTZcmGxtH8pXP1Jaki6yxXRe/K7ePdYTPugBNrBt5NHs+ujToEipXhdVl1PUv/6T UmSf/YMXs5qmIRkv/sAuOjYzap0HVOZcJkfUXUci+cw1Typvq/fyXA2+S29aJ2kkHCrDzhNCaRh qK4SFihKf1CY2amCLIX6kFodwROaZ+8+Yd4cP4bar48S1ST2LPx1DT8XusJquNSZYZM+jxUfevK qZJmlJW9zuXC/RL8GZHox/gSsTGJajInUj7VSd+9Sqmvbpgy3daCWfjgD2B6bpmyqYdUinV1Ut5 0cNtNL/9ADbAmIpciLGxo5b0Gu9y6CqqGiAVqfNdN5bgvfFXTGZrvh+CtHhIAvoT0oj8pU08UPP an8EonMLe7N/A4L3YI078BjsthfKCM8eEQqrWtsMIJq7SZ6en4IFwqq3x3BSpP92IcSzFAw8pyY n7LsyoTBZyh58msAljGoYvg6g/K1ucQd/VT6igykhUksCYB8oT3W0Zwnil7No6OJwcDwbI0mRne v3xq1fqZIwC5uqBqFetnBzGyoTaOFmArkSTWNaPYMV8QP2Vnq7OSI3OrwWDK/ugqYeUZW37QlWS XczgQI8vdQOf0EHyS+sVEALzHQs85DFCRM77UX9Tog== X-Received: by 2002:a05:7022:ed02:b0:167:d73a:e994 with SMTP id a92af1059eb24-1b5568f335cmr33129547c88.24.1796103453834; Mon, 30 Nov 2026 21:37:33 -0800 (PST) Received: from penguin.lxd ([12.187.136.98]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1b5566c3052sm81183292c88.1.2026.11.30.21.37.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 21:37:33 -0800 (PST) From: "Kanchana P. Sridhar" To: hannes@cmpxchg.org, yosry@kernel.org, nphamcs@gmail.com, chengming.zhou@linux.dev, akpm@linux-foundation.org, linux-mm@kvack.org, herbert@gondor.apana.org.au, davem@davemloft.net, linux-crypto@vger.kernel.org, kanchanapsridhar2026@gmail.com, linux-kernel@vger.kernel.org Cc: rientjes@google.com, willy@infradead.org, senozhatsky@chromium.org Subject: [PATCH v5 8/9] mm: zswap: Refactor and optimize zswap_store_pages() batch operations. Date: Mon, 30 Nov 2026 21:37:10 -0800 Message-Id: <20261201053711.8090-9-kanchanapsridhar2026@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20261201053711.8090-1-kanchanapsridhar2026@gmail.com> References: <20261201053711.8090-1-kanchanapsridhar2026@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The various stages of zswap_store_pages() are broken down into routines that work on a batch of data when a large folio is being processed: zswap_init_entries() zswap_tree_stores() The struct zswap_store_context is used to pass constraints and data among these batching components. The resulting code is highly simplified and maintainable, where each of the batching sub-routines of zswap_store() can be optimized modularly. For instance, the zswap_tree_stores() now does the zswap atomics counter increments, getting pool references, getting objcg references and objcg zswap charging at the batch level. A new obj_cgroup_charge_zswap_many() is added to enable batch charging of compressed bytes for multiple pages/incompressible pages in the batch, in order to accurately update the MEMCG_ZSWAP* counters. The next commit will implement batching of zswap_compress(). Subsequent RFC patch series will be submitted, that implement ideas building upon this batching framework: 1) Replacing zsmalloc with SLUB allocator, comprising of kmem_caches for a finite # of compressed size bins. This enables bulk allocation of compressed buffers in a batch that belong to the same size bin. It gets rid of significant zsmalloc latency overhead (locks, compressed data spanning zspages, etc.), without compromising memory efficiency. 2) vmscan reclaim batching of hybrid any-order folios, that will submit a batch of folios to zswap_store()'s batching framework to optimize storing of batches of pages from different folios, in addition to within large folios. Signed-off-by: Kanchana P. Sridhar --- include/linux/memcontrol.h | 13 ++- mm/memcontrol.c | 16 ++-- mm/zswap.c | 187 +++++++++++++++++++++++++------------ 3 files changed, 146 insertions(+), 70 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 980358bc6f49..1a0425c53d06 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1244,6 +1244,11 @@ struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css) return NULL; } +static inline void obj_cgroup_get_many(struct obj_cgroup *objcg, + unsigned long nr) +{ +} + static inline void obj_cgroup_get(struct obj_cgroup *objcg) { } @@ -1920,7 +1925,8 @@ static inline void mem_cgroup_calculate_protection_path(struct mem_cgroup *root, #if defined(CONFIG_MEMCG) && defined(CONFIG_ZSWAP) bool obj_cgroup_may_zswap(struct obj_cgroup *objcg); -void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size); +void obj_cgroup_charge_zswap_many(struct obj_cgroup *objcg, size_t size, + u8 nr_pages, u8 nr_incomp_pages); void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size); bool mem_cgroup_zswap_writeback_enabled(const struct mem_cgroup *memcg); #else @@ -1928,8 +1934,9 @@ static inline bool obj_cgroup_may_zswap(struct obj_cgroup *objcg) { return true; } -static inline void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, - size_t size) +static inline void obj_cgroup_charge_zswap_many(struct obj_cgroup *objcg, + size_t size, u8 nr_pages, + u8 nr_incomp_pages) { } static inline void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 7e0bc63f0a11..739d47c969f9 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -6348,14 +6348,18 @@ bool obj_cgroup_may_zswap(struct obj_cgroup *objcg) } /** - * obj_cgroup_charge_zswap - charge compression backend memory + * obj_cgroup_charge_zswap_many - bulk charge compression backend memory + * from compressing a batch of pages. * @objcg: the object cgroup - * @size: size of compressed object + * @size: total compressed length of a batch of compressed objects + * @nr_pages: # of pages that were compressed to store in zswap + * @nr_incomp_pages: # of incompressible pages to store in zswap * * This forces the charge after obj_cgroup_may_zswap() allowed * compression and storage in zswap for this cgroup to go ahead. */ -void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size) +void obj_cgroup_charge_zswap_many(struct obj_cgroup *objcg, size_t size, + u8 nr_pages, u8 nr_incomp_pages) { struct mem_cgroup *memcg; @@ -6374,9 +6378,9 @@ void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size) rcu_read_lock(); memcg = obj_cgroup_memcg(objcg); mod_memcg_state(memcg, MEMCG_ZSWAP_B, size); - mod_memcg_state(memcg, MEMCG_ZSWAPPED, 1); - if (size == PAGE_SIZE) - mod_memcg_state(memcg, MEMCG_ZSWAP_INCOMP, 1); + mod_memcg_state(memcg, MEMCG_ZSWAPPED, nr_pages); + if (nr_incomp_pages) + mod_memcg_state(memcg, MEMCG_ZSWAP_INCOMP, nr_incomp_pages); rcu_read_unlock(); } diff --git a/mm/zswap.c b/mm/zswap.c index acfb29f3404b..8a80b6e8e235 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -491,12 +491,12 @@ static int __must_check zswap_pool_tryget(struct zswap_pool *pool) } /* The caller must already have a reference. */ -static void zswap_pool_get(struct zswap_pool *pool) +static inline void zswap_pool_get_many(struct zswap_pool *pool, u8 nr) { - percpu_ref_get(&pool->ref); + percpu_ref_get_many(&pool->ref, nr); } -static void zswap_pool_put(struct zswap_pool *pool) +static inline void zswap_pool_put(struct zswap_pool *pool) { percpu_ref_put(&pool->ref); } @@ -1601,55 +1601,60 @@ static void shrink_worker(struct work_struct *w) * main API **********************************/ +/* Functions called by zswap_store(), that operate on batches of pages. */ + /* - * Store multiple pages in @zc->folio, starting from the page at index @start up - * to the page at index @end-1. + * zswap_init_entries() co-locates most of the zswap entry fields' setup under + * the assumption that all phases of store processing will succeed. An important + * marker to simplify catch-all cleanup in zswap_store_pages() is done by + * initializing each entry's @handle to ERR_PTR(-EINVAL). */ -static bool zswap_store_pages(struct zswap_store_context *zc, - long start, - long end, - struct zswap_pool *pool, - struct obj_cgroup *objcg) +static inline void zswap_init_entries(struct zswap_store_context *zc, + struct zswap_pool *pool, + struct obj_cgroup *objcg) { - u8 i, store_fail_idx = 0; - bool ret; - - zc->nr_pages = end - start; - zc->folio_start = start; - - VM_WARN_ON_ONCE(zc->nr_pages <= 0 || zc->nr_pages > ZSWAP_MAX_BATCH_SIZE); - - ret = zswap_entries_cache_alloc_batch(zc); - if (unlikely(!ret)) - return false; + struct zswap_entry *entry; + u8 i; /* * We co-locate entry initialization as much as possible here to * minimize potential cache misses. */ for (i = 0; i < zc->nr_pages; ++i) { + entry = zc->entries[i]; /* - * Set pool_idx before the xa_store() below publishes the entry, or a - * concurrent reader could resolve a stale pool_idx left by slab reuse - * to an unrelated live pool. + * Set pool_idx before the xa_store() in zswap_tree_stores() + * publishes the entry, or a concurrent reader could resolve a + * stale pool_idx left by slab reuse to an unrelated live pool. */ - zc->entries[i]->pool_idx = pool->idx; - zc->entries[i]->swpentry = folio_swap_entry(zc->folio, - zc->folio_start + i); - zc->entries[i]->objcg = objcg; - zc->entries[i]->referenced = true; - zc->entries[i]->nid = zc->nid; - INIT_LIST_HEAD(&zc->entries[i]->lru); + entry->pool_idx = pool->idx; + entry->swpentry = folio_swap_entry(zc->folio, + zc->folio_start + i); + entry->objcg = objcg; + entry->referenced = true; + entry->nid = zc->nid; + INIT_LIST_HEAD(&entry->lru); } +} - for (i = 0; i < zc->nr_pages; ++i) { - if (!zswap_compress(zc->folio, zc->folio_start + i, - zc->entries[i], pool, zc->wb_enabled)) - goto store_pages_failed; - } +/* + * zswap_tree_stores() stores the batch entries in xarray and adds them to the + * zswap LRU list. If an error occurs during xa_store(), localized state + * unwinding is done to erase prior batch entries from the xarray and delete + * them from the zswap LRU list. Remaining batch cleanup occurs in + * zswap_store_pages(). As batch entries are processed, we maintain aggregate + * statistics to do the accounting in one bulk operation. + */ +static bool zswap_tree_stores(struct zswap_store_context *zc, + struct zswap_pool *pool, + struct obj_cgroup *objcg) +{ + u8 i, store_fail_idx = 0, nr_incomp = 0; + size_t total_batch_comp_length = 0; + struct zswap_entry *old, *entry; for (i = 0; i < zc->nr_pages; ++i) { - struct zswap_entry *old, *entry = zc->entries[i]; + entry = zc->entries[i]; old = xa_store(swap_zswap_tree(entry->swpentry), swp_offset(entry->swpentry), @@ -1661,13 +1666,11 @@ static bool zswap_store_pages(struct zswap_store_context *zc, zswap_reject_alloc_fail++; /* * Entries up to this point have been stored in the - * xarray. zswap_store() will erase them from the xarray - * and call zswap_entry_free(). Local cleanup in - * 'store_pages_failed' only needs to happen for - * entries from [@i to @nr_pages). + * xarray. Local cleanup in 'tree_store_failed' needs to + * erase entries up until now. */ store_fail_idx = i; - goto store_pages_failed; + goto tree_store_failed; } /* @@ -1678,22 +1681,10 @@ static bool zswap_store_pages(struct zswap_store_context *zc, if (unlikely(old)) zswap_entry_free(old); - /* - * The entry is successfully compressed and stored in the tree, - * and further failures will be cleaned up in zswap_store(). - * Grab refs to the pool and objcg, charge zswap memory, and - * increment zswap_stored_pages. The opposite actions will be - * performed by zswap_entry_free() when the entry is removed - * from the tree. - */ - zswap_pool_get(pool); - if (objcg) { - obj_cgroup_get(objcg); - obj_cgroup_charge_zswap(objcg, entry->length); - } - atomic_long_inc(&zswap_stored_pages); + total_batch_comp_length += entry->length; + if (unlikely(entry->length == PAGE_SIZE)) - atomic_long_inc(&zswap_stored_incompressible_pages); + ++nr_incomp; /* * We finish by adding the entry to the LRU while it's already @@ -1708,17 +1699,91 @@ static bool zswap_store_pages(struct zswap_store_context *zc, zswap_lru_add(entry); } + /* + * Each batch entry is successfully compressed and stored in the tree, + * and further failures will be cleaned up in zswap_store(). + * Next, we do bulk accounting for the batch: grab refs to the pool and + * objcg, charge zswap memory, increment zswap_stored_pages and + * zswap_stored_incompressible_pages. The opposite actions will be + * performed by zswap_entry_free() when the entry is removed from the + * tree. + */ + zswap_pool_get_many(pool, zc->nr_pages); + if (objcg) { + obj_cgroup_get_many(objcg, zc->nr_pages); + obj_cgroup_charge_zswap_many(objcg, total_batch_comp_length, + zc->nr_pages, nr_incomp); + } + atomic_long_add(zc->nr_pages, &zswap_stored_pages); + if (unlikely(nr_incomp)) + atomic_long_add(nr_incomp, &zswap_stored_incompressible_pages); + + return true; + +tree_store_failed: + /* + * We only cleanup the current batch here. The cleanup of other + * batches in the folio is deferred to zswap_store(). + */ + for (i = 0; i < store_fail_idx; ++i) { + xa_erase(swap_zswap_tree(zc->entries[i]->swpentry), + swp_offset(zc->entries[i]->swpentry)); + zswap_lru_del(zc->entries[i]); + } + + return false; +} + +/* + * Store multiple pages in @zc->folio, starting from the page at index @start up + * to the page at index @end-1, i.e. @zc->nr_pages. Each phase of + * zswap_store_pages() does batch processing of @zc->nr_pages in the folio. + * State unwinding is kept to a minimum as a result of the batch processing: a + * batch proceeds to the next phase of store processing only if it passes this + * stage. The batch cleanup is consolidated under the @store_pages_failed + * label. Errors in storing a batch result in folio level cleanup occurring in + * zswap_store(). + */ +static bool zswap_store_pages(struct zswap_store_context *zc, + long start, + long end, + struct zswap_pool *pool, + struct obj_cgroup *objcg) +{ + bool ret; + u8 i; + + zc->nr_pages = end - start; + zc->folio_start = start; + + VM_WARN_ON_ONCE(zc->nr_pages <= 0 || zc->nr_pages > ZSWAP_MAX_BATCH_SIZE); + + ret = zswap_entries_cache_alloc_batch(zc); + if (unlikely(!ret)) + return false; + + zswap_init_entries(zc, pool, objcg); + + for (i = 0; i < zc->nr_pages; ++i) { + if (!zswap_compress(zc->folio, zc->folio_start + i, + zc->entries[i], pool, zc->wb_enabled)) + goto store_pages_failed; + } + + ret = zswap_tree_stores(zc, pool, objcg); + + if (unlikely(!ret)) + goto store_pages_failed; + return true; store_pages_failed: - for (i = store_fail_idx; i < zc->nr_pages; ++i) { + for (i = 0; i < zc->nr_pages; ++i) { if (zs_handle_is_allocated(&zc->entries[i]->handle)) zs_free(pool->zs_pool, &zc->entries[i]->handle); } - zswap_entries_cache_free_batch(&zc->entries[store_fail_idx], - zc->nr_pages - store_fail_idx); - + zswap_entries_cache_free_batch(zc->entries, zc->nr_pages); return false; } -- 2.39.5