Skip to contentSkip to conversation

fs, audit: Avoid excessive dput/dget in audit_context setup and reset paths

PATCH Superseded Waiman Long 4 participants 12 messages Feb 6 – Feb 28

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover fs, audit: Avoid excessive dput/dget in audit_context setup and reset paths

v3:
- Add a new counter in fs_struct to track # of additional references to pwd and add a new get_fs_pwd_pool() and put_fs_pwd_pool helpers to use the new counter.
- Make audit use the new helpers instead of storing pwd reference internally.

v2: https://lore.kernel.org/lkml/[email protected]/

When the audit subsystem is enabled, it can do a lot of get_fs_pwd() calls to get references to fs->pwd and then releasing those references back with path_put() later. That may cause a lot of spinlock contention on a single pwd's dentry lock because of the constant changes to the reference count when there are many processes on the same working directory actively doing open/close system calls. This can cause noticeable performance regresssion when compared with the case where the audit subsystem is turned off especially on systems with a lot of CPUs which is becoming more common these days.

This patch series aim to avoid this type of performance regression caused by audit by adding a new set of fs_struct helpers to reduce unncessary path_get() and path_put() calls and the audit code is modified to use these new helpers.

Waiman Long (2):
fs: Add a pool of extra fs->pwd references to fs_struct
audit: Use the new {get,put}_fs_pwd_pool() APIs to get/put pwd
references

 fs/fs_struct.c            | 26 +++++++++++++++++++++-----
 fs/namespace.c            |  8 ++++++++
 include/linux/fs_struct.h | 30 +++++++++++++++++++++++++++++-
 kernel/auditsc.c          |  7 +++++--
 4 files changed, 63 insertions(+), 8 deletions(-)
Signature · 1 line
2.52.0
#1 lore raw

PATCH v3 1/2 fs: Add a pool of extra fs->pwd references to fs_struct

When the audit subsystem is enabled, it can do a lot of get_fs_pwd() calls to get references to fs->pwd and then releasing those references back with path_put() later. That may cause a lot of spinlock contention on a single pwd's dentry lock because of the constant changes to the reference count when there are many processes on the same working directory actively doing open/close system calls. This can cause noticeable performance regresssion when compared with the case where the audit subsystem is turned off especially on systems with a lot of CPUs which is becoming more common these days.

A simple and elegant solution to avoid this kind of performance regression is to add a common pool of extra fs->pwd references inside the fs_struct. When a caller needs a pwd reference, it can borrow one from pool, if available, to avoid an explicit path_get(). When it is time to release the reference, it can put it back into the common pool if fs->pwd isn't changed before without doing a path_put(). We still need to acquire the fs's spinlock, but fs_struct is more distributed and it is less common to have many tasks sharing a single fs_struct.

A new set of get_fs_pwd_pool/put_fs_pwd_pool() APIs are introduced with this patch to enable other subsystems to acquire and release a pwd reference from the common pool without doing unnecessary path_get/path_put().

Besides fs/fs_struct.c, the copy_mnt_ns() function of fs/namespace.c is also modified to properly handle the extra pwd references, if available.

---
 fs/fs_struct.c            | 26 +++++++++++++++++++++-----
 fs/namespace.c            |  8 ++++++++
 include/linux/fs_struct.h | 30 +++++++++++++++++++++++++++++-
 3 files changed, 58 insertions(+), 6 deletions(-)
fs/fs_struct.c +21 -5
index b8c46c5a38a0..621fe1677913 100644
@@ -32,15 +32,19 @@ void set_fs_root(struct fs_struct *fs, const struct path *path)
 void set_fs_pwd(struct fs_struct *fs, const struct path *path)
 {
 	struct path old_pwd;
+	int count;
 
 	path_get(path);
 	write_seqlock(&fs->seq);
 	old_pwd = fs->pwd;
 	fs->pwd = *path;
+	count = fs->pwd_refs + 1;
+	fs->pwd_refs = 0;
 	write_sequnlock(&fs->seq);
 
 	if (old_pwd.dentry)
-		path_put(&old_pwd);
+		while (count--)
+			path_put(&old_pwd);
 }
 
 static inline int replace_path(struct path *p, const struct path *old, const struct path *new)
