Received: by 2002:ab2:788f:0:b0:1ee:8f2e:70ae with SMTP id b15csp290607lqi; Wed, 6 Mar 2024 18:17:37 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVqcKEzLUT0cX2BuD9Vu7Xyaji5O5xk26Ny4icNU9gCLeQD1lwRyBhkIS12GPeWgwyqI1tmTyS3bO06fAqCsvt6MbpvT8nBDR31rCG6Bg== X-Google-Smtp-Source: AGHT+IF/NFKobHn6cinJSNLyjHAYavfhrPiJVrVSWU43YSPfuCiQmjYXgvGPqFpInazRev+0Wxqr X-Received: by 2002:a05:622a:40a:b0:42e:bcd9:df0 with SMTP id n10-20020a05622a040a00b0042ebcd90df0mr7226108qtx.62.1709777856879; Wed, 06 Mar 2024 18:17:36 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709777856; cv=pass; d=google.com; s=arc-20160816; b=i2qZtV9p5q14Xr+9WznJvBIx1kjqPDkKhzE4QL0vRqTJmRqEUrfxF3OQs1vL1l6jwk ksR8vntnXvYbH0FeqWlvEipu0wSEHTccc3zE7XX18jFE+2Cj5Oq/RQ0C6Z5aqVLD8Cya Z5M6b1CJbdKGcdzUg6Hq+s/u9t8hLJXrr3Ji3HtoIPMcJaHF1fHBXMzaasNBGaPsAp6g B+llm0RrtNxqchs8BBEbweZHxPGhxH1wRuP9ZxjRlidLBouxOtl9x3/KUgAB0OaBbYte eHE4Atn1KHpoOXWIrUsAttVx4iFQL94Pn51laiZT640EMlBO/7mEIXCiig+ceM9likVK HJBw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=Zh+BLBA/5W+m6OdKvw3MFrvOs9dYu4ONkOQWKZRMgtY=; fh=SdU741ZmqM66Zu7xc6q3Eph7n4/hxDUZs1OnmA2okps=; b=qqWvnb1HF7M4rpsx2LvGH46dMHa0WTD//cBypUXUJ+UcHf3jQof9SFSbzT2xbsx61W Qq6O7iKox1wsEnKBKAcQoMUq0gPFQQlTFPck6MsX7w8+qZaWJz68hPCSMgTEB3r4TSWN 1X0sAXlP7Pxor3b9J6dBQU3EOUZq+FOZVaFyO4SMmINI3Ui7//cQ6iNe9ingtdh4bR5x rLXGwTPdxwEXQjx2W2ad4dHSzhWWMM0HhR6TYXDENPvTp0suYDLQaOpz5rL8pxb+nw0a 6w5vSPPtrYFmcgQStZRy+4ct5y+S7wuvh2iCJ/OT/kebawIit/pa/EvDACezxj7dIK37 pm3Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=Ng26Bvvf; arc=pass (i=1 spf=pass spfdomain=bytedance.com dkim=pass dkdomain=bytedance.com dmarc=pass fromdomain=bytedance.com); spf=pass (google.com: domain of linux-kernel+bounces-94860-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-94860-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id c2-20020ac85a82000000b0042f0a6c6ecdsi3319714qtc.391.2024.03.06.18.17.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 18:17:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-94860-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=Ng26Bvvf; arc=pass (i=1 spf=pass spfdomain=bytedance.com dkim=pass dkdomain=bytedance.com dmarc=pass fromdomain=bytedance.com); spf=pass (google.com: domain of linux-kernel+bounces-94860-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-94860-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 849601C218FF for ; Thu, 7 Mar 2024 02:17:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3112C6139; Thu, 7 Mar 2024 02:17:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="Ng26Bvvf" Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 028994C8B for ; Thu, 7 Mar 2024 02:17:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.52 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709777851; cv=none; b=YQyzceJ40yANYZB1zBA6J7gUhEA0b2OgZm19boioznPm2WLKXLiUtmfK/JGSvsvxlez8gupJ0gYaqyDN7SRgb0Pi6tMl2/r5xYsCe8Q0cp47kAmJWf+gmzilV3Ejuw0buybadJfGKrHYwp5gfHsisxA6Zt4QziW+fgzHocrM+mc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709777851; c=relaxed/simple; bh=aFu4YL6lK3K9KevCkte/apdg+absxttVWpiuTL5ABew=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=hxIo5+rbpHhguY27dxeBL6rv0t7J0D/3zHeqhLlQEajQh2mT0/Tzratq4VtwdzE7eGqAXEwGJyPpkfE3hDvkbwUHHLdRcDtxjH0w/zMw5AYdE1ru3d3Wj72ceogeJ6/sJrh3Q4dvWOuGyT2fW6J6XfYKfcxS8d9Wy5w8QqP4z+o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=Ng26Bvvf; arc=none smtp.client-ip=209.85.216.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-29a5f100c1aso189864a91.0 for ; Wed, 06 Mar 2024 18:17:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1709777848; x=1710382648; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=Zh+BLBA/5W+m6OdKvw3MFrvOs9dYu4ONkOQWKZRMgtY=; b=Ng26BvvfIZSp1O+Ck8/d9YDM8wIqgROE6fnTmU8F2lb64aXEzHX+KwUstbivxp5gDk iN2Ch05TkAQ64FNcRdf0a/lo7egoMV/2nHJC82xqE2PIuLIY/HHDmg1/qAK5cVIhY24L xfd143PkM77FigSqcOTocv9nz8hHlqJcT4f44DZn/qZr7Q6EyYO3QXFeyAn1PlTmq+if xJ1Zr4MxgyHDf0VK70tjFjjRM2ov1gYnkDQ6QnibcGUOL2hdVCb3qXGh1lJehOBxEvEU FX9fKden95IKieIeBpxHvXz9w/lS4Xq6U3nM+MMwaDywIN0MV31JNbcrL0rhrBeJGuQf Z9zQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709777848; x=1710382648; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Zh+BLBA/5W+m6OdKvw3MFrvOs9dYu4ONkOQWKZRMgtY=; b=Nm23bnM1Y3T2SIlG70/yy+A4745hbGUyn9A2cpDqfxbjwN6Qwms/wSWw4YjrrSNuh1 5A+ImRvrExD0Z8euDeuC26s0pP1BHUmwmenDCj2ITLhclHc3PBmCK16g0IveXcZA1c6W 7KIEUfZymuP7q54PlrVz6bAvkFWzgBZ+gqWmerp3yFuKy2iDPY9BS/NKDMdTiRVZonnk TtQvbOnN+Obm10QYUvXIsGT7BJChVv2H1RSTBbDpUqGGvJ9UCW69aMICaN3tTy40/hAX 54yNg86h7HECfaIjP+kRCtxs39/kyDc60ui/DKH8wOsox1ZBcp/YxRjA+xS7UEi20AD6 CwnQ== X-Forwarded-Encrypted: i=1; AJvYcCUx0P2sSj+XztN7HfMqDjLYsvv+cPSE5p3ro7HjuJAoOPwIIYwbFazDHEDjx/S0BGLo1b8hRsMHLUUswkLy2IaGesvrbgxANbNQpefF X-Gm-Message-State: AOJu0Yw3Ge+uKLVZCZBgKtm7y1Hd6gfJg2FMZK7rw5EoWIZ89bWpH8dV jGVn6svfib0L55E8veuvMnO69qYAtfcBky3RCzNLGRZ00jHWlBwI5allgY5JiDA= X-Received: by 2002:a17:90a:4615:b0:29b:a59:a12d with SMTP id w21-20020a17090a461500b0029b0a59a12dmr13038209pjg.25.1709777848051; Wed, 06 Mar 2024 18:17:28 -0800 (PST) Received: from [10.254.1.224] ([139.177.225.229]) by smtp.gmail.com with ESMTPSA id e8-20020a170902784800b001dcc7795524sm13531279pln.24.2024.03.06.18.17.24 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 06 Mar 2024 18:17:27 -0800 (PST) Message-ID: <000b8cee-79cc-4f28-b180-105f2abe6b82@bytedance.com> Date: Thu, 7 Mar 2024 10:17:22 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v4] zswap: replace RB tree with xarray Content-Language: en-US To: Chris Li Cc: Andrew Morton , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Yosry Ahmed , Nhat Pham , Johannes Weiner , "Matthew Wilcox (Oracle)" , Barry Song References: <20240304-zswap-xarray-v4-1-c4b45670cc30@kernel.org> <05cbfe34-bac1-4c16-92fa-f38b09160458@bytedance.com> From: Chengming Zhou In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 2024/3/7 05:12, Chris Li wrote: > Hi Chengming, > > On Mon, Mar 4, 2024 at 6:52 PM Chengming Zhou > wrote: >> >> Hi Chris, >> >> On 2024/3/5 05:32, Chris Li wrote: >>> Very deep RB tree requires rebalance at times. That >>> contributes to the zswap fault latencies. Xarray does not >>> need to perform tree rebalance. Replacing RB tree to xarray >>> can have some small performance gain. >>> >>> One small difference is that xarray insert might fail with >>> ENOMEM, while RB tree insert does not allocate additional >>> memory. >>> >>> The zswap_entry size will reduce a bit due to removing the >>> RB node, which has two pointers and a color field. Xarray >>> store the pointer in the xarray tree rather than the >>> zswap_entry. Every entry has one pointer from the xarray >>> tree. Overall, switching to xarray should save some memory, >>> if the swap entries are densely packed. >>> >>> Notice the zswap_rb_search and zswap_rb_insert always >>> followed by zswap_rb_erase. Use xa_erase directly. The entry >>> erase into zswap_xa_insert as well. That saves one tree >>> lookup as well. >>> >>> Remove zswap_invalidate_entry due to no need to call >>> zswap_rb_erase any more. Use zswap_free_entry instead. >>> >>> The "struct zswap_tree" has been replaced by "struct xarray". >>> The tree spin lock has transferred to the xarray lock. >>> >>> Run the kernel build testing 10 times for each version, averages: >>> (memory.max=2GB, zswap shrinker and writeback enabled, one 50GB swapfile.) >>> >>> mm-9a0181a3710eb xarray v4 >>> user 3526.829 3526.930 >>> sys 532.754 526.525 >>> real 198.748 198.850 >>> >>> --- >>> >>> >>> Signed-off-by: Chris Li >>> --- >>> Changes in v4: >>> - Remove zswap_xa_search_and_earse, use xa_erase directly. >>> - Move charge of objcg after zswap_xa_insert. >>> - Avoid erase old entry on insert fail error path. >>> - Remove not needed swap_zswap_tree change >>> - Link to v3: https://lore.kernel.org/r/20240302-zswap-xarray-v3-1-5900252f2302@kernel.org >>> >>> Changes in v3: >>> - Use xa_cmpxchg instead of zswap_xa_search_and_delete in zswap_writeback_entry. >>> - Use xa_store in zswap_xa_insert directly. Reduce the scope of spinlock. >>> - Fix xa_store error handling for same page fill case. >>> - Link to v2: https://lore.kernel.org/r/20240229-zswap-xarray-v2-1-e50284dfcdb1@kernel.org >>> >>> Changes in v2: >>> - Replace struct zswap_tree with struct xarray. >>> - Remove zswap_tree spinlock, use xarray lock instead. >>> - Fold zswap_rb_erase() into zswap_xa_search_and_delete() and zswap_xa_insert(). >>> - Delete zswap_invalidate_entry(), use zswap_free_entry() instead. >>> - Link to v1: https://lore.kernel.org/r/20240117-zswap-xarray-v1-0-6daa86c08fae@kernel.org >>> --- >>> mm/zswap.c | 186 ++++++++++++++++++++++++------------------------------------- >>> 1 file changed, 72 insertions(+), 114 deletions(-) >>> >>> diff --git a/mm/zswap.c b/mm/zswap.c >>> index 011e068eb355..4f4a3f452b76 100644 >>> --- a/mm/zswap.c >>> +++ b/mm/zswap.c >>> @@ -20,7 +20,6 @@ >>> #include >>> #include >>> #include >>> -#include >>> #include >>> #include >>> #include >>> @@ -71,6 +70,8 @@ static u64 zswap_reject_compress_poor; >>> static u64 zswap_reject_alloc_fail; >>> /* Store failed because the entry metadata could not be allocated (rare) */ >>> static u64 zswap_reject_kmemcache_fail; >>> +/* Store failed because xarray can't insert the entry*/ >>> +static u64 zswap_reject_xarray_fail; >>> >>> /* Shrinker work queue */ >>> static struct workqueue_struct *shrink_wq; >>> @@ -196,7 +197,6 @@ static struct { >>> * This structure contains the metadata for tracking a single compressed >>> * page within zswap. >>> * >>> - * rbnode - links the entry into red-black tree for the appropriate swap type >>> * swpentry - associated swap entry, the offset indexes into the red-black tree >>> * length - the length in bytes of the compressed page data. Needed during >>> * decompression. For a same value filled page length is 0, and both >>> @@ -208,7 +208,6 @@ static struct { >>> * lru - handle to the pool's lru used to evict pages. >>> */ >>> struct zswap_entry { >>> - struct rb_node rbnode; >>> swp_entry_t swpentry; >>> unsigned int length; >>> struct zswap_pool *pool; >>> @@ -220,12 +219,7 @@ struct zswap_entry { >>> struct list_head lru; >>> }; >>> >>> -struct zswap_tree { >>> - struct rb_root rbroot; >>> - spinlock_t lock; >>> -}; >>> - >>> -static struct zswap_tree *zswap_trees[MAX_SWAPFILES]; >>> +static struct xarray *zswap_trees[MAX_SWAPFILES]; >>> static unsigned int nr_zswap_trees[MAX_SWAPFILES]; >>> >>> /* RCU-protected iteration */ >>> @@ -253,7 +247,7 @@ static bool zswap_has_pool; >>> * helpers and fwd declarations >>> **********************************/ >>> >>> -static inline struct zswap_tree *swap_zswap_tree(swp_entry_t swp) >>> +static inline struct xarray *swap_zswap_tree(swp_entry_t swp) >>> { >>> return &zswap_trees[swp_type(swp)][swp_offset(swp) >>> >> SWAP_ADDRESS_SPACE_SHIFT]; >>> @@ -805,60 +799,33 @@ void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg) >>> } >>> >>> /********************************* >>> -* rbtree functions >>> +* xarray functions >>> **********************************/ >>> -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); >>> - entry_offset = swp_offset(entry->swpentry); >>> - if (entry_offset > offset) >>> - node = node->rb_left; >>> - else if (entry_offset < offset) >>> - node = node->rb_right; >>> - else >>> - return entry; >>> - } >>> - return NULL; >>> -} >>> >>> /* >>> * In the case that a entry with the same offset is found, a pointer to >>> - * the existing entry is stored in dupentry and the function returns -EEXIST >>> + * the existing entry is stored in old and erased from the tree. >>> + * Function return error on insert. >>> */ >>> -static int zswap_rb_insert(struct rb_root *root, struct zswap_entry *entry, >>> - struct zswap_entry **dupentry) >>> +static int zswap_xa_insert(struct xarray *tree, struct zswap_entry *entry, >>> + struct zswap_entry **old) >>> { >>> - 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); >>> - myentry_offset = swp_offset(myentry->swpentry); >>> - if (myentry_offset > entry_offset) >>> - link = &(*link)->rb_left; >>> - else if (myentry_offset < entry_offset) >>> - link = &(*link)->rb_right; >>> - else { >>> - *dupentry = myentry; >>> - return -EEXIST; >>> - } >>> - } >>> - rb_link_node(&entry->rbnode, parent, link); >>> - rb_insert_color(&entry->rbnode, root); >>> - return 0; >>> -} >>> + int err; >>> + struct zswap_entry *e; >>> + pgoff_t offset = swp_offset(entry->swpentry); >>> >>> -static void zswap_rb_erase(struct rb_root *root, struct zswap_entry *entry) >>> -{ >>> - rb_erase(&entry->rbnode, root); >>> - RB_CLEAR_NODE(&entry->rbnode); >>> + e = xa_store(tree, offset, entry, GFP_KERNEL); >>> + err = xa_err(e); >>> + >>> + if (err) { >>> + e = xa_erase(tree, offset); >>> + if (err == -ENOMEM) >>> + zswap_reject_alloc_fail++; >>> + else >>> + zswap_reject_xarray_fail++; >>> + } >>> + *old = e; >>> + return err; >>> } >>> >>> /********************************* >>> @@ -872,7 +839,6 @@ static struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp, int nid) >>> entry = kmem_cache_alloc_node(zswap_entry_cache, gfp, nid); >>> if (!entry) >>> return NULL; >>> - RB_CLEAR_NODE(&entry->rbnode); >>> return entry; >>> } >>> >>> @@ -914,17 +880,6 @@ static void zswap_entry_free(struct zswap_entry *entry) >>> zswap_update_total_size(); >>> } >>> >>> -/* >>> - * The caller hold the tree lock and search the entry from the tree, >>> - * so it must be on the tree, remove it from the tree and free it. >>> - */ >>> -static void zswap_invalidate_entry(struct zswap_tree *tree, >>> - struct zswap_entry *entry) >>> -{ >>> - zswap_rb_erase(&tree->rbroot, entry); >>> - zswap_entry_free(entry); >>> -} >>> - >>> /********************************* >>> * compressed storage functions >>> **********************************/ >>> @@ -1113,7 +1068,9 @@ static void zswap_decompress(struct zswap_entry *entry, struct page *page) >>> static int zswap_writeback_entry(struct zswap_entry *entry, >>> swp_entry_t swpentry) >>> { >>> - struct zswap_tree *tree; >>> + struct xarray *tree; >>> + pgoff_t offset = swp_offset(swpentry); >>> + struct zswap_entry *e; >>> struct folio *folio; >>> struct mempolicy *mpol; >>> bool folio_was_allocated; >>> @@ -1150,19 +1107,14 @@ static int zswap_writeback_entry(struct zswap_entry *entry, >>> * be dereferenced. >>> */ >>> tree = swap_zswap_tree(swpentry); >>> - spin_lock(&tree->lock); >>> - if (zswap_rb_search(&tree->rbroot, swp_offset(swpentry)) != entry) { >>> - spin_unlock(&tree->lock); >>> + e = xa_cmpxchg(tree, offset, entry, NULL, GFP_KERNEL); >>> + if (e != entry) { >> >> Maybe "if (xa_cmpxchg() != entry)" look better, so "e" variable can be removed, >> since we don't use it. > > I thought about that. The main reason "if (xa_cmpxchg(tree, offset, > entry, NULL, GFP_KERNEL) != entry)" is a long expression. Having > != entry at the end of the expression makes it harder to read. Have > the temporary variable IMHO help reading the comparison. > > Not sure I understand the motivation to save a temporary variable. The > compiler always generates temporary variables internally. Having one > here does not affect the compiling result at all. It is just there to > help reading. If you still think it has the reverse effect on reading > I can of course remove it. It generates the same code anyway. You are right, just a minor type suggestion, no much difference if you keep it. > >> >>> delete_from_swap_cache(folio); >>> folio_unlock(folio); >>> folio_put(folio); >>> return -ENOMEM; >>> } >>> >>> - /* Safe to deref entry after the entry is verified above. */ >>> - zswap_rb_erase(&tree->rbroot, entry); >>> - spin_unlock(&tree->lock); >>> - >>> zswap_decompress(entry, &folio->page); >>> >>> count_vm_event(ZSWPWB); >>> @@ -1471,10 +1423,12 @@ bool zswap_store(struct folio *folio) >>> { >>> swp_entry_t swp = folio->swap; >>> pgoff_t offset = swp_offset(swp); >>> - struct zswap_tree *tree = swap_zswap_tree(swp); >>> - struct zswap_entry *entry, *dupentry; >>> + struct xarray *tree = swap_zswap_tree(swp); >>> + struct zswap_entry *entry, *old; >>> struct obj_cgroup *objcg = NULL; >>> struct mem_cgroup *memcg = NULL; >>> + int err; >>> + bool old_erased = false; >>> >>> VM_WARN_ON_ONCE(!folio_test_locked(folio)); >>> VM_WARN_ON_ONCE(!folio_test_swapcache(folio)); >>> @@ -1526,6 +1480,7 @@ bool zswap_store(struct folio *folio) >>> kunmap_local(src); >>> entry->length = 0; >>> entry->value = value; >>> + entry->pool = NULL; >>> atomic_inc(&zswap_same_filled_pages); >>> goto insert_entry; >>> } >>> @@ -1555,28 +1510,31 @@ bool zswap_store(struct folio *folio) >>> insert_entry: >>> entry->swpentry = swp; >>> entry->objcg = objcg; >>> - if (objcg) { >>> - obj_cgroup_charge_zswap(objcg, entry->length); >>> - /* Account before objcg ref is moved to tree */ >>> - count_objcg_event(objcg, ZSWPOUT); >>> - } >>> >>> /* map */ >>> - spin_lock(&tree->lock); >>> /* >>> * The folio may have been dirtied again, invalidate the >>> * possibly stale entry before inserting the new entry. >>> */ >>> - if (zswap_rb_insert(&tree->rbroot, entry, &dupentry) == -EEXIST) { >>> - zswap_invalidate_entry(tree, dupentry); >>> - WARN_ON(zswap_rb_insert(&tree->rbroot, entry, &dupentry)); >>> + err = zswap_xa_insert(tree, entry, &old); >>> + if (old) >>> + zswap_entry_free(old); >>> + if (err) { >>> + old_erased = true; >>> + goto insert_failed; >>> } >> >> It looks a little complicated for me :( How do you think this like below? >> >> old = xa_store(tree, offset, entry, GFP_KERNEL); >> if (xa_is_err(old)) > > Might want to bump up the ENOMEM error count if it is out of memory > since zswap tracks it. Right. > >> goto store_failed; >> >> if (old) >> zswap_entry_free(old); >> >> Then zswap_xa_insert() wrapper can be removed since no use elsewhere. >> So the error handling path is kept much the same as before and simpler. > > Sure. > >> >>> + >>> + if (objcg) { >>> + obj_cgroup_charge_zswap(objcg, entry->length); >>> + /* Account before objcg ref is moved to tree */ >>> + count_objcg_event(objcg, ZSWPOUT); >>> + } >>> + >>> if (entry->length) { >>> INIT_LIST_HEAD(&entry->lru); >>> zswap_lru_add(&zswap.list_lru, entry); >>> atomic_inc(&zswap.nr_stored); >>> } >>> - spin_unlock(&tree->lock); >>> >>> /* update stats */ >>> atomic_inc(&zswap_stored_pages); >>> @@ -1585,6 +1543,12 @@ bool zswap_store(struct folio *folio) >>> >>> return true; >>> >>> +insert_failed: >>> + if (!entry->length) { >>> + atomic_dec(&zswap_same_filled_pages); >>> + goto freepage; >>> + } >>> + zpool_free(zswap_find_zpool(entry), entry->handle); >> >> entry->pool can be used here instead of zswap_find_zpool(). > > Ack. > > I was following the code in zswap_entry_free(). Does it mean the > zswap_find_zpool(entry) can also use entry->pool? Ah, I just see your other email, you are right, this is zpool. Thanks. > > Thanks for the feedback. > > Chris > >> >> Thanks! >> >>> put_pool: >>> zswap_pool_put(entry->pool); >>> freepage: >>> @@ -1592,17 +1556,19 @@ bool zswap_store(struct folio *folio) >>> reject: >>> if (objcg) >>> obj_cgroup_put(objcg); >>> + >>> + if (old_erased) >>> + goto failed; >>> check_old: >>> /* >>> * If the zswap store fails or zswap is disabled, we must invalidate the >>> * possibly stale entry which was previously stored at this offset. >>> * Otherwise, writeback could overwrite the new data in the swapfile. >>> */ >>> - spin_lock(&tree->lock); >>> - entry = zswap_rb_search(&tree->rbroot, offset); >>> + entry = xa_erase(tree, offset); >>> if (entry) >>> - zswap_invalidate_entry(tree, entry); >>> - spin_unlock(&tree->lock); >>> + zswap_entry_free(entry); >>> +failed: >>> return false; >>> >>> shrink: >>> @@ -1615,20 +1581,15 @@ bool zswap_load(struct folio *folio) >>> swp_entry_t swp = folio->swap; >>> pgoff_t offset = swp_offset(swp); >>> struct page *page = &folio->page; >>> - struct zswap_tree *tree = swap_zswap_tree(swp); >>> + struct xarray *tree = swap_zswap_tree(swp); >>> struct zswap_entry *entry; >>> u8 *dst; >>> >>> VM_WARN_ON_ONCE(!folio_test_locked(folio)); >>> >>> - spin_lock(&tree->lock); >>> - entry = zswap_rb_search(&tree->rbroot, offset); >>> - if (!entry) { >>> - spin_unlock(&tree->lock); >>> + entry = xa_erase(tree, offset); >>> + if (!entry) >>> return false; >>> - } >>> - zswap_rb_erase(&tree->rbroot, entry); >>> - spin_unlock(&tree->lock); >>> >>> if (entry->length) >>> zswap_decompress(entry, page); >>> @@ -1652,19 +1613,17 @@ bool zswap_load(struct folio *folio) >>> void zswap_invalidate(swp_entry_t swp) >>> { >>> pgoff_t offset = swp_offset(swp); >>> - struct zswap_tree *tree = swap_zswap_tree(swp); >>> + struct xarray *tree = swap_zswap_tree(swp); >>> struct zswap_entry *entry; >>> >>> - spin_lock(&tree->lock); >>> - entry = zswap_rb_search(&tree->rbroot, offset); >>> + entry = xa_erase(tree, offset); >>> if (entry) >>> - zswap_invalidate_entry(tree, entry); >>> - spin_unlock(&tree->lock); >>> + zswap_entry_free(entry); >>> } >>> >>> int zswap_swapon(int type, unsigned long nr_pages) >>> { >>> - struct zswap_tree *trees, *tree; >>> + struct xarray *trees, *tree; >>> unsigned int nr, i; >>> >>> nr = DIV_ROUND_UP(nr_pages, SWAP_ADDRESS_SPACE_PAGES); >>> @@ -1674,11 +1633,8 @@ int zswap_swapon(int type, unsigned long nr_pages) >>> return -ENOMEM; >>> } >>> >>> - for (i = 0; i < nr; i++) { >>> - tree = trees + i; >>> - tree->rbroot = RB_ROOT; >>> - spin_lock_init(&tree->lock); >>> - } >>> + for (i = 0; i < nr; i++) >>> + xa_init(trees + i); >>> >>> nr_zswap_trees[type] = nr; >>> zswap_trees[type] = trees; >>> @@ -1687,7 +1643,7 @@ int zswap_swapon(int type, unsigned long nr_pages) >>> >>> void zswap_swapoff(int type) >>> { >>> - struct zswap_tree *trees = zswap_trees[type]; >>> + struct xarray *trees = zswap_trees[type]; >>> unsigned int i; >>> >>> if (!trees) >>> @@ -1695,7 +1651,7 @@ void zswap_swapoff(int type) >>> >>> /* try_to_unuse() invalidated all the entries already */ >>> for (i = 0; i < nr_zswap_trees[type]; i++) >>> - WARN_ON_ONCE(!RB_EMPTY_ROOT(&trees[i].rbroot)); >>> + WARN_ON_ONCE(!xa_empty(trees + i)); >>> >>> kvfree(trees); >>> nr_zswap_trees[type] = 0; >>> @@ -1727,6 +1683,8 @@ static int zswap_debugfs_init(void) >>> zswap_debugfs_root, &zswap_reject_kmemcache_fail); >>> debugfs_create_u64("reject_compress_fail", 0444, >>> zswap_debugfs_root, &zswap_reject_compress_fail); >>> + debugfs_create_u64("reject_xarray_fail", 0444, >>> + zswap_debugfs_root, &zswap_reject_xarray_fail); >>> debugfs_create_u64("reject_compress_poor", 0444, >>> zswap_debugfs_root, &zswap_reject_compress_poor); >>> debugfs_create_u64("written_back_pages", 0444, >>> >>> --- >>> base-commit: 9a0181a3710eba1f5c6d19eadcca888be3d54e4f >>> change-id: 20240104-zswap-xarray-716260e541e3 >>> >>> Best regards, >>