From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f42.google.com (mail-wr1-f42.google.com [209.85.221.42]) (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 94F6C391E4C for ; Mon, 22 Jun 2026 08:42:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.42 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782117769; cv=none; b=GVXNOq4my+m9mwnwRJzWwVjRiHh2Pr8teWWuMmSkCve7yr9zAxKWCqudyOz1sspist5gF7B90vEiyuq/lCgrzfWefrC3c+cne5JF6ONiSX3P6CoV+woaWDikswZxvOrxbBQR12uVf0w0lkQyXdaHkL9Sl++TgsFoX3XgopdLusY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782117769; c=relaxed/simple; bh=IER6Q4RbEHU+TqBNCFXousE9wjBARqNFqdXSUnckiEg=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=FLVeNsCmZXJbf2GpVbtQEyfEbgxhdEKzXndO//tokSNcUb5iTFWxjOFoKcSjd6Wcj7Lvlkb+SxzQGb9Ai4MkjvNNmTVGvj3sMGvO16oi5FzbqJpRPMXJXkiFWSkQIyu+6XUpOpVon+OmDzzXTbVUtoY80E3DFK+LIHQmPEQxl+s= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ROskh/8t; arc=none smtp.client-ip=209.85.221.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ROskh/8t" Received: by mail-wr1-f42.google.com with SMTP id ffacd0b85a97d-464192ab2e1so2533345f8f.0 for ; Mon, 22 Jun 2026 01:42:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1782117765; x=1782722565; darn=vger.kernel.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=bFnA4MB3rhX4hW9kwKpEDdWQh6n3FEg0cpZAkJWsmhQ=; b=ROskh/8tfIJaOesnEZqCHlQNJbZWki5RbTqza/lsX5eovRkaYimKsd+YXB/k2zMi/m /oMxYwZTti4VBo4Zu7g+ne/XnDzRPkeytOwEHJ1QPmKrsfs8gcwvBhLar3NiR6OJGf0L +J8cEmu4EM9EXTaOXzKCB3rcDsY6oG2rx0tNv24ObV21OlAfSpd7lVCwHX7BM5qlSMrU 0Y+stJ4hQbL/MqG06XGIR3AP4JZd7J5kU0fBgPGMIRm0x4a2O+CoKjF5iYge8niCKb3T 4w+mIabC6ghr2q6JO+5qYmEFnGpSPuxK8BZC5l6QgOh4KqNC3bjBRyqs8iNZbdgiJDoz PvCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782117765; x=1782722565; 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=bFnA4MB3rhX4hW9kwKpEDdWQh6n3FEg0cpZAkJWsmhQ=; b=R6s3EFENSVu7QcXZPS8j3Cun0WaB7Ib+rjyXXZwoggrW37OyfW3jKvQ+2BN9+rubK7 nFmBcAX7UeFE3rlqykPzRzOHZtwdugmrXqF8AGvUh8tBJOLEI9IcZMfFgfGFrA6zR6Or p7UZ1PuhOn9imZ1CnqSnoZCJPGE3f4eqWVZukaeTCwqb7Ghyiu/aTgs8ZTa5oc6B7b4u pCHnzzxP0AU+WYponpv3aHMIs+j3qDYEVpjr7FvnKUchjbYaziORsIA00uJEtCvZ9XXu zxjGhkYm712f/kKFC1aaJy6sTYTfm26fE7s+fyAyh4OiKZERGXwK6oZqp1U82Fl34Ufn 6Jfg== X-Forwarded-Encrypted: i=1; AHgh+Rq5pVqMd4E5wCvsWxSc+KzsmorjJDcgft4A49Bw4uamKoLw2jx7EQjqbim5gBDsBYgMhDSGAgQU/A==@vger.kernel.org X-Gm-Message-State: AOJu0YxCwhsLkURAh6xNM+FzIYtyO2poefIq7hyrwf0jpSo01ESQBLed lc2Uz5x0VJNjXsdmU0aJhP7kfCV2upebbH6SmazeNGkdChShi1qpBYdp X-Gm-Gg: AfdE7cn6JqLkWXu1mxYeoGUGpV4rFMBgS/lw8hZK3FW4OIueWK2gnhHyJi3MSAZdbgQ U0Q75/5q1nbPkDmgfwXPtjSO0vWiT9JQNcw4NQWYXZv8gNBQY9yqK24xtCIuTUFOf1jQhWvtfFQ 9XBdCnkt1U9uloNH5UiPlHyInG6oY/rZ5ufYRRpDfiVRO1ks3zC9IWVJblyIzqmi5Bge1Z7d6NN ZxaUeiHAX6OPb+Iw4+/6c7+McQuzYPnNIJwZ+doWEIU02exx7BDVvtgxUyApq9Ea8UAGAUiMoBS pTvBpAY7fwv6SiN1Q0PiJzRnlSNPVoZzdaPdoCKuWmHGGEXjdX8yD9aDfqEc5cYP+VBFhp6TpfR dJ89d9fUeGIjx1NDBJJqrwQwp2DVhngsV+vcLekX6OXDx8HeWtgacRYzgYouJCQ8cbBLp+cASQD kCMk9sNtKrl7PLysUfPQ8EIpM6ifnNQAiGDzwlTMYLz4w3+MLlV57hY1cm8chQ X-Received: by 2002:a05:6000:2006:b0:460:1e5a:2267 with SMTP id ffacd0b85a97d-4650090b736mr21019711f8f.17.1782117764761; Mon, 22 Jun 2026 01:42:44 -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-46666c57afasm24839972f8f.29.2026.06.22.01.42.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jun 2026 01:42:44 -0700 (PDT) Date: Mon, 22 Jun 2026 09:42:42 +0100 From: David Laight To: Kaitao Cheng Cc: 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 , Christian =?UTF-8?B?S8O2bmln?= , 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: <20260622094242.64531b9a@pumpkin> In-Reply-To: <20260622040533.29824-2-kaitao.cheng@linux.dev> References: <20260622040533.29824-1-kaitao.cheng@linux.dev> <20260622040533.29824-2-kaitao.cheng@linux.dev> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Mon, 22 Jun 2026 12:05:31 +0800 Kaitao Cheng wrote: > 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 helpers > support both forms: callers may keep passing an explicit temporary cursor > when they need to inspect or reset it, or omit it and let the helper use > a unique internal cursor. I'm not really sure 'mutable' means anything either. It is possible to make it valid for the loop body (or even other threads) to delete arbitrary list items - but that needs significant extra overheads. 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. > > This makes call sites that only mutate the list through the current entry > 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 > > #include > > @@ -763,28 +764,72 @@ static inline void list_splice_tail_init(struct list_head *list, > #define list_for_each_prev(pos, head) \ > for (pos = (head)->prev; !list_is_head(pos, (head)); pos = pos->prev) > > -/** > - * 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 = (head)->next, n = pos->next; \ > !list_is_head(pos, (head)); \ > pos = n, n = pos->next) > > +#define __list_for_each_mutable_internal(pos, tmp, head) \ > + for (typeof(pos) tmp = (pos = (head)->next)->next; \ Use auto > + !list_is_head(pos, (head)); \ > + pos = tmp, tmp = 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 against removal of list entry > + * list_for_each_mutable - iterate over a list safe against entry removal > * @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 storage. > + * 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__) 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. David