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 E4631C021BC for ; Wed, 26 Feb 2025 07:11:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 58BB2280003; Wed, 26 Feb 2025 02:11:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 53B31280001; Wed, 26 Feb 2025 02:11:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3DC8F280003; Wed, 26 Feb 2025 02:11:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 1DB59280001 for ; Wed, 26 Feb 2025 02:11:57 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 7DCBA80D61 for ; Wed, 26 Feb 2025 07:11:56 +0000 (UTC) X-FDA: 83161226232.13.28D96F9 Received: from out30-112.freemail.mail.aliyun.com (out30-112.freemail.mail.aliyun.com [115.124.30.112]) by imf13.hostedemail.com (Postfix) with ESMTP id EAA652000A for ; Wed, 26 Feb 2025 07:11:52 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=guCTusCI; spf=pass (imf13.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.112 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com; dmarc=pass (policy=none) header.from=linux.alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1740553915; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=yDdB4bIyML299YEUA1lHDugeZxZJO/0yzLNbJ+H1stA=; b=CxHBuJTbdDNh7PV9Lg4tTzHoYv0QslgFzeJMYEWXEg++jGcAC8FommE+pGa7csUxPFerLm US3uf8MeH7h5DI25MgAEggxaaAca2fik2UeHhNUEAJxEYsQPlYovqJr61KfLV8FrGMDgFK iwExW6li7WoTPnzC32jAXzWIogH5ZzI= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=guCTusCI; spf=pass (imf13.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.112 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com; dmarc=pass (policy=none) header.from=linux.alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740553915; a=rsa-sha256; cv=none; b=RhuxMaB8Y8fnV7LX+dhFPNgrryJCvw8HCJtx2i5tiX7pOZ/I/3DK9E4uGgR98J6IuPqw5U FjtFuN7pPPsOUWdFWTKdpE8reJZUdcNbN9/FN5mWCc/gJ5sApbQuuROLm2lnz0Ta0R2qp6 mJDWkJTH+ElhwXHlxPBMgSpNQWu2QiI= DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1740553907; h=Message-ID:Date:MIME-Version:Subject:To:From:Content-Type; bh=yDdB4bIyML299YEUA1lHDugeZxZJO/0yzLNbJ+H1stA=; b=guCTusCIgt3gg3pXZWmj8lBU/6e3ZZly/yqNbg3BJuSAU+ecqSIuU6KPVCdZG5JWihykT6mY9AK8pgwqTsOCbMj3jBmvDQjWO43isg7eXh5tQMq26c6R4y2Dckh0Mgv5gWWy9IyQ7fftfoNy00Pwfr7E0KdKYVnzRvAFPsmgUDI= Received: from 30.74.144.124(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WQHLFQu_1740553905 cluster:ay36) by smtp.aliyun-inc.com; Wed, 26 Feb 2025 15:11:45 +0800 Message-ID: Date: Wed, 26 Feb 2025 15:11:45 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v8 1/8] xarray: add xas_try_split() to split a multi-index entry To: Zi Yan , linux-mm@kvack.org, Andrew Morton , "Kirill A . Shutemov" , "Matthew Wilcox (Oracle)" Cc: Ryan Roberts , Hugh Dickins , David Hildenbrand , Yang Shi , Miaohe Lin , Kefeng Wang , Yu Zhao , John Hubbard , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org References: <20250218235012.1542225-1-ziy@nvidia.com> <20250218235012.1542225-2-ziy@nvidia.com> From: Baolin Wang In-Reply-To: <20250218235012.1542225-2-ziy@nvidia.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: EAA652000A X-Stat-Signature: tz4he3eyeb4di6hcbgdoxgp1n769j4ya X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1740553912-303810 X-HE-Meta: U2FsdGVkX18ZYgazDEkExHcN4FKUGq/etuMQpbMT0d0jEleXWr8NyOPoHboiOgLdGnPgWN9mchs4LviAhaVjHgTEmqlwa51Zs5tx1/ESZ2/YUzn4AYa8YSYpLjXUEp5k7wq/8W8NKrc96sram2FLS7tLOPy+aMKCe4oWAL6jDNHzgiWs1yLVaBni/g68bfg/P8SPxtIDwPIokVKfSh/7ThJGpH1kzkoCpjZHL3eTUiYvEkCzTsZyYibJxwgLDt/PHb/xkYK9CKo+x1dt13dXyHL1B24f+JoZFx6FpJQHzOWD+WyPr+/qFtrCTG7UTZ6ewLhwsCBLUOMkW3dCYtYLBtlS49DZoUO+r0umBC/smzdGPyDvL1y1AZPh02h0hACVOIfDkuVN+ACE1day939dwDLFomYajXBut0REkPWt+jh5ycBlCJTalQ+JuNQp7spGtuJ7YS+KsHkW4Km1ljhM87uOqdHDHJzSXCiGmJvMwHg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mVIqIdjPx7k21NVOGt4niukGaUzd1DVfHJNBXs3cJD0W7yQJ7qzQgaJXunB4ia9oppQ0Vh/bzsi2W4Q2hCBqlLaipM5v+W0CqNffLHDA+cCVWVluxdwqo1JDcusZ9YTB0F8zL0= 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: List-Subscribe: List-Unsubscribe: Hi Zi, On 2025/2/19 07:50, Zi Yan wrote: > A preparation patch for non-uniform folio split, which always split a > folio into half iteratively, and minimal xarray entry split. > > Currently, xas_split_alloc() and xas_split() always split all slots from a > multi-index entry. They cost the same number of xa_node as the > to-be-split slots. For example, to split an order-9 entry, which takes > 2^(9-6)=8 slots, assuming XA_CHUNK_SHIFT is 6 (!CONFIG_BASE_SMALL), 8 > xa_node are needed. Instead xas_try_split() is intended to be used > iteratively to split the order-9 entry into 2 order-8 entries, then split > one order-8 entry, based on the given index, to 2 order-7 entries, ..., > and split one order-1 entry to 2 order-0 entries. When splitting the > order-6 entry and a new xa_node is needed, xas_try_split() will try to > allocate one if possible. As a result, xas_try_split() would only need > one xa_node instead of 8. > > When a new xa_node is needed during the split, xas_try_split() can try to > allocate one but no more. -ENOMEM will be return if a node cannot be > allocated. -EINVAL will be return if a sibling node is split or cascade > split happens, where two or more new nodes are needed, and these are not > supported by xas_try_split(). > > xas_split_alloc() and xas_split() split an order-9 to order-0: > > --------------------------------- > | | | | | | | | | > | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | > | | | | | | | | | > --------------------------------- > | | | | > ------- --- --- ------- > | | ... | | > V V V V > ----------- ----------- ----------- ----------- > | xa_node | | xa_node | ... | xa_node | | xa_node | > ----------- ----------- ----------- ----------- > > xas_try_split() splits an order-9 to order-0: > --------------------------------- > | | | | | | | | | > | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | > | | | | | | | | | > --------------------------------- > | > | > V > ----------- > | xa_node | > ----------- > > Signed-off-by: Zi Yan > Cc: Baolin Wang > Cc: David Hildenbrand > Cc: Hugh Dickins > Cc: John Hubbard > Cc: Kefeng Wang > Cc: Kirill A. Shuemov > Cc: Miaohe Lin > Cc: Matthew Wilcox > Cc: Ryan Roberts > Cc: Yang Shi > Cc: Yu Zhao > Cc: Zi Yan > --- > Documentation/core-api/xarray.rst | 14 ++- > include/linux/xarray.h | 7 ++ > lib/test_xarray.c | 47 ++++++++++ > lib/xarray.c | 138 ++++++++++++++++++++++++++---- > tools/testing/radix-tree/Makefile | 1 + > 5 files changed, 190 insertions(+), 17 deletions(-) > > diff --git a/Documentation/core-api/xarray.rst b/Documentation/core-api/xarray.rst > index f6a3eef4fe7f..c6c91cbd0c3c 100644 > --- a/Documentation/core-api/xarray.rst > +++ b/Documentation/core-api/xarray.rst > @@ -489,7 +489,19 @@ Storing ``NULL`` into any index of a multi-index entry will set the > entry at every index to ``NULL`` and dissolve the tie. A multi-index > entry can be split into entries occupying smaller ranges by calling > xas_split_alloc() without the xa_lock held, followed by taking the lock > -and calling xas_split(). > +and calling xas_split() or calling xas_try_split() with xa_lock. The > +difference between xas_split_alloc()+xas_split() and xas_try_alloc() is > +that xas_split_alloc() + xas_split() split the entry from the original > +order to the new order in one shot uniformly, whereas xas_try_split() > +iteratively splits the entry containing the index non-uniformly. > +For example, to split an order-9 entry, which takes 2^(9-6)=8 slots, > +assuming ``XA_CHUNK_SHIFT`` is 6, xas_split_alloc() + xas_split() need > +8 xa_node. xas_try_split() splits the order-9 entry into > +2 order-8 entries, then split one order-8 entry, based on the given index, > +to 2 order-7 entries, ..., and split one order-1 entry to 2 order-0 entries. > +When splitting the order-6 entry and a new xa_node is needed, xas_try_split() > +will try to allocate one if possible. As a result, xas_try_split() would only > +need 1 xa_node instead of 8. > > Functions and structures > ======================== > diff --git a/include/linux/xarray.h b/include/linux/xarray.h > index 0b618ec04115..9eb8c7425090 100644 > --- a/include/linux/xarray.h > +++ b/include/linux/xarray.h > @@ -1555,6 +1555,8 @@ int xa_get_order(struct xarray *, unsigned long index); > int xas_get_order(struct xa_state *xas); > void xas_split(struct xa_state *, void *entry, unsigned int order); > void xas_split_alloc(struct xa_state *, void *entry, unsigned int order, gfp_t); > +void xas_try_split(struct xa_state *xas, void *entry, unsigned int order, > + gfp_t gfp); > #else > static inline int xa_get_order(struct xarray *xa, unsigned long index) > { > @@ -1576,6 +1578,11 @@ static inline void xas_split_alloc(struct xa_state *xas, void *entry, > unsigned int order, gfp_t gfp) > { > } > + > +static inline void xas_try_split(struct xa_state *xas, void *entry, > + unsigned int order, gfp_t gfp) > +{ > +} > #endif > > /** [snip] > diff --git a/lib/xarray.c b/lib/xarray.c > index 116e9286c64e..b9a63d7fbd58 100644 > --- a/lib/xarray.c > +++ b/lib/xarray.c > @@ -1007,6 +1007,31 @@ static void node_set_marks(struct xa_node *node, unsigned int offset, > } > } > > +static struct xa_node *__xas_alloc_node_for_split(struct xa_state *xas, > + void *entry, gfp_t gfp) > +{ > + unsigned int i; > + void *sibling = NULL; > + struct xa_node *node; > + unsigned int mask = xas->xa_sibs; > + > + node = kmem_cache_alloc_lru(radix_tree_node_cachep, xas->xa_lru, gfp); > + if (!node) > + return NULL; > + node->array = xas->xa; > + for (i = 0; i < XA_CHUNK_SIZE; i++) { > + if ((i & mask) == 0) { > + RCU_INIT_POINTER(node->slots[i], entry); > + sibling = xa_mk_sibling(i); > + } else { > + RCU_INIT_POINTER(node->slots[i], sibling); > + } > + } > + RCU_INIT_POINTER(node->parent, xas->xa_alloc); > + > + return node; > +} > + > /** > * xas_split_alloc() - Allocate memory for splitting an entry. > * @xas: XArray operation state. > @@ -1025,7 +1050,6 @@ void xas_split_alloc(struct xa_state *xas, void *entry, unsigned int order, > gfp_t gfp) > { > unsigned int sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1; > - unsigned int mask = xas->xa_sibs; > > /* XXX: no support for splitting really large entries yet */ > if (WARN_ON(xas->xa_shift + 2 * XA_CHUNK_SHIFT <= order)) > @@ -1034,23 +1058,9 @@ void xas_split_alloc(struct xa_state *xas, void *entry, unsigned int order, > return; > > do { > - unsigned int i; > - void *sibling = NULL; > - struct xa_node *node; > - > - node = kmem_cache_alloc_lru(radix_tree_node_cachep, xas->xa_lru, gfp); > + struct xa_node *node = __xas_alloc_node_for_split(xas, entry, gfp); > if (!node) > goto nomem; > - node->array = xas->xa; > - for (i = 0; i < XA_CHUNK_SIZE; i++) { > - if ((i & mask) == 0) { > - RCU_INIT_POINTER(node->slots[i], entry); > - sibling = xa_mk_sibling(i); > - } else { > - RCU_INIT_POINTER(node->slots[i], sibling); > - } > - } > - RCU_INIT_POINTER(node->parent, xas->xa_alloc); > xas->xa_alloc = node; > } while (sibs-- > 0); > > @@ -1122,6 +1132,102 @@ void xas_split(struct xa_state *xas, void *entry, unsigned int order) > xas_update(xas, node); > } > EXPORT_SYMBOL_GPL(xas_split); > + > +/** > + * xas_try_split() - Try to split a multi-index entry. > + * @xas: XArray operation state. > + * @entry: New entry to store in the array. > + * @order: Current entry order. > + * @gfp: Memory allocation flags. > + * > + * The size of the new entries is set in @xas. The value in @entry is > + * copied to all the replacement entries. If and only if one xa_node needs to > + * be allocated, the function will use @gfp to get one. If more xa_node are > + * needed, the function gives EINVAL error. > + * > + * Context: Any context. The caller should hold the xa_lock. > + */ > +void xas_try_split(struct xa_state *xas, void *entry, unsigned int order, > + gfp_t gfp) The xas_try_split() may sleep if ‘gfp’ flags permit while holding the xa_lock, which can cause issues. So can we add a check for the ‘gfp’ or only use GFP_NOWAIT?