From mboxrd@z Thu Jan 1 00:00:00 1970 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 CD1E3C433F5 for ; Fri, 1 Apr 2022 03:23:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 191A76B007B; Thu, 31 Mar 2022 23:23:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 118A36B007D; Thu, 31 Mar 2022 23:23:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ED3558D0002; Thu, 31 Mar 2022 23:23:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.27]) by kanga.kvack.org (Postfix) with ESMTP id D73B26B007B for ; Thu, 31 Mar 2022 23:23:48 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id A3D82698 for ; Fri, 1 Apr 2022 03:23:38 +0000 (UTC) X-FDA: 79306865316.10.07FE114 Received: from mail-yw1-f175.google.com (mail-yw1-f175.google.com [209.85.128.175]) by imf06.hostedemail.com (Postfix) with ESMTP id AA494180011 for ; Fri, 1 Apr 2022 03:23:37 +0000 (UTC) Received: by mail-yw1-f175.google.com with SMTP id 00721157ae682-2e5827a76f4so19072617b3.6 for ; Thu, 31 Mar 2022 20:23:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=VQmEc11aIbx+ZtNr6xhDNIOvDmC310KGZSAfuh7/n4o=; b=U2sd5IBeDZM9I11jk51mO17fAqz78NJ9AkSAFGNgkLDMHoTwPcuzlCX0EJbmd9bb/H fP4CvGZIHZj4HHO5u90gNLl3WMM6i3r468t2r4byAfmm2eMWDnVBZ/zkIq/aCvYjWtKS 71Iw8C3q63aPogvu4Rp2N3lhiYPzCqEOTu3zrLJ0tA/wKS3Pzihb34DdzTTMtGW6wwsm 9PvekKjKqPVnHzp2WlDmgVJzU4eP0AKp2zaTKnakYKifBbiVSape10kLEbT6je8VLFOF aaNnqaFeXKLP2wDuS8/I2cRd07mwfhzDgGaOmp1+dsRhNRdzxyl+f1jFuiXITaE384Zr Pygg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=VQmEc11aIbx+ZtNr6xhDNIOvDmC310KGZSAfuh7/n4o=; b=gYC91NGB7rBqGd9b1jIFPQ0etEkU6wBG7wP1iQ9nb1CstLF5yTrMJWZuVkpZWAvAAA 1TYgMC5olqWjRrpF3cGgltshWfZC/zj/azgLoWf+N2TkVocToaHIVoYC7fwKvfBuiKW2 YdxSZhlvuPn+Xs1VgmTGHSQokcgP76wpro2cm8gNfnexpxkZxshP70u5ANjj3PvloVZS EimATOs3h+7l+fTAbL2AoxaNWc3nwfOk6kG8rfmK5kATWO6rS+pfrDvdEVMnNUFG0+0O ezM7RsClR1C59wsqg5SdjWh2hLnbZrTwF/0i7jGE9MGSjJ+MPK/rgYTImT8atnXQ70OZ 0Cng== X-Gm-Message-State: AOAM532pyDifUhMLXd7P5bXUyVYNCwb5JIsYHKxStfKbYpq6fUNYn4p5 EAq9NRJdp+IvK3wrSrsXwTyjuShHo4J3KHN4zpfawg== X-Google-Smtp-Source: ABdhPJyfBNiFGOcyCVVnQq3QlsWIGX+YGW+qL+Jm/E/uL9rdKI+a88ky2Pv25kLFMeoOO0k8ts7UGdkHow0gWNpe3hE= X-Received: by 2002:a0d:f685:0:b0:2e2:22e6:52d7 with SMTP id g127-20020a0df685000000b002e222e652d7mr8046178ywf.418.1648783416948; Thu, 31 Mar 2022 20:23:36 -0700 (PDT) MIME-Version: 1.0 References: <20220228122126.37293-1-songmuchun@bytedance.com> <20220228122126.37293-13-songmuchun@bytedance.com> In-Reply-To: From: Muchun Song Date: Fri, 1 Apr 2022 11:23:01 +0800 Message-ID: Subject: Re: [PATCH v6 12/16] mm: list_lru: replace linear array with xarray To: Matthew Wilcox Cc: Andrew Morton , Johannes Weiner , Michal Hocko , Vladimir Davydov , Shakeel Butt , Roman Gushchin , Yang Shi , Alex Shi , Wei Yang , Dave Chinner , trond.myklebust@hammerspace.com, anna.schumaker@netapp.com, jaegeuk@kernel.org, chao@kernel.org, Kari Argillander , Vlastimil Babka , linux-fsdevel , LKML , Linux Memory Management List , linux-nfs@vger.kernel.org, Qi Zheng , Xiongchun duan , Fam Zheng , Muchun Song Content-Type: text/plain; charset="UTF-8" X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: AA494180011 X-Rspam-User: Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=U2sd5IBe; dmarc=pass (policy=none) header.from=bytedance.com; spf=pass (imf06.hostedemail.com: domain of songmuchun@bytedance.com designates 209.85.128.175 as permitted sender) smtp.mailfrom=songmuchun@bytedance.com X-Stat-Signature: hfo8ub84dy3gh78gd73jneynpozkkj1t X-HE-Tag: 1648783417-315757 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: On Thu, Mar 31, 2022 at 11:01 PM Matthew Wilcox wrote: > > On Mon, Feb 28, 2022 at 08:21:22PM +0800, Muchun Song wrote: > > @@ -586,27 +508,48 @@ int memcg_list_lru_alloc(struct mem_cgroup *memcg, struct list_lru *lru, > > } > > } > > > > + xas_lock_irqsave(&xas, flags); > > while (i--) { > > + int index = READ_ONCE(table[i].memcg->kmemcg_id); > > struct list_lru_per_memcg *mlru = table[i].mlru; > > > > + xas_set(&xas, index); > > +retry: > > + if (unlikely(index < 0 || xas_error(&xas) || xas_load(&xas))) { > > kfree(mlru); > > + } else { > > + xas_store(&xas, mlru); > > + if (xas_error(&xas) == -ENOMEM) { > > + xas_unlock_irqrestore(&xas, flags); > > + if (xas_nomem(&xas, gfp)) > > + xas_set_err(&xas, 0); > > + xas_lock_irqsave(&xas, flags); > > + /* > > + * The xas lock has been released, this memcg > > + * can be reparented before us. So reload > > + * memcg id. More details see the comments > > + * in memcg_reparent_list_lrus(). > > + */ > > + index = READ_ONCE(table[i].memcg->kmemcg_id); > > + if (index < 0) > > + xas_set_err(&xas, 0); > > + else if (!xas_error(&xas) && index != xas.xa_index) > > + xas_set(&xas, index); > > + goto retry; > > + } > > + } > > } > > + /* xas_nomem() is used to free memory instead of memory allocation. */ > > + if (xas.xa_alloc) > > + xas_nomem(&xas, gfp); > > + xas_unlock_irqrestore(&xas, flags); > > kfree(table); > > > > + return xas_error(&xas); > > } > > This is really unidiomatic. And so much more complicated than it needs > to be. > > while (i--) { > do { > int index = READ_ONCE(table[i].memcg->kmemcg_id); > struct list_lru_per_memcg *mlru = table[i].mlru; > > xas_set(&xas, index); > xas_lock_irqsave(&xas, flags); > if (index < 0 || xas_load(&xas)) > xas_set_err(&xas, -EBUSY); > xas_store(&xas, mlru); > if (!xas_error(&xas)) > break; > xas_unlock_irqrestore(&xas, flags); > } while (xas_nomem(&xas, gfp)); > > if (xas_error(&xas)) > kfree(mlru); > } > > kfree(table); > return xas_error(&xas); > > (you might want to mask out the EBUSY error here) > I'm not familiar with xarray APIs. I'll dig deeper to see if this code works properly. Thanks for your suggestions.