Received: by 2002:a05:7412:ba23:b0:fa:4c10:6cad with SMTP id jp35csp797337rdb; Thu, 18 Jan 2024 21:49:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IGro8TTS7ycwdJycim2T0B2xIUJCV2kJfyQNdJYuAoroEboI/fAUWBXbYCkm4Y5TQzCCTn1 X-Received: by 2002:aa7:c0ce:0:b0:553:5577:dd52 with SMTP id j14-20020aa7c0ce000000b005535577dd52mr1031272edp.81.1705643387128; Thu, 18 Jan 2024 21:49:47 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705643387; cv=pass; d=google.com; s=arc-20160816; b=TjwRDctv0JPeij503SJFWuCet37D5vAYQ8NY+vZJVbLJfb5S0TDev+treMEzHejqse /ZTnuZxblBllbwpuw2r84kSWqqWAASb1PUmJbnG31tK4/iPTckq87mVzAXzGgGQdBjIO 3Zah0HPnK3O5N2JmV/YGGwruk/4lfY++qzJa9caX7hCHxf1lj0V86vwDlXhRLRYmr18Q fQW9bZfnxaqeQyIWkqdXtOI7m/iF2ba2tAsHwRggDQa25JSvz5NhcXju0bOUjiseGZqt mbVP13T716NiFBeglXQ6Yh0s53ruh4y2UxpqvnBOb+ch13c+rOEOoL+sVdcXmq/2aXRC 8OLw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=W4+vuLtpjFuzZZSsQ7+j0wMlZoDWStDlcw372yDvlZw=; fh=3G+LrlaXkaxjxsos+peGn2UxPridobGTAuxKHlBj55Y=; b=xKIgkvNgwj9nIcixIjNE4ZenKqHgI2Jfzm+FascTV0141/Q63nz/8Z20+jbK58Hscw 5arlzXp4OWdzjNE+Yhuj32r6MyIKdqHbJl7Q6b7wOV0JymwiIASMzWgQXdHvWymk0G+l TtIeDyPEpkJZUxsk54j3q5RLjpN6wvN6V7xPLAORP/qWJ/noHxtf96JKvLwfAPcv2ebq SXvkRno5zOtkIPeMzaXv+AaSiWzcqGh50R+LNzOlaEj9DGDzQepKFLqcP/lKTqNWyZbb yhrJn6HDFbdcWsek4jhZYLflX1qYInj38p1J6/1bsELNYlzlvT46ftueSaP/TnxAjuj9 mWzw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=FTxQXpuL; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-30788-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-30788-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id z18-20020aa7c652000000b00559cfccce2bsi2638954edr.394.2024.01.18.21.49.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Jan 2024 21:49:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-30788-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=FTxQXpuL; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-30788-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-30788-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org 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 am.mirrors.kernel.org (Postfix) with ESMTPS id 3CD7C1F23901 for ; Fri, 19 Jan 2024 05:49:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 45B4863C1; Fri, 19 Jan 2024 05:49:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="FTxQXpuL" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5340F4A0A for ; Fri, 19 Jan 2024 05:49:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705643355; cv=none; b=ppRO/n5lxLL62CTPuzqHdJoPO4B5RYtnpH14tFuJzO5cqaU9RXamaDgQOO4mCRVUxCQc+hjYUFTImESxLuw3qEV+K8+d2S75d5EhUETws4EE5x0byixrR1YjZCHaT3mfVYzKdtJY0aoRA6kgFL1LZPiBBEYkxxUN1xUzgbz850Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705643355; c=relaxed/simple; bh=gFaoz5Lad+xTGdXdZ7SAqJDCdLlZZMjHIoUjSxAe/Ws=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=df9EsaHv5Ecz4mJ0muL81Tx15qKr9aX9TsPqqNEpyziYlvE3SpFbx47hSDShHhP0h1KXfAlBl33qtPEf6rdfAL7mYl65xVWOxTyGY4//WF1urkb4gyRj/3q/RMmPeM2Tl+cz58wvYz5SoiAC17NmMKKn1uOcjqMu2BH0EWSvrNU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FTxQXpuL; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 16377C433F1 for ; Fri, 19 Jan 2024 05:49:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1705643355; bh=gFaoz5Lad+xTGdXdZ7SAqJDCdLlZZMjHIoUjSxAe/Ws=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=FTxQXpuLFOJfe145uoTZI2lFRgbOWDzvMPE6rUfq4pGJhAbWk0jYK+jbs94X16kXJ PSpyO0xNKyPGV5B1WyAfVCu0WMXPJhKxi1MW35bLoqlbgfFIeZ7ZZpa6V1lsOzVp0w nqKiYJ8aSn1wAjuvX4kL3aCCZLf7ibD40/FNZ6VCDHM3sk9Y9gbpDlum9Dd7KkGRIf O3KuK8GJqZWAXDOexLp8WN6jCIrgIQBlkDMWkQXaOdPiraaxu1818hI88cjkDS2llT qmlEh0BxSE2SfXHQCsa9w718A9vEDcvAjDF8RMzcD4EeBiGrpRWCPH83c9PTJnPQi5 slkIOH7urPmNw== Received: by mail-oo1-f45.google.com with SMTP id 006d021491bc7-59960d26f65so8941eaf.0 for ; Thu, 18 Jan 2024 21:49:14 -0800 (PST) X-Gm-Message-State: AOJu0YzK0XM4FgpMfZlZsnFRPDneZE8ncKD/fAhsN2Wdeho2vLevPlxv cmlPrhmhDwtS0sOTMglnAKovA2lmVwTsV5WeF0PeqnZ1XGz0QXeq/XYV3W/kIBaKP2jmLJ7ycLq PBiCS6oWiOzJBrHdYUCxLwadRbZz7kLUFgApo X-Received: by 2002:a05:6359:320c:b0:175:67e3:cb28 with SMTP id rj12-20020a056359320c00b0017567e3cb28mr1756236rwb.12.1705643353344; Thu, 18 Jan 2024 21:49:13 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240117-zswap-xarray-v1-0-6daa86c08fae@kernel.org> <20240117-zswap-xarray-v1-2-6daa86c08fae@kernel.org> In-Reply-To: From: Chris Li Date: Thu, 18 Jan 2024 21:49:02 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 2/2] mm: zswap.c: remove RB tree To: Yosry Ahmed Cc: Andrew Morton , linux-kernel@vger.kernel.org, linux-mm@kvack.org, =?UTF-8?B?V2VpIFh177+8?= , Yu Zhao , Greg Thelen , Chun-Tse Shao , =?UTF-8?Q?Suren_Baghdasaryan=EF=BF=BC?= , Brain Geffon , Minchan Kim , Michal Hocko , Mel Gorman , Huang Ying , Nhat Pham , Johannes Weiner , Kairui Song , Zhongkun He , Kemeng Shi , Barry Song , "Matthew Wilcox (Oracle)" , "Liam R. Howlett" , Joel Fernandes , Chengming Zhou Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Jan 18, 2024 at 11:36=E2=80=AFAM Yosry Ahmed wrote: > > On Wed, Jan 17, 2024 at 10:35=E2=80=AFPM Yosry Ahmed wrote: > > > > > @@ -493,45 +471,47 @@ static struct zswap_entry *zswap_search(struct = zswap_tree *tree, pgoff_t offset) > > > static int zswap_insert(struct zswap_tree *tree, struct zswap_entry = *entry, > > > struct zswap_entry **dupentry) > > > { > > > - struct rb_root *root =3D &tree->rbroot; > > > - struct rb_node **link =3D &root->rb_node, *parent =3D NULL; > > > - struct zswap_entry *myentry, *old; > > > - pgoff_t myentry_offset, entry_offset =3D swp_offset(entry->sw= pentry); > > > - > > > - > > > - while (*link) { > > > - parent =3D *link; > > > - myentry =3D rb_entry(parent, struct zswap_entry, rbno= de); > > > - myentry_offset =3D swp_offset(myentry->swpentry); > > > - if (myentry_offset > entry_offset) > > > - link =3D &(*link)->rb_left; > > > - else if (myentry_offset < entry_offset) > > > - link =3D &(*link)->rb_right; > > > - else { > > > - old =3D xa_load(&tree->xarray, entry_offset); > > > - BUG_ON(old !=3D myentry); > > > - *dupentry =3D myentry; > > > + struct zswap_entry *e; > > > + pgoff_t offset =3D swp_offset(entry->swpentry); > > > + XA_STATE(xas, &tree->xarray, offset); > > > + > > > + do { > > > + xas_lock_irq(&xas); > > > + do { > > > + e =3D xas_load(&xas); > > > + if (xa_is_zero(e)) > > > + e =3D NULL; > > > + } while (xas_retry(&xas, e)); > > > + if (xas_valid(&xas) && e) { > > > + xas_unlock_irq(&xas); > > > + *dupentry =3D e; > > > return -EEXIST; > > > } > > > - } > > > - rb_link_node(&entry->rbnode, parent, link); > > > - rb_insert_color(&entry->rbnode, root); > > > - old =3D xa_store(&tree->xarray, entry_offset, entry, GFP_KERN= EL); > > > - return 0; > > > + xas_store(&xas, entry); > > > + xas_unlock_irq(&xas); > > > + } while (xas_nomem(&xas, GFP_KERNEL)); > > > + return xas_error(&xas); > > > > I think using the xas_* APIs can be avoided here. The only reason we > > need it is that we want to check if there's an existing entry first, > > and return -EEXIST. However, in that case, the caller will replace it > > anyway (and do some operations on the dupentry): > > > > while (zswap_rb_insert(&tree->rbroot, entry, &dupentry) =3D=3D -EEXIST)= { > > WARN_ON(1); > > zswap_duplicate_entry++; > > zswap_invalidate_entry(tree, dupentry); > > } > > > > So I think we can do something like this in zswap_insert() instead: > > > > dupentry =3D xa_store(..); > > if (WARN_ON(dupentry)) { > > zswap_duplicate_entry++; > > zswap_invalidate_entry(tree, dupentry); > > } > > If this is doable, I think we can return xa_store(..) and keep the > logic in the caller. I think there's a chance > zswap_{search/insert/erase} may end up being very thin wrappers around > xa_{load/store/erase}, and we may be able to remove them completely. > Let's see how it goes. > See my other email about erasing an entry raced into a new entry. That is the part I am not certain. Anyway, when zswap adopte folio, then it might need to handle multi entry, we will be back to using the XAS API. Chris