From patchwork Mon Mar 25 23:50:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13603183 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 6AAE3CD11BF for ; Mon, 25 Mar 2024 23:50:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AC4116B0085; Mon, 25 Mar 2024 19:50:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9ADC46B0087; Mon, 25 Mar 2024 19:50:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 802E96B0088; Mon, 25 Mar 2024 19:50: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 5F0286B0085 for ; Mon, 25 Mar 2024 19:50:26 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 39C5A40157 for ; Mon, 25 Mar 2024 23:50:26 +0000 (UTC) X-FDA: 81937208052.20.98FE569 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf25.hostedemail.com (Postfix) with ESMTP id 6A7CFA0011 for ; Mon, 25 Mar 2024 23:50:24 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=R1JBf600; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf25.hostedemail.com: domain of 3vw0CZgoKCBoOEIHO07C436EE6B4.2ECB8DKN-CCAL02A.EH6@flex--yosryahmed.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3vw0CZgoKCBoOEIHO07C436EE6B4.2ECB8DKN-CCAL02A.EH6@flex--yosryahmed.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711410624; a=rsa-sha256; cv=none; b=RHZye6qW16MnjzI9XHtw3j/EKbUz52kXU2TwR7sMGomPPH00EeDCpSmWxwOussL7yie1vD Nra2mE9bc3D8s72yJp7Z7VWCMNCxfFol32plsZ2otCOJt7rw3g3ZLoKZXmVhyHaaGKZ8mZ LhSpPnB7ELE08Y0MIHzhbJf3GMq8/mU= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=R1JBf600; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf25.hostedemail.com: domain of 3vw0CZgoKCBoOEIHO07C436EE6B4.2ECB8DKN-CCAL02A.EH6@flex--yosryahmed.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3vw0CZgoKCBoOEIHO07C436EE6B4.2ECB8DKN-CCAL02A.EH6@flex--yosryahmed.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711410624; 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=ltPcwa62HBgtxDU26adJs5Ek9NcA4wCnN/irpaDy45s=; b=ZrH0I8bGhGfLbitPAU51Ivrww5pH0Gu1j1YpmcQduw2aG5vm+Amp99XWgHgZAQbRvRv6DM SwRH2qUovEbUf2DwDxunOlPQbydaKSsrQuLoEG51LANCWGpXlj0YJLz07blBKBR7+v/XYQ C6xqMTQGJvhflDl2gLmLniIRrBeT0LM= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-60cbba6f571so93109107b3.1 for ; Mon, 25 Mar 2024 16:50:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711410623; x=1712015423; 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=ltPcwa62HBgtxDU26adJs5Ek9NcA4wCnN/irpaDy45s=; b=R1JBf600OGOWrvvrk3j88awfQZH+5VhixQpUc08U4RyW0OgBJD+v8ZKA4rmzLwHF6F DNO3ODQaHBy6041eiJqjYnXOWCS/SNPbXt7BZPgJEv61yhm4eUFlArxn3nifHOKhiDqk n/68CwS2aVbDUOHbms2ocKkDGUAhdO1S50xCVC4gvsUTreWmS1sG0lG4f41FkE5QVzWk O6itxxdjxaeqkooYplxwfP+OyRLiyrsZ3Pia9QZ1b1Ka802b63witUSBcZMcm3GkQ5um SlfRbX+b314SUQRD1bcsRMy58xfpvB9HbI0r+VAjhhJOkL913fOKpPxutW1zNKykMLLg qdyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711410623; x=1712015423; 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=ltPcwa62HBgtxDU26adJs5Ek9NcA4wCnN/irpaDy45s=; b=n7xLJ/mwi2w66gZih/WeroCIy+8sl9z8skb9Sg09v+BDvKxbXPz4BYNamY0d3RTH5p huHpfumlRuYqAc93wLlgTPpTbyqhslDOLSeyCpVIsxEPxNw39oq5gfRGwfCSPZp7PFCs DP0Yow1YStpQN53wjyx+0MDahr+n2zR3fP51fjWNx/mDwwUFbeLUFREXNuX2Qwl7wUYY 8sVP5QWoU1ovN4LtOmhu/z2OQOOeGytW2BWhcFkVoFR/M59wZu3qYsqbhsktQvCW8dcN zPXznH5sE8eNERBmU3qNg0xBq0bdoKLA1GYLc5y1VgAhOWn8ZHZMD7Q5Men89e0qeiaD pHYQ== X-Forwarded-Encrypted: i=1; AJvYcCURH+o6/efJHZkmQqORqMFkeE+6bZSKkr7ykSsSu8bwmVJC+WK3Ep581vBnVdGIYCllCXTbBWdZ81rpvDiFrxQTtRY= X-Gm-Message-State: AOJu0YyK96yVDyEI8XmxBABAlz6ScdWSSTkgylw7pt0adsHdaZzWFWNF tyrnbJJAB8cXx+uv5WawCYU1QWJ8pxbPEwZVAx0rXsOAw3diSbMADYmblwjcReuIC3SslyJuZg1 PKuOuvzy6qFFlzzglxw== X-Google-Smtp-Source: AGHT+IETdRLWcKvQU7ALPH/FxGMfk6VVMBGbdetkJernCRp2EMtMjfpphTVvqJ9nLnh8UXcxjIFAV/8JU9lCEqEU X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a0d:eb04:0:b0:610:d0c9:696b with SMTP id u4-20020a0deb04000000b00610d0c9696bmr2096495ywe.8.1711410623435; Mon, 25 Mar 2024 16:50:23 -0700 (PDT) Date: Mon, 25 Mar 2024 23:50:10 +0000 In-Reply-To: <20240325235018.2028408-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20240325235018.2028408-1-yosryahmed@google.com> X-Mailer: git-send-email 2.44.0.396.g6e790dbe36-goog Message-ID: <20240325235018.2028408-3-yosryahmed@google.com> Subject: [RFC PATCH 2/9] mm: zswap: refactor storing to the tree out of zswap_store() 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: rspam06 X-Rspamd-Queue-Id: 6A7CFA0011 X-Stat-Signature: 518h3xeyaknmdaok6bjag18y7sekw7wt X-HE-Tag: 1711410624-487062 X-HE-Meta: U2FsdGVkX19bRK8avoBZjEAfCTArpO05bwH2ZvqRAFj8GXiH+9UIW3NctzI6FY9Y4A0q0lNk4A8aI6J8WBecqPKEER+fHytgLr2RVgTa8WGmLinED7ne/M84YF7EYeb3kKgj8N9NMGM1NlIb2IOmfNzfA6eKW3VIqJbrDbAF5UBrl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I8fsFUBw/btkdPJ8UUbp67I0Itw/E2z+OUA4MEFaStstkWd3gHsEM29xU5qyl4W0n8fWTN9QN6Z4epop+SYTGzYXmGYBfnZ9744OdqIOpqHTVUm6UcvQwLVOvLHnZWa8nxXTBlCWps5+R3RTvS3fHCtn35kdGMGIKv9bjfb29+o48XtDxr4wDyF8XVCgW36oyFb3tgk68UgRq4m1cEuohh9sxEeFtDSSTuj+FX4n6iEpP12MvLrV2bkMF1WAyxM8Xl8NiU97zEEwNK8crDnrV2VVJEErDvJPkH+LeH+jmK0vGmOEmP/cyPbPQZHC8PE6vsQZhLPcmBXDEME/rDgKdOpX6q7M5oe1yhyh18Z6VnDb2H7Noll/TXGM4gPkSiZ1uNX2DtBkKBs/dyORNMuoD/9yPx5WWBbbcpxoxN7RijDrrFoVd9TGLiDb7A0gMIos6w== 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: Refactor the code that attempts storing to the xarray, handling erros, and freeing stale entries into a helper. This will be reused in a following patch to free other types of tree elements as well. Signed-off-by: Yosry Ahmed --- mm/zswap.c | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 1cf3ab4b22e64..ff1975afb7e3d 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -827,6 +827,30 @@ static void zswap_entry_free(struct zswap_entry *entry) atomic_dec(&zswap_stored_pages); } +/********************************* +* zswap tree functions +**********************************/ +static int zswap_tree_store(struct xarray *tree, pgoff_t offset, void *new) +{ + void *old; + int err; + + old = xa_store(tree, offset, new, GFP_KERNEL); + err = xa_is_err(old); + if (err) { + WARN_ONCE(err != -ENOMEM, "unexpected xarray error: %d\n", err); + zswap_reject_alloc_fail++; + } else if (old) { + /* + * We may have had an existing entry that became stale when + * the folio was redirtied and now the new version is being + * swapped out. Get rid of the old. + */ + zswap_entry_free(old); + } + return err; +} + /********************************* * compressed storage functions **********************************/ @@ -1396,10 +1420,10 @@ bool zswap_store(struct folio *folio) swp_entry_t swp = folio->swap; pgoff_t offset = swp_offset(swp); struct xarray *tree = swap_zswap_tree(swp); - struct zswap_entry *entry, *old; struct obj_cgroup *objcg = NULL; struct mem_cgroup *memcg = NULL; unsigned long max_pages, cur_pages; + struct zswap_entry *entry; VM_WARN_ON_ONCE(!folio_test_locked(folio)); VM_WARN_ON_ONCE(!folio_test_swapcache(folio)); @@ -1485,22 +1509,8 @@ bool zswap_store(struct folio *folio) entry->swpentry = swp; entry->objcg = objcg; - old = xa_store(tree, offset, entry, GFP_KERNEL); - if (xa_is_err(old)) { - int err = xa_err(old); - - WARN_ONCE(err != -ENOMEM, "unexpected xarray error: %d\n", err); - zswap_reject_alloc_fail++; + if (zswap_tree_store(tree, offset, entry)) goto store_failed; - } - - /* - * We may have had an existing entry that became stale when - * the folio was redirtied and now the new version is being - * swapped out. Get rid of the old. - */ - if (old) - zswap_entry_free(old); if (objcg) { obj_cgroup_charge_zswap(objcg, entry->length);