@@ -62,10 +66,15 @@ void chroot_fs_refs(const struct path *old_root, const struct path *new_root)
 		task_lock(p);
 		fs = p->fs;
 		if (fs) {
-			int hits = 0;
+			int hits;
+
 			write_seqlock(&fs->seq);
+			hits = replace_path(&fs->pwd, old_root, new_root);
+			if (hits && fs->pwd_refs) {
+				count += fs->pwd_refs;
+				fs->pwd_refs = 0;
+			}
 			hits += replace_path(&fs->root, old_root, new_root);
-			hits += replace_path(&fs->pwd, old_root, new_root);
 			while (hits--) {
 				count++;
 				path_get(new_root);
@@ -81,8 +90,11 @@ void chroot_fs_refs(const struct path *old_root, const struct path *new_root)
 
 void free_fs_struct(struct fs_struct *fs)
 {
+	int count = fs->pwd_refs + 1;
+
 	path_put(&fs->root);
-	path_put(&fs->pwd);
+	while (count--)
+		path_put(&fs->pwd);
 	kmem_cache_free(fs_cachep, fs);
 }
 
@@ -110,6 +122,7 @@ struct fs_struct *copy_fs_struct(struct fs_struct *old)
 	if (fs) {
 		fs->users = 1;
 		fs->in_exec = 0;
+		fs->pwd_refs = 0;
 		seqlock_init(&fs->seq);
 		fs->umask = old->umask;
 
@@ -117,7 +130,10 @@ struct fs_struct *copy_fs_struct(struct fs_struct *old)
 		fs->root = old->root;
 		path_get(&fs->root);
 		fs->pwd = old->pwd;
-		path_get(&fs->pwd);
+		if (old->pwd_refs)
+			old->pwd_refs--;
+		else
+			path_get(&fs->pwd);
 		read_sequnlock_excl(&old->seq);
 	}
 	return fs;
fs/namespace.c +8 -0
index c58674a20cad..a2323ba84d76 100644
@@ -4135,6 +4135,14 @@ struct mnt_namespace *copy_mnt_ns(u64 flags, struct mnt_namespace *ns,
 	 * as belonging to new namespace.  We have already acquired a private
 	 * fs_struct, so tsk->fs->lock is not needed.
 	 */
+	if (new_fs)
+		WARN_ON_ONCE(new_fs->users != 1);
+
+	/* Release the extra pwd references of new_fs, if present. */
+	while (new_fs && new_fs->pwd_refs) {
+		path_put(&new_fs->pwd);
+		new_fs->pwd_refs--;
+	}
 	p = old;
 	q = new;
 	while (p) {
include/linux/fs_struct.h +29 -1
index 0070764b790a..093648e65c20 100644
@@ -8,10 +8,11 @@
 #include <linux/seqlock.h>
 
 struct fs_struct {
-	int users;
 	seqlock_t seq;
+	int users;
 	int umask;
 	int in_exec;
+	int pwd_refs;	/* A pool of extra pwd references */
 	struct path root, pwd;
 } __randomize_layout;
 
@@ -40,6 +41,33 @@ static inline void get_fs_pwd(struct fs_struct *fs, struct path *pwd)
 	read_sequnlock_excl(&fs->seq);
 }
 
+/* Acquire a pwd reference from the pwd_refs pool, if available */
+static inline void get_fs_pwd_pool(struct fs_struct *fs, struct path *pwd)
+{
+	read_seqlock_excl(&fs->seq);
+	*pwd = fs->pwd;
+	if (fs->pwd_refs)
+		fs->pwd_refs--;
+	else
+		path_get(pwd);
+	read_sequnlock_excl(&fs->seq);
+}
+
+/* Release a pwd reference back to the pwd_refs pool, if appropriate */
+static inline void put_fs_pwd_pool(struct fs_struct *fs, struct path *pwd)
+{
+	bool put = false;
+
+	read_seqlock_excl(&fs->seq);
+	if ((fs->pwd.dentry == pwd->dentry) && (fs->pwd.mnt == pwd->mnt))
+		fs->pwd_refs++;
+	else
+		put = true;
+	read_sequnlock_excl(&fs->seq);
+	if (put)
+		path_put(pwd);
+}
+
 extern bool current_chrooted(void);
 
 static inline int current_umask(void)
Signature · 1 line
2.52.0
#8 lore raw

PATCH v3 2/2 audit: Use the new {get,put}_fs_pwd_pool() APIs to get/put pwd references

In reply to Waiman Long

When the audit subsystem is enabled, it can do a lot of get_fs_pwd() calls to get references to fs->pwd and then releasing those references back with path_put() later. That may cause a lot of spinlock contention on a single pwd's dentry lock because of the constant changes to the reference count when there are many processes on the same working directory actively doing open/close system calls. This can cause noticeable performance regresssion when compared with the case where the audit subsystem is turned off especially on systems with a lot of CPUs which is becoming more common these days.

To avoid this kind of performance regression, use the new get_fs_pwd_pool() and put_fs_pwd_pool() APIs to acquire and release a fs->pwd reference. This should greatly reduce the number of path_get() and path_put() calls that are needed.

After installing a test kernel with auditing enabled and counters added to track the get_fs_pwd_pool() and put_fs_pwd_pool() calls on a 2-socket 96-core test system and running a parallel kernel build, the counter values for this particular test run were shown below.

fs_get_path=307,903
fs_get_pool=56,583,192
fs_put_path=6,209
fs_put_pool=56,885,147

Of the about 57M calls to get_fs_pwd_pool() and put_fs_pwd_pool(), the majority of them are just updating the pwd_refs counters. Only less than 1% of those calls require an actual path_get() and path_put() calls. The difference between fs_get_path and fs_put_path represents the extra pwd references that were still stored in various active task->fs's when the counter values were retrieved.

It can be seen that the number of path_get() and path_put() calls are reduced by quite a lot.

---
 kernel/auditsc.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
kernel/auditsc.c +5 -2
index dd0563a8e0be..af22f8be4d70 100644
@@ -931,6 +931,9 @@ static inline void audit_free_names(struct audit_context *context)
 {
 	struct audit_names *n, *next;
 
+	if (!context->name_count)
+		return;	/* audit_alloc_name() has not been called */
+
 	list_for_each_entry_safe(n, next, &context->names_list, list) {
 		list_del(&n->list);
 		if (n->name)
@@ -939,7 +942,7 @@ static inline void audit_free_names(struct audit_context *context)
 			kfree(n);
 	}
 	context->name_count = 0;
-	path_put(&context->pwd);
+	put_fs_pwd_pool(current->fs, &context->pwd);
 	context->pwd.dentry = NULL;
 	context->pwd.mnt = NULL;
 }
@@ -2165,7 +2168,7 @@ static struct audit_names *audit_alloc_name(struct audit_context *context,
 
 	context->name_count++;
 	if (!context->pwd.dentry)
-		get_fs_pwd(current->fs, &context->pwd);
+		get_fs_pwd_pool(current->fs, &context->pwd);
 	return aname;
 }
 
Signature · 1 line
2.52.0
Reviewed-by Tested-by on the v3 series
#6 lore raw
In reply to Waiman Long
Quoted 30 lines · Waiman Long

v3:
- Add a new counter in fs_struct to track # of additional references to pwd and add a new get_fs_pwd_pool() and put_fs_pwd_pool helpers to use the new counter.
- Make audit use the new helpers instead of storing pwd reference internally.

v2: https://lore.kernel.org/lkml/[email protected]/

When the audit subsystem is enabled, it can do a lot of get_fs_pwd() calls to get references to fs->pwd and then releasing those references back with path_put() later. That may cause a lot of spinlock contention on a single pwd's dentry lock because of the constant changes to the reference count when there are many processes on the same working directory actively doing open/close system calls. This can cause noticeable performance regresssion when compared with the case where the audit subsystem is turned off especially on systems with a lot of CPUs which is becoming more common these days.

This patch series aim to avoid this type of performance regression caused by audit by adding a new set of fs_struct helpers to reduce unncessary path_get() and path_put() calls and the audit code is modified to use these new helpers.

Waiman Long (2):
fs: Add a pool of extra fs->pwd references to fs_struct
audit: Use the new {get,put}_fs_pwd_pool() APIs to get/put pwd
references

Quoted 9 lines
 fs/fs_struct.c            | 26 +++++++++++++++++++++-----
 fs/namespace.c            |  8 ++++++++
 include/linux/fs_struct.h | 30 +++++++++++++++++++++++++++++-
 kernel/auditsc.c          |  7 +++++--
 4 files changed, 63 insertions(+), 8 deletions(-)

--
2.52.0

Looks good to me. I ran a local kernel build against the audit-testsuite and verified there are no noticeable audit functional regressions.

# uname -r
6.19.0+
# make test
Running as   user    root
        with context unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
        on   system  Fedora

amcast_joinpart/test ................. ok backlog_wait_time_actual_reset/test .. ok
bpf/test ............................. ok exec_execve/test ..................... ok
exec_name/test ....................... ok
fanotify/test ........................ ok field_compare/test ................... ok file_create/test ..................... ok file_delete/test ..................... ok file_permission/test ................. ok file_rename/test ..................... ok filter_exclude/test .................. ok filter_exit/test ..................... ok filter_saddr_fam/test ................ ok filter_sessionid/test ................ ok
io_uring/test ........................ ok
login_tty/test ....................... ok lost_reset/test ...................... ok netfilter_pkt/test ................... ok
signal/test .......................... ok syscalls_file/test ................... ok syscall_module/test .................. ok syscall_socketcall/test .............. ok time_change/test ..................... ok
user_msg/test ........................ ok
All tests successful.
Files=25, Tests=282, 70 wallclock secs ( 0.09 usr 0.01 sys + 18.61 cusr 1.47 csys = 20.18 CPU)
Result: PASS

#7 lore raw

v3 cover fs, audit: Avoid excessive dput/dget in audit_context setup and reset paths

In reply to Waiman Long

v3:
- Add a new counter in fs_struct to track # of additional references to pwd and add a new get_fs_pwd_pool() and put_fs_pwd_pool helpers to use the new counter.
- Make audit use the new helpers instead of storing pwd reference internally.

v2: https://lore.kernel.org/lkml/[email protected]/

When the audit subsystem is enabled, it can do a lot of get_fs_pwd() calls to get references to fs->pwd and then releasing those references back with path_put() later. That may cause a lot of spinlock contention on a single pwd's dentry lock because of the constant changes to the reference count when there are many processes on the same working directory actively doing open/close system calls. This can cause noticeable performance regresssion when compared with the case where the audit subsystem is turned off especially on systems with a lot of CPUs which is becoming more common these days.

This patch series aim to avoid this type of performance regression caused by audit by adding a new set of fs_struct helpers to reduce unncessary path_get() and path_put() calls and the audit code is modified to use these new helpers.

Waiman Long (2):
fs: Add a pool of extra fs->pwd references to fs_struct
audit: Use the new {get,put}_fs_pwd_pool() APIs to get/put pwd
references

 fs/fs_struct.c            | 26 +++++++++++++++++++++-----
 fs/namespace.c            |  8 ++++++++
 include/linux/fs_struct.h | 30 +++++++++++++++++++++++++++++-
 kernel/auditsc.c          |  7 +++++--
 4 files changed, 63 insertions(+), 8 deletions(-)
Signature · 1 line
2.52.0
#3 lore raw

PATCH RESEND v3 1/2 fs: Add a pool of extra fs->pwd references to fs_struct

In reply to Waiman Long

When the audit subsystem is enabled, it can do a lot of get_fs_pwd() calls to get references to fs->pwd and then releasing those references back with path_put() later. That may cause a lot of spinlock contention on a single pwd's dentry lock because of the constant changes to the reference count when there are many processes on the same working directory actively doing open/close system calls. This can cause noticeable performance regresssion when compared with the case where the audit subsystem is turned off especially on systems with a lot of CPUs which is becoming more common these days.

A simple and elegant solution to avoid this kind of performance regression is to add a common pool of extra fs->pwd references inside the fs_struct. When a caller needs a pwd reference, it can borrow one from pool, if available, to avoid an explicit path_get(). When it is time to release the reference, it can put it back into the common pool if fs->pwd isn't changed before without doing a path_put(). We still need to acquire the fs's spinlock, but fs_struct is more distributed and it is less common to have many tasks sharing a single fs_struct.

A new set of get_fs_pwd_pool/put_fs_pwd_pool() APIs are introduced with this patch to enable other subsystems to acquire and release a pwd reference from the common pool without doing unnecessary path_get/path_put().

Besides fs/fs_struct.c, the copy_mnt_ns() function of fs/namespace.c is also modified to properly handle the extra pwd references, if available.

---
 fs/fs_struct.c            | 26 +++++++++++++++++++++-----
 fs/namespace.c            |  8 ++++++++
 include/linux/fs_struct.h | 30 +++++++++++++++++++++++++++++-
 3 files changed, 58 insertions(+), 6 deletions(-)
fs/fs_struct.c +21 -5
index b8c46c5a38a0..621fe1677913 100644
@@ -32,15 +32,19 @@ void set_fs_root(struct fs_struct *fs, const struct path *path)
 void set_fs_pwd(struct fs_struct *fs, const struct path *path)
 {
 	struct path old_pwd;
+	int count;
 
 	path_get(path);
 	write_seqlock(&fs->seq);
 	old_pwd = fs->pwd;
 	fs->pwd = *path;
+	count = fs->pwd_refs + 1;
+	fs->pwd_refs = 0;
 	write_sequnlock(&fs->seq);
 
 	if (old_pwd.dentry)
-		path_put(&old_pwd);
+		while (count--)
+			path_put(&old_pwd);
 }
 
 static inline int replace_path(struct path *p, const struct path *old, const struct path *new)
@@ -62,10 +66,15 @@ void chroot_fs_refs(const struct path *old_root, const struct path *new_root)
 		task_lock(p);
 		fs = p->fs;
 		if (fs) {
-			int hits = 0;
+			int hits;
+
 			write_seqlock(&fs->seq);
+			hits = replace_path(&fs->pwd, old_root, new_root);
+			if (hits && fs->pwd_refs) {
+				count += fs->pwd_refs;
+				fs->pwd_refs = 0;
+			}
 			hits += replace_path(&fs->root, old_root, new_root);
-			hits += replace_path(&fs->pwd, old_root, new_root);
 			while (hits--) {
 				count++;
 				path_get(new_root);
@@ -81,8 +90,11 @@ void chroot_fs_refs(const struct path *old_root, const struct path *new_root)
 
 void free_fs_struct(struct fs_struct *fs)
 {
+	int count = fs->pwd_refs + 1;
+
 	path_put(&fs->root);
-	path_put(&fs->pwd);
+	while (count--)
+		path_put(&fs->pwd);
 	kmem_cache_free(fs_cachep, fs);
 }
 
@@ -110,6 +122,7 @@ struct fs_struct *copy_fs_struct(struct fs_struct *old)
 	if (fs) {
 		fs->users = 1;
 		fs->in_exec = 0;
+		fs->pwd_refs = 0;
 		seqlock_init(&fs->seq);
 		fs->umask = old->umask;
 
@@ -117,7 +130,10 @@ struct fs_struct *copy_fs_struct(struct fs_struct *old)
 		fs->root = old->root;
 		path_get(&fs->root);
 		fs->pwd = old->pwd;
-		path_get(&fs->pwd);
+		if (old->pwd_refs)
+			old->pwd_refs--;
+		else
+			path_get(&fs->pwd);
 		read_sequnlock_excl(&old->seq);
 	}
 	return fs;
fs/namespace.c +8 -0
index c58674a20cad..a2323ba84d76 100644
@@ -4135,6 +4135,14 @@ struct mnt_namespace *copy_mnt_ns(u64 flags, struct mnt_namespace *ns,
 	 * as belonging to new namespace.  We have already acquired a private
 	 * fs_struct, so tsk->fs->lock is not needed.
 	 */
+	if (new_fs)
+		WARN_ON_ONCE(new_fs->users != 1);
+
+	/* Release the extra pwd references of new_fs, if present. */
+	while (new_fs && new_fs->pwd_refs) {
+		path_put(&new_fs->pwd);
+		new_fs->pwd_refs--;
+	}
 	p = old;
 	q = new;
 	while (p) {
include/linux/fs_struct.h +29 -1
index 0070764b790a..093648e65c20 100644
@@ -8,10 +8,11 @@
 #include <linux/seqlock.h>
 
 struct fs_struct {
-	int users;
 	seqlock_t seq;
+	int users;
 	int umask;
 	int in_exec;
+	int pwd_refs;	/* A pool of extra pwd references */
 	struct path root, pwd;
 } __randomize_layout;
 
@@ -40,6 +41,33 @@ static inline void get_fs_pwd(struct fs_struct *fs, struct path *pwd)
 	read_sequnlock_excl(&fs->seq);
 }
 
+/* Acquire a pwd reference from the pwd_refs pool, if available */
+static inline void get_fs_pwd_pool(struct fs_struct *fs, struct path *pwd)
+{
+	read_seqlock_excl(&fs->seq);
+	*pwd = fs->pwd;
+	if (fs->pwd_refs)
+		fs->pwd_refs--;
+	else
+		path_get(pwd);
+	read_sequnlock_excl(&fs->seq);
+}
+
+/* Release a pwd reference back to the pwd_refs pool, if appropriate */
+static inline void put_fs_pwd_pool(struct fs_struct *fs, struct path *pwd)
+{
+	bool put = false;
+
+	read_seqlock_excl(&fs->seq);
+	if ((fs->pwd.dentry == pwd->dentry) && (fs->pwd.mnt == pwd->mnt))
+		fs->pwd_refs++;
+	else
+		put = true;
+	read_sequnlock_excl(&fs->seq);
+	if (put)
+		path_put(pwd);
+}
+
 extern bool current_chrooted(void);
 
 static inline int current_umask(void)
Signature · 1 line
2.52.0
#10 lore raw

PATCH RESEND v3 2/2 audit: Use the new {get,put}_fs_pwd_pool() APIs to get/put pwd references

In reply to Waiman Long

When the audit subsystem is enabled, it can do a lot of get_fs_pwd() calls to get references to fs->pwd and then releasing those references back with path_put() later. That may cause a lot of spinlock contention on a single pwd's dentry lock because of the constant changes to the reference count when there are many processes on the same working directory actively doing open/close system calls. This can cause noticeable performance regresssion when compared with the case where the audit subsystem is turned off especially on systems with a lot of CPUs which is becoming more common these days.

To avoid this kind of performance regression, use the new get_fs_pwd_pool() and put_fs_pwd_pool() APIs to acquire and release a fs->pwd reference. This should greatly reduce the number of path_get() and path_put() calls that are needed.

After installing a test kernel with auditing enabled and counters added to track the get_fs_pwd_pool() and put_fs_pwd_pool() calls on a 2-socket 96-core test system and running a parallel kernel build, the counter values for this particular test run were shown below.

fs_get_path=307,903
fs_get_pool=56,583,192
fs_put_path=6,209
fs_put_pool=56,885,147

Of the about 57M calls to get_fs_pwd_pool() and put_fs_pwd_pool(), the majority of them are just updating the pwd_refs counters. Only less than 1% of those calls require an actual path_get() and path_put() calls. The difference between fs_get_path and fs_put_path represents the extra pwd references that were still stored in various active task->fs's when the counter values were retrieved.

It can be seen that the number of path_get() and path_put() calls are reduced by quite a lot.

---
 kernel/auditsc.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
kernel/auditsc.c +5 -2
index dd0563a8e0be..af22f8be4d70 100644
@@ -931,6 +931,9 @@ static inline void audit_free_names(struct audit_context *context)
 {
 	struct audit_names *n, *next;
 
+	if (!context->name_count)
+		return;	/* audit_alloc_name() has not been called */
+
 	list_for_each_entry_safe(n, next, &context->names_list, list) {
 		list_del(&n->list);
 		if (n->name)
@@ -939,7 +942,7 @@ static inline void audit_free_names(struct audit_context *context)
 			kfree(n);
 	}
 	context->name_count = 0;
-	path_put(&context->pwd);
+	put_fs_pwd_pool(current->fs, &context->pwd);
 	context->pwd.dentry = NULL;
 	context->pwd.mnt = NULL;
 }
@@ -2165,7 +2168,7 @@ static struct audit_names *audit_alloc_name(struct audit_context *context,
 
 	context->name_count++;
 	if (!context->pwd.dentry)
-		get_fs_pwd(current->fs, &context->pwd);
+		get_fs_pwd_pool(current->fs, &context->pwd);
 	return aname;
 }
 
Signature · 1 line
2.52.0
maintainer Acked-by on v3 2/2
#11 lore raw

Re: [RESEND PATCH v3 2/2] audit: Use the new {get,put}_fs_pwd_pool() APIs to get/put pwd references

Quoted 33 lines · Waiman Long

When the audit subsystem is enabled, it can do a lot of get_fs_pwd() calls to get references to fs->pwd and then releasing those references back with path_put() later. That may cause a lot of spinlock contention on a single pwd's dentry lock because of the constant changes to the reference count when there are many processes on the same working directory actively doing open/close system calls. This can cause noticeable performance regresssion when compared with the case where the audit subsystem is turned off especially on systems with a lot of CPUs which is becoming more common these days.

To avoid this kind of performance regression, use the new get_fs_pwd_pool() and put_fs_pwd_pool() APIs to acquire and release a fs->pwd reference. This should greatly reduce the number of path_get() and path_put() calls that are needed.

After installing a test kernel with auditing enabled and counters added to track the get_fs_pwd_pool() and put_fs_pwd_pool() calls on a 2-socket 96-core test system and running a parallel kernel build, the counter values for this particular test run were shown below.

fs_get_path=307,903
fs_get_pool=56,583,192
fs_put_path=6,209
fs_put_pool=56,885,147

Of the about 57M calls to get_fs_pwd_pool() and put_fs_pwd_pool(), the majority of them are just updating the pwd_refs counters. Only less than 1% of those calls require an actual path_get() and path_put() calls. The difference between fs_get_path and fs_put_path represents the extra pwd references that were still stored in various active task->fs's when the counter values were retrieved.

Quoted 7 lines

It can be seen that the number of path_get() and path_put() calls are reduced by quite a lot.

---
 kernel/auditsc.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

Apologies on the delay in responding, I had limited network access and it looked like you and Al had this under control. Regardless, the audit part of this patchset looks fine to me, my ACK is below if Al wants to take this, otherwise I can merge the patchset via the audit tree if Al ACKs it.

Signature · 1 line
paul-moore.com
maintainer
#4 lore raw

Re: [RESEND PATCH v3 1/2] fs: Add a pool of extra fs->pwd references to fs_struct

In reply to Waiman Long
Quoted 34 lines · Waiman Long

When the audit subsystem is enabled, it can do a lot of get_fs_pwd() calls to get references to fs->pwd and then releasing those references back with path_put() later. That may cause a lot of spinlock contention on a single pwd's dentry lock because of the constant changes to the reference count when there are many processes on the same working directory actively doing open/close system calls. This can cause noticeable performance regresssion when compared with the case where the audit subsystem is turned off especially on systems with a lot of CPUs which is becoming more common these days.

A simple and elegant solution to avoid this kind of performance regression is to add a common pool of extra fs->pwd references inside the fs_struct. When a caller needs a pwd reference, it can borrow one from pool, if available, to avoid an explicit path_get(). When it is time to release the reference, it can put it back into the common pool if fs->pwd isn't changed before without doing a path_put(). We still need to acquire the fs's spinlock, but fs_struct is more distributed and it is less common to have many tasks sharing a single fs_struct.

A new set of get_fs_pwd_pool/put_fs_pwd_pool() APIs are introduced with this patch to enable other subsystems to acquire and release a pwd reference from the common pool without doing unnecessary path_get/path_put().

Besides fs/fs_struct.c, the copy_mnt_ns() function of fs/namespace.c is also modified to properly handle the extra pwd references, if available.

---
 fs/fs_struct.c            | 26 +++++++++++++++++++++-----
 fs/namespace.c            |  8 ++++++++
 include/linux/fs_struct.h | 30 +++++++++++++++++++++++++++++-
 3 files changed, 58 insertions(+), 6 deletions(-)

...

Quoted 24 lines
fs/namespace.c +16 -0
index c58674a20cad..a2323ba84d76 100644
@@ -40,6 +41,33 @@ static inline void get_fs_pwd(struct fs_struct *fs, struct path *pwd)
        read_sequnlock_excl(&fs->seq);
 }
 
+/* Acquire a pwd reference from the pwd_refs pool, if available */
+static inline void get_fs_pwd_pool(struct fs_struct *fs, struct path *pwd)
+{
+       read_seqlock_excl(&fs->seq);
+       *pwd = fs->pwd;
+       if (fs->pwd_refs)
+               fs->pwd_refs--;
+       else
+               path_get(pwd);
+       read_sequnlock_excl(&fs->seq);
+}
+
+/* Release a pwd reference back to the pwd_refs pool, if appropriate */
+static inline void put_fs_pwd_pool(struct fs_struct *fs, struct path *pwd)
+{
+       bool put = false;

fs/namespace.c static inline void get_fs_pwd(struct fs_struct *fs, struct path *pwd) · line 69

Quoted 10 lines
fs/namespace.c +10 -0
+
+       read_seqlock_excl(&fs->seq);
+       if ((fs->pwd.dentry == pwd->dentry) && (fs->pwd.mnt == pwd->mnt))
+               fs->pwd_refs++;
+       else
+               put = true;
+       read_sequnlock_excl(&fs->seq);
+       if (put)
+               path_put(pwd);
+}

This is a nitpick, and perhaps I'm missing something, but I think you could skip the local 'put' boolean by setting 'pwd' to NULL in the pool case, e.g.

static inline void put_fs_pwd_pool(fs, pwd)
{
read_seqlock_excl(&fs)
if (fs == pwd) {
fs->pwd_refs++
pwd = NULL
}
read_sequnlock_excl(&fs)
if (pwd)
path_put(pwd)
}

Signature · 1 line
paul-moore.com
Reviewed-by on v3 1/2
#2 lore raw

Re: [PATCH v3 1/2] fs: Add a pool of extra fs->pwd references to fs_struct

In reply to Waiman Long
Quoted 24 lines · Waiman Long

When the audit subsystem is enabled, it can do a lot of get_fs_pwd() calls to get references to fs->pwd and then releasing those references back with path_put() later. That may cause a lot of spinlock contention on a single pwd's dentry lock because of the constant changes to the reference count when there are many processes on the same working directory actively doing open/close system calls. This can cause noticeable performance regresssion when compared with the case where the audit subsystem is turned off especially on systems with a lot of CPUs which is becoming more common these days.

A simple and elegant solution to avoid this kind of performance regression is to add a common pool of extra fs->pwd references inside the fs_struct. When a caller needs a pwd reference, it can borrow one from pool, if available, to avoid an explicit path_get(). When it is time to release the reference, it can put it back into the common pool if fs->pwd isn't changed before without doing a path_put(). We still need to acquire the fs's spinlock, but fs_struct is more distributed and it is less common to have many tasks sharing a single fs_struct.

A new set of get_fs_pwd_pool/put_fs_pwd_pool() APIs are introduced with this patch to enable other subsystems to acquire and release a pwd reference from the common pool without doing unnecessary path_get/path_put().

Quoted 4 lines

Besides fs/fs_struct.c, the copy_mnt_ns() function of fs/namespace.c is also modified to properly handle the extra pwd references, if available.

Quoted 155 lines
---
 fs/fs_struct.c            | 26 +++++++++++++++++++++-----
 fs/namespace.c            |  8 ++++++++
 include/linux/fs_struct.h | 30 +++++++++++++++++++++++++++++-
 3 files changed, 58 insertions(+), 6 deletions(-)
fs/fs_struct.c +21 -5
index b8c46c5a38a0..621fe1677913 100644
@@ -32,15 +32,19 @@ void set_fs_root(struct fs_struct *fs, const struct path *path)
 void set_fs_pwd(struct fs_struct *fs, const struct path *path)
 {
 	struct path old_pwd;
+	int count;
 
 	path_get(path);
 	write_seqlock(&fs->seq);
 	old_pwd = fs->pwd;
 	fs->pwd = *path;
+	count = fs->pwd_refs + 1;
+	fs->pwd_refs = 0;
 	write_sequnlock(&fs->seq);
 
 	if (old_pwd.dentry)
-		path_put(&old_pwd);
+		while (count--)
+			path_put(&old_pwd);
 }
 
 static inline int replace_path(struct path *p, const struct path *old, const struct path *new)
@@ -62,10 +66,15 @@ void chroot_fs_refs(const struct path *old_root, const struct path *new_root)
 		task_lock(p);
 		fs = p->fs;
 		if (fs) {
-			int hits = 0;
+			int hits;
+
 			write_seqlock(&fs->seq);
+			hits = replace_path(&fs->pwd, old_root, new_root);
+			if (hits && fs->pwd_refs) {
+				count += fs->pwd_refs;
+				fs->pwd_refs = 0;
+			}
 			hits += replace_path(&fs->root, old_root, new_root);
-			hits += replace_path(&fs->pwd, old_root, new_root);
 			while (hits--) {
 				count++;
 				path_get(new_root);
@@ -81,8 +90,11 @@ void chroot_fs_refs(const struct path *old_root, const struct path *new_root)
 
 void free_fs_struct(struct fs_struct *fs)
 {
+	int count = fs->pwd_refs + 1;
+
 	path_put(&fs->root);
-	path_put(&fs->pwd);
+	while (count--)
+		path_put(&fs->pwd);
 	kmem_cache_free(fs_cachep, fs);
 }
 
@@ -110,6 +122,7 @@ struct fs_struct *copy_fs_struct(struct fs_struct *old)
 	if (fs) {
 		fs->users = 1;
 		fs->in_exec = 0;
+		fs->pwd_refs = 0;
 		seqlock_init(&fs->seq);
 		fs->umask = old->umask;
 
@@ -117,7 +130,10 @@ struct fs_struct *copy_fs_struct(struct fs_struct *old)
 		fs->root = old->root;
 		path_get(&fs->root);
 		fs->pwd = old->pwd;
-		path_get(&fs->pwd);
+		if (old->pwd_refs)
+			old->pwd_refs--;
+		else
+			path_get(&fs->pwd);
 		read_sequnlock_excl(&old->seq);
 	}
 	return fs;
fs/namespace.c +8 -0
index c58674a20cad..a2323ba84d76 100644
@@ -4135,6 +4135,14 @@ struct mnt_namespace *copy_mnt_ns(u64 flags, struct mnt_namespace *ns,
 	 * as belonging to new namespace.  We have already acquired a private
 	 * fs_struct, so tsk->fs->lock is not needed.
 	 */
+	if (new_fs)
+		WARN_ON_ONCE(new_fs->users != 1);
+
+	/* Release the extra pwd references of new_fs, if present. */
+	while (new_fs && new_fs->pwd_refs) {
+		path_put(&new_fs->pwd);
+		new_fs->pwd_refs--;
+	}
 	p = old;
 	q = new;
 	while (p) {
include/linux/fs_struct.h +29 -1
index 0070764b790a..093648e65c20 100644
@@ -8,10 +8,11 @@
 #include <linux/seqlock.h>
 
 struct fs_struct {
-	int users;
 	seqlock_t seq;
+	int users;
 	int umask;
 	int in_exec;
+	int pwd_refs;	/* A pool of extra pwd references */
 	struct path root, pwd;
 } __randomize_layout;
 
@@ -40,6 +41,33 @@ static inline void get_fs_pwd(struct fs_struct *fs, struct path *pwd)
 	read_sequnlock_excl(&fs->seq);
 }
 
+/* Acquire a pwd reference from the pwd_refs pool, if available */
+static inline void get_fs_pwd_pool(struct fs_struct *fs, struct path *pwd)
+{
+	read_seqlock_excl(&fs->seq);
+	*pwd = fs->pwd;
+	if (fs->pwd_refs)
+		fs->pwd_refs--;
+	else
+		path_get(pwd);
+	read_sequnlock_excl(&fs->seq);
+}
+
+/* Release a pwd reference back to the pwd_refs pool, if appropriate */
+static inline void put_fs_pwd_pool(struct fs_struct *fs, struct path *pwd)
+{
+	bool put = false;
+
+	read_seqlock_excl(&fs->seq);
+	if ((fs->pwd.dentry == pwd->dentry) && (fs->pwd.mnt == pwd->mnt))
+		fs->pwd_refs++;
+	else
+		put = true;
+	read_sequnlock_excl(&fs->seq);
+	if (put)
+		path_put(pwd);
+}
+
 extern bool current_chrooted(void);
 
 static inline int current_umask(void)
Signature · 1 line
2.52.0

- RGB

--
Richard Guy Briggs <[email protected]>
Sr. S/W Engineer, Kernel Security, Base Operating Systems Remote, Ottawa, Red Hat Canada
Upstream IRC: SunRaycer
Voice: +1.613.860 2354 SMS: +1.613.518.6570

Reviewed-by on v3 2/2
#9 lore raw

Re: [PATCH v3 2/2] audit: Use the new {get,put}_fs_pwd_pool() APIs to get/put pwd references

In reply to Waiman Long
Quoted 32 lines · Waiman Long

When the audit subsystem is enabled, it can do a lot of get_fs_pwd() calls to get references to fs->pwd and then releasing those references back with path_put() later. That may cause a lot of spinlock contention on a single pwd's dentry lock because of the constant changes to the reference count when there are many processes on the same working directory actively doing open/close system calls. This can cause noticeable performance regresssion when compared with the case where the audit subsystem is turned off especially on systems with a lot of CPUs which is becoming more common these days.

To avoid this kind of performance regression, use the new get_fs_pwd_pool() and put_fs_pwd_pool() APIs to acquire and release a fs->pwd reference. This should greatly reduce the number of path_get() and path_put() calls that are needed.

After installing a test kernel with auditing enabled and counters added to track the get_fs_pwd_pool() and put_fs_pwd_pool() calls on a 2-socket 96-core test system and running a parallel kernel build, the counter values for this particular test run were shown below.

fs_get_path=307,903
fs_get_pool=56,583,192
fs_put_path=6,209
fs_put_pool=56,885,147

Of the about 57M calls to get_fs_pwd_pool() and put_fs_pwd_pool(), the majority of them are just updating the pwd_refs counters. Only less than 1% of those calls require an actual path_get() and path_put() calls. The difference between fs_get_path and fs_put_path represents the extra pwd references that were still stored in various active task->fs's when the counter values were retrieved.

Quoted 4 lines

It can be seen that the number of path_get() and path_put() calls are reduced by quite a lot.

Quoted 39 lines
---
 kernel/auditsc.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
kernel/auditsc.c +5 -2
index dd0563a8e0be..af22f8be4d70 100644
@@ -931,6 +931,9 @@ static inline void audit_free_names(struct audit_context *context)
 {
 	struct audit_names *n, *next;
 
+	if (!context->name_count)
+		return;	/* audit_alloc_name() has not been called */
+
 	list_for_each_entry_safe(n, next, &context->names_list, list) {
 		list_del(&n->list);
 		if (n->name)
@@ -939,7 +942,7 @@ static inline void audit_free_names(struct audit_context *context)
 			kfree(n);
 	}
 	context->name_count = 0;
-	path_put(&context->pwd);
+	put_fs_pwd_pool(current->fs, &context->pwd);
 	context->pwd.dentry = NULL;
 	context->pwd.mnt = NULL;
 }
@@ -2165,7 +2168,7 @@ static struct audit_names *audit_alloc_name(struct audit_context *context,
 
 	context->name_count++;
 	if (!context->pwd.dentry)
-		get_fs_pwd(current->fs, &context->pwd);
+		get_fs_pwd_pool(current->fs, &context->pwd);
 	return aname;
 }
 
Signature · 1 line
2.52.0

- RGB

--
Richard Guy Briggs <[email protected]>
Sr. S/W Engineer, Kernel Security, Base Operating Systems Remote, Ottawa, Red Hat Canada
Upstream IRC: SunRaycer
Voice: +1.613.860 2354 SMS: +1.613.518.6570

#5 lore raw

Re: [RESEND PATCH v3 1/2] fs: Add a pool of extra fs->pwd references to fs_struct

In reply to Paul Moore
Quoted 75 lines · Paul Moore
Quoted 33 lines · Waiman Long

When the audit subsystem is enabled, it can do a lot of get_fs_pwd() calls to get references to fs->pwd and then releasing those references back with path_put() later. That may cause a lot of spinlock contention on a single pwd's dentry lock because of the constant changes to the reference count when there are many processes on the same working directory actively doing open/close system calls. This can cause noticeable performance regresssion when compared with the case where the audit subsystem is turned off especially on systems with a lot of CPUs which is becoming more common these days.

A simple and elegant solution to avoid this kind of performance regression is to add a common pool of extra fs->pwd references inside the fs_struct. When a caller needs a pwd reference, it can borrow one from pool, if available, to avoid an explicit path_get(). When it is time to release the reference, it can put it back into the common pool if fs->pwd isn't changed before without doing a path_put(). We still need to acquire the fs's spinlock, but fs_struct is more distributed and it is less common to have many tasks sharing a single fs_struct.

A new set of get_fs_pwd_pool/put_fs_pwd_pool() APIs are introduced with this patch to enable other subsystems to acquire and release a pwd reference from the common pool without doing unnecessary path_get/path_put().

Besides fs/fs_struct.c, the copy_mnt_ns() function of fs/namespace.c is also modified to properly handle the extra pwd references, if available.

---
  fs/fs_struct.c            | 26 +++++++++++++++++++++-----
  fs/namespace.c            |  8 ++++++++
  include/linux/fs_struct.h | 30 +++++++++++++++++++++++++++++-
  3 files changed, 58 insertions(+), 6 deletions(-)

...

Quoted 24 lines
fs/namespace.c +16 -0
index c58674a20cad..a2323ba84d76 100644
@@ -40,6 +41,33 @@ static inline void get_fs_pwd(struct fs_struct *fs, struct path *pwd)
         read_sequnlock_excl(&fs->seq);
  }
 
+/* Acquire a pwd reference from the pwd_refs pool, if available */
+static inline void get_fs_pwd_pool(struct fs_struct *fs, struct path *pwd)
+{
+       read_seqlock_excl(&fs->seq);
+       *pwd = fs->pwd;
+       if (fs->pwd_refs)
+               fs->pwd_refs--;
+       else
+               path_get(pwd);
+       read_sequnlock_excl(&fs->seq);
+}
+
+/* Release a pwd reference back to the pwd_refs pool, if appropriate */
+static inline void put_fs_pwd_pool(struct fs_struct *fs, struct path *pwd)
+{
+       bool put = false;

fs/namespace.c static inline void get_fs_pwd(struct fs_struct *fs, struct path *pwd) · line 69

Quoted 10 lines
fs/namespace.c +10 -0
+
+       read_seqlock_excl(&fs->seq);
+       if ((fs->pwd.dentry == pwd->dentry) && (fs->pwd.mnt == pwd->mnt))
+               fs->pwd_refs++;
+       else
+               put = true;
+       read_sequnlock_excl(&fs->seq);
+       if (put)
+               path_put(pwd);
+}

This is a nitpick, and perhaps I'm missing something, but I think you could skip the local 'put' boolean by setting 'pwd' to NULL in the pool case, e.g.

static inline void put_fs_pwd_pool(fs, pwd)

Quoted 10 lines
{
   read_seqlock_excl(&fs)
   if (fs == pwd) {
     fs->pwd_refs++
     pwd = NULL
   }
   read_sequnlock_excl(&fs)
   if (pwd)
     path_put(pwd)
}

Thanks for the suggestion. It does make the function simpler. I have included that change in my v4 patch.

Al & Christian, are you OK with taking these patches for the next v7.1 release or do you have other suggested changes you would like to see?

Thanks,
Longman