From patchwork Wed Mar 20 02:08:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13597212 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 CBE2DCD11BF for ; Wed, 20 Mar 2024 02:08:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 58D6B6B009C; Tue, 19 Mar 2024 22:08:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 53E5D6B009D; Tue, 19 Mar 2024 22:08:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 342B56B009E; Tue, 19 Mar 2024 22:08:30 -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 181606B009C for ; Tue, 19 Mar 2024 22:08:30 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id C40991606C1 for ; Wed, 20 Mar 2024 02:08:29 +0000 (UTC) X-FDA: 81915783138.27.3A11EF3 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf03.hostedemail.com (Postfix) with ESMTP id 0B5D920005 for ; Wed, 20 Mar 2024 02:08:27 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=nr92ik4T; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf03.hostedemail.com: domain of 3G0X6ZQoKCMQ8y218krwonqyyqvo.mywvsx47-wwu5kmu.y1q@flex--yosryahmed.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3G0X6ZQoKCMQ8y218krwonqyyqvo.mywvsx47-wwu5kmu.y1q@flex--yosryahmed.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1710900508; 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:in-reply-to:references:references:dkim-signature; bh=4f2cmQGIIvYk0KlMI8Lk64bezK2vGQGAMrw9pw7eCqU=; b=wMyY06EujWz7U3Sb30J7klYivcU3fHvVviKnrDIjAua99QfzE2gdh1QiY6eJSm+cQ661Ic M56SXxY+p8fqBFa4kISxoHikvm3/vDA1ShRIfmECMXJuwNMudMmyEgSwDA126hQpOpsdFV xPBPj4upzurIn9lcZgl2WAnx2TmxSsw= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=nr92ik4T; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf03.hostedemail.com: domain of 3G0X6ZQoKCMQ8y218krwonqyyqvo.mywvsx47-wwu5kmu.y1q@flex--yosryahmed.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3G0X6ZQoKCMQ8y218krwonqyyqvo.mywvsx47-wwu5kmu.y1q@flex--yosryahmed.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1710900508; a=rsa-sha256; cv=none; b=B+Fs52NJ1g3ZJDcs0CJknNv2B6v4zcq40DDGhhF+Okym5eS82cTNGGDzG+ng8m4cG7aS4N ltuAUuyzeFfFGgoxyf8vnwlBMAMERO1FQox9XbLyZNtQfE3mI/xkBQCmrYufVcwvYQLxvF AqfyUERR2CBZXW+vaHw/4XXDJNw74Vg= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-60f9d800a29so76023587b3.0 for ; Tue, 19 Mar 2024 19:08:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1710900507; x=1711505307; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=4f2cmQGIIvYk0KlMI8Lk64bezK2vGQGAMrw9pw7eCqU=; b=nr92ik4TjIqF8P/OnSCFULuZoYrf4ggvgy5y2oNw2P7pcBBIDGdK3TdCnnif33TYnT wHe7wm+LJyMbu1FPUXAIOrst4DFWL0Mi5do9jECK9s0Sr70YTgZ60qi8jwVy+a7pErvn E+JQqPeu6Edge3hAqu7EJOB4f0Znsh/v9RRlr+TAc0ruHEUEsc3Z6omTjEpsV+WseisX aDPM6RagNg3FkCslu0xA3vd5wWODW6q8VLGNUMV+ZgeIZoD9shs+VE9exJXIcMF3rmCz +IC8KOnNOqtA0aob0vTudwZtPamMpt0Xfh3injNGU359OGGzgS9gI/H1qXaQsmBLzQht QGkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710900507; x=1711505307; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=4f2cmQGIIvYk0KlMI8Lk64bezK2vGQGAMrw9pw7eCqU=; b=T3+r+ckbuAjAPGoDKrKFLfgKF/LxXmneElpchhGYWO+VKrfiBzpyH2nxYSqIF3+/TZ j9efjAPuruPYImxgmrhEvigEzneTbZZaugttHMz/8hXhIplOz60ogwR2owZrrpGRhIrp vt6x3P5y0GOg41KL8Y1Z7Du03hA7R9qlaMmibQwjzFqrM692hglSMpBsTNeYkSNtA8ub 4yBpmvwJyoFYR6u7RId9RUhi4cVGFYxWi9MHuUo//tPiXsUoUlkDmqBUp/j2Z7DuyiRL ObTDjnhVfA35VLRt5HMHd8eeH7qTWuKR7qxh+6iqT+vkS7YhSut1ta5tD+H6sZAsz5av 962Q== X-Forwarded-Encrypted: i=1; AJvYcCW6Zy+4qzn3zjqI9z7Xq3NkOtm0Z2SQVKUqpvK13ef538GHf49qaEuHftYcHZ4XLsq7o729SXvSDr89iI+RguBb2MM= X-Gm-Message-State: AOJu0Yz0p/n6H7YS5tRfx2v5P9AXOyoivzzNg1S8hCVsqSieM09wt5MW s8ORepoXTMRDMrpzODmPLWb0gnw5k0txj5EsxE9TiYjgTyZyLw7xpj9lAIEVSmXkHpa3A1Ds42N PkXG9zKlI8jLtCMY2XQ== X-Google-Smtp-Source: AGHT+IFsvhJOuJBMAwJK0L9AcJGw/zZI7HjSUBr+0ZmCaaIAiZxx6HFfHdp9ILj8xjHSBNtVwPjPcmu8Za+Wats5 X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a0d:d54e:0:b0:60a:2307:e3b with SMTP id x75-20020a0dd54e000000b0060a23070e3bmr3040512ywd.3.1710900507103; Tue, 19 Mar 2024 19:08:27 -0700 (PDT) Date: Wed, 20 Mar 2024 02:08:23 +0000 In-Reply-To: <20240320020823.337644-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20240320020823.337644-1-yosryahmed@google.com> X-Mailer: git-send-email 2.44.0.291.gc1ea87d7ee-goog Message-ID: <20240320020823.337644-2-yosryahmed@google.com> Subject: [PATCH 2/2] 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-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 0B5D920005 X-Stat-Signature: 8bb65gwsete8m58gkjyxqszki6g1xu77 X-HE-Tag: 1710900507-256696 X-HE-Meta: U2FsdGVkX1/UhaQyW+bigzuJ/22wsMUAgL/A+OTRpbbyvhVHwyBFCs22h0LXeHLqyUqBLXaK7dlDBC16vNvAm7e/jix86NEnhaNx9gZ+AEl4GzY6ddN2vmlF5fGmJjMPQdAgixpdI2EJptERFOKk7ZElmVtHKyI6J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1TgB++5XtKx2zbhCG2tB/mKaZ9FxHEXRCBwcFMvsDyoQmt5bDzstFp96qq06Uv+kSSW8kH9I89VOWcFDfs/PynVA4Zd+KPZUFbtsdKMM82CdKSMzjvRt3l3w3LwX+pPm/YM2rnQzrZlVw66yEXaT1q5edjh4wVYGKr17ZjPDSkrmEheNz4LnxO8gRA+wyWzCEJu1XBW5IqD6HSAIIMjBayKSFjcPIApD3+iNo2oksK+dxpDz1pIY0KpczIyYt+34C3aX9cQBgkZvQzYvq2Q5KrdL6eHwOmUjGVI7CJPX4Se17qQkPDAOUdCzDTUMD1qWu8pOqVIzbb/9+/pDFwj9OIJO7OacpmUIUFI5y1QhnYyoaP9jgaDHflsPdqkj5QndyU8QMP8z9ZlRcVsP4CEjKT6x8vxVztDb6gAw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000120, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: zswap_nr_stored is used to maintain the number of stored pages in zswap 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. However, the need for this counter is questionable due to two reasons: - It is redundant. The value can be inferred from (zswap_stored_pages - zswap_same_filled_pages). - 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 --- mm/zswap.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 323f1dea43d22..ffcfce05a4408 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); @@ -880,7 +878,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) { @@ -1305,7 +1302,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) @@ -1325,6 +1322,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); } @@ -1570,7 +1572,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);