From: Yosry Ahmed <yosry.ahmed@linux.dev>
To: Shakeel Butt <shakeel.butt@linux.dev>
Cc: "Tejun Heo" <tj@kernel.org>,
"Andrew Morton" <akpm@linux-foundation.org>,
"Alexei Starovoitov" <ast@kernel.org>,
"Johannes Weiner" <hannes@cmpxchg.org>,
"Michal Hocko" <mhocko@kernel.org>,
"Roman Gushchin" <roman.gushchin@linux.dev>,
"Muchun Song" <muchun.song@linux.dev>,
"Michal Koutný" <mkoutny@suse.com>,
"Vlastimil Babka" <vbabka@suse.cz>,
"Sebastian Andrzej Siewior" <bigeasy@linutronix.de>,
"JP Kobryn" <inwardvessel@gmail.com>,
bpf@vger.kernel.org, linux-mm@kvack.org, cgroups@vger.kernel.org,
linux-kernel@vger.kernel.org,
"Meta kernel team" <kernel-team@meta.com>
Subject: Re: [RFC PATCH 1/3] llist: add list_add_iff_not_on_list()g
Date: Wed, 30 Apr 2025 05:44:54 -0700 [thread overview]
Message-ID: <aBIbRhLjmO-fKKGr@Asmaa.> (raw)
In-Reply-To: <20250429061211.1295443-2-shakeel.butt@linux.dev>
On Mon, Apr 28, 2025 at 11:12:07PM -0700, Shakeel Butt wrote:
> As the name implies, list_add_iff_not_on_list() adds the given node to
> the given only if the node is not on any list. Many CPUs can call this
> concurrently on the same node and only one of them will succeed.
>
> This is also useful to be used by different contexts like task, irq and
> nmi. In the case of failure either the node as already present on some
> list or the caller can lost the race to add the given node to a list.
> That node will eventually be added to a list by the winner.
>
> Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
> ---
> include/linux/llist.h | 3 +++
> lib/llist.c | 30 ++++++++++++++++++++++++++++++
> 2 files changed, 33 insertions(+)
>
> diff --git a/include/linux/llist.h b/include/linux/llist.h
> index 2c982ff7475a..030cfec8778b 100644
> --- a/include/linux/llist.h
> +++ b/include/linux/llist.h
> @@ -236,6 +236,9 @@ static inline bool __llist_add_batch(struct llist_node *new_first,
> return new_last->next == NULL;
> }
>
> +extern bool llist_add_iff_not_on_list(struct llist_node *new,
> + struct llist_head *head);
> +
> /**
> * llist_add - add a new entry
> * @new: new entry to be added
> diff --git a/lib/llist.c b/lib/llist.c
> index f21d0cfbbaaa..9d743164720f 100644
> --- a/lib/llist.c
> +++ b/lib/llist.c
> @@ -36,6 +36,36 @@ bool llist_add_batch(struct llist_node *new_first, struct llist_node *new_last,
> }
> EXPORT_SYMBOL_GPL(llist_add_batch);
>
> +/**
> + * llist_add_iff_not_on_list - add an entry if it is not on list
> + * @new: entry to be added
> + * @head: the head for your lock-less list
> + *
> + * Adds the given entry to the given list only if the entry is not on any list.
> + * This is useful for cases where multiple CPUs tries to add the same node to
> + * the list or multiple contexts (process, irq or nmi) may add the same node to
> + * the list.
> + *
> + * Return true only if the caller has successfully added the given node to the
> + * list. Returns false if entry is already on some list or if another inserter
> + * wins the race to eventually add the given node to the list.
> + */
> +bool llist_add_iff_not_on_list(struct llist_node *new, struct llist_head *head)
What about llist_try_add()?
> +{
> + struct llist_node *first = READ_ONCE(head->first);
> +
> + if (llist_on_list(new))
> + return false;
> +
> + if (cmpxchg(&new->next, new, first) != new)
> + return false;
Here we will set new->next to the current head of the list, but this may
change from under us, and the next loop will then set it correctly
anyway. This is a bit confusing though.
Would it be better if we set new->next to NULL here, and then completely
rely on the loop below to set it properly?
> +
> + while (!try_cmpxchg(&head->first, &first, new))
> + new->next = first;
Not a big deal, but should we use llist_add_batch() here instead?
> + return true;
> +}
> +EXPORT_SYMBOL_GPL(llist_add_iff_not_on_list);
> +
> /**
> * llist_del_first - delete the first entry of lock-less list
> * @head: the head for your lock-less list
> --
> 2.47.1
>
next prev parent reply other threads:[~2025-04-30 12:45 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-29 6:12 [RFC PATCH 0/3] cgroup: nmi safe css_rstat_updated Shakeel Butt
2025-04-29 6:12 ` [RFC PATCH 1/3] llist: add list_add_iff_not_on_list() Shakeel Butt
2025-04-30 12:44 ` Yosry Ahmed [this message]
2025-04-29 6:12 ` [RFC PATCH 2/3] cgroup: support to enable nmi-safe css_rstat_updated Shakeel Butt
2025-04-29 6:12 ` [RFC PATCH 3/3] cgroup: make css_rstat_updated nmi safe Shakeel Butt
2025-04-30 13:14 ` Yosry Ahmed
2025-05-01 22:10 ` Shakeel Butt
2025-05-06 9:41 ` Yosry Ahmed
2025-05-06 19:30 ` Shakeel Butt
2025-05-07 6:52 ` Yosry Ahmed
2025-04-29 6:12 ` [OFFLIST PATCH 1/2] cgroup: use separate rstat trees for each subsystem Shakeel Butt
2025-04-29 6:12 ` [OFFLIST PATCH 2/2] cgroup: use subsystem-specific rstat locks to avoid contention Shakeel Butt
2025-04-29 6:15 ` Shakeel Butt
2025-05-21 22:23 ` Klara Modin
2025-05-21 22:29 ` Tejun Heo
2025-05-21 23:23 ` Shakeel Butt
2025-05-21 23:33 ` Shakeel Butt
2025-05-21 23:47 ` JP Kobryn
2025-05-21 23:50 ` Shakeel Butt
2025-05-21 23:52 ` JP Kobryn
2025-05-21 23:47 ` Shakeel Butt
2025-04-29 6:15 ` [OFFLIST PATCH 1/2] cgroup: use separate rstat trees for each subsystem Shakeel Butt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aBIbRhLjmO-fKKGr@Asmaa. \
--to=yosry.ahmed@linux.dev \
--cc=akpm@linux-foundation.org \
--cc=ast@kernel.org \
--cc=bigeasy@linutronix.de \
--cc=bpf@vger.kernel.org \
--cc=cgroups@vger.kernel.org \
--cc=hannes@cmpxchg.org \
--cc=inwardvessel@gmail.com \
--cc=kernel-team@meta.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@kernel.org \
--cc=mkoutny@suse.com \
--cc=muchun.song@linux.dev \
--cc=roman.gushchin@linux.dev \
--cc=shakeel.butt@linux.dev \
--cc=tj@kernel.org \
--cc=vbabka@suse.cz \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox