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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 99670CDB479 for ; Wed, 24 Jun 2026 14:23:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Subject:Cc:To: From:Date:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=Per6MI6n5CBZ4jce2Yah0coKW3mXxHCaIKwWKmwmsgQ=; b=bvy4G92e2VnfCS5rLQW4xO32SJ OZxR8w6d5sWKAT/b9Vtxip0XexNqhVIci8Qcd+9fxGwkHOpEKcWFpWmhg2VDe6NQfYM9vzILbGIVp Sh1yH8Wo0Dip/HpURbtp/m0OH4jAI68XaDl0F6llHMAb8w9bv7wHTaymli4Grs9roClU0M2oCQSNE zyAoadKK6S0/IOXDdrqamwi25bCCfsFJZF1reDeBQv5YTWJyU7LUOSSlpETPkUyYcLf1vkdanluez cUgIPHz0OC3IhO6pgviY8lGLHPYgtdxF1Nf2IY0ifF+d+LCjPtCkxmP1CxbtaUlazNhrfsko/Bv58 e1ptdFig==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.99.1 #2 (Red Hat Linux)) id 1wcOVr-00000007uYQ-2PGw; Wed, 24 Jun 2026 14:23:35 +0000 Received: from mail-wr1-x42e.google.com ([2a00:1450:4864:20::42e]) by bombadil.infradead.org with esmtps (Exim 4.99.1 #2 (Red Hat Linux)) id 1wcOVl-00000007uXH-2A7h for kexec@lists.infradead.org; Wed, 24 Jun 2026 14:23:30 +0000 Received: by mail-wr1-x42e.google.com with SMTP id ffacd0b85a97d-46cdcec58c2so213783f8f.1 for ; Wed, 24 Jun 2026 07:23:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1782311007; x=1782915807; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=Per6MI6n5CBZ4jce2Yah0coKW3mXxHCaIKwWKmwmsgQ=; b=nP666snM/JbOVu3FDhdW1IjMsIYgHC04dMtbmQU9DDwpsB/7TnCTC0t7qCpUuDFw40 Xvl2JjB5oLQbkiB4B+3B37jIjZKXcQhKLEbj65FX6swmSnAoE+mupc6+INxFvgXMW6Tg Ubgmpe6EvwKU3NqN8tXfOWulZSbNnqhldzErD+rxtBzGbNTlfgBHGVB6GumpWvSzs493 Gx2VQGdRm905IYqayl1UXWeOnBMcnJhWoyWM2xjXWd2pztGI2RY0St/SU9tb5tp9R/3H 589FzaBSij0RGFw3l7xy23GdPp9RoIccs4kBtYvW+V4MN4pX8A12jAcVqmtRawcb5g+f i9EA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782311007; x=1782915807; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=Per6MI6n5CBZ4jce2Yah0coKW3mXxHCaIKwWKmwmsgQ=; b=mvDvcT9cMGxG2VCWM9E1+JJE30+ikEEttInhwmStVpptazzscAiSxnNVF/L9yAEk/w 8ai48iQv5+ea3o8Zsqfm0b9p/dgO7BqdT8VmP0iirJdQa/0p8xX9xaADpvZIIwcUuIRB 2UQAIQVq8n2mnJLC6ar0TYprXvCGLkHQqUU+Ux1FBUB1CRGdeE3kQAmz/iOeuS4OV38g nnouawTh4xEum8b97qTwqps+zKSNMcuzPtIfBIo2N5zeMlPsPWkmpoBMW0Vkm2FIw/DK 1sdkciZfKcKFvI7Qcx8ZVgpnQ6YxdzWDYBxqtxr50NAJZqH8msi5bJW2WUS1suUFLayk +/sw== X-Forwarded-Encrypted: i=1; AHgh+RqBhQWNBh8aRa6Zc2Wy/1TpDa4PgRKKfHFq07zlyBWkb3ONimR/O4/+3oIf7UVqfVqhG7D7hw==@lists.infradead.org X-Gm-Message-State: AOJu0Ywbny19cnoMHXdkzYy9WNSi4PXDfagEdpdWovbYv0sAJ9FzSz3s XbTEyXcmKSABoAV3dWOROgH1kseH5Fbl32cWxFvH8nxpcs4ZhncbwfN/ X-Gm-Gg: AfdE7cnAx+KRTQhsU+ktkp06s1BEF+KayfGtClX35my//gNvRGsA3A1tA8KUgMXB+AJ kNFvLoCRFO9Lg4T1puCPkKtqUqlVZY8jyKKmmw58Tkd4cTIYHkHCE1Wv0l//utsTs5nR1zaujZ8 qmBI04LUEjE+fLb3RTMdTqquQRvwICrrfWOYrbkNAnPBW/aE5QXxThXc+ZaHhycNS1Goes2Nksk jFm2zJSrCtgfRF4WOykiaZ0gxmWHpIrbR0UHCw7slz+0gQhE0w6BmjDBJIC+pqiM93pKatK7doG JbPMi6mmquJD8QB8DHHQY543PN07HU0/uwWvtjZ0/llXPPHRslfejIx3ev9yiw4p5++0X3dt5Vc bQWrYpedSejGgpgRWSd5ZsE/kp3C7T/80TOjm+FmviKoC2cTxOeOztFUNPRxhxNWSB2GK+01jbI mRMeIeD2nNi9EVfgtw1sR2RgIr0MmhWx646zURuejIO1SW87JT3A== X-Received: by 2002:a05:6000:471a:b0:460:2e53:a6f6 with SMTP id ffacd0b85a97d-46d04583b35mr1261972f8f.12.1782311006854; Wed, 24 Jun 2026 07:23:26 -0700 (PDT) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-46c221d9405sm8183744f8f.22.2026.06.24.07.23.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jun 2026 07:23:26 -0700 (PDT) Date: Wed, 24 Jun 2026 15:23:24 +0100 From: David Laight To: Christian =?UTF-8?B?S8O2bmln?= Cc: Kaitao Cheng , Andrew Morton , David Hildenbrand , Jens Axboe , Tejun Heo , Alexander Viro , Christian Brauner , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Johannes Weiner , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Thomas Gleixner , Juri Lelli , Vincent Guittot , Paul Moore , Andy Shevchenko , "Paul E. McKenney" , Shakeel Butt , David Howells , Simona Vetter , Randy Dunlap , Luca Ceresoli , Philipp Stanner , linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-ntfs-dev@lists.sourceforge.net, linux-fsdevel@vger.kernel.org, io-uring@vger.kernel.org, audit@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-perf-users@vger.kernel.org, linux-trace-kernel@vger.kernel.org, kexec@lists.infradead.org, live-patching@vger.kernel.org, linux-modules@vger.kernel.org, linux-crypto@vger.kernel.org, linux-pm@vger.kernel.org, rcu@vger.kernel.org, sched-ext@lists.linux.dev, linux-mm@kvack.org, virtualization@lists.linux.dev, damon@lists.linux.dev, llvm@lists.linux.dev, Kaitao Cheng Subject: Re: [PATCH v3 1/7] list: Add mutable iterator variants Message-ID: <20260624152324.3def88ce@pumpkin> In-Reply-To: References: <20260622040533.29824-1-kaitao.cheng@linux.dev> <20260622040533.29824-2-kaitao.cheng@linux.dev> <20260622094242.64531b9a@pumpkin> <351a6b67-b394-4c58-aee2-88b6c8089ad5@linux.dev> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260624_072329_601310_D8F0ED65 X-CRM114-Status: GOOD ( 42.43 ) X-BeenThere: kexec@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "kexec" Errors-To: kexec-bounces+kexec=archiver.kernel.org@lists.infradead.org On Wed, 24 Jun 2026 15:23:47 +0200 Christian K=C3=B6nig wrote: > On 6/24/26 15:14, Kaitao Cheng wrote: > >=20 > >=20 > > =E5=9C=A8 2026/6/22 16:42, David Laight =E5=86=99=E9=81=93: =20 > >> On Mon, 22 Jun 2026 12:05:31 +0800 > >> Kaitao Cheng wrote: > >> =20 > >>> From: Kaitao Cheng > >>> > >>> The list_for_each*_safe() helpers are used when the loop body may > >>> remove the current entry. Their API exposes the temporary cursor at > >>> every call site, even though most users only need it for the iterator > >>> implementation and never reference it in the loop body. > >>> > >>> Add *_mutable() variants for list and hlist iteration. The new helpe= rs > >>> support both forms: callers may keep passing an explicit temporary cu= rsor > >>> when they need to inspect or reset it, or omit it and let the helper = use > >>> a unique internal cursor. =20 > >> > >> I'm not really sure 'mutable' means anything either. > >> It is possible to make it valid for the loop body (or even other threa= ds) > >> to delete arbitrary list items - but that needs significant extra over= heads. > >> > >> It might be worth doing something that doesn't need the extra variable, > >> but there is little point doing all the churn just to rename things. > >> =20 > >>> > >>> This makes call sites that only mutate the list through the current e= ntry > >>> less noisy, while keeping the existing *_safe() helpers available for > >>> compatibility. > >>> > >>> Signed-off-by: Kaitao Cheng > >>> --- > >>> include/linux/list.h | 269 +++++++++++++++++++++++++++++++++++++----= -- > >>> 1 file changed, 231 insertions(+), 38 deletions(-) > >>> > >>> diff --git a/include/linux/list.h b/include/linux/list.h > >>> index 09d979976b3b..1081def7cea9 100644 > >>> --- a/include/linux/list.h > >>> +++ b/include/linux/list.h > >>> @@ -7,6 +7,7 @@ > >>> #include > >>> #include > >>> #include > >>> +#include > >>> =20 > >>> #include > >>> =20 > >>> @@ -763,28 +764,72 @@ static inline void list_splice_tail_init(struct= list_head *list, > >>> #define list_for_each_prev(pos, head) \ > >>> for (pos =3D (head)->prev; !list_is_head(pos, (head)); pos =3D pos-= >prev) > >>> =20 > >>> -/** > >>> - * list_for_each_safe - iterate over a list safe against removal of = list entry > >>> - * @pos: the &struct list_head to use as a loop cursor. > >>> - * @n: another &struct list_head to use as temporary storage > >>> - * @head: the head for your list. > >>> +/* > >>> + * list_for_each_safe is an old interface, use list_for_each_mutable= instead. > >>> */ > >>> #define list_for_each_safe(pos, n, head) \ > >>> for (pos =3D (head)->next, n =3D pos->next; \ > >>> !list_is_head(pos, (head)); \ > >>> pos =3D n, n =3D pos->next) > >>> =20 > >>> +#define __list_for_each_mutable_internal(pos, tmp, head) \ > >>> + for (typeof(pos) tmp =3D (pos =3D (head)->next)->next; \ =20 > >> > >> Use auto > >> =20 > >>> + !list_is_head(pos, (head)); \ > >>> + pos =3D tmp, tmp =3D pos->next) > >>> + > >>> +#define __list_for_each_mutable1(pos, head) \ > >>> + __list_for_each_mutable_internal(pos, __UNIQUE_ID(next), head) > >>> + > >>> +#define __list_for_each_mutable2(pos, next, head) \ > >>> + list_for_each_safe(pos, next, head) > >>> + > >>> /** > >>> - * list_for_each_prev_safe - iterate over a list backwards safe agai= nst removal of list entry > >>> + * list_for_each_mutable - iterate over a list safe against entry re= moval > >>> * @pos: the &struct list_head to use as a loop cursor. > >>> - * @n: another &struct list_head to use as temporary storage > >>> - * @head: the head for your list. > >>> + * @...: either (head) or (next, head) > >>> + * > >>> + * next: another &struct list_head to use as optional temporary stor= age. > >>> + * The temporary cursor is internal unless explicitly supplied by > >>> + * the caller. > >>> + * head: the head for your list. > >>> + */ > >>> +#define list_for_each_mutable(pos, ...) \ > >>> + CONCATENATE(__list_for_each_mutable, COUNT_ARGS(__VA_ARGS__)) \ > >>> + (pos, __VA_ARGS__) =20 > >> > >> The variable argument count logic really just slows down compilation. > >> Maybe there aren't enough copies of this code to make that significant. > >> But just because you can do it doesn't mean it is a gooD idea. > >> I'm also not sure it really adds anything to the readability. > >> > >> And, it you are going to make the middle argument optional there is > >> no need to change the macro name. =20 > >=20 > > Christian K=C3=B6nig and Jani Nikula also disagree with the variadic-ar= gument > > implementation approach. If we abandon that method, it means we will > > inevitably need to add some new macros. If mutable is not a good name, > > suggestions for better alternatives would be welcome; coming up with a > > suitable name is indeed rather tricky. =20 >=20 > I don't think you need to add a new macro for the specific use case that = people want to modify the next element of the iteration. >=20 > If I remember your numbers correctly that is a really corner case and kee= ping using the existing *_safe() macros for that sounds perfectly fine to m= e. IIRC currently you have a choice of either: define Item that can't be deleted list_for_each() The current item. list_for_each_safe() The next item. There is also likely to be code that updates the variables to allow for other scenarios. Note that if increase a reference count and release a lock then list_for_ea= ch() is likely safer than list_for_each_safe() :-) list.h has 9 variants of the 'safe' loop. The bloat of another 9 is getting excessive. It has to be said that this is one of my least favourite type of list... David >=20 > Regards, > Christian.