Received: by 2002:a05:7412:ba23:b0:fa:4c10:6cad with SMTP id jp35csp1214472rdb; Fri, 19 Jan 2024 11:37:16 -0800 (PST) X-Google-Smtp-Source: AGHT+IGapuu6/VnaH36gaOrAJNT9r+8dAy0IMZBhpWADtNI3fwq8mdP6auSrXhuWKGKlS3P03gcF X-Received: by 2002:a05:6a20:3d02:b0:19a:e283:9c69 with SMTP id y2-20020a056a203d0200b0019ae2839c69mr402726pzi.71.1705693036257; Fri, 19 Jan 2024 11:37:16 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705693036; cv=pass; d=google.com; s=arc-20160816; b=NySPJMa2WF3N9A44KhkdNhJBKKgSEnrhyHoW28+jrurC4ws1dpUsJF0CVMHlNkXjEl oJwd9CShoSly8BqBPVTgyps8DIfjKC+YcvI8CEBwWpeA6WCqgCeZNbX4nfRjCL5vYDzQ wKEvwUulyIhL60Fxc4Lt7kzShv0323zaE3KZR4NeP8z/v5Zk4YZmT56wqlbfdsYTWJuZ /uLCDJKeqCV6qOTAvIM+1E5OrKXUHFk+Lpnu6VVooOGH+pfA3uuSBlpa7tHssCm3TXCq qv2heU13WSUqg4ksHDEfCZEhWwBjTwKKDPHwEPsmpCjXdRnu+iOflwA+2XIsjE4HV7Qc X30g== 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=Fjm7bz8eyCbh/wR7R5WfPWiMGpqtxISCBuFNemwrIvk=; fh=X6ryCwQ0Aal2Ve8IO+xkZ22uIPGtGnQM9Gfby62l8Cw=; b=XfsqO3ah2LDF8NJ4vAmLxYpa4D6pWheu5F4xYXa043GaQtnTSTuWs6ffy8jzaVgCbn RVXinD825OauX6UobEod/BQdYF7dHXRmP8rcUPkkHQVHqhqYS5/Z45tn03NYssa+3EwT 9rBqlAnQ5ogWdBLyPTGjc+3avHzzDuxW9HVegG4jrd1fu50DWUE+EehMLXAbUdlWIpYJ 7iVmgPfTrBPeoyCT1eK+Z1TPWCP/TcplLgZ+rRrlr/cdQgUcd7ZEtH6mFEw61i3qb0FG s2GcLl/oiPccKO3LX3D1imaZThHd68AzhltosP+zZgo/BgrvsU7nG9j4vvIhTgp/iUKc 2Omg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=2tRIcBQf; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-31517-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-31517-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id jw20-20020a056a00929400b006dba0e6f35bsi5394123pfb.47.2024.01.19.11.37.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 11:37:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-31517-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=2tRIcBQf; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-31517-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-31517-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 sy.mirrors.kernel.org (Postfix) with ESMTPS id B7B05B23926 for ; Fri, 19 Jan 2024 19:37:12 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id ACFF255E7F; Fri, 19 Jan 2024 19:37:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="2tRIcBQf" Received: from mail-ej1-f49.google.com (mail-ej1-f49.google.com [209.85.218.49]) (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 0DD831E480 for ; Fri, 19 Jan 2024 19:37:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.49 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705693023; cv=none; b=dQzuxN+kUkCo2yH4Z4I3w/QiLdzTCiLLiJvJ0Ym7zx84wNZDdMxjxlxy0NgTTY0tkw56ilBxDO+TlU1ez9rrRYOFXaEWReilVEcGK75qODDFpLLd+lxkTORplK0ekW+QwH6AzupF8UJOeSWRRgJBod026KFf6/YwjUh7NSRAE9I= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705693023; c=relaxed/simple; bh=4LjheHUisz2n6Y5plVFsRQnMTaIzFhW0WKqk3P3eJa0=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=fZ+xj/lcdF1jAiYiArrGedYI6Bly1gMcVnHSwVxS4X9vOmDJfKmUCd2bbIMsakfGq030pQAZ3S7L/IcSWm5apm0Qc3HjTNuCTS3CFtijSNoFKUlW/6tVv1EZ6AbcG6mSqGOqSnS54/+heNwfU6A7RQufhfmq27E1/EDs9b+bJO4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=2tRIcBQf; arc=none smtp.client-ip=209.85.218.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-ej1-f49.google.com with SMTP id a640c23a62f3a-a26ed1e05c7so131689866b.2 for ; Fri, 19 Jan 2024 11:37:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1705693020; x=1706297820; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=Fjm7bz8eyCbh/wR7R5WfPWiMGpqtxISCBuFNemwrIvk=; b=2tRIcBQfhrXXN1pI2agEqS6kp8/nBsSuJjOyn07Cx4h6N3FIdfsFgCoFoPpywM9yuD KBFJG/Efq1V8YCOco7VdEENF1bjbozsIMf8jxwOyNQ7pjCRr+fnNGuQxkYt2EZ4/f5iz Uiios5h8bM6MRFlLSb9NTtPo2G+je+lpCiSnz1RT4UV0bb1sZT4uLbOhuoVSYSf3jDKB 3D196uI3oYaV41rGMVnw14EmVZAquh3AyCeenNPHMUkg1camzQhJIREPZbzIqJJl2qkj XDylXmrceCb/hycouJcsHWzJWZrU1d48wiWJetkm1d8SrH8ntmtnhWfr3TbVvJTnh3Wa CyGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705693020; x=1706297820; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Fjm7bz8eyCbh/wR7R5WfPWiMGpqtxISCBuFNemwrIvk=; b=Kg2Qdf86aNL57e0cIvrQ1yh93jAS3LrIclJp9ocwgmeCBaNfb/WL0o+1QaYKZdw6iA l4pgu2GgevpriMKIFXZ03PuBepauDpiQHh0nem4mgiEjybpfGnVTx9gnGA2YnTErkM1G tckh/2EePr7zQhjjo6G2eSPzPq/3adftI7QkbdI6pqirmoOpwZwIkMWgOVXyMYYJzHSN DGF7tJpa7Hcj2PIfBp55PeZu3QnvZzk6MGXuOwMYWghA1w+GiONozHinmxYvORGczFFW n+8jI6o0U+EhIzTOOetHGlJWx3jtBakrgA564RRhrRF4UAfWZcT7pKBxI4bcxJSIKZ9B w7xQ== X-Gm-Message-State: AOJu0YwThJFmU4tJ67COaQ/7j1gfwdueQQc60B6Njx+nKt9d0I6sTUBw 9/1I3YaIb2ryibGYeRIhkpSihdjBoj02FwVymDMD0vFXmFTH4lmjcPsHDLkzuiKaFzoT4yTs3TQ eIJ9WE1YD3ApUV8Ij7t9tzxeOeNtWWWXShmrg X-Received: by 2002:a17:906:b741:b0:a2e:893f:7333 with SMTP id fx1-20020a170906b74100b00a2e893f7333mr111511ejb.5.1705693020064; Fri, 19 Jan 2024 11:37:00 -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: Yosry Ahmed Date: Fri, 19 Jan 2024 11:36:23 -0800 Message-ID: Subject: Re: [PATCH 2/2] mm: zswap.c: remove RB tree To: Chris Li 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 9:43=E2=80=AFPM Chris Li wrote: > > Hi Yosry, > > 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): > > We might be able to for the insert case if we don't mind changing the > code behavior a bit. My original intent is to keep close to the > original zswap code and not stir the pot too much for the xarray > replacement. We can always make more adjustment once the RB tree is > gone. I don't see how this changes code behavior though. The current code in zswap_store() will do the following: - Hold the tree lock to make sure no one else modifies it. - Try to insert, check if there is already a dupentry at the index and return -EEXIST. - Warn, increment zswap_duplicate_entry, and invalidate the dupentry. - Try to insert again (this should always succeed since we are holding the lock). What I am proposing is: - zswap_xa_insert() is a thin wrapper around xa_store() (or we can remove it completely). - zswap_store() does the following: - Use zswap_xa_insert() and check if there is a returned dupentry. - Warn, increment zswap_duplicate_entry, and invalidate the dupentry. Either way, we always place the entry we have in the tree, and if there is a dupentry we warn and invalidate it. If anything, the latter is more straightforward. Am I missing something? > > > > > } > > > > > > static bool zswap_erase(struct zswap_tree *tree, struct zswap_entry = *entry) > > > { > > > + struct zswap_entry *e; > > > pgoff_t offset =3D swp_offset(entry->swpentry); > > > - if (!RB_EMPTY_NODE(&entry->rbnode)) { > > > - struct zswap_entry *old; > > > - old =3D xa_erase(&tree->xarray, offset); > > > - BUG_ON(old !=3D entry); > > > - rb_erase(&entry->rbnode, &tree->rbroot); > > > - RB_CLEAR_NODE(&entry->rbnode); > > > - return true; > > > - } > > > - return false; > > > + XA_STATE(xas, &tree->xarray, offset); > > > + > > > + do { > > > + xas_lock_irq(&xas); > > > + do { > > > + e =3D xas_load(&xas); > > > + } while (xas_retry(&xas, e)); > > > + if (xas_valid(&xas) && e !=3D entry) { > > > + xas_unlock_irq(&xas); > > > + return false; > > > + } > > > + xas_store(&xas, NULL); > > > + xas_unlock_irq(&xas); > > > + } while (xas_nomem(&xas, GFP_KERNEL)); > > > + return !xas_error(&xas); > > > } > > > > Same here, I think we just want: > > > > return !!xa_erase(..); > > For the erase case it is tricky. > The current zswap code does not erase an entry if the tree entry at > the same offset has been changed. It should be fine if the new entry > is NULL. Basically some race to remove the entry already. However, if > the entry is not NULL, then force resetting it to NULL will change > behavior compared to the current. I see, very good point. I think we can use xa_cmpxchg() and pass in NULL?