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 C054FC369D9 for ; Wed, 30 Apr 2025 12:45:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A9D006B00A1; Wed, 30 Apr 2025 08:45:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A4C2F6B00A6; Wed, 30 Apr 2025 08:45:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8C7136B00B5; Wed, 30 Apr 2025 08:45:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 6A0596B00A1 for ; Wed, 30 Apr 2025 08:45:08 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 65EE91A17ED for ; Wed, 30 Apr 2025 12:45:09 +0000 (UTC) X-FDA: 83390680338.13.F296BD8 Received: from out-177.mta1.migadu.com (out-177.mta1.migadu.com [95.215.58.177]) by imf15.hostedemail.com (Postfix) with ESMTP id 9004AA0012 for ; Wed, 30 Apr 2025 12:45:07 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=ERwIhe2K; spf=pass (imf15.hostedemail.com: domain of yosry.ahmed@linux.dev designates 95.215.58.177 as permitted sender) smtp.mailfrom=yosry.ahmed@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1746017107; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=OL6pMpPrHxyalMOEj7Fjs8lPAUCwXWRv7qPE6i/mzfw=; b=F98eoHd6sf7alOVMup7zrC4yn9rvGFav8Kgp0PDlvVirKSx48EHha3KAOjcJfL4mNGqmIq VcwiASCc4yaoBgojEzconkJOxJEVSSK6RcEH3hasGjyzQHLyhchb0Ln4FFe4Y2zgdWh9KZ hyQe36aypKt9bjIbzfY0BUkVjxrjalg= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=ERwIhe2K; spf=pass (imf15.hostedemail.com: domain of yosry.ahmed@linux.dev designates 95.215.58.177 as permitted sender) smtp.mailfrom=yosry.ahmed@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1746017107; a=rsa-sha256; cv=none; b=rdEDc0gMRCSaBvXGBwcMjo2i7vkP5nZ5hngEi69pzKbW+cjXAqiQr8jDIzIo+H6MyGLKja jS91Mrelx1EUN+lmcTXizbk8DFKs4zKxq/lBUDmPtmWGqKzLHsAiMLdTuZ7gEiKznOr5Wc H/7qF0oNzJXmhbkOz1EE49q8ZeK2x/k= Date: Wed, 30 Apr 2025 05:44:54 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1746017103; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=OL6pMpPrHxyalMOEj7Fjs8lPAUCwXWRv7qPE6i/mzfw=; b=ERwIhe2KSaPvDzZid2uMe37+8TaX+7mKx4+9WR7jMGe8+VJZdwPZmH2/xOXwnWqVITMkiR Ui97Kbpco+3k4kBbX4xeHq+Iuy6Q4h8AANT93TUkd1Hps8TLlTlxh2fTZmSCTYODBRpvcE hX7V3ZbTLrXw956QCoYrR+LCdSU0HVQ= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Yosry Ahmed To: Shakeel Butt Cc: Tejun Heo , Andrew Morton , Alexei Starovoitov , Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , Michal =?utf-8?Q?Koutn=C3=BD?= , Vlastimil Babka , Sebastian Andrzej Siewior , JP Kobryn , bpf@vger.kernel.org, linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Meta kernel team Subject: Re: [RFC PATCH 1/3] llist: add list_add_iff_not_on_list()g Message-ID: References: <20250429061211.1295443-1-shakeel.butt@linux.dev> <20250429061211.1295443-2-shakeel.butt@linux.dev> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20250429061211.1295443-2-shakeel.butt@linux.dev> X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 9004AA0012 X-Stat-Signature: n6jtfryc934e14bwjejpiwizsjpdtt53 X-HE-Tag: 1746017107-147430 X-HE-Meta: U2FsdGVkX1+eKj4mSMJFcZwBFbNnsaCaM2Ky3BrdOpZ0ddWCmBTYr3k2L7lfmzUomsOS5ECQSSMBKP2I4fYvjltDE7I29IQz/nTxp/8bdPVfllHuEuyQwpukT/ZPXF0vv7nmjd50NpICrN7ndA0UIriOKogXyyr7J3vKIkFtNKPk83dga8yZqbpFWRMcAdXMLAE7/uMPZf3KwrnfThWE2MlX1gtf7OqH1C82S0bClF+GD+hA0ewlqZesGrWaFAwYumLQvEOx0IKZLBWuYurELa2F0rLabCZsNVP7fbIROAhJEVRaAWvmBA1kEeYERPvpPvYWSOW8gHwWCD4TdpXi7DTB0WDdC6FrDhFSNzYcKgNVOmNRhNy8g4MNDAPVMh/r7lsKDLOOW3eFrENPCkDWyaViwfbw2raYtQ6cNUot4pjVONySYc5ENiHWclR62+jyP18YrmZjmPIbPIso8iH5ybm/fZxpj5fl78ZC4CApXhQspvA1qgozyr8s4DgZ8zl+knh7jO9UkO/pYiQyxYzDVuJ+wSe32vcOb1aJmzO96PM6n9NELWWEo/r4Oj+Ui/d9mlQeHFlzF+fIOwNoDgboGo10rB4VMKu/OF/GhxP5yiKuB/v7hMth+KmJ3jDzUNAm7xavU3ULYEjDtdpPpRlzYBI5Inuesivh+Pnb4XFxxYiSvxunctQrgbC3Qal1ashFPRZaVSU8vPi6mdtDRlZUw/c1qrihfk/N7J0A2/svMGMVZo3XA/GuGrTpQIPyNb5fGF+U6CTlLav54lEEGLpf04g7txLG96eZ+CMy5j+j4xqZDSrmyFeq4Pa4CqKmpLLpHe+djhx1SULD8MyURIy8pnneY+rTFu05R2mNZwNriNGV+MR85Qm23Z6DMye78UJgQjoqlN7nVDwA8FRaF8XEsGqUMtCtYnIhNgqh6Y7Dmu2Jw6pSjR4tKD0BMndSrzIRV+ljq9pscM63NuI5h9W SVJ33BHp XfqzsQ3SwqkdBPJX9DdPoOFydNTmhLb5W93OsDC/fPLU92GxxzAJDOPQzAARuk9sxuZticNI+XaGhG/rA4G7RsaGIPnV5cJe4iCL4XA1L64KZCG6OGQtB+g9pYH0gm7Vd0CDITsG+Ms+6k0tCrrsteF5q3NxortA56shtVPicwpLKbNbyMD2175KQv+KqZFSHiSXdNyz2JH4Uu97EsjGHfwA8EztRrGEe5pv9Bs7+TCcatks= 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: 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 > --- > 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 >