From patchwork Fri Mar 22 00:10:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13599484 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 C64D7C54E58 for ; Fri, 22 Mar 2024 00:10:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5DB1B6B0089; Thu, 21 Mar 2024 20:10:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 58B806B008A; Thu, 21 Mar 2024 20:10:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 47B0C6B008C; Thu, 21 Mar 2024 20:10:07 -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 395666B0089 for ; Thu, 21 Mar 2024 20:10:07 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 07963A170A for ; Fri, 22 Mar 2024 00:10:07 +0000 (UTC) X-FDA: 81922742454.17.54414BA Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf13.hostedemail.com (Postfix) with ESMTP id 7B32120015 for ; Fri, 22 Mar 2024 00:10:04 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="k/femeQQ"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 3W8z8ZQoKCB4SIMLS4BG87AIIAF8.6IGFCHOR-GGEP46E.ILA@flex--yosryahmed.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3W8z8ZQoKCB4SIMLS4BG87AIIAF8.6IGFCHOR-GGEP46E.ILA@flex--yosryahmed.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711066204; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=w5K+vZiiCmgzoDXluPHngobfmz20rWM1f5eE3ArbH1M=; b=iCYx7f2fwwNkz5RmKLPHpyqUBrsWGiNCGzKc+uW4GfkCs3Oz6Gy4gVj6zBQslrSc6aPXPN l/lw/cxC0VC4R6mb9DMzS1OdAsUspc67phBRHvuk8olCp32ls1HOmSTbNgSE5mzWaHac47 0YZg6K94sduw1TcLYKnlyT/IBC0EiKA= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="k/femeQQ"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 3W8z8ZQoKCB4SIMLS4BG87AIIAF8.6IGFCHOR-GGEP46E.ILA@flex--yosryahmed.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3W8z8ZQoKCB4SIMLS4BG87AIIAF8.6IGFCHOR-GGEP46E.ILA@flex--yosryahmed.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711066204; a=rsa-sha256; cv=none; b=yYk6pbX6c6c/fFnCeNwgfGg0JC704Ezvrx8Zam5+lVod13l7YOwNpTutsLVrTXwUcAfOOz oOwLqte88lAJ6YvMabHgMt2acRk1COwhosYgHSA99hLHpiYLbAEsXH0w/wI1B3X0z2aSFa Xzv9NKGXgVJyyUf+/p1p+QcjCXw1/JA= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-607838c0800so28352237b3.1 for ; Thu, 21 Mar 2024 17:10:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711066203; x=1711671003; darn=kvack.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=w5K+vZiiCmgzoDXluPHngobfmz20rWM1f5eE3ArbH1M=; b=k/femeQQSXNHZCaIoGKEI6a57L9KLUZAhTUM0OG1CNSVwXVkqVsYjG5Rg6Duq4fXQw 7M8w98okJliRAONSv9Ar+ILLBW3RvyPg+28xRwj56nWR49fXCJN8wazp4ktnPg+r9FLF OM4ZD2bRMFTZrS8udRbeSJItA9pyVtC+gtkycw/lIPyeFmhmncBOvCHTKoKwkRGPIcIY rfyFb4+2Eci71Z0h1XLN/KuaThjJSxhtYXDI5OXBqVf52YD/ZD/lkZu7JmpoiphZP9EU swYgQS4uP5UOhUCZnJi6O3fJNgHcjeDlyHImaIRS9gwDLAfN9hQ1yabPZ2HUajurZ9Pu kOpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711066203; x=1711671003; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=w5K+vZiiCmgzoDXluPHngobfmz20rWM1f5eE3ArbH1M=; b=VBXO/rgEnM3OgKSCtORoVmuZ/6k9x17C7Gzdv0H6pm1FtqSlK+1QBgV4gTqst1Sx26 uu23w+vAhXlts3CTXhGFW821IF/WMc9dYqs49R0ZhkRLpSfU4roFGZc2xm8YUqeyatM1 Rv21AD2ZszAtMKf+JMT/bTkwUQHYNGub1NuujXWXnhZKMnETzrb99lXmfGWAjn6pR+H6 Kq2Ea+4EAOcKAzueM+jIusvhR9odFfhIcX5zIOi4t0bnNN63yeFvtjTUIYAxMT5eELjg cbqhNZ/nLkEAIJxU/rv51jrUeP9SYV3u0siya86n8LwPg8JwADSIuSuLWiW8GhZKfSoF zWsg== X-Forwarded-Encrypted: i=1; AJvYcCV8ojJd0CUyv45IgXRfCZ0zuEuzEOglbwTHnAG1CR5Jd9yQ4IQO6WSIvWwgiHTR1M24vdABzvnlZXV7cSqt4IzTqQM= X-Gm-Message-State: AOJu0YyYTRhd0i6nZAaExAY/RY+4Zp139YCXRweV+EMQLKrO26rYNejl 5uzgrQ53Njny76gLgNp+ts4gILf+x/2g5dSUvs81QTmyLmas9UBmWkyNwxkohPzB78ZC0Kzyb3K 4cYSULNw0fOj05d+kZg== X-Google-Smtp-Source: AGHT+IEyYfewi4gqRaFyQty4UL6G5sLBNRtvajjK0MhkKkIGFmsL8zb8iknEx5/grRR7qegyoT1CCr8zQBwOxswF X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a81:498c:0:b0:60a:13b8:6a5c with SMTP id w134-20020a81498c000000b0060a13b86a5cmr166506ywa.0.1711066203594; Thu, 21 Mar 2024 17:10:03 -0700 (PDT) Date: Fri, 22 Mar 2024 00:10:01 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.44.0.396.g6e790dbe36-goog Message-ID: <20240322001001.1562517-1-yosryahmed@google.com> Subject: [PATCH v2] mm: zswap: remove nr_zswap_stored atomic From: Yosry Ahmed To: Andrew Morton Cc: Johannes Weiner , Nhat Pham , Chengming Zhou , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yosry Ahmed X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 7B32120015 X-Stat-Signature: huhb4u8zo7t3116iq4tn4onakfaoesmp X-Rspam-User: X-HE-Tag: 1711066204-332301 X-HE-Meta: U2FsdGVkX1+GnLXCyKZiAoe0XYbbVrCQO/Mc2WkZHLoN2TsrpesRxG0QwsHGBLq0lL6XSm3TM0Ykn/1l6bPSWXbTIUOdceZtjWOpx40pp/NOZBe0K0CT17ZVLI0pnQ6sfocgOJDyH3lneCKLY93WUQxHtMcD6CIMchIPM4fp1+oEK8bpoC3WshxoEjGT0HgTcgZ0S0f/Jc8KynMLt7NqNOSgd09eAnBUdnAe8umaOJPCQib7Av7fNc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/+E4kRq754noshBtOPhbChNjEB5NOkS5nkVUkb3EUm4dXBOEku4vOdC4NiT6kuY661AMIkojH10EQCYE3+fXfnVq5sLhgZGk3RUumk= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: nr_stored was introduced by commit b5ba474f3f51 ("zswap: shrink zswap pool based on memory pressure") as a per zswap_pool counter of the number of stored pages that are not same-filled pages. It is used in zswap_shrinker_count() to scale the number of freeable compressed pages by the compression ratio. That is, to reduce the amount of writeback from zswap with higher compression ratios as the ROI from IO diminishes. Later on, commit bf9b7df23cb3 ("mm/zswap: global lru and shrinker shared by all zswap_pools") made the shrinker global (not per zswap_pool), and replaced nr_stored with nr_zswap_stored (initially introduced as zswap.nr_stored), which is now a global counter. The counter is now awfully close to zswap_stored_pages. The only difference is that the latter also includes same-filled pages. Also, when memcgs are enabled, we use memcg_page_state(memcg, MEMCG_ZSWAPPED), which includes same-filled pages anyway (i.e. equivalent to zswap_stored_pages). Use zswap_stored_pages instead in zswap_shrinker_count() to keep things consistent whether memcgs are enabled or not, and add a comment about the number of freeable pages possibly being scaled down more than it should if we have lots of same-filled pages (i.e. inflated compression ratio). Remove nr_zswap_stored and one atomic operation in the store and free paths. Signed-off-by: Yosry Ahmed Reviewed-by: Nhat Pham Reviewed-by: Chengming Zhou Acked-by: Johannes Weiner --- mm/zswap.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index b31c977f53e9c..1a79f99606cef 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -181,8 +181,6 @@ struct zswap_pool { /* Global LRU lists shared by all zswap pools. */ static struct list_lru zswap_list_lru; -/* counter of pages stored in all zswap pools. */ -static atomic_t zswap_nr_stored = ATOMIC_INIT(0); /* The lock protects zswap_next_shrink updates. */ static DEFINE_SPINLOCK(zswap_shrink_lock); @@ -885,7 +883,6 @@ static void zswap_entry_free(struct zswap_entry *entry) else { zswap_lru_del(&zswap_list_lru, entry); zpool_free(zswap_find_zpool(entry), entry->handle); - atomic_dec(&zswap_nr_stored); zswap_pool_put(entry->pool); } if (entry->objcg) { @@ -1310,7 +1307,7 @@ static unsigned long zswap_shrinker_count(struct shrinker *shrinker, #else /* use pool stats instead of memcg stats */ nr_backing = zswap_total_pages(); - nr_stored = atomic_read(&zswap_nr_stored); + nr_stored = atomic_read(&zswap_stored_pages); #endif if (!nr_stored) @@ -1330,6 +1327,11 @@ static unsigned long zswap_shrinker_count(struct shrinker *shrinker, * This ensures that the better zswap compresses memory, the fewer * pages we will evict to swap (as it will otherwise incur IO for * relatively small memory saving). + * + * The memory saving factor calculated here takes same-filled pages into + * account, but those are not freeable since they almost occupy no + * space. Hence, we may scale nr_freeable down a little bit more than we + * should if we have a lot of same-filled pages. */ return mult_frac(nr_freeable, nr_backing, nr_stored); } @@ -1575,7 +1577,6 @@ bool zswap_store(struct folio *folio) if (entry->length) { INIT_LIST_HEAD(&entry->lru); zswap_lru_add(&zswap_list_lru, entry); - atomic_inc(&zswap_nr_stored); } spin_unlock(&tree->lock);