Received: by 2002:a05:7412:ba23:b0:fa:4c10:6cad with SMTP id jp35csp161744rdb; Wed, 17 Jan 2024 22:36:03 -0800 (PST) X-Google-Smtp-Source: AGHT+IHB15U6n9OtqVi++Hhn0p5A13sMrLMmY80YxYiWIEqK5g04NeuENVlcBX51uDR5PUg+xSe8 X-Received: by 2002:a17:90a:c696:b0:290:28ea:3de5 with SMTP id n22-20020a17090ac69600b0029028ea3de5mr140867pjt.36.1705559763143; Wed, 17 Jan 2024 22:36:03 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705559763; cv=pass; d=google.com; s=arc-20160816; b=Tf3d0gYsgoPVZbbagYYNFKKLYQGaD39gczjpBa6/nD9H2JILB2hT1AM/U9DwCNYCJn mWX7nPfX5Fkw8+N0zB4RwoI0VCANlJxu6GYb7ag7TYIBrkdLpjDJiOkEAA1C8vExZn4I 5gwp+6EEzqS3RG5Tf1iWvcoS1Z47rGR5ERhq5SeprAwnisX6uaI0dkw/I4g8bjztEnCE zbAErIarbWSgw72R44TfTBGEFZ99HygyyWaVP36jc8rq2sTRHmM7D66HklZytSKXw3Hy 1dm4H9b+lp24FhYl41b681V84rCMp8NpMZ5EqoqUGMm3eojxQZq9XlhgkSSKmTp1cRUf koMg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :dkim-signature; bh=F6Z6wuTdIE3oI+ENMJR97SNTKTQo97xJmtmIXWm79Rs=; fh=X6ryCwQ0Aal2Ve8IO+xkZ22uIPGtGnQM9Gfby62l8Cw=; b=Tkl/r9r5pjQkNCOTHuqqyUqKH/t2SfB8NIsRQyoOdSqWcCAkVc8PIC0Qlas09IFvMv wYNjwYH4g7BiUA3y2vwzyQCdg5b0JNHIKpL/Hi39V/rSQhKlPYfnA98iBuScmUe6QLxA 4uNUxP5BaRUyE/Cn+kc7W4sS9BwjhbnbaBqSgVeLvxIkNA87MSXVdpTJQ4R47D6G6idH Q1pSXoGySWC48ODtkA4hQJlsNAt0Eka8rdTY3X5SyNCx1jPcdF3OnKRnAOjfeOpkcVgO 4Bb28wtMcd7k8Q2FguO2STv+YR/ZC6Qybaj82I5DHORnK5FeWLWA6kPBjvdhvIitk77a fTKQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=J9ObBuiU; 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-29740-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-29740-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id co1-20020a17090afe8100b0028dd0ff8c93si949224pjb.73.2024.01.17.22.36.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Jan 2024 22:36:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-29740-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=J9ObBuiU; 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-29740-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-29740-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id BC06A28497A for ; Thu, 18 Jan 2024 06:36:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6E5779447; Thu, 18 Jan 2024 06:35:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="J9ObBuiU" Received: from mail-ej1-f44.google.com (mail-ej1-f44.google.com [209.85.218.44]) (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 DD9F45CB0 for ; Thu, 18 Jan 2024 06:35:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.44 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705559755; cv=none; b=IWwcaMF56ruzIqUFrw0J7raUAoioxi1YJKNjKyzhu2C0IihVaj0vBVqg9oxT67ZMReo6qFJXp8OUX5JDAcwqU3TBNfH6G/gYO/RiychCQnCluk+bvamHKnej+y7rezMOHpaP+d1vQ+HCplOGA1RukV7Oq9ABky70vZ0UExkWDFI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705559755; c=relaxed/simple; bh=eTHiBnyqir1sEFqwoXdsbYZ1XGTsUucLPBVw+yxu1PI=; h=Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Google-Smtp-Source:X-Received:MIME-Version: References:In-Reply-To:From:Date:Message-ID:Subject:To:Cc: Content-Type; b=ZdeOZh2OiG/iSlah+yfQwNu8gkExfO+e4FG+XDyKven6DhVIZ4A7USZ+KpaVX9AiPvu3mC6XUHLrmANnaK8JCjabVyTfwAnFCKn/80c0+oM/fECyGi00MkKZ1dNBqOJ6rcR7z/X/Kw30yX9bhDbY8HoczAPZY3tqe0bzJCdlsH4= 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=J9ObBuiU; arc=none smtp.client-ip=209.85.218.44 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-f44.google.com with SMTP id a640c23a62f3a-a2cea0563cbso884754866b.3 for ; Wed, 17 Jan 2024 22:35:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1705559752; x=1706164552; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=F6Z6wuTdIE3oI+ENMJR97SNTKTQo97xJmtmIXWm79Rs=; b=J9ObBuiUD/YGf3i65QumcD/nlhg1QTBRggxkZC1a8b/VcwF8Gqef32FwhnnpmQ4zcX YHTkPQ5JFABIkWCcqHfJ31ouMZhNr0ElHpgLC7NOPMvmxd/jm0k1fTVZlSAaHiXqfCJC 9BYPovdvQW5X7/PnEuBHe4I0pNhg583y3Jn4rvvcpvS1hqBtrRpsUTi6G6uwcGF0iF4p 3hD1wAZ232lMdmoBmGxtvzTUy1qyolxqgCp310vNPXbOhKmvrL+VQl3R4aNJg84ONywn HIW0nyxM72ZanV5s4CEaJ8/Op9cjRjmUhOWxW7mxOIjXyqm+/tFe0d3eF0rd6doQd3xp /xYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705559752; x=1706164552; h=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=F6Z6wuTdIE3oI+ENMJR97SNTKTQo97xJmtmIXWm79Rs=; b=GxO31UaqhTnSdQjKVVNXTdx9CtEwpb7wHu9qwb+A0AKaLMDb3k21R3uzxxY3Qqxkca 09u81WIBlnd8VXzcmea3lGiYiD86frJfyf7ombPd+Paozgq/9W9zWHw+1oBVH3ZMWC2a m15chsXlWR0iwDWmWPyE4hIRDDE8hROIWRsdzjeVOzAJMMzQkdgM0ok/bZ1VFDffg7Gl MPu8ima1NLn5aVNMH0tTsRhCsDJn+ed4fuV36jq2n8qNC4uMHNo5KrNr70oPsEs838bV P1TA/ZJjT2vyT770i3rAprlKPIQ1pxdzpST6yUQGIF7sXoB813tlnWoEIe7+088tDt4i jFvw== X-Gm-Message-State: AOJu0YwkyPaXrppQkrc/t1Dgfr1BCf9njHx5uKpplQruZhX5V8nb9Ypr lqw2tCUBB+mOZuSJ4WeLXws21jxHmBeN1yUw3hRJfyuFwvKbq9leyPp1d2guj5O+AtFPKbfwjh3 NuSlsB6m3Sef74Rnctz9Q5hBD3Iirox0lZt9Q X-Received: by 2002:a17:906:35d8:b0:a2d:a5b5:70ab with SMTP id p24-20020a17090635d800b00a2da5b570abmr195429ejb.100.1705559751871; Wed, 17 Jan 2024 22:35:51 -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: <20240117-zswap-xarray-v1-2-6daa86c08fae@kernel.org> From: Yosry Ahmed Date: Wed, 17 Jan 2024 22:35:15 -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" > @@ -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 = &tree->rbroot; > - struct rb_node **link = &root->rb_node, *parent = NULL; > - struct zswap_entry *myentry, *old; > - 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 { > - old = xa_load(&tree->xarray, entry_offset); > - BUG_ON(old != myentry); > - *dupentry = myentry; > + struct zswap_entry *e; > + pgoff_t offset = swp_offset(entry->swpentry); > + XA_STATE(xas, &tree->xarray, offset); > + > + do { > + xas_lock_irq(&xas); > + do { > + e = xas_load(&xas); > + if (xa_is_zero(e)) > + e = NULL; > + } while (xas_retry(&xas, e)); > + if (xas_valid(&xas) && e) { > + xas_unlock_irq(&xas); > + *dupentry = e; > return -EEXIST; > } > - } > - rb_link_node(&entry->rbnode, parent, link); > - rb_insert_color(&entry->rbnode, root); > - old = xa_store(&tree->xarray, entry_offset, entry, GFP_KERNEL); > - 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) == -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 = xa_store(..); if (WARN_ON(dupentry)) { zswap_duplicate_entry++; zswap_invalidate_entry(tree, dupentry); } WDYT? > } > > static bool zswap_erase(struct zswap_tree *tree, struct zswap_entry *entry) > { > + struct zswap_entry *e; > pgoff_t offset = swp_offset(entry->swpentry); > - if (!RB_EMPTY_NODE(&entry->rbnode)) { > - struct zswap_entry *old; > - old = xa_erase(&tree->xarray, offset); > - BUG_ON(old != 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 = xas_load(&xas); > + } while (xas_retry(&xas, e)); > + if (xas_valid(&xas) && e != 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(..); > > static struct zpool *zswap_find_zpool(struct zswap_entry *entry) > @@ -583,7 +563,6 @@ static void zswap_entry_put(struct zswap_tree *tree, > > WARN_ON_ONCE(refcount < 0); > if (refcount == 0) { > - WARN_ON_ONCE(!RB_EMPTY_NODE(&entry->rbnode)); > zswap_free_entry(entry); > } nit: the braces are no longer needed here