From patchwork Mon Dec 18 08:22:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengming Zhou X-Patchwork-Id: 13496355 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 09783C35274 for ; Mon, 18 Dec 2023 08:22:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 931088D000B; Mon, 18 Dec 2023 03:22:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8B95A8D0001; Mon, 18 Dec 2023 03:22:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 732898D000B; Mon, 18 Dec 2023 03:22:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 4F54D8D0001 for ; Mon, 18 Dec 2023 03:22:48 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 21F9A14016F for ; Mon, 18 Dec 2023 08:22:48 +0000 (UTC) X-FDA: 81579248016.20.A838C55 Received: from out-171.mta1.migadu.com (out-171.mta1.migadu.com [95.215.58.171]) by imf28.hostedemail.com (Postfix) with ESMTP id 157A5C001A for ; Mon, 18 Dec 2023 08:22:45 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=bytedance.com (policy=quarantine); spf=pass (imf28.hostedemail.com: domain of chengming.zhou@linux.dev designates 95.215.58.171 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702887766; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=581OMV4XATgN37VOge48RZVp6yt/xtrZwVFcAWt1qoE=; b=3ZCD5aX+y01RITdhzaTM5WENXhBini8AQxIj/gNi6tRY1NxpDByvZV8kBpgt2H91tyXgOc S97C02Wk0ZC7kTM11YmzvH9qTnBDcw5RKxsQFYaAsbeK43hyO5okdMV2Z63u1ZPKc/2DCI oSZ9Pzyax6aY94MZ8ZB8aFS6PPzfIqU= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=bytedance.com (policy=quarantine); spf=pass (imf28.hostedemail.com: domain of chengming.zhou@linux.dev designates 95.215.58.171 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702887766; a=rsa-sha256; cv=none; b=Tl4cK+o2F40w3Q5sxuxLviuF5mNcMIpVT4BIp4TIqtmvZOKpzPxPpiPJPl3Kb/hOcvg0XB ZjxyZhJGvphYbnXh/AjUIXenxCm5biALuynCvGtFxj8nj4yQgvUIqGVjB0UP2IRgEYeY+a 1VIMPUMI3TBTqwUAFz4dC2Xy1hteXng= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Chengming Zhou Date: Mon, 18 Dec 2023 08:22:05 +0000 Subject: [PATCH v2 6/6] mm/zswap: directly use percpu mutex and buffer in load/store MIME-Version: 1.0 Message-Id: <20231213-zswap-dstmem-v2-6-daa5d9ae41a7@bytedance.com> References: <20231213-zswap-dstmem-v2-0-daa5d9ae41a7@bytedance.com> In-Reply-To: <20231213-zswap-dstmem-v2-0-daa5d9ae41a7@bytedance.com> To: Seth Jennings , Dan Streetman , Chris Li , Nhat Pham , Vitaly Wool , Yosry Ahmed , Andrew Morton , Johannes Weiner Cc: linux-kernel@vger.kernel.org, Nhat Pham , linux-mm@kvack.org, Yosry Ahmed , Chengming Zhou , Chris Li X-Developer-Signature: v=1; a=ed25519-sha256; t=1702887745; l=10608; i=zhouchengming@bytedance.com; s=20231204; h=from:subject:message-id; bh=k6WkjFerLmMPPnB4BHXU0oLODY4+tgFUEVqHpaqy/GE=; b=L3QlA6B3Z8gMjlH/tKC9DFpE0gONp9++QxZWqj4jttxtA28i2k06FoJnhzPXl2dTnUCRe0O2I /yfAOqL4yTTBVAWzMzyY/vp/6g77noItOcvUhDa+h91UaW5hpum/Bxo X-Developer-Key: i=zhouchengming@bytedance.com; a=ed25519; pk=xFTmRtMG3vELGJBUiml7OYNdM393WOMv0iWWeQEVVdA= X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Rspamd-Pre-Result: action=add header; module=dmarc; Action set by DMARC X-Stat-Signature: 4n1rgybxi6ipsfahuqcfbbba5guz78g6 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 157A5C001A X-Rspam: Yes X-HE-Tag: 1702887765-638061 X-HE-Meta: U2FsdGVkX1+4NoNTxzAcNfTcD1Srt3TLsH6NWJxLUY6CAilHGjDhLYuQMa+6vPlVcw+EJP6o3Hh7T5gMfSHQAuGKci18pk/BM1+jGdbxClLqdCU5c7pZs3t69PMLoHg+yw5LHeFB312rLaEDBdbqi1IGoeIrZKkNzIiF49AD9lvPD7oYlOMMUgSACFudsahGS0ziRZt45zw57oeROMAeEpnsPg40LfwTiguCbSyNRWPexTqTksrfECqCjtIVRH1fFgItOwf2t0Ac1YCkizRvlp2R8f1Sucx95B/D9XdIiyzdBlaJwxqCD0L8Y2DTUEeiVaA1FkPJpod68xFTMKcrc7Xk945Y/kgk0xktPS8g/g4PeBT0lS4jTLuq9ObYsq2u5Y/6HubeIO3DEPol2AypxPeZiRmT94KqLRcM/ve27cnsHTR9xUTDRUEOKn8ZwI2/9rgtsP2L7AU9Lf1VNNejtv/6WDTjcVVdJQ29nPQvLwZOsSpj5kHs+N8UNeeCkyigwEm6/ZTKTwiaNz7ESgkGp3XO/PYWDlldT3WfW4o44hozNS/xcx+C/n1PrvKY70L1PYuvTAL5PMX33/ByHly40ixO3mNqbrSkrvT8Oc5vFQgdUZGLDqELYU3LiwlbN2o74kuBkBKAOBjtQSiMwEfF7jh6/kNh2wfYZkc993veuxnuYw+3XEbgEUI5XK26jq4nM5BC4q/9TfKUdtMRr/BmnJmZmUFhWb4VzAGuJHR9w3lcubbHOsj2V972UlOviZU2/1VPH0axHcw0pNgEjn0bo5lfHu65bDirmk2q6xiFCq0eaRlnWJHCG5cEyiAStECo7nMfYTDr/6DDBG/Eym0QcEraUeqzrrMPbG0j8qRXc+9TLo0x/o/EpL8AkIaun1vI109BynhZmBlpAFwq20Z5s0ijJGx45XKJvrnvZ8KTRDfbuYJYTZoMS86q0pRxF4U4rWeCH03ZO+BnfCnLKUT fWrLXxhF Adkomrpj4JpVXHYuofwM1bdN2Non5TQqH+a6agRDIqXYHk0Lc6kICMh90HFwyAdp0FE+V8qaPXCUCMBqsLMLzDwL9BmE23OzM5o5hSb3eI+XfflSUb7HRIlxTdI2fe4dJKLh5Ps2xGk8hqMLEGhAF/a1LCLcmqzR25krASyV/MJN+/f6ccIlnggs5H79a8WjZHEREN40pNTsKGVRDRbCipvRO4ZJwmVqw2jVC0amB7wuD2F2z6/uYgBtsGpQaDoMpmbBBNY7882gRUkaEKfMwT0wAjuv7JGGh6l8Rn+iXouhZan+SPugjjqqbamigODl/M1xlrM0eYvAP4djF4ofznwsdf+fmmcEz5yI1+9kc6iFdqROseyj+U3chnZmiS4yrGzWi X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Since the introduce of reusing the dstmem in the load path, it seems confusing that we are now using acomp_ctx->dstmem and acomp_ctx->mutex now for purposes other than what the naming suggests. Yosry suggested removing these two fields from acomp_ctx, and directly using zswap_dstmem and zswap_mutex in both the load and store paths, rename them, and add proper comments above their definitions that they are for generic percpu buffering on the load and store paths. So this patch remove dstmem and mutex from acomp_ctx, and rename the zswap_dstmem to zswap_buffer, using the percpu mutex and buffer on the load and store paths. And refactor out __zswap_store() to only include the compress & store, since I found zswap_store() is too long. Suggested-by: Yosry Ahmed Signed-off-by: Chengming Zhou --- mm/zswap.c | 193 +++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 104 insertions(+), 89 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 2c349fd88904..b7449294ec3a 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -166,8 +166,6 @@ struct crypto_acomp_ctx { struct crypto_acomp *acomp; struct acomp_req *req; struct crypto_wait wait; - u8 *dstmem; - struct mutex *mutex; }; /* @@ -694,7 +692,7 @@ static void zswap_alloc_shrinker(struct zswap_pool *pool) /********************************* * per-cpu code **********************************/ -static DEFINE_PER_CPU(u8 *, zswap_dstmem); +static DEFINE_PER_CPU(u8 *, zswap_buffer); /* * If users dynamically change the zpool type and compressor at runtime, i.e. * zswap is running, zswap can have more than one zpool on one cpu, but they @@ -702,39 +700,39 @@ static DEFINE_PER_CPU(u8 *, zswap_dstmem); */ static DEFINE_PER_CPU(struct mutex *, zswap_mutex); -static int zswap_dstmem_prepare(unsigned int cpu) +static int zswap_buffer_prepare(unsigned int cpu) { struct mutex *mutex; - u8 *dst; + u8 *buf; - dst = kmalloc_node(PAGE_SIZE, GFP_KERNEL, cpu_to_node(cpu)); - if (!dst) + buf = kmalloc_node(PAGE_SIZE, GFP_KERNEL, cpu_to_node(cpu)); + if (!buf) return -ENOMEM; mutex = kmalloc_node(sizeof(*mutex), GFP_KERNEL, cpu_to_node(cpu)); if (!mutex) { - kfree(dst); + kfree(buf); return -ENOMEM; } mutex_init(mutex); - per_cpu(zswap_dstmem, cpu) = dst; + per_cpu(zswap_buffer, cpu) = buf; per_cpu(zswap_mutex, cpu) = mutex; return 0; } -static int zswap_dstmem_dead(unsigned int cpu) +static int zswap_buffer_dead(unsigned int cpu) { struct mutex *mutex; - u8 *dst; + u8 *buf; mutex = per_cpu(zswap_mutex, cpu); kfree(mutex); per_cpu(zswap_mutex, cpu) = NULL; - dst = per_cpu(zswap_dstmem, cpu); - kfree(dst); - per_cpu(zswap_dstmem, cpu) = NULL; + buf = per_cpu(zswap_buffer, cpu); + kfree(buf); + per_cpu(zswap_buffer, cpu) = NULL; return 0; } @@ -772,9 +770,6 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node) acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, crypto_req_done, &acomp_ctx->wait); - acomp_ctx->mutex = per_cpu(zswap_mutex, cpu); - acomp_ctx->dstmem = per_cpu(zswap_dstmem, cpu); - return 0; } @@ -1392,20 +1387,98 @@ static int zswap_enabled_param_set(const char *val, return ret; } +static int __zswap_store(struct zswap_entry *entry, struct page *page) +{ + struct scatterlist input, output; + struct crypto_acomp_ctx *acomp_ctx; + struct zpool *zpool; + unsigned long handle; + unsigned int dlen; + u8 *buf, *dst; + gfp_t gfp; + int ret; + int cpu; + struct mutex *mutex; + + cpu = raw_smp_processor_id(); + mutex = per_cpu(zswap_mutex, cpu); + mutex_lock(mutex); + + acomp_ctx = per_cpu_ptr(entry->pool->acomp_ctx, cpu); + buf = per_cpu(zswap_buffer, cpu); + + sg_init_table(&input, 1); + sg_set_page(&input, page, PAGE_SIZE, 0); + sg_init_one(&output, buf, PAGE_SIZE); + acomp_request_set_params(acomp_ctx->req, &input, &output, PAGE_SIZE, PAGE_SIZE); + /* + * it maybe looks a little bit silly that we send an asynchronous request, + * then wait for its completion synchronously. This makes the process look + * synchronous in fact. + * Theoretically, acomp supports users send multiple acomp requests in one + * acomp instance, then get those requests done simultaneously. but in this + * case, zswap actually does store and load page by page, there is no + * existing method to send the second page before the first page is done + * in one thread doing zwap. + * but in different threads running on different cpu, we have different + * acomp instance, so multiple threads can do (de)compression in parallel. + */ + ret = crypto_wait_req(crypto_acomp_compress(acomp_ctx->req), &acomp_ctx->wait); + dlen = acomp_ctx->req->dlen; + + if (ret) { + zswap_reject_compress_fail++; + goto unlock; + } + + /* store */ + zpool = zswap_find_zpool(entry); + gfp = __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM; + if (zpool_malloc_support_movable(zpool)) + gfp |= __GFP_HIGHMEM | __GFP_MOVABLE; + ret = zpool_malloc(zpool, dlen, gfp, &handle); + if (ret == -ENOSPC) { + zswap_reject_compress_poor++; + goto unlock; + } + if (ret) { + zswap_reject_alloc_fail++; + goto unlock; + } + dst = zpool_map_handle(zpool, handle, ZPOOL_MM_WO); + memcpy(dst, buf, dlen); + zpool_unmap_handle(zpool, handle); + mutex_unlock(mutex); + + entry->handle = handle; + entry->length = dlen; + return 0; + +unlock: + mutex_unlock(mutex); + return ret; +} + static void __zswap_load(struct zswap_entry *entry, struct page *page) { struct zpool *zpool = zswap_find_zpool(entry); struct scatterlist input, output; struct crypto_acomp_ctx *acomp_ctx; - u8 *src; + u8 *src, *buf; + int cpu; + struct mutex *mutex; + + cpu = raw_smp_processor_id(); + mutex = per_cpu(zswap_mutex, cpu); + mutex_lock(mutex); - acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx); - mutex_lock(acomp_ctx->mutex); + acomp_ctx = per_cpu_ptr(entry->pool->acomp_ctx, cpu); src = zpool_map_handle(zpool, entry->handle, ZPOOL_MM_RO); if (!zpool_can_sleep_mapped(zpool)) { - memcpy(acomp_ctx->dstmem, src, entry->length); - src = acomp_ctx->dstmem; + buf = per_cpu(zswap_buffer, cpu); + memcpy(buf, src, entry->length); + src = buf; zpool_unmap_handle(zpool, entry->handle); } @@ -1415,7 +1488,7 @@ static void __zswap_load(struct zswap_entry *entry, struct page *page) acomp_request_set_params(acomp_ctx->req, &input, &output, entry->length, PAGE_SIZE); BUG_ON(crypto_wait_req(crypto_acomp_decompress(acomp_ctx->req), &acomp_ctx->wait)); BUG_ON(acomp_ctx->req->dlen != PAGE_SIZE); - mutex_unlock(acomp_ctx->mutex); + mutex_unlock(mutex); if (zpool_can_sleep_mapped(zpool)) zpool_unmap_handle(zpool, entry->handle); @@ -1539,18 +1612,11 @@ bool zswap_store(struct folio *folio) struct page *page = &folio->page; struct zswap_tree *tree = zswap_trees[type]; struct zswap_entry *entry, *dupentry; - struct scatterlist input, output; - struct crypto_acomp_ctx *acomp_ctx; struct obj_cgroup *objcg = NULL; struct mem_cgroup *memcg = NULL; struct zswap_pool *pool; - struct zpool *zpool; - unsigned int dlen = PAGE_SIZE; - unsigned long handle, value; - char *buf; - u8 *src, *dst; - gfp_t gfp; - int ret; + u8 *src; + unsigned long value; VM_WARN_ON_ONCE(!folio_test_locked(folio)); VM_WARN_ON_ONCE(!folio_test_swapcache(folio)); @@ -1635,60 +1701,11 @@ bool zswap_store(struct folio *folio) mem_cgroup_put(memcg); } - /* compress */ - acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx); - - mutex_lock(acomp_ctx->mutex); - - dst = acomp_ctx->dstmem; - sg_init_table(&input, 1); - sg_set_page(&input, page, PAGE_SIZE, 0); - - sg_init_one(&output, dst, PAGE_SIZE); - acomp_request_set_params(acomp_ctx->req, &input, &output, PAGE_SIZE, dlen); - /* - * it maybe looks a little bit silly that we send an asynchronous request, - * then wait for its completion synchronously. This makes the process look - * synchronous in fact. - * Theoretically, acomp supports users send multiple acomp requests in one - * acomp instance, then get those requests done simultaneously. but in this - * case, zswap actually does store and load page by page, there is no - * existing method to send the second page before the first page is done - * in one thread doing zwap. - * but in different threads running on different cpu, we have different - * acomp instance, so multiple threads can do (de)compression in parallel. - */ - ret = crypto_wait_req(crypto_acomp_compress(acomp_ctx->req), &acomp_ctx->wait); - dlen = acomp_ctx->req->dlen; - - if (ret) { - zswap_reject_compress_fail++; - goto put_dstmem; - } - - /* store */ - zpool = zswap_find_zpool(entry); - gfp = __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM; - if (zpool_malloc_support_movable(zpool)) - gfp |= __GFP_HIGHMEM | __GFP_MOVABLE; - ret = zpool_malloc(zpool, dlen, gfp, &handle); - if (ret == -ENOSPC) { - zswap_reject_compress_poor++; - goto put_dstmem; - } - if (ret) { - zswap_reject_alloc_fail++; - goto put_dstmem; - } - buf = zpool_map_handle(zpool, handle, ZPOOL_MM_WO); - memcpy(buf, dst, dlen); - zpool_unmap_handle(zpool, handle); - mutex_unlock(acomp_ctx->mutex); + if (__zswap_store(entry, page)) + goto put_pool; /* populate entry */ entry->swpentry = swp_entry(type, offset); - entry->handle = handle; - entry->length = dlen; insert_entry: entry->objcg = objcg; @@ -1725,8 +1742,6 @@ bool zswap_store(struct folio *folio) return true; -put_dstmem: - mutex_unlock(acomp_ctx->mutex); put_pool: zswap_pool_put(entry->pool); freepage: @@ -1902,10 +1917,10 @@ static int zswap_setup(void) } ret = cpuhp_setup_state(CPUHP_MM_ZSWP_MEM_PREPARE, "mm/zswap:prepare", - zswap_dstmem_prepare, zswap_dstmem_dead); + zswap_buffer_prepare, zswap_buffer_dead); if (ret) { - pr_err("dstmem alloc failed\n"); - goto dstmem_fail; + pr_err("buffer alloc failed\n"); + goto buffer_fail; } ret = cpuhp_setup_state_multi(CPUHP_MM_ZSWP_POOL_PREPARE, @@ -1940,7 +1955,7 @@ static int zswap_setup(void) zswap_pool_destroy(pool); hp_fail: cpuhp_remove_state(CPUHP_MM_ZSWP_MEM_PREPARE); -dstmem_fail: +buffer_fail: kmem_cache_destroy(zswap_entry_cache); cache_fail: /* if built-in, we aren't unloaded on failure; don't allow use */