From patchwork Wed Jan 29 10:08:44 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hyeonggon Yoo <42.hyeyoo@gmail.com> X-Patchwork-Id: 13953592 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 2E6A1C0218D for ; Wed, 29 Jan 2025 10:09:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7B727280040; Wed, 29 Jan 2025 05:09:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 78DC1280025; Wed, 29 Jan 2025 05:09:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 62E12280040; Wed, 29 Jan 2025 05:09:22 -0500 (EST) 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 455FE280025 for ; Wed, 29 Jan 2025 05:09:22 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 6AFE8C0B47 for ; Wed, 29 Jan 2025 10:09:00 +0000 (UTC) X-FDA: 83060066082.03.5FD9D24 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf25.hostedemail.com (Postfix) with ESMTP id A267EA0007 for ; Wed, 29 Jan 2025 10:08:58 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=UclrBbcR; spf=pass (imf25.hostedemail.com: domain of 42.hyeyoo@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=42.hyeyoo@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=1738145338; 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:references:dkim-signature; bh=4eFjJLmA7+jY5KjyB1yzIOfrExQ6VpabMonRw1hJ84E=; b=6Fpmf0C09x89f8JOzzDl163t5ezHglcqcV/VCe4pXDGS8zeIwztVqWRAwZIe8cFUAHMZ2n 83X+P1B5Vn6jW1HbzXF7CclnvW4GV6WAh0x7gLCFZtrf+9yUX3R/wqy5BOcJxcGP2/tSiG plaTICwXgbPmRjUI+a65EsLdaHVwp/0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738145338; a=rsa-sha256; cv=none; b=vcDwXdLnmo0yg/8gKaLFyk6r0Sc7DE/xJR8FoegvwFBnRXQEFdBJiw/E0esycFoV/v0t/S ZcA0nvJg4gWySJK8VftRtmZ5NMGXbmui4Aa7oEeJAFP9X3IspCxq2qxFe/jykWxXBNbIve mLmPOxVY77N88hkwq6wKxQzccvNESBk= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=UclrBbcR; spf=pass (imf25.hostedemail.com: domain of 42.hyeyoo@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=42.hyeyoo@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-2163dc5155fso121061375ad.0 for ; Wed, 29 Jan 2025 02:08:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1738145337; x=1738750137; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=4eFjJLmA7+jY5KjyB1yzIOfrExQ6VpabMonRw1hJ84E=; b=UclrBbcRDdprtTeKeJogc9llM89cy+ikUxj9OlftZkC9AsRsPg3uNr4BEi/Or3b2UW BrTwd5jBikIqcJdi2Pik5d8bVc6aPcYHTmVKNf0KobViECWy0IbcCXXQU2w6B7mzqr3f 0fsr/TqPqO+BR4J7z94Fd7a4R2GKNT3YwKhj2uEl9IibG+cKcCqPo5H5jWqpdzfKJH7e VX4hqwANJH2E8hO2nXGkCe8zFYJIl3XpRL70JIqbrrXuKFDiL8QqmpYQh3UmuvBW0G6t xWariC6CETPcP9Ctk/6YqF0YBm+iGIRxlmIf6/yHkX/rQiP5TCeggtF2oQJMiqIxBS/h NOiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738145337; x=1738750137; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=4eFjJLmA7+jY5KjyB1yzIOfrExQ6VpabMonRw1hJ84E=; b=kjIwc+9sKHfyotnqFzv2zN+duQff2OPLT8FaAk/CLNv2Z8DwSKwZEkhW8ZX+rzmxO3 US95LnXdvEA/8PIzipXaAf9+nsAbCAGfIJ02uD56Ta5fKrVQOb4TC6DfHr0XlMTckQeA 3bPx3z2Qlkm+bpZuRT+yDbL0k7q+L4CYJyhxRerVqnvXqS7w71hv9M9c+S92pRojxpLr j4PfVjNG/rgccwyTbVfIwbK0iNkMm1Oi0GQlOkqwTnIkyHaVSmG5MkoRS6F6/uPnHUua BZxGKm8vUEplDuL6K/mFyPzbiGEwZaiN5+2p8Vxwed/n8kAwrXpeCa4Vylf45KChVjAD nFQQ== X-Gm-Message-State: AOJu0YyhHNfltP+P0EbQQP0Ip+WuqziCKj4k08ZIkDXxVB8nRcyrfXQP 4CnFRhx+1l6yY06D5rOjYdBXet/IIKVbH55cDPC4XZmyjKBsQ3B1 X-Gm-Gg: ASbGnctlHZcivGl/zW3IEmNIdkf+zRShj4CZZ14fuVG9Qp+qvIRw4d4A8jqd1OIwduR kiLP/ycl+ZBFjKdtkJwh9k+aBs+rXY4E95edg2S+3KBHsBJ7TUj2FHg5833GW7St/qd+9R60726 CQ+aGuITWorjgHIGXpMDcftYZU2XOx5GAccozV/Ok28OT9SgKRWWcwj1g5DVFiZVj/4DIudtaRw sXSyUivrQP0mvS1M9sklu5mtouhTbL3mamHDhaP5Fvjiu57Sas7MAcZPqXrgjtBXjPgFOkrbEl2 4r0YbJwT96RmkliP1JFGl4f0EabGok2u X-Google-Smtp-Source: AGHT+IFu9zutwwiyXSYXOvcIiiLZl95OQxh8Qw0wNADOnSiF1SeNm1oMoqrIjLHbcsHK4atO62YRog== X-Received: by 2002:a17:902:ec8f:b0:216:5561:70d7 with SMTP id d9443c01a7336-21dd7e4264emr39113415ad.52.1738145337032; Wed, 29 Jan 2025 02:08:57 -0800 (PST) Received: from hyeyoo-laptop.localdomain ([114.29.17.235]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21da555a428sm94359375ad.84.2025.01.29.02.08.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Jan 2025 02:08:56 -0800 (PST) From: Hyeonggon Yoo <42.hyeyoo@gmail.com> To: Kanchana P Sridhar , Johannes Weiner , Yosry Ahmed , Nhat Pham , Chengming Zhou , Andrew Morton Cc: linux-mm@kvack.org, Hyeonggon Yoo <42.hyeyoo@gmail.com>, stable@vger.kernel.org Subject: [PATCH v3 mm-hotfixes] mm/zswap: fix inconsistency when zswap_store_page() fails Date: Wed, 29 Jan 2025 19:08:44 +0900 Message-ID: <20250129100844.2935-1-42.hyeyoo@gmail.com> X-Mailer: git-send-email 2.47.1 MIME-Version: 1.0 X-Stat-Signature: 84z3xy7ie4rhrasn75q89gf46bpia9an X-Rspamd-Queue-Id: A267EA0007 X-Rspam-User: X-Rspamd-Server: rspam06 X-HE-Tag: 1738145338-926916 X-HE-Meta: U2FsdGVkX1/4t00KCSl+yauqq+RlDAmtXJ7hnsrjyQ9UuRW10/GzTiYVjhrF3TSqJ7TZIP5389ciGykxyTFhLM7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/A1aVSjVkUA+NCWMqa76YDo+y49XRMPMX+dvkEoPlsTQkIAOLNQrKO5ZWoGjsbrY/IE55YFsGAm1C1Uz21oOV8J6dl4HNrqejpRiiUqXC3AC1SPzVbX9sQbF5sBB8wheocBiJEK5GNHlX+KEGeG9Pu7saqNEMPaklrRzKRdarSsyiNlIEmwVukTRgFODBfhPMRlmSuEw63dwCjGZVoTEf8Jz4Yb6jbd2R3SkpecIUgGvqKvNNi05mdJhSR0SnwHk//nrVi7t02dqxkZUvKhLLdbJDOQNHFSwZOHtt94lDfs6Zd/lFunpl2IgXqHto6UnLBBAGTbfg33LIme/M/WmT3kYInS5nHEndf/uDgfuPsA/UVPLBKFdaZitPcSgz+PG+x++X7/MszEAmXaMKwS6U2B5Eo 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: Commit b7c0ccdfbafd ("mm: zswap: support large folios in zswap_store()") skips charging any zswap entries when it failed to zswap the entire folio. However, when some base pages are zswapped but it failed to zswap the entire folio, the zswap operation is rolled back. When freeing zswap entries for those pages, zswap_entry_free() uncharges the zswap entries that were not previously charged, causing zswap charging to become inconsistent. This inconsistency triggers two warnings with following steps: # On a machine with 64GiB of RAM and 36GiB of zswap $ stress-ng --bigheap 2 # wait until the OOM-killer kills stress-ng $ sudo reboot The two warnings are: in mm/memcontrol.c:163, function obj_cgroup_release(): WARN_ON_ONCE(nr_bytes & (PAGE_SIZE - 1)); in mm/page_counter.c:60, function page_counter_cancel(): if (WARN_ONCE(new < 0, "page_counter underflow: %ld nr_pages=%lu\n", new, nr_pages)) zswap_stored_pages also becomes inconsistent in the same way. As suggested by Kanchana, increment zswap_stored_pages and charge zswap entries within zswap_store_page() when it succeeds. This way, zswap_entry_free() will decrement the counter and uncharge the entries when it failed to zswap the entire folio. While this could potentially be optimized by batching objcg charging and incrementing the counter, let's focus on fixing the bug this time and leave the optimization for later after some evaluation. After resolving the inconsistency, the warnings disappear. Fixes: b7c0ccdfbafd ("mm: zswap: support large folios in zswap_store()") Cc: stable@vger.kernel.org Co-developed-by: Kanchana P Sridhar Signed-off-by: Kanchana P Sridhar Signed-off-by: Hyeonggon Yoo <42.hyeyoo@gmail.com> Acked-by: Yosry Ahmed --- v2 -> v3: - Adjusted Kanchana's feedback: - Fixed inconsistency in zswap_stored_pages - Now objcg charging and incrementing zswap_store_pages is done within zswap_stored_pages, one by one mm/zswap.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 6504174fbc6a..f0bd962bffd5 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -1504,11 +1504,14 @@ static ssize_t zswap_store_page(struct page *page, entry->pool = pool; entry->swpentry = page_swpentry; entry->objcg = objcg; + if (objcg) + obj_cgroup_charge_zswap(objcg, entry->length); entry->referenced = true; if (entry->length) { INIT_LIST_HEAD(&entry->lru); zswap_lru_add(&zswap_list_lru, entry); } + atomic_long_inc(&zswap_stored_pages); return entry->length; @@ -1526,7 +1529,6 @@ bool zswap_store(struct folio *folio) struct obj_cgroup *objcg = NULL; struct mem_cgroup *memcg = NULL; struct zswap_pool *pool; - size_t compressed_bytes = 0; bool ret = false; long index; @@ -1569,15 +1571,11 @@ bool zswap_store(struct folio *folio) bytes = zswap_store_page(page, objcg, pool); if (bytes < 0) goto put_pool; - compressed_bytes += bytes; } - if (objcg) { - obj_cgroup_charge_zswap(objcg, compressed_bytes); + if (objcg) count_objcg_events(objcg, ZSWPOUT, nr_pages); - } - atomic_long_add(nr_pages, &zswap_stored_pages); count_vm_events(ZSWPOUT, nr_pages); ret = true;