From patchwork Mon Jun 12 09:38:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Domenico Cerasuolo X-Patchwork-Id: 13275952 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 1F4F8C7EE2E for ; Mon, 12 Jun 2023 09:38:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A9FEA8E0002; Mon, 12 Jun 2023 05:38:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A4C9F6B0075; Mon, 12 Jun 2023 05:38:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7B5758E0002; Mon, 12 Jun 2023 05:38:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 6194A6B0074 for ; Mon, 12 Jun 2023 05:38:25 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 2A7361C75A9 for ; Mon, 12 Jun 2023 09:38:25 +0000 (UTC) X-FDA: 80893595370.27.31D4917 Received: from mail-ej1-f53.google.com (mail-ej1-f53.google.com [209.85.218.53]) by imf21.hostedemail.com (Postfix) with ESMTP id 2BC761C000C for ; Mon, 12 Jun 2023 09:38:22 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=KZfdk105; spf=pass (imf21.hostedemail.com: domain of cerasuolodomenico@gmail.com designates 209.85.218.53 as permitted sender) smtp.mailfrom=cerasuolodomenico@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1686562703; a=rsa-sha256; cv=none; b=nLdrTCwaGlW0Msdw1IaX/XMCpwCYyuIXzpRxfSFu/CYQYnlmP54+p+zehQOoXVSsq+GJhI ptGINnkEyVnFUG/NfUj0y5l7xi2qqJvzzs77a4I6G1mjJjrpvo0xrft4WkxNujPxSvaIJl mj84nvItdomUohipFGh4aEPyVZ56bGs= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=KZfdk105; spf=pass (imf21.hostedemail.com: domain of cerasuolodomenico@gmail.com designates 209.85.218.53 as permitted sender) smtp.mailfrom=cerasuolodomenico@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686562703; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ovwqjP88DLJU174wX93bEruUbTnFh4DciYUv65znDHc=; b=UW3t6cay1Uk4MX+HG7pi2f/EJ0x+i9uZZLLyRwqng5jgKec3wkAK9LUNzybH0W2nu++B8a /y+CfhoGuaIYKsRDrS0xv/HHFYEknE/sAFPvFrpxEHoYelooGIQJnPWOENdloqjrfapmbv mZl1ED0uHOxlfqqlknoATf2SDulVpqE= Received: by mail-ej1-f53.google.com with SMTP id a640c23a62f3a-97458c97333so695630366b.2 for ; Mon, 12 Jun 2023 02:38:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686562702; x=1689154702; 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=ovwqjP88DLJU174wX93bEruUbTnFh4DciYUv65znDHc=; b=KZfdk105SVW1ZlUxEwaUHuC2cQAmWKzBmQ2NNpCN4F3/Qm43WAlHWWV3ZtTSbNAihm p/oy3xv28rDYE9OelY/atO/IhnMW0LnAxswzBt3BBluRaJPJhflX1fAuPHKNl4QabPnL /zX/+xDo7pqMY9INVkzGlS3GF/+a6aJ9/dcCU/2uKOsDojvqrzLi9W8lMq8ROjICCmAU IPQPF5kpHn+H1Jvnlh0lJK0QMPzPcLeSllzhYMeaSY5YYMp46GgDI65x5/ra8wpgkTNx J05FuZ2c3f4xqHcZrD/Xq1x9+4K5ss3hewqNuytZuxN8/2L2aXrnj6RQhSVphksTW9xp B1lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686562702; x=1689154702; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ovwqjP88DLJU174wX93bEruUbTnFh4DciYUv65znDHc=; b=idZaPqwN9EKBy9BXe0QOkbCUJPKLDG6A2o3CpPTczLqBQr35q4j3iBLi2lWaUH9hzF i5+gIpU/BDioSdgNNWNKFvcarCaA/RtoQE4AOqcndVnET4cLqUb0l454d9I5Kl/qOshj 1LEHdokrWuN4g4M2QC0wfxzWpcA/AcfP/XQRLHDLgAhJ+J1KsXj3RZLNIKQmIi99Fxjz 9eJVqefFi4+I3JSY+GTsYl0Ktd1KDPQdp361EiCmf+fxFhuE+jTxm9j5GbJ2ClrN1Ftq 6NwiP+I4Scz2G1Y+dFzJg6ePeVedhswo+So05kUtmGYhg/SYT1+QHOvFVMrgH+649v45 m8ZA== X-Gm-Message-State: AC+VfDxAUj6/lpIjCYpPfjw0gDKNhN/qn6tGSuW/McunwVXWQhiUMkgu JwQmDmHpEUqvx9rFR2PLiHY= X-Google-Smtp-Source: ACHHUZ6od08fUS5Y5YBcxZ9WTfK/OI6dT7FI8Sb1oVaF31Hk1VrgKGdgSM0hpkvAGBFP0SFa0Z/byw== X-Received: by 2002:a17:907:1c93:b0:97d:f8aa:6d37 with SMTP id nb19-20020a1709071c9300b0097df8aa6d37mr8045773ejc.61.1686562701654; Mon, 12 Jun 2023 02:38:21 -0700 (PDT) Received: from lelloman-5950.homenet.telecomitalia.it (host-212-171-43-8.retail.telecomitalia.it. [212.171.43.8]) by smtp.gmail.com with ESMTPSA id ce23-20020a170906b25700b0097887b68c17sm4951358ejb.98.2023.06.12.02.38.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Jun 2023 02:38:21 -0700 (PDT) From: Domenico Cerasuolo To: vitaly.wool@konsulko.com, minchan@kernel.org, senozhatsky@chromium.org, yosryahmed@google.com, linux-mm@kvack.org Cc: ddstreet@ieee.org, sjenning@redhat.com, nphamcs@gmail.com, hannes@cmpxchg.org, akpm@linux-foundation.org, linux-kernel@vger.kernel.org, kernel-team@meta.com, Domenico Cerasuolo Subject: [PATCH v3 1/7] mm: zswap: add pool shrinking mechanism Date: Mon, 12 Jun 2023 11:38:09 +0200 Message-Id: <20230612093815.133504-2-cerasuolodomenico@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230612093815.133504-1-cerasuolodomenico@gmail.com> References: <20230612093815.133504-1-cerasuolodomenico@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 2BC761C000C X-Stat-Signature: 5b11ofewubuf7b87edpmw3onkcqu6r1b X-Rspam-User: X-HE-Tag: 1686562702-647358 X-HE-Meta: U2FsdGVkX194OXPmsZKmCxNWmnWhPXB6NrEjFLveLe5sSiPgnxR4BvFkwdf+3PjpONeB2ibT06n7WdLf/EAk7IBh3DUxB/gvhs22VO0j4xIEZ8oa8/e5osg1PtDfqFj5KprPt9nmAKYcrrkv8OTuxoiKHqsLWYSO4LQyNlhf/8WGJxKOms0kbBUvG+3uzwP08ct5IgqF4qMRbQL6AZtIoTgcKQxEus9tf9FZv73Wri9I3beKNvCO+SqxxvUvdFmhUuXVj+sJAC78jV5WqdnI72J1AkQ+mz+15dkM58MBb1DPge45w+tKkjPazj5R6/D++uzqU04VMcmHYQ8mc3zAOofgbOENLRWaP2OM1aAEJ32KroVriYGrKrN1Pw3749yJPPvW8pdWgQZqqxZEvfAXKWmAmZ8/BJmjKiIBhjpbOOy6IT35XQJyzGQglETBrqQ5J/8pe2HCCtM21sRhpB2WEjNlSc0kHraVCAnwE9kmXGPwWAeAItY2jjr5aauxRbsgCtiP/zShgdq+jcLrWeTw3k2q/lEgmKo8enZFb0TtHRloxjo4JIBA29n/U+BOp3uzpZ55NCSY6Mrk1K8gEM/qLbGNi9iNABB0MLr7B9Nb13jzWMC9u1l7WLfF8fRDg7jcg6hAOO2fKEmyzmUHT47miSIw3rX5g/2s4SJqf/jERbKRpp8fWQgvsrjBba78oh4y47Awov76FFDkZ98hTd5r62JzqBlpabb1ndtUlGcKgOK+qFcFDE3sXEYPrN9HixR6fdACHBAnVTUSm38quE7264cLJIICd+e8cKuzoebAiy8YKYwTHRCBH6sYz4+g+q5yAanUfBYuSdFM7GlJtcf60MhyFIivuFb+GyfyiKInyq6ppzb2FYSDrID3BdfB6TwgXL0MfpQpQuZUY9/r2nfFS50rvAoLDhLcw6tpl3rJsUJEU1IfBhVhdHxus7tilO6sIw4tauXfIS91Mt2TK71 DH4dBqoj 1aDclD/UqKx6+GliGHSB26/D/mdA2YMMvkrBgSch56MxV9V88C0X5L50Qe3XMeKVg4wkhoEvFV8SqYf25tpiy2XNyI/Ldtl5kqfSqtwKbKAq/tfIb3UKk5XY+s/weiOuU+AGT9HXKLrtm4EQB1+mc0QquInax6xmytVv6tH39xx9qJHLSVl35KIK9NqWvLgv/ba2DIl6QhT6PbZ0B+kdumK2YgCWZ48jCtXJvVOiTlCJI2wIoAdlHWErKaHZLbhnLQ2jFQ7CHiLrqEnyYoRsDMRrvtby3UqyIUhBZg0P3papauGQEuAh/Zrr2e5Iy/OEkMrmJRYQsHZ6kgrq0FP2A3Q8gERjvVXmJO/9TXHILrewxk/J9vM6KAfDJXjVroeV7WtRiK+4v93Fmz4Rm2xMi6yKDO0Ap+Q46wRfa+C7y1bnBfa9Xlv5ctlrK+AhSqHIJswvCbI78SmS5mGHvGM1/dHPjPQ== 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: Each zpool driver (zbud, z3fold and zsmalloc) implements its own shrink function, which is called from zpool_shrink. However, with this commit, a unified shrink function is added to zswap. The ultimate goal is to eliminate the need for zpool_shrink once all zpool implementations have dropped their shrink code. To ensure the functionality of each commit, this change focuses solely on adding the mechanism itself. No modifications are made to the backends, meaning that functionally, there are no immediate changes. The zswap mechanism will only come into effect once the backends have removed their shrink code. The subsequent commits will address the modifications needed in the backends. Acked-by: Nhat Pham Tested-by: Yosry Ahmed Signed-off-by: Domenico Cerasuolo Acked-by: Johannes Weiner Reviewed-by: Yosry Ahmed --- mm/zswap.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 92 insertions(+), 5 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 9fa86265f6dd..0024ec5ed574 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -154,6 +154,12 @@ struct crypto_acomp_ctx { struct mutex *mutex; }; +/* + * The lock ordering is zswap_tree.lock -> zswap_pool.lru_lock. + * The only case where lru_lock is not acquired while holding tree.lock is + * when a zswap_entry is taken off the lru for writeback, in that case it + * needs to be verified that it's still valid in the tree. + */ struct zswap_pool { struct zpool *zpool; struct crypto_acomp_ctx __percpu *acomp_ctx; @@ -163,6 +169,8 @@ struct zswap_pool { struct work_struct shrink_work; struct hlist_node node; char tfm_name[CRYPTO_MAX_ALG_NAME]; + struct list_head lru; + spinlock_t lru_lock; }; /* @@ -180,10 +188,12 @@ struct zswap_pool { * be held while changing the refcount. Since the lock must * be held, there is no reason to also make refcount atomic. * length - the length in bytes of the compressed page data. Needed during - * decompression. For a same value filled page length is 0. + * decompression. For a same value filled page length is 0, and both + * pool and lru are invalid and must be ignored. * pool - the zswap_pool the entry's data is in * handle - zpool allocation handle that stores the compressed page data * value - value of the same-value filled pages which have same content + * lru - handle to the pool's lru used to evict pages. */ struct zswap_entry { struct rb_node rbnode; @@ -196,6 +206,7 @@ struct zswap_entry { unsigned long value; }; struct obj_cgroup *objcg; + struct list_head lru; }; struct zswap_header { @@ -368,6 +379,12 @@ static void zswap_free_entry(struct zswap_entry *entry) if (!entry->length) atomic_dec(&zswap_same_filled_pages); else { + /* zpool_evictable will be removed once all 3 backends have migrated */ + if (!zpool_evictable(entry->pool->zpool)) { + spin_lock(&entry->pool->lru_lock); + list_del(&entry->lru); + spin_unlock(&entry->pool->lru_lock); + } zpool_free(entry->pool->zpool, entry->handle); zswap_pool_put(entry->pool); } @@ -588,14 +605,72 @@ static struct zswap_pool *zswap_pool_find_get(char *type, char *compressor) return NULL; } +static int zswap_reclaim_entry(struct zswap_pool *pool) +{ + struct zswap_header *zhdr; + struct zswap_entry *entry; + struct zswap_tree *tree; + pgoff_t swpoffset; + int ret; + + /* Get an entry off the LRU */ + spin_lock(&pool->lru_lock); + if (list_empty(&pool->lru)) { + spin_unlock(&pool->lru_lock); + return -EINVAL; + } + entry = list_last_entry(&pool->lru, struct zswap_entry, lru); + list_del_init(&entry->lru); + zhdr = zpool_map_handle(pool->zpool, entry->handle, ZPOOL_MM_RO); + tree = zswap_trees[swp_type(zhdr->swpentry)]; + zpool_unmap_handle(pool->zpool, entry->handle); + /* + * Once the lru lock is dropped, the entry might get freed. The + * swpoffset is copied to the stack, and entry isn't deref'd again + * until the entry is verified to still be alive in the tree. + */ + swpoffset = swp_offset(zhdr->swpentry); + spin_unlock(&pool->lru_lock); + + /* Check for invalidate() race */ + spin_lock(&tree->lock); + if (entry != zswap_rb_search(&tree->rbroot, swpoffset)) { + ret = -EAGAIN; + goto unlock; + } + /* Hold a reference to prevent a free during writeback */ + zswap_entry_get(entry); + spin_unlock(&tree->lock); + + ret = zswap_writeback_entry(pool->zpool, entry->handle); + + spin_lock(&tree->lock); + if (ret) { + /* Writeback failed, put entry back on LRU */ + spin_lock(&pool->lru_lock); + list_move(&entry->lru, &pool->lru); + spin_unlock(&pool->lru_lock); + } + + /* Drop local reference */ + zswap_entry_put(tree, entry); +unlock: + spin_unlock(&tree->lock); + return ret ? -EAGAIN : 0; +} + static void shrink_worker(struct work_struct *w) { struct zswap_pool *pool = container_of(w, typeof(*pool), shrink_work); int ret, failures = 0; + /* zpool_evictable will be removed once all 3 backends have migrated */ do { - ret = zpool_shrink(pool->zpool, 1, NULL); + if (zpool_evictable(pool->zpool)) + ret = zpool_shrink(pool->zpool, 1, NULL); + else + ret = zswap_reclaim_entry(pool); if (ret) { zswap_reject_reclaim_fail++; if (ret != -EAGAIN) @@ -659,6 +734,8 @@ static struct zswap_pool *zswap_pool_create(char *type, char *compressor) */ kref_init(&pool->kref); INIT_LIST_HEAD(&pool->list); + INIT_LIST_HEAD(&pool->lru); + spin_lock_init(&pool->lru_lock); INIT_WORK(&pool->shrink_work, shrink_worker); zswap_pool_debug("created", pool); @@ -1274,7 +1351,7 @@ static int zswap_frontswap_store(unsigned type, pgoff_t offset, } /* store */ - hlen = zpool_evictable(entry->pool->zpool) ? sizeof(zhdr) : 0; + hlen = sizeof(zhdr); gfp = __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM; if (zpool_malloc_support_movable(entry->pool->zpool)) gfp |= __GFP_HIGHMEM | __GFP_MOVABLE; @@ -1317,6 +1394,12 @@ static int zswap_frontswap_store(unsigned type, pgoff_t offset, zswap_entry_put(tree, dupentry); } } while (ret == -EEXIST); + /* zpool_evictable will be removed once all 3 backends have migrated */ + if (entry->length && !zpool_evictable(entry->pool->zpool)) { + spin_lock(&entry->pool->lru_lock); + list_add(&entry->lru, &entry->pool->lru); + spin_unlock(&entry->pool->lru_lock); + } spin_unlock(&tree->lock); /* update stats */ @@ -1398,8 +1481,7 @@ static int zswap_frontswap_load(unsigned type, pgoff_t offset, /* decompress */ dlen = PAGE_SIZE; src = zpool_map_handle(entry->pool->zpool, entry->handle, ZPOOL_MM_RO); - if (zpool_evictable(entry->pool->zpool)) - src += sizeof(struct zswap_header); + src += sizeof(struct zswap_header); if (!zpool_can_sleep_mapped(entry->pool->zpool)) { memcpy(tmp, src, entry->length); @@ -1432,6 +1514,11 @@ static int zswap_frontswap_load(unsigned type, pgoff_t offset, if (!ret && zswap_exclusive_loads_enabled) { zswap_invalidate_entry(tree, entry); *exclusive = true; + } else if (entry->length && !zpool_evictable(entry->pool->zpool)) { + /* zpool_evictable will be removed once all 3 backends have migrated */ + spin_lock(&entry->pool->lru_lock); + list_move(&entry->lru, &entry->pool->lru); + spin_unlock(&entry->pool->lru_lock); } spin_unlock(&tree->lock); From patchwork Mon Jun 12 09:38:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Domenico Cerasuolo X-Patchwork-Id: 13275953 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 65B68C7EE45 for ; Mon, 12 Jun 2023 09:38:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EE2B18E0003; Mon, 12 Jun 2023 05:38:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E6BE06B0075; Mon, 12 Jun 2023 05:38:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C4B378E0003; Mon, 12 Jun 2023 05:38:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id B61B86B0074 for ; Mon, 12 Jun 2023 05:38:26 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 8D5CCAF11A for ; Mon, 12 Jun 2023 09:38:26 +0000 (UTC) X-FDA: 80893595412.17.0D4A96A Received: from mail-ej1-f48.google.com (mail-ej1-f48.google.com [209.85.218.48]) by imf25.hostedemail.com (Postfix) with ESMTP id 9A492A0005 for ; Mon, 12 Jun 2023 09:38:24 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=BS47izYk; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf25.hostedemail.com: domain of cerasuolodomenico@gmail.com designates 209.85.218.48 as permitted sender) smtp.mailfrom=cerasuolodomenico@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686562704; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=BYQpKJ28RkPZW3NObwb2r+lw4fKc8lZ4o5qJebIhoW0=; b=BEHtVOdEfWF/ckaAgVYstzIwPnDUMhTI9DrGddcyYZ9b1R6YMZqGGmenk7jSP1dmfbfSy6 fCfFvQzFDXdWORWxGI8kMxjJbpF5A18DTpkL8TjFEXxaGZZ2NpU/awBoVJ2j842iaOCOQO FqYoKDv6z1+bHX4Nbep0w2qob5qOUBE= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=BS47izYk; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf25.hostedemail.com: domain of cerasuolodomenico@gmail.com designates 209.85.218.48 as permitted sender) smtp.mailfrom=cerasuolodomenico@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1686562704; a=rsa-sha256; cv=none; b=kVOzVZpyGXdrFzoWe//JcpSCoA8L5+YpEXtP2f04TPGIsX3TIQNSjay6T182X6a7P46MT4 P+f8z4rwIjEaDFIzC91RcisfFTTg7MQDb6OIzDzPk/gPZLWfFf16K+RYZ2rYsa8kmo8+FO W398TP2RERwiNDWv800NYqvfYIm+uWM= Received: by mail-ej1-f48.google.com with SMTP id a640c23a62f3a-97668583210so611803066b.1 for ; Mon, 12 Jun 2023 02:38:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686562703; x=1689154703; 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=BYQpKJ28RkPZW3NObwb2r+lw4fKc8lZ4o5qJebIhoW0=; b=BS47izYkA4or+JYwN3wR0jWIAr7/ME4QNRglwncxoAQO9o+6LY1zTB3kY03hwJHju2 WMBjUOjW/qkJNlH28JdJ+xMVSh3pUuJFN7OzEc0McBsreP74hbwgjYAzIoGkOnD0l5R+ r6QSkkJ2vh72n0/AFNniae1QDv+V4M4vYBiYrsQlOafLteiOGGVzFeSu2+DZXGjsCezo vZkT2lVVm5g49PS8kRftdY2RJiwWYCJJN6sI2bNJ/eySFQ2SQIXXzmm2FAJNYtOdFqxO 6oq2UZjsDZ+09NG2OC5ZxQn26q5+eO6dFGX0+U+ujkJT9Lajq6pG9FJYc1FbfqSwS/t0 N3Bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686562703; x=1689154703; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BYQpKJ28RkPZW3NObwb2r+lw4fKc8lZ4o5qJebIhoW0=; b=S3EHKNZman1ectvt0dkeSmNW41n06iVJKHqcTmNEegoCJnFQt+B+Y7dknGxetkCC8N 02TOlyFFWqqEWrs71UzXecwdcB1wwQnH0UaqbOgv9Cv6XSWkZllm/VZaDVTJN/vbcNYF bQg/5xw/Pm7C+i7xq9DOLCiVwHMY54Q2DwhuYSqQulJmdlkafsuFbgqFeQ2kGM7DwmHl CLBnvD3OUKL/NMP68y5D9uYqxWJuBOpXf9rWYNQ41EKr7OWT0PSsfkab1cEt18f1GF8x IzR+bVYRrvDLkRRvDioX4QhhoG3fvLTpwrkzfKGCFIJNV6SPoMNqNEyfRej5jFTC665B vzVA== X-Gm-Message-State: AC+VfDz6/J/D0DLCoJrUjS3qObMa7H4o2Ecw+OW+kRQmN+xqtq0GVYKG jR1H3AfmoFkhNJ8qkrB3KaE= X-Google-Smtp-Source: ACHHUZ6Ig9GsZfwV+Y9+P73Dv4hJNyxOlnWH4bLFoYv6LKiG4MBKz3X8HRd4297KFDPeb/M14sM/3w== X-Received: by 2002:a17:907:6da5:b0:974:4a33:83a7 with SMTP id sb37-20020a1709076da500b009744a3383a7mr8294900ejc.12.1686562702987; Mon, 12 Jun 2023 02:38:22 -0700 (PDT) Received: from lelloman-5950.homenet.telecomitalia.it (host-212-171-43-8.retail.telecomitalia.it. [212.171.43.8]) by smtp.gmail.com with ESMTPSA id ce23-20020a170906b25700b0097887b68c17sm4951358ejb.98.2023.06.12.02.38.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Jun 2023 02:38:22 -0700 (PDT) From: Domenico Cerasuolo To: vitaly.wool@konsulko.com, minchan@kernel.org, senozhatsky@chromium.org, yosryahmed@google.com, linux-mm@kvack.org Cc: ddstreet@ieee.org, sjenning@redhat.com, nphamcs@gmail.com, hannes@cmpxchg.org, akpm@linux-foundation.org, linux-kernel@vger.kernel.org, kernel-team@meta.com, Domenico Cerasuolo Subject: [PATCH v3 2/7] mm: zswap: remove page reclaim logic from zbud Date: Mon, 12 Jun 2023 11:38:10 +0200 Message-Id: <20230612093815.133504-3-cerasuolodomenico@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230612093815.133504-1-cerasuolodomenico@gmail.com> References: <20230612093815.133504-1-cerasuolodomenico@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 9A492A0005 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: oeb1syzzo7rhsph7694ki9inxxxoowka X-HE-Tag: 1686562704-787476 X-HE-Meta: U2FsdGVkX1+NXcBWxrJTfEHZiNogOUoD1Z0lOoxlHCG6WsRcxcy2/tamZHx9pRCCjYS8s2rWOTGUAGz8VtNJr6Ol0jLCPY6Fq0CztkkqtoJk+11uR9RNuwdbiT/pnU/XU4e0umnQdmgJ9blPAZZD8kyzYz+LD7iAVwZVtzxfSnJyjXTwy6yQDTUl9Qonei8LzJtqY+SYqE+n3Ilc3j9lDe3+QZT6e6tYNvgcWyKxsfSmEu9H8meYWv/E5dukes4UCyUon31+0keKdkmzN5TYfX8fHB/92G3Oytiio1oqy1wtGyNGvJrSEocS05YmvZU9YlwiKunn/5w8NuIPHTkMC0e0r/yH26WiqvyEKs9hgwGUbGao9c7ANP+pmGYlVbydMpnb15xvQXpXPnLnba/YePocpGd2GXIUT1U68LB7ZbxWuv0AEapnsRgc1wU+RfkVmrSF7MjE9iZeGhV/kIVNU+4jrcZwyLu2ipscP8Xwr+BLYqwoHmM0Ztc+mdzHj1Z6gEUuozQu1mG7OR3wLWClNhlvVdqCYM577voMpTM3N6cIuZ6HT3OorT6pV5HhRfcOq2YfuX2ZdzUYbH1GLZICl/QPXrnJpYfRWtwCnlPsyYZwWNWEl1nCmkdtG650t+G7aaV+hd1RsZbEhconm1frs/ZVCsMZbPJvfK/cNco0z3rondIcNWzTLc5n7jOEQhSc81H3TpHgTIgqZgTp39QtFPBRVkFbyiyXALL6wKBMMTxnN7YBWHS++XmyYjzvEo7QvCxmsNKWS4vG4caTb7X3RNJ7EqlXgJmyOAQ1fKUxgeg6PMANMBDncnOGtQLKHJsl9dhFx8LhuvKqMMOAF4VBwWKAB23fFxRlZfDNSIOlg7eI1I0p7YteYt2b7eiwT88s2STNNIXFYlcbDpwwtcWNslzVBiBvd/8Dw4mhw2OHt3lCoE92Bauan8UGQ59ReCOxxafUcCrND8ZsItVXKER Viyvvra4 NoYxH9+fF02afuKVsae5UvtaIK3dw44IxitkmM3ahCeZO/wFX+ISjK6kOZe0nsWyVbMvGS/36tsvx1ILdFGD01sp8A64NWOa2ziRwXR8xzGj/IjK4lInxMuylotIkyHJvfal1L2Q/ZhPIsogPn6/1D/vLfYBw1YGHaWw2xlm4uJsnqmzsa3Nf1sFAmoVSWVVRM649t1V+KaySVl52tDGIK63DtyfPCV/3Z3pJSLw2OIqEkCc+YeK6/LNQkDgjkLOQtr7apmnRPDJWRenoX4WZrm4SINVPYIOWQ2VN56icb40sKUWiTEPEzoZUsKR6ilV0yMWLyiGvwJv9LiJl73qM7oyhU/qhcoMpYqF9So4ylwQ0BxVaDcTi4kOgF2+OsnoUzx2z9XSeRVONY7+uS9lOeB2eQw07YIyMmVakeE8F+Tu4h07BJ3wTLMOfQSPzBpgY9CJVK+IRFKTrCz6lhk8PNOgfC1EXPLD65fPk/UZlI+EEDy/jyoJWXq511u7/KsN00jGK 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: Switch zbud to the new generic zswap LRU and remove its custom implementation. Acked-by: Johannes Weiner Signed-off-by: Domenico Cerasuolo --- mm/zbud.c | 163 +----------------------------------------------------- 1 file changed, 1 insertion(+), 162 deletions(-) diff --git a/mm/zbud.c b/mm/zbud.c index 3acd26193920..9d35fd4091ed 100644 --- a/mm/zbud.c +++ b/mm/zbud.c @@ -83,11 +83,7 @@ struct zbud_pool; * its free region. * @buddied: list tracking the zbud pages that contain two buddies; * these zbud pages are full - * @lru: list tracking the zbud pages in LRU order by most recently - * added buddy. * @pages_nr: number of zbud pages in the pool. - * @zpool: zpool driver - * @zpool_ops: zpool operations structure with an evict callback * * This structure is allocated at pool creation time and maintains metadata * pertaining to a particular zbud pool. @@ -102,26 +98,20 @@ struct zbud_pool { struct list_head buddied; struct list_head unbuddied[NCHUNKS]; }; - struct list_head lru; u64 pages_nr; - struct zpool *zpool; - const struct zpool_ops *zpool_ops; }; /* * struct zbud_header - zbud page metadata occupying the first chunk of each * zbud page. * @buddy: links the zbud page into the unbuddied/buddied lists in the pool - * @lru: links the zbud page into the lru list in the pool * @first_chunks: the size of the first buddy in chunks, 0 if free * @last_chunks: the size of the last buddy in chunks, 0 if free */ struct zbud_header { struct list_head buddy; - struct list_head lru; unsigned int first_chunks; unsigned int last_chunks; - bool under_reclaim; }; /***************** @@ -149,8 +139,6 @@ static struct zbud_header *init_zbud_page(struct page *page) zhdr->first_chunks = 0; zhdr->last_chunks = 0; INIT_LIST_HEAD(&zhdr->buddy); - INIT_LIST_HEAD(&zhdr->lru); - zhdr->under_reclaim = false; return zhdr; } @@ -221,7 +209,6 @@ static struct zbud_pool *zbud_create_pool(gfp_t gfp) for_each_unbuddied_list(i, 0) INIT_LIST_HEAD(&pool->unbuddied[i]); INIT_LIST_HEAD(&pool->buddied); - INIT_LIST_HEAD(&pool->lru); pool->pages_nr = 0; return pool; } @@ -310,11 +297,6 @@ static int zbud_alloc(struct zbud_pool *pool, size_t size, gfp_t gfp, list_add(&zhdr->buddy, &pool->buddied); } - /* Add/move zbud page to beginning of LRU */ - if (!list_empty(&zhdr->lru)) - list_del(&zhdr->lru); - list_add(&zhdr->lru, &pool->lru); - *handle = encode_handle(zhdr, bud); spin_unlock(&pool->lock); @@ -325,11 +307,6 @@ static int zbud_alloc(struct zbud_pool *pool, size_t size, gfp_t gfp, * zbud_free() - frees the allocation associated with the given handle * @pool: pool in which the allocation resided * @handle: handle associated with the allocation returned by zbud_alloc() - * - * In the case that the zbud page in which the allocation resides is under - * reclaim, as indicated by the PG_reclaim flag being set, this function - * only sets the first|last_chunks to 0. The page is actually freed - * once both buddies are evicted (see zbud_reclaim_page() below). */ static void zbud_free(struct zbud_pool *pool, unsigned long handle) { @@ -345,18 +322,11 @@ static void zbud_free(struct zbud_pool *pool, unsigned long handle) else zhdr->first_chunks = 0; - if (zhdr->under_reclaim) { - /* zbud page is under reclaim, reclaim will free */ - spin_unlock(&pool->lock); - return; - } - /* Remove from existing buddy list */ list_del(&zhdr->buddy); if (zhdr->first_chunks == 0 && zhdr->last_chunks == 0) { /* zbud page is empty, free */ - list_del(&zhdr->lru); free_zbud_page(zhdr); pool->pages_nr--; } else { @@ -368,110 +338,6 @@ static void zbud_free(struct zbud_pool *pool, unsigned long handle) spin_unlock(&pool->lock); } -/** - * zbud_reclaim_page() - evicts allocations from a pool page and frees it - * @pool: pool from which a page will attempt to be evicted - * @retries: number of pages on the LRU list for which eviction will - * be attempted before failing - * - * zbud reclaim is different from normal system reclaim in that the reclaim is - * done from the bottom, up. This is because only the bottom layer, zbud, has - * information on how the allocations are organized within each zbud page. This - * has the potential to create interesting locking situations between zbud and - * the user, however. - * - * To avoid these, this is how zbud_reclaim_page() should be called: - * - * The user detects a page should be reclaimed and calls zbud_reclaim_page(). - * zbud_reclaim_page() will remove a zbud page from the pool LRU list and call - * the user-defined eviction handler with the pool and handle as arguments. - * - * If the handle can not be evicted, the eviction handler should return - * non-zero. zbud_reclaim_page() will add the zbud page back to the - * appropriate list and try the next zbud page on the LRU up to - * a user defined number of retries. - * - * If the handle is successfully evicted, the eviction handler should - * return 0 _and_ should have called zbud_free() on the handle. zbud_free() - * contains logic to delay freeing the page if the page is under reclaim, - * as indicated by the setting of the PG_reclaim flag on the underlying page. - * - * If all buddies in the zbud page are successfully evicted, then the - * zbud page can be freed. - * - * Returns: 0 if page is successfully freed, otherwise -EINVAL if there are - * no pages to evict or an eviction handler is not registered, -EAGAIN if - * the retry limit was hit. - */ -static int zbud_reclaim_page(struct zbud_pool *pool, unsigned int retries) -{ - int i, ret, freechunks; - struct zbud_header *zhdr; - unsigned long first_handle = 0, last_handle = 0; - - spin_lock(&pool->lock); - if (list_empty(&pool->lru)) { - spin_unlock(&pool->lock); - return -EINVAL; - } - for (i = 0; i < retries; i++) { - zhdr = list_last_entry(&pool->lru, struct zbud_header, lru); - list_del(&zhdr->lru); - list_del(&zhdr->buddy); - /* Protect zbud page against free */ - zhdr->under_reclaim = true; - /* - * We need encode the handles before unlocking, since we can - * race with free that will set (first|last)_chunks to 0 - */ - first_handle = 0; - last_handle = 0; - if (zhdr->first_chunks) - first_handle = encode_handle(zhdr, FIRST); - if (zhdr->last_chunks) - last_handle = encode_handle(zhdr, LAST); - spin_unlock(&pool->lock); - - /* Issue the eviction callback(s) */ - if (first_handle) { - ret = pool->zpool_ops->evict(pool->zpool, first_handle); - if (ret) - goto next; - } - if (last_handle) { - ret = pool->zpool_ops->evict(pool->zpool, last_handle); - if (ret) - goto next; - } -next: - spin_lock(&pool->lock); - zhdr->under_reclaim = false; - if (zhdr->first_chunks == 0 && zhdr->last_chunks == 0) { - /* - * Both buddies are now free, free the zbud page and - * return success. - */ - free_zbud_page(zhdr); - pool->pages_nr--; - spin_unlock(&pool->lock); - return 0; - } else if (zhdr->first_chunks == 0 || - zhdr->last_chunks == 0) { - /* add to unbuddied list */ - freechunks = num_free_chunks(zhdr); - list_add(&zhdr->buddy, &pool->unbuddied[freechunks]); - } else { - /* add to buddied list */ - list_add(&zhdr->buddy, &pool->buddied); - } - - /* add to beginning of LRU */ - list_add(&zhdr->lru, &pool->lru); - } - spin_unlock(&pool->lock); - return -EAGAIN; -} - /** * zbud_map() - maps the allocation associated with the given handle * @pool: pool in which the allocation resides @@ -518,14 +384,7 @@ static void *zbud_zpool_create(const char *name, gfp_t gfp, const struct zpool_ops *zpool_ops, struct zpool *zpool) { - struct zbud_pool *pool; - - pool = zbud_create_pool(gfp); - if (pool) { - pool->zpool = zpool; - pool->zpool_ops = zpool_ops; - } - return pool; + return zbud_create_pool(gfp); } static void zbud_zpool_destroy(void *pool) @@ -543,25 +402,6 @@ static void zbud_zpool_free(void *pool, unsigned long handle) zbud_free(pool, handle); } -static int zbud_zpool_shrink(void *pool, unsigned int pages, - unsigned int *reclaimed) -{ - unsigned int total = 0; - int ret = -EINVAL; - - while (total < pages) { - ret = zbud_reclaim_page(pool, 8); - if (ret < 0) - break; - total++; - } - - if (reclaimed) - *reclaimed = total; - - return ret; -} - static void *zbud_zpool_map(void *pool, unsigned long handle, enum zpool_mapmode mm) { @@ -585,7 +425,6 @@ static struct zpool_driver zbud_zpool_driver = { .destroy = zbud_zpool_destroy, .malloc = zbud_zpool_malloc, .free = zbud_zpool_free, - .shrink = zbud_zpool_shrink, .map = zbud_zpool_map, .unmap = zbud_zpool_unmap, .total_size = zbud_zpool_total_size, From patchwork Mon Jun 12 09:38:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Domenico Cerasuolo X-Patchwork-Id: 13275954 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 02D55C7EE43 for ; Mon, 12 Jun 2023 09:38:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 521AF8E0005; Mon, 12 Jun 2023 05:38:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 436156B0075; Mon, 12 Jun 2023 05:38:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1C44B8E0005; Mon, 12 Jun 2023 05:38:28 -0400 (EDT) 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 04B416B0074 for ; Mon, 12 Jun 2023 05:38:28 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id C72C81201A1 for ; Mon, 12 Jun 2023 09:38:27 +0000 (UTC) X-FDA: 80893595454.09.9D9B2E8 Received: from mail-ej1-f50.google.com (mail-ej1-f50.google.com [209.85.218.50]) by imf15.hostedemail.com (Postfix) with ESMTP id D594EA000A for ; Mon, 12 Jun 2023 09:38:25 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=UsGToKvO; spf=pass (imf15.hostedemail.com: domain of cerasuolodomenico@gmail.com designates 209.85.218.50 as permitted sender) smtp.mailfrom=cerasuolodomenico@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686562706; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=jWcn4pimJ5KW1PBG1fIPKyRzJ8b978SaqQ0pm1IG1/8=; b=ZxxvekyDwQ9ywFvleX0AEKUykzn/AiW9p8Ag4Ubt4IWugCcJFPNaI9ccMdHc8jHw8Vf+CD Glfkl5A5fZb4AlfSdli0N88PXfqHHKJNHSAkcWFcLQ73oEqeauw1wg2od6WUvRXD77VxTS CFlR8lzKoZ0+Oe6daI4VraCRb/57evU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1686562706; a=rsa-sha256; cv=none; b=I9Twzx155xSZR0CZNsx/Kl3iqlhOI9gS+wcDNukNIOTHcbspWla+IuZBHSNwPg2N1B2nSG keiJR+FXEEFjNyHeJY4Q78DJEGBEHJEPtS/okvxSoixz+E+/2BWwxQEVtW9eNhKUkUEIjU AyI8gYbUhjHKwVUtJw3dC5mOf5gSbaE= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=UsGToKvO; spf=pass (imf15.hostedemail.com: domain of cerasuolodomenico@gmail.com designates 209.85.218.50 as permitted sender) smtp.mailfrom=cerasuolodomenico@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-ej1-f50.google.com with SMTP id a640c23a62f3a-970056276acso624954666b.2 for ; Mon, 12 Jun 2023 02:38:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686562704; x=1689154704; 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=jWcn4pimJ5KW1PBG1fIPKyRzJ8b978SaqQ0pm1IG1/8=; b=UsGToKvO4y90q4yPLfqeWwZ/1V3U/Fn8s4qhkbX9Y0D9gowJK4N1qYjtTOtJZiTZ0U GiBeZ3eWkRFQ0fzeJ3ERdtZaSGlKW1hrZiq19+LMbKSeGowZAlRYAbwcLsR2zdro1wFN d1WW2BnINvfFGYPgsDk8EaD/w8EKgECRYGik8RnfdqKxEDtFXxL7ZlQUi8uMk+NHfORK 2mnKNSBtv4eAJsG2l2AjLI1iZFJ6cCKwSxL6zu/E4V3LpKwU0qdKG8x/QPTe/EmMDSUZ Eilm18mT8nvlwgTRYy3W5o4nzPV4EzMAEg07fT+8jK4uUojKmBbedhpFS59yQ2+ix95i a7oQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686562704; x=1689154704; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jWcn4pimJ5KW1PBG1fIPKyRzJ8b978SaqQ0pm1IG1/8=; b=RBp/BbzVuiYH7fy4L+6EOk5q3nzxAxfWuoJ6dslt1VNXOoepk6Dw3VbIZKxX/18ket wX92rl5z9UTVs4zKNrUckCbBjFEq6iCzwhHAEIP/t7W2Qk32Tweor4zatFQZVqMmR58K qz5OtEcacq/S2VtGw71nhhMclIpc0QctwU3maTNLvrPjv5kGNKkxGX8rZKFZ6WCJLrH1 cMXFspUellFRdew7jdEOdeZR6Hh1XvbJaoAG26XoUB8r8FABYRF+KLEMAKMIL/ChDB1M YPKoUOXG6OEjEQ6gorKCKbzvlEhyInzaaFl88GiT8Gaa1+sG9pkHh/czS54BslFPQME8 M5iQ== X-Gm-Message-State: AC+VfDzFZ5vBvDGujZ4MI7v6nfSVQ7Gl36ABOe2Y4deHIwrZZNP57ONn 5hlsmhmAKCg4CsWRF2LT6Yo= X-Google-Smtp-Source: ACHHUZ6tHRVdQFPG4ynNEr8OcqEEKS8YgYXjZBp+ML8Rf4c1enXgl3lldAM9eCWh7Htg9JlTd86A8A== X-Received: by 2002:a17:906:da83:b0:974:1ced:6a59 with SMTP id xh3-20020a170906da8300b009741ced6a59mr8089543ejb.3.1686562704358; Mon, 12 Jun 2023 02:38:24 -0700 (PDT) Received: from lelloman-5950.homenet.telecomitalia.it (host-212-171-43-8.retail.telecomitalia.it. [212.171.43.8]) by smtp.gmail.com with ESMTPSA id ce23-20020a170906b25700b0097887b68c17sm4951358ejb.98.2023.06.12.02.38.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Jun 2023 02:38:23 -0700 (PDT) From: Domenico Cerasuolo To: vitaly.wool@konsulko.com, minchan@kernel.org, senozhatsky@chromium.org, yosryahmed@google.com, linux-mm@kvack.org Cc: ddstreet@ieee.org, sjenning@redhat.com, nphamcs@gmail.com, hannes@cmpxchg.org, akpm@linux-foundation.org, linux-kernel@vger.kernel.org, kernel-team@meta.com, Domenico Cerasuolo Subject: [PATCH v3 3/7] mm: zswap: remove page reclaim logic from z3fold Date: Mon, 12 Jun 2023 11:38:11 +0200 Message-Id: <20230612093815.133504-4-cerasuolodomenico@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230612093815.133504-1-cerasuolodomenico@gmail.com> References: <20230612093815.133504-1-cerasuolodomenico@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: D594EA000A X-Rspam-User: X-Stat-Signature: airjokxpg6em1adajh8mapr5zyez77xu X-Rspamd-Server: rspam03 X-HE-Tag: 1686562705-286051 X-HE-Meta: U2FsdGVkX1/g1e2uN/hVFhi//TA/fvAdsk6AyoN3MUNtliw7+5BMUjYwGDauIVPoewsaPEyxTVflS6kY9MJPsHA3xUfl68G/kg9xVZd06KlF7kCWZv4RBHRwqkBfkskwJjP2b/TZ4QRTlVzJbrumUvVSkt8xirlivh9SHTMehAM65x5elJSViLhXxxPJCYl8tKpIfKMDst/D24o3FV2hFjufIgLrQJlkf9tROcmuTPwHCHkRqwmFvPDf0sjCmYebVPZUbdBBNii7cO0QRS3PXj6l5wsjRFoT2mPAKquL0SMycijEyvKYUlVCgUy2nFlWCdjcHUphhcqe8schOhIWkonkYeoKZs+fOdqQ1Bhns2VVbLsNBDUZijfzGwjsA1pNtawQv2mlYSuvfiWlXRF//5wE0YusVZQocIoFLCa3wiICJRzmvQYBgKI7MWWTAKpUthFNH44zU0i/Vise7xq86zJGSy/I6cFpHP3FmQXbeCOVS7I3eEFVUWAxIYsqZvwKAs3Ap160AOnL6+NDf9XbOSgdRq2T2OowgokN10AJQPlpD3LC9tbjasLRjCFCW5K243E3ecAmalra8q1SjMC3mZKINUJ50trR54DmYC1VNt8iCzA1Fb3JLMKQXkUpsF2TLsLizPfOQauB22sOxg9Fc0YZbRm1vA1i1Xu0x6UwkZmHaxiKnuFHNXcI3CG2yZc8NEBkxrf5PEZql8axYfALJfm3oDJCvlr+zpqCnQNNzNO7HoeNuHyqoxTcPd9d0RtGzlIPzFq2NNritS3gfT64R2kN+e9T6MXu7t+5qYAQfla2xiB0sOj1kmSNy3eRO5ZtDFUOXq0y3qE38T4HXiqicinMSF/IDeVqKIPRXIr5q5foOVlgTcLLMYc1LMndPKbpT6YDkSmOwPWLuBu1kXjUd8P0kwTgqVORDho4h4mlx4OcjVggPRwvQF1qbuUmuhyalh65zancC3jB3rxa7rq gtcE5qhk I0gjWZSz8tClYsGfx5odGVt+kDsJYfj8TG1gVHy/L+b3bs6eSzyRY3tRRXSEMgyoDsRtCjD8ERsY6wkX/79RgkqSpho4+hjwkrk1B+wtj6A4yrRPoDDIFPGRaAcdtWBhHlK+M5ykYr2klEVqjbYr4a+deOttAg1dZhlVACwaGUsGxx1T8IusCyTXIskQI5bjV4YAW8UVfhCb5qlooVEzTGDo9g3FhCMtDpXDWfMfLkNz4SZHnvOg+6vwFz8bwCuC2X68we20l2XzUGHIeYoIm4ZWbe6ZSp6bTcbof0PS3c7b1AbuzlKER0yUrH1qsDthluahFY/HJRNuWTztZPBMT2bm7th5xuFGS0gflxMw4xVWB0kFcR6pi4SMoL2g/M/UJertbrBN7hAfpwCbd69IAZKSLIYNPU+0l8MN6/0yVjWd6q0azieUgHP5I8gPgQwa960N7RDlMDhWMUaLaKnmDnHiQQEyVRDC6DW9o7h2XgZWgt9lxs++F+Foar69x+SXq4l7l 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: Switch z3fold to the new generic zswap LRU and remove its custom implementation. Acked-by: Johannes Weiner Signed-off-by: Domenico Cerasuolo --- mm/z3fold.c | 245 +--------------------------------------------------- 1 file changed, 2 insertions(+), 243 deletions(-) diff --git a/mm/z3fold.c b/mm/z3fold.c index 0cef845d397b..238a214de59f 100644 --- a/mm/z3fold.c +++ b/mm/z3fold.c @@ -125,13 +125,11 @@ struct z3fold_header { /** * struct z3fold_pool - stores metadata for each z3fold pool * @name: pool name - * @lock: protects pool unbuddied/lru lists + * @lock: protects pool unbuddied lists * @stale_lock: protects pool stale page list * @unbuddied: per-cpu array of lists tracking z3fold pages that contain 2- * buddies; the list each z3fold page is added to depends on * the size of its free region. - * @lru: list tracking the z3fold pages in LRU order by most recently - * added buddy. * @stale: list of pages marked for freeing * @pages_nr: number of z3fold pages in the pool. * @c_handle: cache for z3fold_buddy_slots allocation @@ -149,12 +147,9 @@ struct z3fold_pool { spinlock_t lock; spinlock_t stale_lock; struct list_head *unbuddied; - struct list_head lru; struct list_head stale; atomic64_t pages_nr; struct kmem_cache *c_handle; - struct zpool *zpool; - const struct zpool_ops *zpool_ops; struct workqueue_struct *compact_wq; struct workqueue_struct *release_wq; struct work_struct work; @@ -329,7 +324,6 @@ static struct z3fold_header *init_z3fold_page(struct page *page, bool headless, struct z3fold_header *zhdr = page_address(page); struct z3fold_buddy_slots *slots; - INIT_LIST_HEAD(&page->lru); clear_bit(PAGE_HEADLESS, &page->private); clear_bit(MIDDLE_CHUNK_MAPPED, &page->private); clear_bit(NEEDS_COMPACTING, &page->private); @@ -451,8 +445,6 @@ static void __release_z3fold_page(struct z3fold_header *zhdr, bool locked) set_bit(PAGE_STALE, &page->private); clear_bit(NEEDS_COMPACTING, &page->private); spin_lock(&pool->lock); - if (!list_empty(&page->lru)) - list_del_init(&page->lru); spin_unlock(&pool->lock); if (locked) @@ -930,7 +922,6 @@ static struct z3fold_pool *z3fold_create_pool(const char *name, gfp_t gfp) for_each_unbuddied_list(i, 0) INIT_LIST_HEAD(&unbuddied[i]); } - INIT_LIST_HEAD(&pool->lru); INIT_LIST_HEAD(&pool->stale); atomic64_set(&pool->pages_nr, 0); pool->name = name; @@ -1073,12 +1064,6 @@ static int z3fold_alloc(struct z3fold_pool *pool, size_t size, gfp_t gfp, headless: spin_lock(&pool->lock); - /* Add/move z3fold page to beginning of LRU */ - if (!list_empty(&page->lru)) - list_del(&page->lru); - - list_add(&page->lru, &pool->lru); - *handle = encode_handle(zhdr, bud); spin_unlock(&pool->lock); if (bud != HEADLESS) @@ -1115,9 +1100,6 @@ static void z3fold_free(struct z3fold_pool *pool, unsigned long handle) * immediately so we don't care about its value any more. */ if (!page_claimed) { - spin_lock(&pool->lock); - list_del(&page->lru); - spin_unlock(&pool->lock); put_z3fold_header(zhdr); free_z3fold_page(page, true); atomic64_dec(&pool->pages_nr); @@ -1172,194 +1154,6 @@ static void z3fold_free(struct z3fold_pool *pool, unsigned long handle) put_z3fold_header(zhdr); } -/** - * z3fold_reclaim_page() - evicts allocations from a pool page and frees it - * @pool: pool from which a page will attempt to be evicted - * @retries: number of pages on the LRU list for which eviction will - * be attempted before failing - * - * z3fold reclaim is different from normal system reclaim in that it is done - * from the bottom, up. This is because only the bottom layer, z3fold, has - * information on how the allocations are organized within each z3fold page. - * This has the potential to create interesting locking situations between - * z3fold and the user, however. - * - * To avoid these, this is how z3fold_reclaim_page() should be called: - * - * The user detects a page should be reclaimed and calls z3fold_reclaim_page(). - * z3fold_reclaim_page() will remove a z3fold page from the pool LRU list and - * call the user-defined eviction handler with the pool and handle as - * arguments. - * - * If the handle can not be evicted, the eviction handler should return - * non-zero. z3fold_reclaim_page() will add the z3fold page back to the - * appropriate list and try the next z3fold page on the LRU up to - * a user defined number of retries. - * - * If the handle is successfully evicted, the eviction handler should - * return 0 _and_ should have called z3fold_free() on the handle. z3fold_free() - * contains logic to delay freeing the page if the page is under reclaim, - * as indicated by the setting of the PG_reclaim flag on the underlying page. - * - * If all buddies in the z3fold page are successfully evicted, then the - * z3fold page can be freed. - * - * Returns: 0 if page is successfully freed, otherwise -EINVAL if there are - * no pages to evict or an eviction handler is not registered, -EAGAIN if - * the retry limit was hit. - */ -static int z3fold_reclaim_page(struct z3fold_pool *pool, unsigned int retries) -{ - int i, ret = -1; - struct z3fold_header *zhdr = NULL; - struct page *page = NULL; - struct list_head *pos; - unsigned long first_handle = 0, middle_handle = 0, last_handle = 0; - struct z3fold_buddy_slots slots __attribute__((aligned(SLOTS_ALIGN))); - - rwlock_init(&slots.lock); - slots.pool = (unsigned long)pool | (1 << HANDLES_NOFREE); - - spin_lock(&pool->lock); - for (i = 0; i < retries; i++) { - if (list_empty(&pool->lru)) { - spin_unlock(&pool->lock); - return -EINVAL; - } - list_for_each_prev(pos, &pool->lru) { - page = list_entry(pos, struct page, lru); - - zhdr = page_address(page); - if (test_bit(PAGE_HEADLESS, &page->private)) { - /* - * For non-headless pages, we wait to do this - * until we have the page lock to avoid racing - * with __z3fold_alloc(). Headless pages don't - * have a lock (and __z3fold_alloc() will never - * see them), but we still need to test and set - * PAGE_CLAIMED to avoid racing with - * z3fold_free(), so just do it now before - * leaving the loop. - */ - if (test_and_set_bit(PAGE_CLAIMED, &page->private)) - continue; - - break; - } - - if (!z3fold_page_trylock(zhdr)) { - zhdr = NULL; - continue; /* can't evict at this point */ - } - - /* test_and_set_bit is of course atomic, but we still - * need to do it under page lock, otherwise checking - * that bit in __z3fold_alloc wouldn't make sense - */ - if (zhdr->foreign_handles || - test_and_set_bit(PAGE_CLAIMED, &page->private)) { - z3fold_page_unlock(zhdr); - zhdr = NULL; - continue; /* can't evict such page */ - } - list_del_init(&zhdr->buddy); - zhdr->cpu = -1; - /* See comment in __z3fold_alloc. */ - kref_get(&zhdr->refcount); - break; - } - - if (!zhdr) - break; - - list_del_init(&page->lru); - spin_unlock(&pool->lock); - - if (!test_bit(PAGE_HEADLESS, &page->private)) { - /* - * We need encode the handles before unlocking, and - * use our local slots structure because z3fold_free - * can zero out zhdr->slots and we can't do much - * about that - */ - first_handle = 0; - last_handle = 0; - middle_handle = 0; - memset(slots.slot, 0, sizeof(slots.slot)); - if (zhdr->first_chunks) - first_handle = __encode_handle(zhdr, &slots, - FIRST); - if (zhdr->middle_chunks) - middle_handle = __encode_handle(zhdr, &slots, - MIDDLE); - if (zhdr->last_chunks) - last_handle = __encode_handle(zhdr, &slots, - LAST); - /* - * it's safe to unlock here because we hold a - * reference to this page - */ - z3fold_page_unlock(zhdr); - } else { - first_handle = encode_handle(zhdr, HEADLESS); - last_handle = middle_handle = 0; - } - /* Issue the eviction callback(s) */ - if (middle_handle) { - ret = pool->zpool_ops->evict(pool->zpool, middle_handle); - if (ret) - goto next; - } - if (first_handle) { - ret = pool->zpool_ops->evict(pool->zpool, first_handle); - if (ret) - goto next; - } - if (last_handle) { - ret = pool->zpool_ops->evict(pool->zpool, last_handle); - if (ret) - goto next; - } -next: - if (test_bit(PAGE_HEADLESS, &page->private)) { - if (ret == 0) { - free_z3fold_page(page, true); - atomic64_dec(&pool->pages_nr); - return 0; - } - spin_lock(&pool->lock); - list_add(&page->lru, &pool->lru); - spin_unlock(&pool->lock); - clear_bit(PAGE_CLAIMED, &page->private); - } else { - struct z3fold_buddy_slots *slots = zhdr->slots; - z3fold_page_lock(zhdr); - if (kref_put(&zhdr->refcount, - release_z3fold_page_locked)) { - kmem_cache_free(pool->c_handle, slots); - return 0; - } - /* - * if we are here, the page is still not completely - * free. Take the global pool lock then to be able - * to add it back to the lru list - */ - spin_lock(&pool->lock); - list_add(&page->lru, &pool->lru); - spin_unlock(&pool->lock); - if (list_empty(&zhdr->buddy)) - add_to_unbuddied(pool, zhdr); - clear_bit(PAGE_CLAIMED, &page->private); - z3fold_page_unlock(zhdr); - } - - /* We started off locked to we need to lock the pool back */ - spin_lock(&pool->lock); - } - spin_unlock(&pool->lock); - return -EAGAIN; -} - /** * z3fold_map() - maps the allocation associated with the given handle * @pool: pool in which the allocation resides @@ -1470,8 +1264,6 @@ static bool z3fold_page_isolate(struct page *page, isolate_mode_t mode) spin_lock(&pool->lock); if (!list_empty(&zhdr->buddy)) list_del_init(&zhdr->buddy); - if (!list_empty(&page->lru)) - list_del_init(&page->lru); spin_unlock(&pool->lock); kref_get(&zhdr->refcount); @@ -1531,9 +1323,6 @@ static int z3fold_page_migrate(struct page *newpage, struct page *page, encode_handle(new_zhdr, MIDDLE); set_bit(NEEDS_COMPACTING, &newpage->private); new_zhdr->cpu = smp_processor_id(); - spin_lock(&pool->lock); - list_add(&newpage->lru, &pool->lru); - spin_unlock(&pool->lock); __SetPageMovable(newpage, &z3fold_mops); z3fold_page_unlock(new_zhdr); @@ -1559,9 +1348,6 @@ static void z3fold_page_putback(struct page *page) INIT_LIST_HEAD(&page->lru); if (kref_put(&zhdr->refcount, release_z3fold_page_locked)) return; - spin_lock(&pool->lock); - list_add(&page->lru, &pool->lru); - spin_unlock(&pool->lock); if (list_empty(&zhdr->buddy)) add_to_unbuddied(pool, zhdr); clear_bit(PAGE_CLAIMED, &page->private); @@ -1582,14 +1368,7 @@ static void *z3fold_zpool_create(const char *name, gfp_t gfp, const struct zpool_ops *zpool_ops, struct zpool *zpool) { - struct z3fold_pool *pool; - - pool = z3fold_create_pool(name, gfp); - if (pool) { - pool->zpool = zpool; - pool->zpool_ops = zpool_ops; - } - return pool; + return z3fold_create_pool(name, gfp); } static void z3fold_zpool_destroy(void *pool) @@ -1607,25 +1386,6 @@ static void z3fold_zpool_free(void *pool, unsigned long handle) z3fold_free(pool, handle); } -static int z3fold_zpool_shrink(void *pool, unsigned int pages, - unsigned int *reclaimed) -{ - unsigned int total = 0; - int ret = -EINVAL; - - while (total < pages) { - ret = z3fold_reclaim_page(pool, 8); - if (ret < 0) - break; - total++; - } - - if (reclaimed) - *reclaimed = total; - - return ret; -} - static void *z3fold_zpool_map(void *pool, unsigned long handle, enum zpool_mapmode mm) { @@ -1649,7 +1409,6 @@ static struct zpool_driver z3fold_zpool_driver = { .destroy = z3fold_zpool_destroy, .malloc = z3fold_zpool_malloc, .free = z3fold_zpool_free, - .shrink = z3fold_zpool_shrink, .map = z3fold_zpool_map, .unmap = z3fold_zpool_unmap, .total_size = z3fold_zpool_total_size, From patchwork Mon Jun 12 09:38:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Domenico Cerasuolo X-Patchwork-Id: 13275955 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 B9FC2C7EE2E for ; Mon, 12 Jun 2023 09:38:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CEAE08E0006; Mon, 12 Jun 2023 05:38:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BFBC06B0075; Mon, 12 Jun 2023 05:38:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A27F48E0006; Mon, 12 Jun 2023 05:38:29 -0400 (EDT) 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 83AE46B0074 for ; Mon, 12 Jun 2023 05:38:29 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 6202A160169 for ; Mon, 12 Jun 2023 09:38:29 +0000 (UTC) X-FDA: 80893595538.24.CB900CC Received: from mail-ed1-f53.google.com (mail-ed1-f53.google.com [209.85.208.53]) by imf16.hostedemail.com (Postfix) with ESMTP id 6647C180014 for ; Mon, 12 Jun 2023 09:38:27 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=VXHkwjjY; spf=pass (imf16.hostedemail.com: domain of cerasuolodomenico@gmail.com designates 209.85.208.53 as permitted sender) smtp.mailfrom=cerasuolodomenico@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686562707; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Qqabn+5EZnE3xDBV/hK/uIK9ftICUILiN887bfpMl50=; b=wE0t3mD2wXJIMq1U0aWymJOrAYJbK4E1+WU+gkYmI+Kq1/zHM/Z8Q4Nmy2YNycO7sA8TjR E2bA9Qc+NAvylU9XOxYjopmEVWOqWwbrbLuVlKeDom9Xe7NIms4MeJI/HPZajT7YzKtHhK moue6GR4DoxgnigOsKo8OnF14r3XRWk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1686562707; a=rsa-sha256; cv=none; b=Evkx8yJ6MbLkvoUKR3mHiNKzfLniYJc1QPOTuGlGeClN+qmP8HqK+11YGNm1BmFcDMkWqo Pua1AzY+ICjBej7qRJUfq79wkTpmz9GcJBw80GyYRk3rOPez8vD3vntITaCoUPD14b2OBR SDJMYUQu6Ixx5f3hbRu+MUb/kUSZ6kE= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=VXHkwjjY; spf=pass (imf16.hostedemail.com: domain of cerasuolodomenico@gmail.com designates 209.85.208.53 as permitted sender) smtp.mailfrom=cerasuolodomenico@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-ed1-f53.google.com with SMTP id 4fb4d7f45d1cf-51458187be1so7383726a12.2 for ; Mon, 12 Jun 2023 02:38:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686562706; x=1689154706; 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=Qqabn+5EZnE3xDBV/hK/uIK9ftICUILiN887bfpMl50=; b=VXHkwjjYAYvgJn0o0mmj8tPgMol+yYARZ/ysRinnWzusuBZgC+a79n6wHSu8WEsx4p EAGxIa/ZXpDS2/l5rLYOQFi4I0qV3cE+prGitoYDeL8bi+QiOTBoYD3wK0FuVyr88G9e 5F8ZoW80SQFRoo/2WMD2/uN64+VYTE0hvwin3Op0ZXSAIDYWpME5qf8l51RJlJQk4P63 fucA6dWn1mgoPV1CdFbl7i4lUZP1ja+JLYMxwlTBCGvtaRnJCEnqtdVQx7hrcAwPh7hR W+g+ONUUpryQ0cQwLv6+JreSgNwJ8SLKMjFdH5biM9SppVCySh1omBRxEuOrlCMeG7rN myTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686562706; x=1689154706; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Qqabn+5EZnE3xDBV/hK/uIK9ftICUILiN887bfpMl50=; b=gWoZfghBQP2byoOx37R5y2Mj/wMbyH3nczQ6cLUuWHNbeK3Zjn0Mee9d+wCCDSVTa8 n/InlXblxI/+cMonx10G/2dryJ+ORh/SzCQq3rcbHk0OqNX6JKNZAWr3U7pWZAxk2FsS A5A2yg4ew7NbUsVW2Xjrk8CMdS8QLgK9I3rljEyKMn7CDqOdOa8UtAiVtI8LVNotS0/W JFtwI2kjpFHCSlyCnkL2RrxNsFMbYbubFTyyo6TtKnKcFiKp1HDBfegZCuUWsQsZO2EV DxfihQxVvkJcgWEdkwWEnyP8UHN6f6xV9xS8V/+pnanKqhDG3+TX9sWGkvLmfbQh+IAh g4Qw== X-Gm-Message-State: AC+VfDw7khGhAFwTsho2ZRj05fY6rt5+8j7ecpDSQl/31HJyVzL6ZIPN VA85Ol4uu0D9hMVoUssbKVY= X-Google-Smtp-Source: ACHHUZ74X9HGGClZK/hRGG7bOiLl3qlJW7y4hEvEU/q03AAT5pBayVQiFxfjnbxw8l8ogxtwH0/Tlg== X-Received: by 2002:a17:907:368d:b0:961:78c2:1d27 with SMTP id bi13-20020a170907368d00b0096178c21d27mr10471708ejc.19.1686562705698; Mon, 12 Jun 2023 02:38:25 -0700 (PDT) Received: from lelloman-5950.homenet.telecomitalia.it (host-212-171-43-8.retail.telecomitalia.it. [212.171.43.8]) by smtp.gmail.com with ESMTPSA id ce23-20020a170906b25700b0097887b68c17sm4951358ejb.98.2023.06.12.02.38.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Jun 2023 02:38:25 -0700 (PDT) From: Domenico Cerasuolo To: vitaly.wool@konsulko.com, minchan@kernel.org, senozhatsky@chromium.org, yosryahmed@google.com, linux-mm@kvack.org Cc: ddstreet@ieee.org, sjenning@redhat.com, nphamcs@gmail.com, hannes@cmpxchg.org, akpm@linux-foundation.org, linux-kernel@vger.kernel.org, kernel-team@meta.com, Domenico Cerasuolo Subject: [PATCH v3 4/7] mm: zswap: remove page reclaim logic from zsmalloc Date: Mon, 12 Jun 2023 11:38:12 +0200 Message-Id: <20230612093815.133504-5-cerasuolodomenico@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230612093815.133504-1-cerasuolodomenico@gmail.com> References: <20230612093815.133504-1-cerasuolodomenico@gmail.com> MIME-Version: 1.0 X-Stat-Signature: 67zpwz9csjw9webwybqji3kunkxrbya8 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 6647C180014 X-Rspam-User: X-HE-Tag: 1686562707-339054 X-HE-Meta: U2FsdGVkX1+yDl71lgYg/Tjy5dOoVZyjYoVJQMW1gDfKyN9gsE5PVllNZZMjPSelXfVuSGcz8i3zj39887pF36rS1mPdMy/5nkAH2QhyfpfFQjfM05pXN0wcqxZrYTGLoeajd0MIlNcQ8kjdoUwZV0de5ra5Tge4Zta68hnOaXm5eg6JPiHf2IIy5I/yGDwvPGESjUW5pzRgADXt0saiJAOKevfiSeVSH1L2Zpu0zcrW/uHi3ceFkAWugF70Op7/ToX8mUdituUvOTIVkXAboYYiQUTosMyvIwMQKwpV3u0I2EcyOyaSOvO8lPlb6NtgflNW7k5k2XL6OdiZsZGaj7YEHDiCghxCDyrxfFhFz2bScvRdV9dv9ioemtk/UjQp1bLmC5AugXjS0x6Gh28XH/LNMMXUSU73FKKiMcM+3UEEDT5FhecMjKR8pybqqJ0wzB7etEtKwkymKYKhxJY5sXQdUuxFwIJckRwOrehAJklRjous+x7/sFBhJ+4HKPygnpFx7aO9xBSVV0tivr0tReH/HhdZQtYgIOQoQr4kwvqaouYEPNFOnFc2Ic346dAS9IHybFBcMjz5sfLG1JSc1IYU351fGFszT/4Hu/Qom9Ss3e2L6NyKqXByAb4m8Fgh2kiyfvbF85EJ/gy0QkQb+snULS0VxLZeVlWK3hOjgnMq0LXJeiHyCjCDWwyiJDRXy5/XNjwnDKJB1q355SiPH+rOhqAGd+/MHSI+y64EaTDT6cxPY+t1DrbUvJasnBrPtXWFIjmrjcEN5ig8I4pm94NHnXc9600fOdeceGXVgn1fduQICwcdFmUNjnlplDm9pHWs8JVhGptXpYxBj86QxoF8lR7G4naVmrM36Kt0w02vTgKN4tepZZk5z54CdimPAS1sbG8RmM9Q0jp7HDaPaLQmR56z9FpKwNV/0lfGRsjvRWwkVuNqzTIAiOGmp5Obkttr1Pugeic5V69ezKN YNL0rbNg eahv1CPKcAWVCzIcej2nTl4NcVB4la++Ep22KNcQppWP/afx5QhZJdFGiKPjuXACpXSKx3QSMijZh9GBZ/x23260yyjjAn+LQb96W/pGRWBli4yVevTrCYpsX5qeFd/sjjKwk/Vwl1l9j5DqSMybF3mTNkQ0GHzPZ4zeZXRD71Q94lJDh9qT7mjbonbodJiadgVULK3yVpA4r4p9uw/HJ+ODqm4qkoXbM7jtQgfOGAmDTph6+GvDhprg5QAnG57pEG0h+LSaaoAFG/Drd63QtmL+etvNrGMlFRHhJBRexUUbxe5KQQXT7YOea+dm7mS1NMN4jSuBqxVTKAhA7UL8Aktok/dkN31qacT8hhOYEF3jA77D5ciAkHGA9PGrnRZRv8msSXmReqRbH35bcux+QKmdic6dtPRaVRTmrILz90c+ODOoluREyVqhvUshPL70Ch9Le1rnwqQ7G94y/LGByDENrOGLDFWl2BkAcb660hP+cwB0TwZ7hKos5ROS/US8saagnJDNIyAM9fERO3wcHdUdSIYS5mjFJwA+HBLxINedFv74fX2Z0k8Ovcf/yR8b8ORh5S9X+vwR9HzJB73dDAnQTNA== 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: Switch zsmalloc to the new generic zswap LRU and remove its custom implementation. Acked-by: Johannes Weiner Acked-by: Nhat Pham Acked-by: Minchan Kim Tested-by: Yosry Ahmed Signed-off-by: Domenico Cerasuolo --- mm/zsmalloc.c | 392 ++------------------------------------------------ 1 file changed, 12 insertions(+), 380 deletions(-) diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index c0d433541636..e4d1ad521738 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -107,21 +107,8 @@ */ #define OBJ_ALLOCATED_TAG 1 -#ifdef CONFIG_ZPOOL -/* - * The second least-significant bit in the object's header identifies if the - * value stored at the header is a deferred handle from the last reclaim - * attempt. - * - * As noted above, this is valid because we have room for two bits. - */ -#define OBJ_DEFERRED_HANDLE_TAG 2 -#define OBJ_TAG_BITS 2 -#define OBJ_TAG_MASK (OBJ_ALLOCATED_TAG | OBJ_DEFERRED_HANDLE_TAG) -#else #define OBJ_TAG_BITS 1 #define OBJ_TAG_MASK OBJ_ALLOCATED_TAG -#endif /* CONFIG_ZPOOL */ #define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS - OBJ_TAG_BITS) #define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1) @@ -227,12 +214,6 @@ struct link_free { * Handle of allocated object. */ unsigned long handle; -#ifdef CONFIG_ZPOOL - /* - * Deferred handle of a reclaimed object. - */ - unsigned long deferred_handle; -#endif }; }; @@ -250,13 +231,6 @@ struct zs_pool { /* Compact classes */ struct shrinker shrinker; -#ifdef CONFIG_ZPOOL - /* List tracking the zspages in LRU order by most recently added object */ - struct list_head lru; - struct zpool *zpool; - const struct zpool_ops *zpool_ops; -#endif - #ifdef CONFIG_ZSMALLOC_STAT struct dentry *stat_dentry; #endif @@ -279,13 +253,6 @@ struct zspage { unsigned int freeobj; struct page *first_page; struct list_head list; /* fullness list */ - -#ifdef CONFIG_ZPOOL - /* links the zspage to the lru list in the pool */ - struct list_head lru; - bool under_reclaim; -#endif - struct zs_pool *pool; rwlock_t lock; }; @@ -393,14 +360,7 @@ static void *zs_zpool_create(const char *name, gfp_t gfp, * different contexts and its caller must provide a valid * gfp mask. */ - struct zs_pool *pool = zs_create_pool(name); - - if (pool) { - pool->zpool = zpool; - pool->zpool_ops = zpool_ops; - } - - return pool; + return zs_create_pool(name); } static void zs_zpool_destroy(void *pool) @@ -422,27 +382,6 @@ static void zs_zpool_free(void *pool, unsigned long handle) zs_free(pool, handle); } -static int zs_reclaim_page(struct zs_pool *pool, unsigned int retries); - -static int zs_zpool_shrink(void *pool, unsigned int pages, - unsigned int *reclaimed) -{ - unsigned int total = 0; - int ret = -EINVAL; - - while (total < pages) { - ret = zs_reclaim_page(pool, 8); - if (ret < 0) - break; - total++; - } - - if (reclaimed) - *reclaimed = total; - - return ret; -} - static void *zs_zpool_map(void *pool, unsigned long handle, enum zpool_mapmode mm) { @@ -481,7 +420,6 @@ static struct zpool_driver zs_zpool_driver = { .malloc_support_movable = true, .malloc = zs_zpool_malloc, .free = zs_zpool_free, - .shrink = zs_zpool_shrink, .map = zs_zpool_map, .unmap = zs_zpool_unmap, .total_size = zs_zpool_total_size, @@ -884,14 +822,6 @@ static inline bool obj_allocated(struct page *page, void *obj, unsigned long *ph return obj_tagged(page, obj, phandle, OBJ_ALLOCATED_TAG); } -#ifdef CONFIG_ZPOOL -static bool obj_stores_deferred_handle(struct page *page, void *obj, - unsigned long *phandle) -{ - return obj_tagged(page, obj, phandle, OBJ_DEFERRED_HANDLE_TAG); -} -#endif - static void reset_page(struct page *page) { __ClearPageMovable(page); @@ -922,39 +852,6 @@ static int trylock_zspage(struct zspage *zspage) return 0; } -#ifdef CONFIG_ZPOOL -static unsigned long find_deferred_handle_obj(struct size_class *class, - struct page *page, int *obj_idx); - -/* - * Free all the deferred handles whose objects are freed in zs_free. - */ -static void free_handles(struct zs_pool *pool, struct size_class *class, - struct zspage *zspage) -{ - int obj_idx = 0; - struct page *page = get_first_page(zspage); - unsigned long handle; - - while (1) { - handle = find_deferred_handle_obj(class, page, &obj_idx); - if (!handle) { - page = get_next_page(page); - if (!page) - break; - obj_idx = 0; - continue; - } - - cache_free_handle(pool, handle); - obj_idx++; - } -} -#else -static inline void free_handles(struct zs_pool *pool, struct size_class *class, - struct zspage *zspage) {} -#endif - static void __free_zspage(struct zs_pool *pool, struct size_class *class, struct zspage *zspage) { @@ -969,9 +866,6 @@ static void __free_zspage(struct zs_pool *pool, struct size_class *class, VM_BUG_ON(get_zspage_inuse(zspage)); VM_BUG_ON(fg != ZS_INUSE_RATIO_0); - /* Free all deferred handles from zs_free */ - free_handles(pool, class, zspage); - next = page = get_first_page(zspage); do { VM_BUG_ON_PAGE(!PageLocked(page), page); @@ -1006,9 +900,6 @@ static void free_zspage(struct zs_pool *pool, struct size_class *class, } remove_zspage(class, zspage, ZS_INUSE_RATIO_0); -#ifdef CONFIG_ZPOOL - list_del(&zspage->lru); -#endif __free_zspage(pool, class, zspage); } @@ -1054,11 +945,6 @@ static void init_zspage(struct size_class *class, struct zspage *zspage) off %= PAGE_SIZE; } -#ifdef CONFIG_ZPOOL - INIT_LIST_HEAD(&zspage->lru); - zspage->under_reclaim = false; -#endif - set_freeobj(zspage, 0); } @@ -1525,20 +1411,13 @@ unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp) /* We completely set up zspage so mark them as movable */ SetZsPageMovable(pool, zspage); out: -#ifdef CONFIG_ZPOOL - /* Add/move zspage to beginning of LRU */ - if (!list_empty(&zspage->lru)) - list_del(&zspage->lru); - list_add(&zspage->lru, &pool->lru); -#endif - spin_unlock(&pool->lock); return handle; } EXPORT_SYMBOL_GPL(zs_malloc); -static void obj_free(int class_size, unsigned long obj, unsigned long *handle) +static void obj_free(int class_size, unsigned long obj) { struct link_free *link; struct zspage *zspage; @@ -1554,25 +1433,12 @@ static void obj_free(int class_size, unsigned long obj, unsigned long *handle) vaddr = kmap_atomic(f_page); link = (struct link_free *)(vaddr + f_offset); - if (handle) { -#ifdef CONFIG_ZPOOL - /* Stores the (deferred) handle in the object's header */ - *handle |= OBJ_DEFERRED_HANDLE_TAG; - *handle &= ~OBJ_ALLOCATED_TAG; - - if (likely(!ZsHugePage(zspage))) - link->deferred_handle = *handle; - else - f_page->index = *handle; -#endif - } else { - /* Insert this object in containing zspage's freelist */ - if (likely(!ZsHugePage(zspage))) - link->next = get_freeobj(zspage) << OBJ_TAG_BITS; - else - f_page->index = 0; - set_freeobj(zspage, f_objidx); - } + /* Insert this object in containing zspage's freelist */ + if (likely(!ZsHugePage(zspage))) + link->next = get_freeobj(zspage) << OBJ_TAG_BITS; + else + f_page->index = 0; + set_freeobj(zspage, f_objidx); kunmap_atomic(vaddr); mod_zspage_inuse(zspage, -1); @@ -1600,21 +1466,7 @@ void zs_free(struct zs_pool *pool, unsigned long handle) class = zspage_class(pool, zspage); class_stat_dec(class, ZS_OBJS_INUSE, 1); - -#ifdef CONFIG_ZPOOL - if (zspage->under_reclaim) { - /* - * Reclaim needs the handles during writeback. It'll free - * them along with the zspage when it's done with them. - * - * Record current deferred handle in the object's header. - */ - obj_free(class->size, obj, &handle); - spin_unlock(&pool->lock); - return; - } -#endif - obj_free(class->size, obj, NULL); + obj_free(class->size, obj); fullness = fix_fullness_group(class, zspage); if (fullness == ZS_INUSE_RATIO_0) @@ -1735,18 +1587,6 @@ static unsigned long find_alloced_obj(struct size_class *class, return find_tagged_obj(class, page, obj_idx, OBJ_ALLOCATED_TAG); } -#ifdef CONFIG_ZPOOL -/* - * Find object storing a deferred handle in header in zspage from index object - * and return handle. - */ -static unsigned long find_deferred_handle_obj(struct size_class *class, - struct page *page, int *obj_idx) -{ - return find_tagged_obj(class, page, obj_idx, OBJ_DEFERRED_HANDLE_TAG); -} -#endif - struct zs_compact_control { /* Source spage for migration which could be a subpage of zspage */ struct page *s_page; @@ -1786,7 +1626,7 @@ static void migrate_zspage(struct zs_pool *pool, struct size_class *class, zs_object_copy(class, free_obj, used_obj); obj_idx++; record_obj(handle, free_obj); - obj_free(class->size, used_obj, NULL); + obj_free(class->size, used_obj); } /* Remember last position in this iteration */ @@ -1846,7 +1686,7 @@ static int putback_zspage(struct size_class *class, struct zspage *zspage) return fullness; } -#if defined(CONFIG_ZPOOL) || defined(CONFIG_COMPACTION) +#ifdef CONFIG_COMPACTION /* * To prevent zspage destroy during migration, zspage freeing should * hold locks of all pages in the zspage. @@ -1888,24 +1728,7 @@ static void lock_zspage(struct zspage *zspage) } migrate_read_unlock(zspage); } -#endif /* defined(CONFIG_ZPOOL) || defined(CONFIG_COMPACTION) */ - -#ifdef CONFIG_ZPOOL -/* - * Unlocks all the pages of the zspage. - * - * pool->lock must be held before this function is called - * to prevent the underlying pages from migrating. - */ -static void unlock_zspage(struct zspage *zspage) -{ - struct page *page = get_first_page(zspage); - - do { - unlock_page(page); - } while ((page = get_next_page(page)) != NULL); -} -#endif /* CONFIG_ZPOOL */ +#endif /* CONFIG_COMPACTION */ static void migrate_lock_init(struct zspage *zspage) { @@ -2126,9 +1949,6 @@ static void async_free_zspage(struct work_struct *work) VM_BUG_ON(fullness != ZS_INUSE_RATIO_0); class = pool->size_class[class_idx]; spin_lock(&pool->lock); -#ifdef CONFIG_ZPOOL - list_del(&zspage->lru); -#endif __free_zspage(pool, class, zspage); spin_unlock(&pool->lock); } @@ -2474,10 +2294,6 @@ struct zs_pool *zs_create_pool(const char *name) */ zs_register_shrinker(pool); -#ifdef CONFIG_ZPOOL - INIT_LIST_HEAD(&pool->lru); -#endif - return pool; err: @@ -2520,190 +2336,6 @@ void zs_destroy_pool(struct zs_pool *pool) } EXPORT_SYMBOL_GPL(zs_destroy_pool); -#ifdef CONFIG_ZPOOL -static void restore_freelist(struct zs_pool *pool, struct size_class *class, - struct zspage *zspage) -{ - unsigned int obj_idx = 0; - unsigned long handle, off = 0; /* off is within-page offset */ - struct page *page = get_first_page(zspage); - struct link_free *prev_free = NULL; - void *prev_page_vaddr = NULL; - - /* in case no free object found */ - set_freeobj(zspage, (unsigned int)(-1UL)); - - while (page) { - void *vaddr = kmap_atomic(page); - struct page *next_page; - - while (off < PAGE_SIZE) { - void *obj_addr = vaddr + off; - - /* skip allocated object */ - if (obj_allocated(page, obj_addr, &handle)) { - obj_idx++; - off += class->size; - continue; - } - - /* free deferred handle from reclaim attempt */ - if (obj_stores_deferred_handle(page, obj_addr, &handle)) - cache_free_handle(pool, handle); - - if (prev_free) - prev_free->next = obj_idx << OBJ_TAG_BITS; - else /* first free object found */ - set_freeobj(zspage, obj_idx); - - prev_free = (struct link_free *)vaddr + off / sizeof(*prev_free); - /* if last free object in a previous page, need to unmap */ - if (prev_page_vaddr) { - kunmap_atomic(prev_page_vaddr); - prev_page_vaddr = NULL; - } - - obj_idx++; - off += class->size; - } - - /* - * Handle the last (full or partial) object on this page. - */ - next_page = get_next_page(page); - if (next_page) { - if (!prev_free || prev_page_vaddr) { - /* - * There is no free object in this page, so we can safely - * unmap it. - */ - kunmap_atomic(vaddr); - } else { - /* update prev_page_vaddr since prev_free is on this page */ - prev_page_vaddr = vaddr; - } - } else { /* this is the last page */ - if (prev_free) { - /* - * Reset OBJ_TAG_BITS bit to last link to tell - * whether it's allocated object or not. - */ - prev_free->next = -1UL << OBJ_TAG_BITS; - } - - /* unmap previous page (if not done yet) */ - if (prev_page_vaddr) { - kunmap_atomic(prev_page_vaddr); - prev_page_vaddr = NULL; - } - - kunmap_atomic(vaddr); - } - - page = next_page; - off %= PAGE_SIZE; - } -} - -static int zs_reclaim_page(struct zs_pool *pool, unsigned int retries) -{ - int i, obj_idx, ret = 0; - unsigned long handle; - struct zspage *zspage; - struct page *page; - int fullness; - - /* Lock LRU and fullness list */ - spin_lock(&pool->lock); - if (list_empty(&pool->lru)) { - spin_unlock(&pool->lock); - return -EINVAL; - } - - for (i = 0; i < retries; i++) { - struct size_class *class; - - zspage = list_last_entry(&pool->lru, struct zspage, lru); - list_del(&zspage->lru); - - /* zs_free may free objects, but not the zspage and handles */ - zspage->under_reclaim = true; - - class = zspage_class(pool, zspage); - fullness = get_fullness_group(class, zspage); - - /* Lock out object allocations and object compaction */ - remove_zspage(class, zspage, fullness); - - spin_unlock(&pool->lock); - cond_resched(); - - /* Lock backing pages into place */ - lock_zspage(zspage); - - obj_idx = 0; - page = get_first_page(zspage); - while (1) { - handle = find_alloced_obj(class, page, &obj_idx); - if (!handle) { - page = get_next_page(page); - if (!page) - break; - obj_idx = 0; - continue; - } - - /* - * This will write the object and call zs_free. - * - * zs_free will free the object, but the - * under_reclaim flag prevents it from freeing - * the zspage altogether. This is necessary so - * that we can continue working with the - * zspage potentially after the last object - * has been freed. - */ - ret = pool->zpool_ops->evict(pool->zpool, handle); - if (ret) - goto next; - - obj_idx++; - } - -next: - /* For freeing the zspage, or putting it back in the pool and LRU list. */ - spin_lock(&pool->lock); - zspage->under_reclaim = false; - - if (!get_zspage_inuse(zspage)) { - /* - * Fullness went stale as zs_free() won't touch it - * while the page is removed from the pool. Fix it - * up for the check in __free_zspage(). - */ - zspage->fullness = ZS_INUSE_RATIO_0; - - __free_zspage(pool, class, zspage); - spin_unlock(&pool->lock); - return 0; - } - - /* - * Eviction fails on one of the handles, so we need to restore zspage. - * We need to rebuild its freelist (and free stored deferred handles), - * put it back to the correct size class, and add it to the LRU list. - */ - restore_freelist(pool, class, zspage); - putback_zspage(class, zspage); - list_add(&zspage->lru, &pool->lru); - unlock_zspage(zspage); - } - - spin_unlock(&pool->lock); - return -EAGAIN; -} -#endif /* CONFIG_ZPOOL */ - static int __init zs_init(void) { int ret; From patchwork Mon Jun 12 09:38:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Domenico Cerasuolo X-Patchwork-Id: 13275956 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 65682C7EE25 for ; Mon, 12 Jun 2023 09:38:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CC2FB6B0074; Mon, 12 Jun 2023 05:38:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C70F08E0007; Mon, 12 Jun 2023 05:38:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A27216B0078; Mon, 12 Jun 2023 05:38:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 88D666B0074 for ; Mon, 12 Jun 2023 05:38:30 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 4C666AF1CA for ; Mon, 12 Jun 2023 09:38:30 +0000 (UTC) X-FDA: 80893595580.16.B8B9337 Received: from mail-ed1-f45.google.com (mail-ed1-f45.google.com [209.85.208.45]) by imf14.hostedemail.com (Postfix) with ESMTP id 74D0510000E for ; Mon, 12 Jun 2023 09:38:28 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=ir5X+fcV; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf14.hostedemail.com: domain of cerasuolodomenico@gmail.com designates 209.85.208.45 as permitted sender) smtp.mailfrom=cerasuolodomenico@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686562708; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=xTNeAR1y7R/gjs/C8GIclCvhUGvklnuLgBw6XnZGDXw=; b=nQf+Deep34OaV9x1Pb3t0yV9L+Kji2BkC3XiI2/SF9KFl4YTaDRUpRjMMDg0zEypBMV8Nx yXwciYPTjdMDUO5CheweoGj0VkFBx1twPhekU4FruK+kK8j2pAB8kwZl1h0yGmaO8GzLfU U+Ox8D74peA+68wofulAK2MKnf6k5dQ= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=ir5X+fcV; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf14.hostedemail.com: domain of cerasuolodomenico@gmail.com designates 209.85.208.45 as permitted sender) smtp.mailfrom=cerasuolodomenico@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1686562708; a=rsa-sha256; cv=none; b=pFxr+K5/W+lxO8W+NAYy5lc+sw7BgC60HW+AVMO6BwNzhMP6TSVpgpPYBzkkuXPeR+NOVH CewxxH0uBmBh0K3/djOqW6yNAIV+11U5e4KmPk7ix77kTm3JFrp5I86sgDmCsGUc3jxZDK 8sPXjCJK8Gt0jUNZK1Ga/OKlVpLgYls= Received: by mail-ed1-f45.google.com with SMTP id 4fb4d7f45d1cf-5151934a4e3so6139582a12.1 for ; Mon, 12 Jun 2023 02:38:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686562707; x=1689154707; 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=xTNeAR1y7R/gjs/C8GIclCvhUGvklnuLgBw6XnZGDXw=; b=ir5X+fcVOPWPnflY9pVtwguKK2ey3SgZuOxuv2ZOkxFDqKz73VdIUv1QOgSTeQaljT UI/fWwyMUUy4mleSW3jXzJ74tzNZF19bZFTmt17x5Id0f13/bZBftEI56XJ++JOpA8ye ZGKSDaJf4MJPj+7yZpVhH9ltSaxKkJf+f19A5v0LJstxmdhz4VrtLOIGrPv8NCzqDVJU wsFfWr8DdsxgONEWJGL0Z8zcEj4XON7Uli+htyIuwZQoIxq09NT4savqJ8upjsIfZYZW 0U2GJPSDw1W1+xKUHWicjr9kM+QlOmdT3nAsSC8E3BKBgSTA8qDe6gylqPzp8N6/Ddcv RdFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686562707; x=1689154707; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=xTNeAR1y7R/gjs/C8GIclCvhUGvklnuLgBw6XnZGDXw=; b=BRPJ9xY4llCY323s1870k4ISM7vtDIEFnjKcAjk5qBqYsgRQqoiq1HNcaLpkXxJBjM m+7KxZ9yGut0Dz+IBTp4UbuGMrOwrSNMSQ8s7/HeEpinFJNoLUNFhTTYeQolKop1I/yw wEnl4KrtJb3J6aH+45OfIRJDwfw+KF4Vkob/X5Ft2xKpQ6A7SLn9w/ZdqqR+JN00PrPQ G0NFq6CNyb0vuO86x4RamSbdQpopmBcw2YjoBwO2Qct1/gULZ6pYPt46QeEK7lVeC6MY CtPtpU7sV0g/HnKz5YMWmflwQZqSqyupNnGhqIOwAvnEDuCJYVSp6YOTxQwnt6DhYIvr e/3g== X-Gm-Message-State: AC+VfDwQPm1sI3niY5DsoDaVaG4CACcEWexWsYjj0Lktf77fflyENhmQ 65blGBHK5UEKdPEi/r+GuhU= X-Google-Smtp-Source: ACHHUZ7yW28J5ffg2/9V/86Xlp2i9s2l/FUpHWl5+YPp3E42r82mnAA7eI63Vhw/XCRR6E3f1h1G6w== X-Received: by 2002:a17:906:da87:b0:974:1c91:a752 with SMTP id xh7-20020a170906da8700b009741c91a752mr8922130ejb.5.1686562707002; Mon, 12 Jun 2023 02:38:27 -0700 (PDT) Received: from lelloman-5950.homenet.telecomitalia.it (host-212-171-43-8.retail.telecomitalia.it. [212.171.43.8]) by smtp.gmail.com with ESMTPSA id ce23-20020a170906b25700b0097887b68c17sm4951358ejb.98.2023.06.12.02.38.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Jun 2023 02:38:26 -0700 (PDT) From: Domenico Cerasuolo To: vitaly.wool@konsulko.com, minchan@kernel.org, senozhatsky@chromium.org, yosryahmed@google.com, linux-mm@kvack.org Cc: ddstreet@ieee.org, sjenning@redhat.com, nphamcs@gmail.com, hannes@cmpxchg.org, akpm@linux-foundation.org, linux-kernel@vger.kernel.org, kernel-team@meta.com, Domenico Cerasuolo Subject: [PATCH v3 5/7] mm: zswap: remove shrink from zpool interface Date: Mon, 12 Jun 2023 11:38:13 +0200 Message-Id: <20230612093815.133504-6-cerasuolodomenico@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230612093815.133504-1-cerasuolodomenico@gmail.com> References: <20230612093815.133504-1-cerasuolodomenico@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 74D0510000E X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 9wxhg4wyfbc4hsjtw86nuwrbu7ao5b4g X-HE-Tag: 1686562708-593628 X-HE-Meta: U2FsdGVkX1/2onqs0PdQX8XzpiXeEFn/OmTbnMuTrjFd1GYKTAyvzSy9pLiaiWVOpkTihTIzofKgrYLt2UhdpYtzzvC0VdVcR+n6zy8Q/n8FcGbjXEcIrQvkQDy/xYswCAo9neCo4xU9R4Ts9S1NHWbrgXHp56C56eA/M7qJAPcgNsLvI55JXyvMFCBTamEv6qdxBlb7UAeJBuFuzTd030b7xfqrgS3VStVHJO8MP7xShk+omUvECPr3qByQi1rns0qIPdaS3UQLawZyVGdLt7XMxyJ1CNP3UGv0JrPgqRUamZD0bSXyAuX3sNXaswy4QZIvqtlO/3t4S4WS62vPftWv/SLMZVF1zmbsvexBf5VlOLfjQPP1I5guhB0KeSpmY2nkDbd2wuORT4ZEztYxYLKjelnxP8FIJTf1mvKmqeoBWQkXnSHWh2I/HR3SoYz66Sb8dMKx7jDx1hd66gT2e7fzrpddlyxyl7VtSjFHOtR3p6Md/3ZCFg8oaJKsmO1Vm6M1H51KPog37dDLxq/DJwtIYUUZpEKtS3Jm2ael089TBkyTFyzfdGlsCeTmY6j9U2VdqX3PJy0uTYmEGNmOQ4JVwWPFlik+CVRAplaSF+E1JyfRREVlWtaO4BE4wx9sOnkx7Q8mMHb1TsS/Mct4OE5FADohTRmxuSCsCCar7fvDkIqvseRcEdcl8eCkGWh8lZIVU+TMdcJyLBLsi7WAKF28IIOZ7WzHJ9EiO/GuNDyqpTONLF+VVJIdoMaiZIaUvaGvIuN45CJ0fZ8WOGbfMb4cwdEYlwnoYTn+NbudpDTs/VgzCTuzTjd8LGhWnyzr8GqA94o/7SGDiX7AwmCm+HlamXCehlEmq4fEPK9jgu/paWFZ6YuWWoeW60wuknh7/Q+NEUiK47lbKaAsplI4UxH3qodKm5LhFkOEi8qHYgLvjI1saf2IS7WbUHXTZwfC27iplczkcB8EtwjN8XG fCgM02a+ zlZlhzE/pkKPheRKIu0goEKmDFf9IDhRUah8SFYzNmimVyCqGsBmrV82CVCyoOKarfA7TUtAdeFLD6BE5SXHCqKC8VjdX9iMyIdQ9SO7m5ez4nfzrar/q55LbTpOx/xVVVprM9tI8lMWU1EHVltITX/+LliCw5yvti3ZRFRgDav6nnR+6/eLfRmm7Ta/Io9efFNTrLQ1OWm6088qyf5VPTC9nfPR1ka6iM/4BR2R0AXvftPjVQYiyZz2sUfErWhBDNoTbac7iygBe95sbZzugp6RXUSTRsgyDwNxA5cPZzWTbULDT6dnFCloHoJlz9w34XKZsiDdvcCusWZE46aL/i0gLzHlV74bITkBLpHHWee7BVA4GXZbPHnAuZ2cDmTCDtpPa97Xoj8RWf5jmnFRjPxbjl/7fEL+11wuvdh8ipI1xsENbbpF2fE1ECmgL86p74W01Il+53VN0DC0PuHt4i+VsmR0tuqIGY5iaB2gt9l5L6idPsfMsOtYWhaeNbtmc6B/jLRT5ArmzCET84EYMxTa5PJOLWIiepFls3enDvHP3t5yk+tR4dCYpEshvaJXerbSGMwRAVfcUTo2AkNFnY3qQAQ== 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: Now that all three zswap backends have removed their shrink code, it is no longer necessary for the zpool interface to include shrink/writeback endpoints. Acked-by: Nhat Pham Acked-by: Johannes Weiner Reviewed-by: Yosry Ahmed Signed-off-by: Domenico Cerasuolo --- include/linux/zpool.h | 20 ++---------------- mm/z3fold.c | 4 +--- mm/zbud.c | 4 +--- mm/zpool.c | 48 ++----------------------------------------- mm/zsmalloc.c | 4 +--- mm/zswap.c | 27 +++++++----------------- 6 files changed, 14 insertions(+), 93 deletions(-) diff --git a/include/linux/zpool.h b/include/linux/zpool.h index e8997010612a..3296438eec06 100644 --- a/include/linux/zpool.h +++ b/include/linux/zpool.h @@ -14,10 +14,6 @@ struct zpool; -struct zpool_ops { - int (*evict)(struct zpool *pool, unsigned long handle); -}; - /* * Control how a handle is mapped. It will be ignored if the * implementation does not support it. Its use is optional. @@ -39,8 +35,7 @@ enum zpool_mapmode { bool zpool_has_pool(char *type); -struct zpool *zpool_create_pool(const char *type, const char *name, - gfp_t gfp, const struct zpool_ops *ops); +struct zpool *zpool_create_pool(const char *type, const char *name, gfp_t gfp); const char *zpool_get_type(struct zpool *pool); @@ -53,9 +48,6 @@ int zpool_malloc(struct zpool *pool, size_t size, gfp_t gfp, void zpool_free(struct zpool *pool, unsigned long handle); -int zpool_shrink(struct zpool *pool, unsigned int pages, - unsigned int *reclaimed); - void *zpool_map_handle(struct zpool *pool, unsigned long handle, enum zpool_mapmode mm); @@ -72,7 +64,6 @@ u64 zpool_get_total_size(struct zpool *pool); * @destroy: destroy a pool. * @malloc: allocate mem from a pool. * @free: free mem from a pool. - * @shrink: shrink the pool. * @sleep_mapped: whether zpool driver can sleep during map. * @map: map a handle. * @unmap: unmap a handle. @@ -87,10 +78,7 @@ struct zpool_driver { atomic_t refcount; struct list_head list; - void *(*create)(const char *name, - gfp_t gfp, - const struct zpool_ops *ops, - struct zpool *zpool); + void *(*create)(const char *name, gfp_t gfp); void (*destroy)(void *pool); bool malloc_support_movable; @@ -98,9 +86,6 @@ struct zpool_driver { unsigned long *handle); void (*free)(void *pool, unsigned long handle); - int (*shrink)(void *pool, unsigned int pages, - unsigned int *reclaimed); - bool sleep_mapped; void *(*map)(void *pool, unsigned long handle, enum zpool_mapmode mm); @@ -113,7 +98,6 @@ void zpool_register_driver(struct zpool_driver *driver); int zpool_unregister_driver(struct zpool_driver *driver); -bool zpool_evictable(struct zpool *pool); bool zpool_can_sleep_mapped(struct zpool *pool); #endif diff --git a/mm/z3fold.c b/mm/z3fold.c index 238a214de59f..e84de91ecccb 100644 --- a/mm/z3fold.c +++ b/mm/z3fold.c @@ -1364,9 +1364,7 @@ static const struct movable_operations z3fold_mops = { * zpool ****************/ -static void *z3fold_zpool_create(const char *name, gfp_t gfp, - const struct zpool_ops *zpool_ops, - struct zpool *zpool) +static void *z3fold_zpool_create(const char *name, gfp_t gfp) { return z3fold_create_pool(name, gfp); } diff --git a/mm/zbud.c b/mm/zbud.c index 9d35fd4091ed..2190cc1f37b3 100644 --- a/mm/zbud.c +++ b/mm/zbud.c @@ -380,9 +380,7 @@ static u64 zbud_get_pool_size(struct zbud_pool *pool) * zpool ****************/ -static void *zbud_zpool_create(const char *name, gfp_t gfp, - const struct zpool_ops *zpool_ops, - struct zpool *zpool) +static void *zbud_zpool_create(const char *name, gfp_t gfp) { return zbud_create_pool(gfp); } diff --git a/mm/zpool.c b/mm/zpool.c index 6a19c4a58f77..846410479c2f 100644 --- a/mm/zpool.c +++ b/mm/zpool.c @@ -133,7 +133,6 @@ EXPORT_SYMBOL(zpool_has_pool); * @type: The type of the zpool to create (e.g. zbud, zsmalloc) * @name: The name of the zpool (e.g. zram0, zswap) * @gfp: The GFP flags to use when allocating the pool. - * @ops: The optional ops callback. * * This creates a new zpool of the specified type. The gfp flags will be * used when allocating memory, if the implementation supports it. If the @@ -145,8 +144,7 @@ EXPORT_SYMBOL(zpool_has_pool); * * Returns: New zpool on success, NULL on failure. */ -struct zpool *zpool_create_pool(const char *type, const char *name, gfp_t gfp, - const struct zpool_ops *ops) +struct zpool *zpool_create_pool(const char *type, const char *name, gfp_t gfp) { struct zpool_driver *driver; struct zpool *zpool; @@ -173,7 +171,7 @@ struct zpool *zpool_create_pool(const char *type, const char *name, gfp_t gfp, } zpool->driver = driver; - zpool->pool = driver->create(name, gfp, ops, zpool); + zpool->pool = driver->create(name, gfp); if (!zpool->pool) { pr_err("couldn't create %s pool\n", type); @@ -279,30 +277,6 @@ void zpool_free(struct zpool *zpool, unsigned long handle) zpool->driver->free(zpool->pool, handle); } -/** - * zpool_shrink() - Shrink the pool size - * @zpool: The zpool to shrink. - * @pages: The number of pages to shrink the pool. - * @reclaimed: The number of pages successfully evicted. - * - * This attempts to shrink the actual memory size of the pool - * by evicting currently used handle(s). If the pool was - * created with no zpool_ops, or the evict call fails for any - * of the handles, this will fail. If non-NULL, the @reclaimed - * parameter will be set to the number of pages reclaimed, - * which may be more than the number of pages requested. - * - * Implementations must guarantee this to be thread-safe. - * - * Returns: 0 on success, negative value on error/failure. - */ -int zpool_shrink(struct zpool *zpool, unsigned int pages, - unsigned int *reclaimed) -{ - return zpool->driver->shrink ? - zpool->driver->shrink(zpool->pool, pages, reclaimed) : -EINVAL; -} - /** * zpool_map_handle() - Map a previously allocated handle into memory * @zpool: The zpool that the handle was allocated from @@ -359,24 +333,6 @@ u64 zpool_get_total_size(struct zpool *zpool) return zpool->driver->total_size(zpool->pool); } -/** - * zpool_evictable() - Test if zpool is potentially evictable - * @zpool: The zpool to test - * - * Zpool is only potentially evictable when it's created with struct - * zpool_ops.evict and its driver implements struct zpool_driver.shrink. - * - * However, it doesn't necessarily mean driver will use zpool_ops.evict - * in its implementation of zpool_driver.shrink. It could do internal - * defragmentation instead. - * - * Returns: true if potentially evictable; false otherwise. - */ -bool zpool_evictable(struct zpool *zpool) -{ - return zpool->driver->shrink; -} - /** * zpool_can_sleep_mapped - Test if zpool can sleep when do mapped. * @zpool: The zpool to test diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index e4d1ad521738..3f057970504e 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -351,9 +351,7 @@ static void record_obj(unsigned long handle, unsigned long obj) #ifdef CONFIG_ZPOOL -static void *zs_zpool_create(const char *name, gfp_t gfp, - const struct zpool_ops *zpool_ops, - struct zpool *zpool) +static void *zs_zpool_create(const char *name, gfp_t gfp) { /* * Ignore global gfp flags: zs_malloc() may be invoked from diff --git a/mm/zswap.c b/mm/zswap.c index 0024ec5ed574..a4f8c20e161b 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -258,10 +258,6 @@ static int zswap_writeback_entry(struct zpool *pool, unsigned long handle); static int zswap_pool_get(struct zswap_pool *pool); static void zswap_pool_put(struct zswap_pool *pool); -static const struct zpool_ops zswap_zpool_ops = { - .evict = zswap_writeback_entry -}; - static bool zswap_is_full(void) { return totalram_pages() * zswap_max_pool_percent / 100 < @@ -379,12 +375,9 @@ static void zswap_free_entry(struct zswap_entry *entry) if (!entry->length) atomic_dec(&zswap_same_filled_pages); else { - /* zpool_evictable will be removed once all 3 backends have migrated */ - if (!zpool_evictable(entry->pool->zpool)) { - spin_lock(&entry->pool->lru_lock); - list_del(&entry->lru); - spin_unlock(&entry->pool->lru_lock); - } + spin_lock(&entry->pool->lru_lock); + list_del(&entry->lru); + spin_unlock(&entry->pool->lru_lock); zpool_free(entry->pool->zpool, entry->handle); zswap_pool_put(entry->pool); } @@ -665,12 +658,8 @@ static void shrink_worker(struct work_struct *w) shrink_work); int ret, failures = 0; - /* zpool_evictable will be removed once all 3 backends have migrated */ do { - if (zpool_evictable(pool->zpool)) - ret = zpool_shrink(pool->zpool, 1, NULL); - else - ret = zswap_reclaim_entry(pool); + ret = zswap_reclaim_entry(pool); if (ret) { zswap_reject_reclaim_fail++; if (ret != -EAGAIN) @@ -708,7 +697,7 @@ static struct zswap_pool *zswap_pool_create(char *type, char *compressor) /* unique name for each pool specifically required by zsmalloc */ snprintf(name, 38, "zswap%x", atomic_inc_return(&zswap_pools_count)); - pool->zpool = zpool_create_pool(type, name, gfp, &zswap_zpool_ops); + pool->zpool = zpool_create_pool(type, name, gfp); if (!pool->zpool) { pr_err("%s zpool not available\n", type); goto error; @@ -1394,8 +1383,7 @@ static int zswap_frontswap_store(unsigned type, pgoff_t offset, zswap_entry_put(tree, dupentry); } } while (ret == -EEXIST); - /* zpool_evictable will be removed once all 3 backends have migrated */ - if (entry->length && !zpool_evictable(entry->pool->zpool)) { + if (entry->length) { spin_lock(&entry->pool->lru_lock); list_add(&entry->lru, &entry->pool->lru); spin_unlock(&entry->pool->lru_lock); @@ -1514,8 +1502,7 @@ static int zswap_frontswap_load(unsigned type, pgoff_t offset, if (!ret && zswap_exclusive_loads_enabled) { zswap_invalidate_entry(tree, entry); *exclusive = true; - } else if (entry->length && !zpool_evictable(entry->pool->zpool)) { - /* zpool_evictable will be removed once all 3 backends have migrated */ + } else if (entry->length) { spin_lock(&entry->pool->lru_lock); list_move(&entry->lru, &entry->pool->lru); spin_unlock(&entry->pool->lru_lock); From patchwork Mon Jun 12 09:38:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Domenico Cerasuolo X-Patchwork-Id: 13275957 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 D3740C7EE45 for ; Mon, 12 Jun 2023 09:38:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2AE578E0008; Mon, 12 Jun 2023 05:38:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2152E8E0007; Mon, 12 Jun 2023 05:38:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 088F08E0008; Mon, 12 Jun 2023 05:38:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id E32F18E0007 for ; Mon, 12 Jun 2023 05:38:31 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id BBA6F1C75EF for ; Mon, 12 Jun 2023 09:38:31 +0000 (UTC) X-FDA: 80893595622.12.DAB8009 Received: from mail-ed1-f45.google.com (mail-ed1-f45.google.com [209.85.208.45]) by imf05.hostedemail.com (Postfix) with ESMTP id D6C8410000C for ; Mon, 12 Jun 2023 09:38:29 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=sbtrlJfj; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf05.hostedemail.com: domain of cerasuolodomenico@gmail.com designates 209.85.208.45 as permitted sender) smtp.mailfrom=cerasuolodomenico@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686562710; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=BtDbY3geJk5ZFtkgOT5O5EUWsoo6AICXqA8Xr4TghOM=; b=Hk5UBGg30q2hTCgSbbSTc97LQcHiAL/vqy1Jd0DSDLieSl+mFTERW1qxFEP9Othfx+LepA nlzqlEIpiu8gCQr3d6nt0rCh8gQ06E1SsdCG9DbD5VUWz1ckV8x5uCMiPFxB0BSZoR+Ls4 vkOfSd+xmrWCjlT5M7GN8JBFwZV9Cv4= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=sbtrlJfj; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf05.hostedemail.com: domain of cerasuolodomenico@gmail.com designates 209.85.208.45 as permitted sender) smtp.mailfrom=cerasuolodomenico@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1686562710; a=rsa-sha256; cv=none; b=fBcKTJqtR2uHbyySHAewOqcv3aJCrN2AcREF5UivQYdN9QqAPDd7N9NDrwY1cgKFBpnQ5s ACuQ5dknfc+hhWghK8i9yqPhuVH7U7MbYiYTopb71BhQ88H/tT5qpSC4rLeTiRAmbuqF/B 4hdMw22LtLqAl6W0TF7yLYba0dlcgAo= Received: by mail-ed1-f45.google.com with SMTP id 4fb4d7f45d1cf-5183101690cso3119468a12.0 for ; Mon, 12 Jun 2023 02:38:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686562708; x=1689154708; 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=BtDbY3geJk5ZFtkgOT5O5EUWsoo6AICXqA8Xr4TghOM=; b=sbtrlJfjq4iOA2iKxVvlVUOSJqU+wWrzWL5qkr7NPaCf0qspebF+DperUWmXtMsLN7 awsXcdmmd3l5lMRpE1uWm4Giyfpswpz59osjNefCCyHA+bAac/onBHIcr7U7qMwjPYPn Y7held6fOw7FIBMb+tQOwpsJtbCGa9QQPho8tZFf1E97/Tmo1q9+MCfnzmdVYUHpWBX/ 9jjsSc9HNbCXfd+48kzRAY23T92961bkPMF7VoN50AjcHvTHkQ7Q+vTchaFUC3bbgbpK FefZWzCUGcr60mNZWhF4mhpksv5TtnPksEsoKqaTkB5qzYsVkIKSRxIsFKaQ2VQfvt71 mF5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686562708; x=1689154708; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BtDbY3geJk5ZFtkgOT5O5EUWsoo6AICXqA8Xr4TghOM=; b=lZqWghCrIgEGvt/VGHKodtlNjemVa2r3h1a6gdXM4Cm/yFOImalkye1IdJLIY7houS YHKhh8iWD4i/SMD9lmDWs0w3lmYYK1Ej/lKXUrG6VCOLuK86zuNvYys0N14gJYeMwz6S JryN4f5BBZd+aPNB2AsSCSOl8l38/oabW4527dCFnq4adzQELN5GtN//ZkXcgOP3HYo4 EDPrN+CWwZrn5eBkNtcIgT1w3ePu5Rm8QTYSD5qAVaUq5E6OEkK5Yrd1I2YpQjkUWpmr 5Dfjw3AREoq5UGJNzVI+zQl42MtuKGnda3JukEsxFBY18rqcDh3L/Hzq8F8K9PD/qIGR bW0A== X-Gm-Message-State: AC+VfDyjREKac77WFBvI7C6Xo/fCrTaNqc5asX5p3V7UbobywWhalMDD v3VPWusWfxvrO8iVr+dUlOs= X-Google-Smtp-Source: ACHHUZ5jdk574K53jAuDd36SfnD+TsI8d0fI044BN7IUN/jGQusjkNJ8HZapcVM3ykunUY7a3uRgmQ== X-Received: by 2002:a17:906:7953:b0:973:92a8:f611 with SMTP id l19-20020a170906795300b0097392a8f611mr8867651ejo.31.1686562708286; Mon, 12 Jun 2023 02:38:28 -0700 (PDT) Received: from lelloman-5950.homenet.telecomitalia.it (host-212-171-43-8.retail.telecomitalia.it. [212.171.43.8]) by smtp.gmail.com with ESMTPSA id ce23-20020a170906b25700b0097887b68c17sm4951358ejb.98.2023.06.12.02.38.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Jun 2023 02:38:27 -0700 (PDT) From: Domenico Cerasuolo To: vitaly.wool@konsulko.com, minchan@kernel.org, senozhatsky@chromium.org, yosryahmed@google.com, linux-mm@kvack.org Cc: ddstreet@ieee.org, sjenning@redhat.com, nphamcs@gmail.com, hannes@cmpxchg.org, akpm@linux-foundation.org, linux-kernel@vger.kernel.org, kernel-team@meta.com, Domenico Cerasuolo Subject: [PATCH v3 6/7] mm: zswap: simplify writeback function Date: Mon, 12 Jun 2023 11:38:14 +0200 Message-Id: <20230612093815.133504-7-cerasuolodomenico@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230612093815.133504-1-cerasuolodomenico@gmail.com> References: <20230612093815.133504-1-cerasuolodomenico@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: D6C8410000C X-Stat-Signature: zft4dia7yzonqqg1zbno3aw97nnzwceo X-HE-Tag: 1686562709-871031 X-HE-Meta: U2FsdGVkX18zXzB2puV4/M5wTLQAe2v5ZTy/I3Zr4kbXJVllJAyGOfBijhXJ0mVeDR1MzisWTY7P8bkDlrN5OPViIbENSW2pv2IsSy2k89O2mOUr6KHOQscnhif0I5AhVY/2S/2knvX4/64w6tLJWB/KERH8UZdpl8Fae5nPzt3kUGZOKNsrAn/1ehw3rbe7lol8zp50cyRjtTmjeOcYdumjO9jkwFQTK6P5eTJmd7pmR7KRwDuj9iG1bQxfF/aHTt+mc7hNY1fGk/68exJXcR4tR99FTXU87IBZLl6CMHAvn4XWlRd74b3sOM9Medox0Duy4BS4/xBgdZGWnMHlOj9UPP7sqFWd3FJHle72t1wX2DU1jygXVj5GOtNootUz9IB0P8UQxC38QCAJz3Vh78xp2+1OvoQR3HijzsOUO0OWXRFogfblGN2LbGfS5XdiotEeou2Ag3nRnJIYAhFdsHnEJMgWLLA9/VyO7g35t/BFYz0pDq5lYTUKtPBWMMB0WAbnk7SjCKvbVS7/NsxxOfmOM3xqf7T0FHLkExYAQ675vXFWlr+lNF/IFw9xjS5rSpVkiQYUhQ8hr3GMgl1FWqXMPqevN0rCXxPO2Qmavps4Xro5Oyv7NLqlLw5OOh9xKN1OmnUA+HdF9dl8XsMgNZ7FS9Suhe+0SjmoO5TIKfH/tLIXzVFVo4itEgEESMb9oc/18QZez++IkbVW3inl00SMtdZdPQ2LKSfbf/uXdv0mC2TKKFwV5PWQVhiDDVgevlnwCOxQOQz9voOPhygK87Ht6JSa3iHmuOa1g9JtXDnAAVPko1ODkZ6SzWWgZztk3404hL6QVstUMu4YXAkQv+W/plzy1903zvc2sFPNHakeI/AciN8XWzwR/mSAvNvzsIXKLJTm0hB5DZJ7bEIETc8mfN+lU7Rctz6Qr1QNHGR1G0cWFZa8yYCOl7SwAiBhlnuIQ1hhFYurIccM3Ig VD5mlHWT v5dtI+fuU/AgSB1Cj9yXO1zudDwmhpZFjKJffsJEMSVPkcFXGLYaSFCaBv9MnValT8D/Jg2hMiTroB5Q2Q2ZFKzXAcBq+0+Vf5EM72VqpKUV3ERJsAs9MJKdIhnmYwmRlVS0w1uhq0348SfMt7sM6gpwSV4poHI5dH/3am23P+9vQ0PgndWcgR8+PrrLRcC8Nhj29dl62pEhBBSKWRyNez2xtrlyI90hsoDB1F4dW+L31NPWALXoEvkHlPdClLWBmLvfTachN/cz8fk4vXsbCqUBCPqOotEKAVmmsFYKKLwzRssnJ8qcd16n36eR8nOCeGPPowWbMm/gtV3erd/u7B2Dj8SmWOusyLNVWGy5tizLfulmxNXpVPsNZjM8+UFVkvpxG6x8hBvDJoukImTUJ/uCkVixmTt/LtxGgTaWqivnAEV79OA7zuJpW3vyvMArDD8g6QeY7qSAL/Ycvh1DkgkNvWA== 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: zswap_writeback_entry() used to be a callback for the backends, which don't know about struct zswap_entry. Now that the only user is the generic zswap LRU reclaimer, it can be simplified: pass the pinned zswap_entry directly, and consolidate the refcount management in the shrink function. Tested-by: Yosry Ahmed Signed-off-by: Domenico Cerasuolo Acked-by: Johannes Weiner --- mm/zswap.c | 70 +++++++++++++++--------------------------------------- 1 file changed, 19 insertions(+), 51 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index a4f8c20e161b..3a6b07a19262 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -254,7 +254,8 @@ static bool zswap_has_pool; pr_debug("%s pool %s/%s\n", msg, (p)->tfm_name, \ zpool_get_type((p)->zpool)) -static int zswap_writeback_entry(struct zpool *pool, unsigned long handle); +static int zswap_writeback_entry(struct zswap_entry *entry, struct zswap_header *zhdr, + struct zswap_tree *tree); static int zswap_pool_get(struct zswap_pool *pool); static void zswap_pool_put(struct zswap_pool *pool); @@ -635,7 +636,7 @@ static int zswap_reclaim_entry(struct zswap_pool *pool) zswap_entry_get(entry); spin_unlock(&tree->lock); - ret = zswap_writeback_entry(pool->zpool, entry->handle); + ret = zswap_writeback_entry(entry, zhdr, tree); spin_lock(&tree->lock); if (ret) { @@ -643,8 +644,17 @@ static int zswap_reclaim_entry(struct zswap_pool *pool) spin_lock(&pool->lru_lock); list_move(&entry->lru, &pool->lru); spin_unlock(&pool->lru_lock); + goto put_unlock; } + /* Check for invalidate() race */ + if (entry != zswap_rb_search(&tree->rbroot, swpoffset)) + goto put_unlock; + + /* Drop base reference */ + zswap_entry_put(tree, entry); + +put_unlock: /* Drop local reference */ zswap_entry_put(tree, entry); unlock: @@ -1045,16 +1055,14 @@ static int zswap_get_swap_cache_page(swp_entry_t entry, * the swap cache, the compressed version stored by zswap can be * freed. */ -static int zswap_writeback_entry(struct zpool *pool, unsigned long handle) +static int zswap_writeback_entry(struct zswap_entry *entry, struct zswap_header *zhdr, + struct zswap_tree *tree) { - struct zswap_header *zhdr; - swp_entry_t swpentry; - struct zswap_tree *tree; - pgoff_t offset; - struct zswap_entry *entry; + swp_entry_t swpentry = zhdr->swpentry; struct page *page; struct scatterlist input, output; struct crypto_acomp_ctx *acomp_ctx; + struct zpool *pool = entry->pool->zpool; u8 *src, *tmp = NULL; unsigned int dlen; @@ -1069,25 +1077,6 @@ static int zswap_writeback_entry(struct zpool *pool, unsigned long handle) return -ENOMEM; } - /* extract swpentry from data */ - zhdr = zpool_map_handle(pool, handle, ZPOOL_MM_RO); - swpentry = zhdr->swpentry; /* here */ - tree = zswap_trees[swp_type(swpentry)]; - offset = swp_offset(swpentry); - zpool_unmap_handle(pool, handle); - - /* find and ref zswap entry */ - spin_lock(&tree->lock); - entry = zswap_entry_find_get(&tree->rbroot, offset); - if (!entry) { - /* entry was invalidated */ - spin_unlock(&tree->lock); - kfree(tmp); - return 0; - } - spin_unlock(&tree->lock); - BUG_ON(offset != entry->offset); - /* try to allocate swap cache page */ switch (zswap_get_swap_cache_page(swpentry, &page)) { case ZSWAP_SWAPCACHE_FAIL: /* no memory or invalidate happened */ @@ -1121,12 +1110,12 @@ static int zswap_writeback_entry(struct zpool *pool, unsigned long handle) acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx); dlen = PAGE_SIZE; - zhdr = zpool_map_handle(pool, handle, ZPOOL_MM_RO); + zhdr = zpool_map_handle(pool, entry->handle, ZPOOL_MM_RO); src = (u8 *)zhdr + sizeof(struct zswap_header); if (!zpool_can_sleep_mapped(pool)) { memcpy(tmp, src, entry->length); src = tmp; - zpool_unmap_handle(pool, handle); + zpool_unmap_handle(pool, entry->handle); } mutex_lock(acomp_ctx->mutex); @@ -1141,7 +1130,7 @@ static int zswap_writeback_entry(struct zpool *pool, unsigned long handle) if (!zpool_can_sleep_mapped(pool)) kfree(tmp); else - zpool_unmap_handle(pool, handle); + zpool_unmap_handle(pool, entry->handle); BUG_ON(ret); BUG_ON(dlen != PAGE_SIZE); @@ -1158,23 +1147,7 @@ static int zswap_writeback_entry(struct zpool *pool, unsigned long handle) put_page(page); zswap_written_back_pages++; - spin_lock(&tree->lock); - /* drop local reference */ - zswap_entry_put(tree, entry); - - /* - * There are two possible situations for entry here: - * (1) refcount is 1(normal case), entry is valid and on the tree - * (2) refcount is 0, entry is freed and not on the tree - * because invalidate happened during writeback - * search the tree and free the entry if find entry - */ - if (entry == zswap_rb_search(&tree->rbroot, offset)) - zswap_entry_put(tree, entry); - spin_unlock(&tree->lock); - return ret; - fail: if (!zpool_can_sleep_mapped(pool)) kfree(tmp); @@ -1183,13 +1156,8 @@ static int zswap_writeback_entry(struct zpool *pool, unsigned long handle) * if we get here due to ZSWAP_SWAPCACHE_EXIST * a load may be happening concurrently. * it is safe and okay to not free the entry. - * if we free the entry in the following put * it is also okay to return !0 */ - spin_lock(&tree->lock); - zswap_entry_put(tree, entry); - spin_unlock(&tree->lock); - return ret; } From patchwork Mon Jun 12 09:38:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Domenico Cerasuolo X-Patchwork-Id: 13275958 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 7A805C7EE25 for ; Mon, 12 Jun 2023 09:38:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 855B98E0007; Mon, 12 Jun 2023 05:38:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7DC6F900002; Mon, 12 Jun 2023 05:38:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5E0F48E0009; Mon, 12 Jun 2023 05:38:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 4162E8E0007 for ; Mon, 12 Jun 2023 05:38:33 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 13D8F1A0181 for ; Mon, 12 Jun 2023 09:38:33 +0000 (UTC) X-FDA: 80893595706.02.1F40D0E Received: from mail-ej1-f51.google.com (mail-ej1-f51.google.com [209.85.218.51]) by imf02.hostedemail.com (Postfix) with ESMTP id 0D5EC8000C for ; Mon, 12 Jun 2023 09:38:30 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=rPyAJhbx; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf02.hostedemail.com: domain of cerasuolodomenico@gmail.com designates 209.85.218.51 as permitted sender) smtp.mailfrom=cerasuolodomenico@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686562711; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=fU56VIBLxnADHQ9EwUvFSMVUPT9Hc1VDImYlSwrw2UY=; b=qsH2pG0VNyVVKNKSeN+utxiz7zdUv/HdyJIbFcnn8b2oPWbgEfCMF3jdAfm7row0vr4Llz GAz4eGxbNNV5z79HKmYzDTUNx7FSJcaNUg4J80OeltoDjxsXfFO9DB+7fcOS4Aa/ozpHz5 sB8PnZbRNIPPngMC6iAfqkPPQT3K12g= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=rPyAJhbx; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf02.hostedemail.com: domain of cerasuolodomenico@gmail.com designates 209.85.218.51 as permitted sender) smtp.mailfrom=cerasuolodomenico@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1686562711; a=rsa-sha256; cv=none; b=lIIBpENHcYBDDffxAG1d4HWH854jFW7D6yqrRyMDrT06/obMTiJdbpoEJ69M06XzJaJ6DJ rzQokLb5UzYoxF4DnoniIQON+oQ4cZlv3bMahzR+7HtXCvd4qe3nuOJu/FMKbADDHf/33J yUo8BOVhOkyNz7zupkesfm4qYK8sBso= Received: by mail-ej1-f51.google.com with SMTP id a640c23a62f3a-977d0288fd2so696846566b.1 for ; Mon, 12 Jun 2023 02:38:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686562709; x=1689154709; 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=fU56VIBLxnADHQ9EwUvFSMVUPT9Hc1VDImYlSwrw2UY=; b=rPyAJhbxdlqwUwyy7dTVMaBUHWn0yYtfrSsTXXjp3XScuykpDtMKUSRhLnII7AOPxy LygimtnshwNmGzZp4SRktLKkKrw8YeGsLVvcXVWqwRg152gnuniKlICI/PUGjYrtNuDq 9X0D0FfNc5DWhWUDwEYpeM09XkhOgj2vDLd+Tjx3qAT/jjzC93d32u43pbaRamHma0RQ FsMxBQqZ7lT0u3yAK7jrmhH1wHXB8K2cfVWkD5Z4zFPU6KuSLfQv4wL/ExHNW6YHyJKr HkrM1linZUifX/ITa62cR+cLUd4YP51PHa/Sj/Xd9+Nw8tflILQf6C5Alyzxrb95S5JA pP4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686562709; x=1689154709; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fU56VIBLxnADHQ9EwUvFSMVUPT9Hc1VDImYlSwrw2UY=; b=FRgEDKQREeuRTwi95CxsgaeY+BIM4dWCXt67OZaSpHT9Df0Mho3OYvzRsDN2yUMsLm 6JzF30PMCK7hfW638bO7SGx1tTgOKfLkd2axfrpGjOUXRQbawIy0RNNwAKOOvLhwOZED nMuyce7C3PdwYVkxnUyhe+pKKlqCupIK5lRNxzTsGsMtTjEd6FEkjfeWMY8mIuyeOxBp OBJUFGasMoXhEt2geSYjGGR7t8JH+imteRitxzwAg/0HBP7GQQSNKZk7Uq3QKgwLyQa5 RL19GrLYPJZbXKRkDfVyIzUTzJ4EUOZuSac8pH6aw0O4At0cjKY8mKP+eUVrRSC4Cc3Y Orcw== X-Gm-Message-State: AC+VfDygD0lU8KRawZrnHiUzDc3GnWN8wW0rWf3Wywc1UVNzrtJRh1AV D7KoWQg3mHVcpKOzZQuRoco= X-Google-Smtp-Source: ACHHUZ4Ka4ubZrdavFDrfWTwpLTvFWbauCZbwoMXabv7dPstChJIOunVXKEghyabVP0TxIpzU3irVQ== X-Received: by 2002:a17:907:7f09:b0:973:a30d:b264 with SMTP id qf9-20020a1709077f0900b00973a30db264mr9422554ejc.46.1686562709657; Mon, 12 Jun 2023 02:38:29 -0700 (PDT) Received: from lelloman-5950.homenet.telecomitalia.it (host-212-171-43-8.retail.telecomitalia.it. [212.171.43.8]) by smtp.gmail.com with ESMTPSA id ce23-20020a170906b25700b0097887b68c17sm4951358ejb.98.2023.06.12.02.38.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Jun 2023 02:38:29 -0700 (PDT) From: Domenico Cerasuolo To: vitaly.wool@konsulko.com, minchan@kernel.org, senozhatsky@chromium.org, yosryahmed@google.com, linux-mm@kvack.org Cc: ddstreet@ieee.org, sjenning@redhat.com, nphamcs@gmail.com, hannes@cmpxchg.org, akpm@linux-foundation.org, linux-kernel@vger.kernel.org, kernel-team@meta.com, Domenico Cerasuolo Subject: [PATCH v3 7/7] mm: zswap: remove zswap_header Date: Mon, 12 Jun 2023 11:38:15 +0200 Message-Id: <20230612093815.133504-8-cerasuolodomenico@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230612093815.133504-1-cerasuolodomenico@gmail.com> References: <20230612093815.133504-1-cerasuolodomenico@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 0D5EC8000C X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: yghjpnfux9gtswn9d8tcopqy7doi8ct5 X-HE-Tag: 1686562710-98027 X-HE-Meta: U2FsdGVkX18YKqa0PMSG8xgj2vD7Weu3fEZgkPUAUjWSaAKP9OKl3OCUuZ2H6//NY3KTXHlV5gN/t5TPm/3urGXr1KR2q3cdl+FbyVNehmG5rnXSPO9dGd/dJlDCnUQdV5D7Vb9VlZ1GX2A0NFsTd1Vnms5vHuoKRfAG+H3hqlAcahyELC9fVsBY9Sgbbu+HBvo5UKrcJyvXFlPQZAHUSrN3qtIxY5m6EiuDJnZMJL7o6VrocZkSk8VpFjSUP9qWWOO9ZXyBiziAMhmVYoVvgKAXngscnAZty3Yq2351o9FNy8aHgoQbNzpSVfY7KSKNZxgPPfmk7aBKWRLc8OjuekuxQi49a5W9fDUilZkbbeo/ma+7v4HBGOZ7PHTL1Yi6bUGgOvEJil1aCgEUJgN24AmRRtG6xeXss4fuhsgg1MV8lPqzD99YzjPUl21/BJbRbBSsgabX4lXAqLuO/gPHd6g67zU5Yt0pupb20dDDIsgT11MK5iB1UFpCyrYOP8HdML1+fEYOLTWNVGvkZF86MEYbg99+ITsviE64M5Tt0u5i+mCwVO1F2V/WoQZqZuexF0gJ1OEcdCwJhl//tWb2pur95GOKIPR/kv+s7BiClsK/TbqE5jgDE8ssqxtBSBerE73A68Tpk/WmWQ0+Zx9/DNMRm3ARcjDB+CHnBtNIu5q3A+OZ0AUfqAC/Zpknnwho/h6wxYYPJyDjvliqzdCTBLTfVYgnTEWKOqoGZ+rYA727e2lHwnctli7leUkyghajLUwI8xFKEhzHMbjnYjs6NOAO6DHhPR/yHtV/mppSks1HXj7JkC4iK2WlGqXNQu/RNHBHF91T+QphuT83wC5TYp7zXnXivuFdAc2GyGXIeUjkQsdQlHZJcYtKEWNhwt8r/ibTkT+5EUcZmsPWDCpAOOnzXIPeW7C9LQ95cMhzZByQTh4L12DBimllutdKrSBZ2NA1Prz1mU2CNQgaLZU rIUnkK8Z dyaIddxZbHTd3yVzRih+28KjvTcvD3/jL9pcBjJP27zoZRZm+BSjGwBrc0nmfMGD3JotktzbWLX1MslmRBuDY4ysKvOmxLgGcJPjqhLUHKJReCpUvh1vlGrpw4/kuAY900JNfKSIdqmzTRuZECsCJ9ntaIYURREfuik0rMyv8uvCm34UjUW9bvM20rL6rYD7XZPADIinp5CknE6SuR5whHCfj0K2B89Yff4KgeMRl2eWZgY/75wmORKkrkLFEsr3vB7cENIRQZjVOPEOFV7XE7jM3orVIyVH51+FbYSMBlAV5ULCtbGrdgFxkHkBZ2o/r73ggGeIw9eY5EBQp9h9L5uin5xytnvSqGY3FKiutc1syoWG8qyaFeC7J6dyHQmBrKnIvmQkPjCwwjouk3GbUB2m6v9qyOjHyRmdX6vGUkSAEm+w5w9Cr3tbgBcWd0pk2kv2rVMPLWM1CLOrzfxkDGB7oicKhRxkV8aYrS7qsBCrQUYE7tbTvmyIi2eU/L+x5V/eD 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: Previously, zswap_header served the purpose of storing the swpentry within zpool pages. This allowed zpool implementations to pass relevant information to the writeback function. However, with the current implementation, writeback is directly handled within zswap. Consequently, there is no longer a necessity for zswap_header, as the swp_entry_t can be stored directly in zswap_entry. Tested-by: Yosry Ahmed Suggested-by: Yosry Ahmed Signed-off-by: Domenico Cerasuolo Acked-by: Johannes Weiner --- mm/zswap.c | 52 ++++++++++++++++++++++------------------------------ 1 file changed, 22 insertions(+), 30 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 3a6b07a19262..02d0a6f46f4a 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -197,7 +197,7 @@ struct zswap_pool { */ struct zswap_entry { struct rb_node rbnode; - pgoff_t offset; + swp_entry_t swpentry; int refcount; unsigned int length; struct zswap_pool *pool; @@ -209,10 +209,6 @@ struct zswap_entry { struct list_head lru; }; -struct zswap_header { - swp_entry_t swpentry; -}; - /* * The tree lock in the zswap_tree struct protects a few things: * - the rbtree @@ -254,7 +250,7 @@ static bool zswap_has_pool; pr_debug("%s pool %s/%s\n", msg, (p)->tfm_name, \ zpool_get_type((p)->zpool)) -static int zswap_writeback_entry(struct zswap_entry *entry, struct zswap_header *zhdr, +static int zswap_writeback_entry(struct zswap_entry *entry, struct zswap_tree *tree); static int zswap_pool_get(struct zswap_pool *pool); static void zswap_pool_put(struct zswap_pool *pool); @@ -315,12 +311,14 @@ static struct zswap_entry *zswap_rb_search(struct rb_root *root, pgoff_t offset) { struct rb_node *node = root->rb_node; struct zswap_entry *entry; + pgoff_t entry_offset; while (node) { entry = rb_entry(node, struct zswap_entry, rbnode); - if (entry->offset > offset) + entry_offset = swp_offset(entry->swpentry); + if (entry_offset > offset) node = node->rb_left; - else if (entry->offset < offset) + else if (entry_offset < offset) node = node->rb_right; else return entry; @@ -337,13 +335,15 @@ static int zswap_rb_insert(struct rb_root *root, struct zswap_entry *entry, { struct rb_node **link = &root->rb_node, *parent = NULL; struct zswap_entry *myentry; + pgoff_t myentry_offset, entry_offset = swp_offset(entry->swpentry); while (*link) { parent = *link; myentry = rb_entry(parent, struct zswap_entry, rbnode); - if (myentry->offset > entry->offset) + myentry_offset = swp_offset(myentry->swpentry); + if (myentry_offset > entry_offset) link = &(*link)->rb_left; - else if (myentry->offset < entry->offset) + else if (myentry_offset < entry_offset) link = &(*link)->rb_right; else { *dupentry = myentry; @@ -601,7 +601,6 @@ static struct zswap_pool *zswap_pool_find_get(char *type, char *compressor) static int zswap_reclaim_entry(struct zswap_pool *pool) { - struct zswap_header *zhdr; struct zswap_entry *entry; struct zswap_tree *tree; pgoff_t swpoffset; @@ -615,15 +614,13 @@ static int zswap_reclaim_entry(struct zswap_pool *pool) } entry = list_last_entry(&pool->lru, struct zswap_entry, lru); list_del_init(&entry->lru); - zhdr = zpool_map_handle(pool->zpool, entry->handle, ZPOOL_MM_RO); - tree = zswap_trees[swp_type(zhdr->swpentry)]; - zpool_unmap_handle(pool->zpool, entry->handle); /* * Once the lru lock is dropped, the entry might get freed. The * swpoffset is copied to the stack, and entry isn't deref'd again * until the entry is verified to still be alive in the tree. */ - swpoffset = swp_offset(zhdr->swpentry); + swpoffset = swp_offset(entry->swpentry); + tree = zswap_trees[swp_type(entry->swpentry)]; spin_unlock(&pool->lru_lock); /* Check for invalidate() race */ @@ -636,7 +633,7 @@ static int zswap_reclaim_entry(struct zswap_pool *pool) zswap_entry_get(entry); spin_unlock(&tree->lock); - ret = zswap_writeback_entry(entry, zhdr, tree); + ret = zswap_writeback_entry(entry, tree); spin_lock(&tree->lock); if (ret) { @@ -1055,10 +1052,10 @@ static int zswap_get_swap_cache_page(swp_entry_t entry, * the swap cache, the compressed version stored by zswap can be * freed. */ -static int zswap_writeback_entry(struct zswap_entry *entry, struct zswap_header *zhdr, +static int zswap_writeback_entry(struct zswap_entry *entry, struct zswap_tree *tree) { - swp_entry_t swpentry = zhdr->swpentry; + swp_entry_t swpentry = entry->swpentry; struct page *page; struct scatterlist input, output; struct crypto_acomp_ctx *acomp_ctx; @@ -1098,7 +1095,7 @@ static int zswap_writeback_entry(struct zswap_entry *entry, struct zswap_header * writing. */ spin_lock(&tree->lock); - if (zswap_rb_search(&tree->rbroot, entry->offset) != entry) { + if (zswap_rb_search(&tree->rbroot, swp_offset(entry->swpentry)) != entry) { spin_unlock(&tree->lock); delete_from_swap_cache(page_folio(page)); ret = -ENOMEM; @@ -1110,8 +1107,7 @@ static int zswap_writeback_entry(struct zswap_entry *entry, struct zswap_header acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx); dlen = PAGE_SIZE; - zhdr = zpool_map_handle(pool, entry->handle, ZPOOL_MM_RO); - src = (u8 *)zhdr + sizeof(struct zswap_header); + src = zpool_map_handle(pool, entry->handle, ZPOOL_MM_RO); if (!zpool_can_sleep_mapped(pool)) { memcpy(tmp, src, entry->length); src = tmp; @@ -1205,11 +1201,10 @@ static int zswap_frontswap_store(unsigned type, pgoff_t offset, struct obj_cgroup *objcg = NULL; struct zswap_pool *pool; int ret; - unsigned int hlen, dlen = PAGE_SIZE; + unsigned int dlen = PAGE_SIZE; unsigned long handle, value; char *buf; u8 *src, *dst; - struct zswap_header zhdr = { .swpentry = swp_entry(type, offset) }; gfp_t gfp; /* THP isn't supported */ @@ -1254,7 +1249,7 @@ static int zswap_frontswap_store(unsigned type, pgoff_t offset, src = kmap_atomic(page); if (zswap_is_page_same_filled(src, &value)) { kunmap_atomic(src); - entry->offset = offset; + entry->swpentry = swp_entry(type, offset); entry->length = 0; entry->value = value; atomic_inc(&zswap_same_filled_pages); @@ -1308,11 +1303,10 @@ static int zswap_frontswap_store(unsigned type, pgoff_t offset, } /* store */ - hlen = sizeof(zhdr); gfp = __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM; if (zpool_malloc_support_movable(entry->pool->zpool)) gfp |= __GFP_HIGHMEM | __GFP_MOVABLE; - ret = zpool_malloc(entry->pool->zpool, hlen + dlen, gfp, &handle); + ret = zpool_malloc(entry->pool->zpool, dlen, gfp, &handle); if (ret == -ENOSPC) { zswap_reject_compress_poor++; goto put_dstmem; @@ -1322,13 +1316,12 @@ static int zswap_frontswap_store(unsigned type, pgoff_t offset, goto put_dstmem; } buf = zpool_map_handle(entry->pool->zpool, handle, ZPOOL_MM_WO); - memcpy(buf, &zhdr, hlen); - memcpy(buf + hlen, dst, dlen); + memcpy(buf, dst, dlen); zpool_unmap_handle(entry->pool->zpool, handle); mutex_unlock(acomp_ctx->mutex); /* populate entry */ - entry->offset = offset; + entry->swpentry = swp_entry(type, offset); entry->handle = handle; entry->length = dlen; @@ -1437,7 +1430,6 @@ static int zswap_frontswap_load(unsigned type, pgoff_t offset, /* decompress */ dlen = PAGE_SIZE; src = zpool_map_handle(entry->pool->zpool, entry->handle, ZPOOL_MM_RO); - src += sizeof(struct zswap_header); if (!zpool_can_sleep_mapped(entry->pool->zpool)) { memcpy(tmp, src, entry->length);