* [PATCH v3] audit: merge loops in __audit_inode_child()
@ 2025-10-31 12:33 Ricardo Robaina
2025-11-07 21:50 ` Paul Moore
0 siblings, 1 reply; 3+ messages in thread
From: Ricardo Robaina @ 2025-10-31 12:33 UTC (permalink / raw)
To: audit, linux-kernel; +Cc: paul, eparis, Ricardo Robaina
Whenever there's audit context, __audit_inode_child() gets called
numerous times, which can lead to high latency in scenarios that
create too many sysfs/debugfs entries at once, for instance, upon
device_add_disk() invocation.
# uname -r
6.18.0-rc2+
# auditctl -a always,exit -F path=/tmp -k foo
# time insmod loop max_loop=1000
real 0m46.676s
user 0m0.000s
sys 0m46.405s
# perf record -a insmod loop max_loop=1000
# perf report --stdio |grep __audit_inode_child
32.73% insmod [kernel.kallsyms] [k] __audit_inode_child
__audit_inode_child() searches for both the parent and the child
in two different loops that iterate over the same list. This
process can be optimized by merging these into a single loop,
without changing the function behavior or affecting the code's
readability.
This patch merges the two loops that walk through the list
context->names_list into a single loop. This optimization resulted
in around 51% performance enhancement for the benchmark.
# uname -r
6.18.0-rc2-enhancedv3+
# auditctl -a always,exit -F path=/tmp -k foo
# time insmod loop max_loop=1000
real 0m22.899s
user 0m0.001s
sys 0m22.652s
Signed-off-by: Ricardo Robaina <rrobaina@redhat.com>
---
kernel/auditsc.c | 43 +++++++++++++++++++------------------------
1 file changed, 19 insertions(+), 24 deletions(-)
diff --git a/kernel/auditsc.c b/kernel/auditsc.c
index d1966144bdfe..dd0563a8e0be 100644
--- a/kernel/auditsc.c
+++ b/kernel/auditsc.c
@@ -2416,41 +2416,36 @@ void __audit_inode_child(struct inode *parent,
if (inode)
handle_one(inode);
- /* look for a parent entry first */
list_for_each_entry(n, &context->names_list, list) {
- if (!n->name ||
- (n->type != AUDIT_TYPE_PARENT &&
- n->type != AUDIT_TYPE_UNKNOWN))
+ /* can only match entries that have a name */
+ if (!n->name)
continue;
- if (n->ino == parent->i_ino && n->dev == parent->i_sb->s_dev &&
- !audit_compare_dname_path(dname,
- n->name->name, n->name_len)) {
- if (n->type == AUDIT_TYPE_UNKNOWN)
- n->type = AUDIT_TYPE_PARENT;
+ /* look for a parent entry first */
+ if (!found_parent &&
+ (n->type == AUDIT_TYPE_PARENT || n->type == AUDIT_TYPE_UNKNOWN) &&
+ (n->ino == parent->i_ino && n->dev == parent->i_sb->s_dev &&
+ !audit_compare_dname_path(dname, n->name->name, n->name_len))) {
+ n->type = AUDIT_TYPE_PARENT;
found_parent = n;
- break;
- }
- }
-
- cond_resched();
-
- /* is there a matching child entry? */
- list_for_each_entry(n, &context->names_list, list) {
- /* can only match entries that have a name */
- if (!n->name ||
- (n->type != type && n->type != AUDIT_TYPE_UNKNOWN))
+ if (found_child)
+ break;
continue;
+ }
- if (!strcmp(dname->name, n->name->name) ||
- !audit_compare_dname_path(dname, n->name->name,
+ /* is there a matching child entry? */
+ if (!found_child &&
+ (n->type == type || n->type == AUDIT_TYPE_UNKNOWN) &&
+ (!strcmp(dname->name, n->name->name) ||
+ !audit_compare_dname_path(dname, n->name->name,
found_parent ?
found_parent->name_len :
- AUDIT_NAME_FULL)) {
+ AUDIT_NAME_FULL))) {
if (n->type == AUDIT_TYPE_UNKNOWN)
n->type = type;
found_child = n;
- break;
+ if (found_parent)
+ break;
}
}
--
2.51.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH v3] audit: merge loops in __audit_inode_child()
2025-10-31 12:33 [PATCH v3] audit: merge loops in __audit_inode_child() Ricardo Robaina
@ 2025-11-07 21:50 ` Paul Moore
2025-11-10 12:11 ` Ricardo Robaina
0 siblings, 1 reply; 3+ messages in thread
From: Paul Moore @ 2025-11-07 21:50 UTC (permalink / raw)
To: Ricardo Robaina, audit, linux-kernel; +Cc: eparis, Ricardo Robaina
On Oct 31, 2025 Ricardo Robaina <rrobaina@redhat.com> wrote:
>
> Whenever there's audit context, __audit_inode_child() gets called
> numerous times, which can lead to high latency in scenarios that
> create too many sysfs/debugfs entries at once, for instance, upon
> device_add_disk() invocation.
>
> # uname -r
> 6.18.0-rc2+
>
> # auditctl -a always,exit -F path=/tmp -k foo
> # time insmod loop max_loop=1000
> real 0m46.676s
> user 0m0.000s
> sys 0m46.405s
>
> # perf record -a insmod loop max_loop=1000
> # perf report --stdio |grep __audit_inode_child
> 32.73% insmod [kernel.kallsyms] [k] __audit_inode_child
>
> __audit_inode_child() searches for both the parent and the child
> in two different loops that iterate over the same list. This
> process can be optimized by merging these into a single loop,
> without changing the function behavior or affecting the code's
> readability.
>
> This patch merges the two loops that walk through the list
> context->names_list into a single loop. This optimization resulted
> in around 51% performance enhancement for the benchmark.
>
> # uname -r
> 6.18.0-rc2-enhancedv3+
>
> # auditctl -a always,exit -F path=/tmp -k foo
> # time insmod loop max_loop=1000
> real 0m22.899s
> user 0m0.001s
> sys 0m22.652s
>
> Signed-off-by: Ricardo Robaina <rrobaina@redhat.com>
> ---
> kernel/auditsc.c | 43 +++++++++++++++++++------------------------
> 1 file changed, 19 insertions(+), 24 deletions(-)
Looks good to me, merged into audit/dev, thanks!
--
paul-moore.com
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v3] audit: merge loops in __audit_inode_child()
2025-11-07 21:50 ` Paul Moore
@ 2025-11-10 12:11 ` Ricardo Robaina
0 siblings, 0 replies; 3+ messages in thread
From: Ricardo Robaina @ 2025-11-10 12:11 UTC (permalink / raw)
To: Paul Moore; +Cc: audit, linux-kernel, eparis
Thanks, Paul!
On Fri, Nov 7, 2025 at 6:50 PM Paul Moore <paul@paul-moore.com> wrote:
>
> On Oct 31, 2025 Ricardo Robaina <rrobaina@redhat.com> wrote:
> >
> > Whenever there's audit context, __audit_inode_child() gets called
> > numerous times, which can lead to high latency in scenarios that
> > create too many sysfs/debugfs entries at once, for instance, upon
> > device_add_disk() invocation.
> >
> > # uname -r
> > 6.18.0-rc2+
> >
> > # auditctl -a always,exit -F path=/tmp -k foo
> > # time insmod loop max_loop=1000
> > real 0m46.676s
> > user 0m0.000s
> > sys 0m46.405s
> >
> > # perf record -a insmod loop max_loop=1000
> > # perf report --stdio |grep __audit_inode_child
> > 32.73% insmod [kernel.kallsyms] [k] __audit_inode_child
> >
> > __audit_inode_child() searches for both the parent and the child
> > in two different loops that iterate over the same list. This
> > process can be optimized by merging these into a single loop,
> > without changing the function behavior or affecting the code's
> > readability.
> >
> > This patch merges the two loops that walk through the list
> > context->names_list into a single loop. This optimization resulted
> > in around 51% performance enhancement for the benchmark.
> >
> > # uname -r
> > 6.18.0-rc2-enhancedv3+
> >
> > # auditctl -a always,exit -F path=/tmp -k foo
> > # time insmod loop max_loop=1000
> > real 0m22.899s
> > user 0m0.001s
> > sys 0m22.652s
> >
> > Signed-off-by: Ricardo Robaina <rrobaina@redhat.com>
> > ---
> > kernel/auditsc.c | 43 +++++++++++++++++++------------------------
> > 1 file changed, 19 insertions(+), 24 deletions(-)
>
> Looks good to me, merged into audit/dev, thanks!
>
> --
> paul-moore.com
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-11-10 12:11 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-31 12:33 [PATCH v3] audit: merge loops in __audit_inode_child() Ricardo Robaina
2025-11-07 21:50 ` Paul Moore
2025-11-10 12:11 ` Ricardo Robaina
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox