From: Pasha Tatashin <pasha.tatashin@soleen.com>
To: pratyush@kernel.org, pasha.tatashin@soleen.com, rppt@kernel.org,
dmatlack@google.com, skhawaja@google.com, rientjes@google.com,
corbet@lwn.net, akpm@linux-foundation.org, kees@kernel.org,
davidgow@google.com, pmladek@suse.com,
linux-kernel@vger.kernel.org, linux-mm@kvack.org,
nicolas.frattaroli@collabora.com, linux-doc@vger.kernel.org,
tamird@gmail.com, raemoar63@gmail.com, graf@amazon.com
Subject: [PATCH v2 1/5] list: add primitives for private list manipulations
Date: Thu, 18 Dec 2025 10:57:48 -0500 [thread overview]
Message-ID: <20251218155752.3045808-2-pasha.tatashin@soleen.com> (raw)
In-Reply-To: <20251218155752.3045808-1-pasha.tatashin@soleen.com>
Linux recently added an ability to add private members to structs (i.e.
__private) and access them via ACCESS_PRIVATE(). This ensures that those
members are only accessible by the subsystem which owns the struct type,
and not to the object owner.
However, struct list_head often needs to be placed into the private
section to be manipulated privately by the subsystem.
Add macros to support private list manipulations in
<linux/list_private.h>.
Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
---
Documentation/core-api/list.rst | 9 ++
include/linux/list_private.h | 256 ++++++++++++++++++++++++++++++++
2 files changed, 265 insertions(+)
create mode 100644 include/linux/list_private.h
diff --git a/Documentation/core-api/list.rst b/Documentation/core-api/list.rst
index 86873ce9adbf..241464ca0549 100644
--- a/Documentation/core-api/list.rst
+++ b/Documentation/core-api/list.rst
@@ -774,3 +774,12 @@ Full List API
.. kernel-doc:: include/linux/list.h
:internal:
+
+Private List API
+================
+
+.. kernel-doc:: include/linux/list_private.h
+ :doc: Private List Primitives
+
+.. kernel-doc:: include/linux/list_private.h
+ :internal:
diff --git a/include/linux/list_private.h b/include/linux/list_private.h
new file mode 100644
index 000000000000..6f93d54e797a
--- /dev/null
+++ b/include/linux/list_private.h
@@ -0,0 +1,256 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Copyright (c) 2025, Google LLC.
+ * Pasha Tatashin <pasha.tatashin@soleen.com>
+ */
+#ifndef _LINUX_LIST_PRIVATE_H
+#define _LINUX_LIST_PRIVATE_H
+
+/**
+ * DOC: Private List Primitives
+ *
+ * Provides a set of list primitives identical in function to those in
+ * ``<linux/list.h>``, but designed for cases where the embedded
+ * ``&struct list_head`` is private member.
+ */
+
+#include <linux/compiler.h>
+#include <linux/list.h>
+
+#define __list_private_offset(type, member) \
+ ((size_t)(&ACCESS_PRIVATE(((type *)0), member)))
+
+/**
+ * list_private_entry - get the struct for this entry
+ * @ptr: the &struct list_head pointer.
+ * @type: the type of the struct this is embedded in.
+ * @member: the identifier passed to ACCESS_PRIVATE.
+ */
+#define list_private_entry(ptr, type, member) ({ \
+ const struct list_head *__mptr = (ptr); \
+ (type *)((char *)__mptr - __list_private_offset(type, member)); \
+})
+
+/**
+ * list_private_first_entry - get the first element from a list
+ * @ptr: the list head to take the element from.
+ * @type: the type of the struct this is embedded in.
+ * @member: the identifier passed to ACCESS_PRIVATE.
+ */
+#define list_private_first_entry(ptr, type, member) \
+ list_private_entry((ptr)->next, type, member)
+
+/**
+ * list_private_last_entry - get the last element from a list
+ * @ptr: the list head to take the element from.
+ * @type: the type of the struct this is embedded in.
+ * @member: the identifier passed to ACCESS_PRIVATE.
+ */
+#define list_private_last_entry(ptr, type, member) \
+ list_private_entry((ptr)->prev, type, member)
+
+/**
+ * list_private_next_entry - get the next element in list
+ * @pos: the type * to cursor
+ * @member: the name of the list_head within the struct.
+ */
+#define list_private_next_entry(pos, member) \
+ list_private_entry(ACCESS_PRIVATE(pos, member).next, typeof(*(pos)), member)
+
+/**
+ * list_private_next_entry_circular - get the next element in list
+ * @pos: the type * to cursor.
+ * @head: the list head to take the element from.
+ * @member: the name of the list_head within the struct.
+ *
+ * Wraparound if pos is the last element (return the first element).
+ * Note, that list is expected to be not empty.
+ */
+#define list_private_next_entry_circular(pos, head, member) \
+ (list_is_last(&ACCESS_PRIVATE(pos, member), head) ? \
+ list_private_first_entry(head, typeof(*(pos)), member) : \
+ list_private_next_entry(pos, member))
+
+/**
+ * list_private_prev_entry - get the prev element in list
+ * @pos: the type * to cursor
+ * @member: the name of the list_head within the struct.
+ */
+#define list_private_prev_entry(pos, member) \
+ list_private_entry(ACCESS_PRIVATE(pos, member).prev, typeof(*(pos)), member)
+
+/**
+ * list_prev_entry_circular - get the prev element in list
+ * @pos: the type * to cursor.
+ * @head: the list head to take the element from.
+ * @member: the name of the list_head within the struct.
+ *
+ * Wraparound if pos is the first element (return the last element).
+ * Note, that list is expected to be not empty.
+ */
+#define list_private_prev_entry_circular(pos, head, member) \
+ (list_is_first(&ACCESS_PRIVATE(pos, member), head) ? \
+ list_private_last_entry(head, typeof(*(pos)), member) : \
+ list_private_prev_entry(pos, member))
+
+/**
+ * list_private_entry_is_head - test if the entry points to the head of the list
+ * @pos: the type * to cursor
+ * @head: the head for your list.
+ * @member: the name of the list_head within the struct.
+ */
+#define list_private_entry_is_head(pos, head, member) \
+ list_is_head(&ACCESS_PRIVATE(pos, member), (head))
+
+/**
+ * list_private_for_each_entry - iterate over list of given type
+ * @pos: the type * to use as a loop cursor.
+ * @head: the head for your list.
+ * @member: the name of the list_head within the struct.
+ */
+#define list_private_for_each_entry(pos, head, member) \
+ for (pos = list_private_first_entry(head, typeof(*pos), member); \
+ !list_private_entry_is_head(pos, head, member); \
+ pos = list_private_next_entry(pos, member))
+
+/**
+ * list_private_for_each_entry_reverse - iterate backwards over list of given type.
+ * @pos: the type * to use as a loop cursor.
+ * @head: the head for your list.
+ * @member: the name of the list_head within the struct.
+ */
+#define list_private_for_each_entry_reverse(pos, head, member) \
+ for (pos = list_private_last_entry(head, typeof(*pos), member); \
+ !list_private_entry_is_head(pos, head, member); \
+ pos = list_private_prev_entry(pos, member))
+
+/**
+ * list_private_for_each_entry_continue - continue iteration over list of given type
+ * @pos: the type * to use as a loop cursor.
+ * @head: the head for your list.
+ * @member: the name of the list_head within the struct.
+ *
+ * Continue to iterate over list of given type, continuing after
+ * the current position.
+ */
+#define list_private_for_each_entry_continue(pos, head, member) \
+ for (pos = list_private_next_entry(pos, member); \
+ !list_private_entry_is_head(pos, head, member); \
+ pos = list_private_next_entry(pos, member))
+
+/**
+ * list_private_for_each_entry_continue_reverse - iterate backwards from the given point
+ * @pos: the type * to use as a loop cursor.
+ * @head: the head for your list.
+ * @member: the name of the list_head within the struct.
+ *
+ * Start to iterate over list of given type backwards, continuing after
+ * the current position.
+ */
+#define list_private_for_each_entry_continue_reverse(pos, head, member) \
+ for (pos = list_private_prev_entry(pos, member); \
+ !list_private_entry_is_head(pos, head, member); \
+ pos = list_private_prev_entry(pos, member))
+
+/**
+ * list_private_for_each_entry_from - iterate over list of given type from the current point
+ * @pos: the type * to use as a loop cursor.
+ * @head: the head for your list.
+ * @member: the name of the list_head within the struct.
+ *
+ * Iterate over list of given type, continuing from current position.
+ */
+#define list_private_for_each_entry_from(pos, head, member) \
+ for (; !list_private_entry_is_head(pos, head, member); \
+ pos = list_private_next_entry(pos, member))
+
+/**
+ * list_private_for_each_entry_from_reverse - iterate backwards over list of given type
+ * from the current point
+ * @pos: the type * to use as a loop cursor.
+ * @head: the head for your list.
+ * @member: the name of the list_head within the struct.
+ *
+ * Iterate backwards over list of given type, continuing from current position.
+ */
+#define list_private_for_each_entry_from_reverse(pos, head, member) \
+ for (; !list_private_entry_is_head(pos, head, member); \
+ pos = list_private_prev_entry(pos, member))
+
+/**
+ * list_private_for_each_entry_safe - iterate over list of given type safe against removal of list entry
+ * @pos: the type * to use as a loop cursor.
+ * @n: another type * to use as temporary storage
+ * @head: the head for your list.
+ * @member: the name of the list_head within the struct.
+ */
+#define list_private_for_each_entry_safe(pos, n, head, member) \
+ for (pos = list_private_first_entry(head, typeof(*pos), member), \
+ n = list_private_next_entry(pos, member); \
+ !list_private_entry_is_head(pos, head, member); \
+ pos = n, n = list_private_next_entry(n, member))
+
+/**
+ * list_private_for_each_entry_safe_continue - continue list iteration safe against removal
+ * @pos: the type * to use as a loop cursor.
+ * @n: another type * to use as temporary storage
+ * @head: the head for your list.
+ * @member: the name of the list_head within the struct.
+ *
+ * Iterate over list of given type, continuing after current point,
+ * safe against removal of list entry.
+ */
+#define list_private_for_each_entry_safe_continue(pos, n, head, member) \
+ for (pos = list_private_next_entry(pos, member), \
+ n = list_private_next_entry(pos, member); \
+ !list_private_entry_is_head(pos, head, member); \
+ pos = n, n = list_private_next_entry(n, member))
+
+/**
+ * list_private_for_each_entry_safe_from - iterate over list from current point safe against removal
+ * @pos: the type * to use as a loop cursor.
+ * @n: another type * to use as temporary storage
+ * @head: the head for your list.
+ * @member: the name of the list_head within the struct.
+ *
+ * Iterate over list of given type from current point, safe against
+ * removal of list entry.
+ */
+#define list_private_for_each_entry_safe_from(pos, n, head, member) \
+ for (n = list_private_next_entry(pos, member); \
+ !list_private_entry_is_head(pos, head, member); \
+ pos = n, n = list_private_next_entry(n, member))
+
+/**
+ * list_private_for_each_entry_safe_reverse - iterate backwards over list safe against removal
+ * @pos: the type * to use as a loop cursor.
+ * @n: another type * to use as temporary storage
+ * @head: the head for your list.
+ * @member: the name of the list_head within the struct.
+ *
+ * Iterate backwards over list of given type, safe against removal
+ * of list entry.
+ */
+#define list_private_for_each_entry_safe_reverse(pos, n, head, member) \
+ for (pos = list_private_last_entry(head, typeof(*pos), member), \
+ n = list_private_prev_entry(pos, member); \
+ !list_private_entry_is_head(pos, head, member); \
+ pos = n, n = list_private_prev_entry(n, member))
+
+/**
+ * list_private_safe_reset_next - reset a stale list_for_each_entry_safe loop
+ * @pos: the loop cursor used in the list_for_each_entry_safe loop
+ * @n: temporary storage used in list_for_each_entry_safe
+ * @member: the name of the list_head within the struct.
+ *
+ * list_safe_reset_next is not safe to use in general if the list may be
+ * modified concurrently (eg. the lock is dropped in the loop body). An
+ * exception to this is if the cursor element (pos) is pinned in the list,
+ * and list_safe_reset_next is called after re-taking the lock and before
+ * completing the current iteration of the loop body.
+ */
+#define list_private_safe_reset_next(pos, n, member) \
+ n = list_private_next_entry(pos, member)
+
+#endif /* _LINUX_LIST_PRIVATE_H */
--
2.52.0.313.g674ac2bdf7-goog
next prev parent reply other threads:[~2025-12-18 15:58 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-18 15:57 [PATCH v2 0/5] list private v2 & luo flb v9 Pasha Tatashin
2025-12-18 15:57 ` Pasha Tatashin [this message]
2025-12-18 15:57 ` [PATCH v2 2/5] list: add kunit test for private list primitives Pasha Tatashin
2025-12-18 15:57 ` [PATCH v2 3/5] liveupdate: luo_file: Use private list Pasha Tatashin
2025-12-18 15:57 ` [PATCH v9 4/5] liveupdate: luo_flb: Introduce File-Lifecycle-Bound global state Pasha Tatashin
2025-12-18 15:57 ` [PATCH v9 5/5] tests/liveupdate: Add in-kernel liveupdate test Pasha Tatashin
2025-12-18 21:07 ` [PATCH v2 0/5] list private v2 & luo flb v9 Andrew Morton
2025-12-18 22:30 ` Pasha Tatashin
2025-12-18 22:34 ` Andrew Morton
2025-12-19 6:46 ` Mike Rapoport
2025-12-19 6:53 ` Pasha Tatashin
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=20251218155752.3045808-2-pasha.tatashin@soleen.com \
--to=pasha.tatashin@soleen.com \
--cc=akpm@linux-foundation.org \
--cc=corbet@lwn.net \
--cc=davidgow@google.com \
--cc=dmatlack@google.com \
--cc=graf@amazon.com \
--cc=kees@kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=nicolas.frattaroli@collabora.com \
--cc=pmladek@suse.com \
--cc=pratyush@kernel.org \
--cc=raemoar63@gmail.com \
--cc=rientjes@google.com \
--cc=rppt@kernel.org \
--cc=skhawaja@google.com \
--cc=tamird@gmail.com \
/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