Skip to contentSkip to conversation

doc/RCU/listRCU: refine example code for eliminating stale data

PATCH In review Wei Yang 3 participants 4 messages Feb 18, 2025 – Mar 5, 2025

Subsystems (2)
lore
Share & tools
lore

Wei Yang

author
#0 lore raw

PATCH v2 1/1 doc/RCU/listRCU: refine example code for eliminating stale data

This patch adjust the example code with following two purpose:

* reduce the confusion on not releasing e->lock
* emphasize e is valid and not stale with e->lock held

CC Boqun Feng <[email protected]>
CC Alan Huang <[email protected]>

---
v2:
* add the missing parameter *key
* make function return struct audit_entry

---
 Documentation/RCU/listRCU.rst | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
Documentation/RCU/listRCU.rst +6 -4
index ed5c9d8c9afe..d8bb98623c12 100644
@@ -334,7 +334,7 @@ If the system-call audit module were to ever need to reject stale data, one way
 to accomplish this would be to add a ``deleted`` flag and a ``lock`` spinlock to the
 ``audit_entry`` structure, and modify audit_filter_task() as follows::
 
-	static enum audit_state audit_filter_task(struct task_struct *tsk)
+	static struct audit_entry *audit_filter_task(struct task_struct *tsk, char **key)
 	{
 		struct audit_entry *e;
 		enum audit_state   state;
@@ -346,16 +346,18 @@ to accomplish this would be to add a ``deleted`` flag and a ``lock`` spinlock to
 				if (e->deleted) {
 					spin_unlock(&e->lock);
 					rcu_read_unlock();
-					return AUDIT_BUILD_CONTEXT;
+					return NULL;
 				}
 				rcu_read_unlock();
 				if (state == AUDIT_STATE_RECORD)
 					*key = kstrdup(e->rule.filterkey, GFP_ATOMIC);
-				return state;
+				/* As long as e->lock is held, e is valid and
+				 * its value is not stale */
+				return e;
 			}
 		}
 		rcu_read_unlock();
-		return AUDIT_BUILD_CONTEXT;
+		return NULL;
 	}
 
 The ``audit_del_rule()`` function would need to set the ``deleted`` flag under the
Signature · 1 line
2.34.1
#1 lore raw
Quoted 9 lines · Wei Yang

This patch adjust the example code with following two purpose:

* reduce the confusion on not releasing e->lock
* emphasize e is valid and not stale with e->lock held

CC Boqun Feng <[email protected]>
CC Alan Huang <[email protected]>

Alan, could you take a look and if all looks reasonable to you, maybe a Reviewed-by or Acked-by? Thanks!

Regards,
Boqun

Quoted 47 lines

---
v2:
* add the missing parameter *key
* make function return struct audit_entry

---
 Documentation/RCU/listRCU.rst | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
Documentation/RCU/listRCU.rst +6 -4
index ed5c9d8c9afe..d8bb98623c12 100644
@@ -334,7 +334,7 @@ If the system-call audit module were to ever need to reject stale data, one way
 to accomplish this would be to add a ``deleted`` flag and a ``lock`` spinlock to the
 ``audit_entry`` structure, and modify audit_filter_task() as follows::
 
-	static enum audit_state audit_filter_task(struct task_struct *tsk)
+	static struct audit_entry *audit_filter_task(struct task_struct *tsk, char **key)
 	{
 		struct audit_entry *e;
 		enum audit_state   state;
@@ -346,16 +346,18 @@ to accomplish this would be to add a ``deleted`` flag and a ``lock`` spinlock to
 				if (e->deleted) {
 					spin_unlock(&e->lock);
 					rcu_read_unlock();
-					return AUDIT_BUILD_CONTEXT;
+					return NULL;
 				}
 				rcu_read_unlock();
 				if (state == AUDIT_STATE_RECORD)
 					*key = kstrdup(e->rule.filterkey, GFP_ATOMIC);
-				return state;
+				/* As long as e->lock is held, e is valid and
+				 * its value is not stale */
+				return e;
 			}
 		}
 		rcu_read_unlock();
-		return AUDIT_BUILD_CONTEXT;
+		return NULL;
 	}
 
 The ``audit_del_rule()`` function would need to set the ``deleted`` flag under the
Signature · 1 line
2.34.1
Reviewed-by on v2 1/1
#2 lore raw
In reply to Wei Yang
Quoted 52 lines · Wei Yang

This patch adjust the example code with following two purpose:

* reduce the confusion on not releasing e->lock
* emphasize e is valid and not stale with e->lock held

CC Boqun Feng <[email protected]>
CC Alan Huang <[email protected]>

---
v2:
* add the missing parameter *key
* make function return struct audit_entry
---
Documentation/RCU/listRCU.rst | 10 ++++++----

1 file changed, 6 insertions(+), 4 deletions(-)
Documentation/RCU/listRCU.rst +0 -0
index ed5c9d8c9afe..d8bb98623c12 100644
@@ -334,7 +334,7 @@ If the system-call audit module were to ever need to reject stale data, one way

to accomplish this would be to add a ``deleted`` flag and a ``lock`` spinlock to the ``audit_entry`` structure, and modify audit_filter_task() as follows::

- static enum audit_state audit_filter_task(struct task_struct *tsk)
+ static struct audit_entry *audit_filter_task(struct task_struct *tsk, char **key) {
struct audit_entry *e;
enum audit_state state;

Documentation/RCU/listRCU.rst +5 -3
@@ -346,16 +346,18 @@ to accomplish this would be to add a ``deleted`` flag and a ``lock`` spinlock to
 if (e->deleted) {
 spin_unlock(&e->lock);
 rcu_read_unlock();
- return AUDIT_BUILD_CONTEXT;
+ return NULL;
 }
 rcu_read_unlock();
 if (state == AUDIT_STATE_RECORD)
 *key = kstrdup(e->rule.filterkey, GFP_ATOMIC);
- return state;
+ /* As long as e->lock is held, e is valid and
+ * its value is not stale */
+ return e;
 }
 }
 rcu_read_unlock();
- return AUDIT_BUILD_CONTEXT;
+ return NULL;
 }
 
Quoted 4 lines

The ``audit_del_rule()`` function would need to set the ``deleted`` flag under the

Signature · 1 line
2.34.1

I think it’s good enough to illustrate the intention here:

Boqun, there is an unreviewed doc patch[1] that fixes the section

“Using RCU hlist_nulls to protect list and objects”

[1] https://lore.kernel.org/rcu/[email protected]/

: )

#3 lore raw
Quoted 53 lines · Alan Huang
Quoted 52 lines · Wei Yang

This patch adjust the example code with following two purpose:

* reduce the confusion on not releasing e->lock
* emphasize e is valid and not stale with e->lock held

CC Boqun Feng <[email protected]>
CC Alan Huang <[email protected]>

---
v2:
* add the missing parameter *key
* make function return struct audit_entry
---
Documentation/RCU/listRCU.rst | 10 ++++++----

1 file changed, 6 insertions(+), 4 deletions(-)
Documentation/RCU/listRCU.rst +0 -0
index ed5c9d8c9afe..d8bb98623c12 100644
@@ -334,7 +334,7 @@ If the system-call audit module were to ever need to reject stale data, one way

to accomplish this would be to add a ``deleted`` flag and a ``lock`` spinlock to the ``audit_entry`` structure, and modify audit_filter_task() as follows::

- static enum audit_state audit_filter_task(struct task_struct *tsk)
+ static struct audit_entry *audit_filter_task(struct task_struct *tsk, char **key) {
struct audit_entry *e;
enum audit_state state;

Documentation/RCU/listRCU.rst +5 -3
@@ -346,16 +346,18 @@ to accomplish this would be to add a ``deleted`` flag and a ``lock`` spinlock to
 if (e->deleted) {
 spin_unlock(&e->lock);
 rcu_read_unlock();
- return AUDIT_BUILD_CONTEXT;
+ return NULL;
 }
 rcu_read_unlock();
 if (state == AUDIT_STATE_RECORD)
 *key = kstrdup(e->rule.filterkey, GFP_ATOMIC);
- return state;
+ /* As long as e->lock is held, e is valid and
+ * its value is not stale */
+ return e;
 }
 }
 rcu_read_unlock();
- return AUDIT_BUILD_CONTEXT;
+ return NULL;
 }
 
Quoted 9 lines
Quoted 4 lines

The ``audit_del_rule()`` function would need to set the ``deleted`` flag under the

Signature · 1 line
2.34.1

I think it’s good enough to illustrate the intention here:

Queued for further tests and reviews, thanks you both!

Quoted 8 lines

Boqun, there is an unreviewed doc patch[1] that fixes the section

“Using RCU hlist_nulls to protect list and objects”

[1] https://lore.kernel.org/rcu/[email protected]/

: )

Will take a look later.

Regards,
Boqun

Quoted 3 lines