]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
fs: dodge an atomic in putname if ref == 1
authorMateusz Guzik <mjguzik@gmail.com>
Tue, 11 Mar 2025 18:18:04 +0000 (19:18 +0100)
committerChristian Brauner <brauner@kernel.org>
Wed, 12 Mar 2025 08:35:33 +0000 (09:35 +0100)
While the structure is refcounted, the only consumer incrementing it is
audit and even then the atomic operation is only needed when it
interacts with io_uring.

If putname spots a count of 1, there is no legitimate way for anyone to
bump it.

If audit is disabled, the count is guaranteed to be 1, which
consistently elides the atomic for all path lookups. If audit is
enabled, it still manages to elide the last decrement.

Note the patch does not do anything to prevent audit from suffering
atomics. See [1] and [2] for a different approach.

Benchmarked on Sapphire Rapids issuing access() (ops/s):
before: 5106246
after:  5269678 (+3%)

Link 1: https://lore.kernel.org/linux-fsdevel/20250307161155.760949-1-mjguzik@gmail.com/
Link 2: https://lore.kernel.org/linux-fsdevel/20250307164216.GI2023217@ZenIV/

Signed-off-by: Mateusz Guzik <mjguzik@gmail.com>
Link: https://lore.kernel.org/r/20250311181804.1165758-1-mjguzik@gmail.com
Reviewed-by: Jens Axboe <axboe@kernel.dk>
Reviewed-by: Jan Kara <jack@suse.cz>
Signed-off-by: Christian Brauner <brauner@kernel.org>
fs/namei.c

index 207898ee9fd54c9d14b2c502c61bebbee352fe34..3209d65b5fc0c07d039af21e586d35977f02ed02 100644 (file)
@@ -275,14 +275,19 @@ EXPORT_SYMBOL(getname_kernel);
 
 void putname(struct filename *name)
 {
+       int refcnt;
+
        if (IS_ERR_OR_NULL(name))
                return;
 
-       if (WARN_ON_ONCE(!atomic_read(&name->refcnt)))
-               return;
+       refcnt = atomic_read(&name->refcnt);
+       if (refcnt != 1) {
+               if (WARN_ON_ONCE(!refcnt))
+                       return;
 
-       if (!atomic_dec_and_test(&name->refcnt))
-               return;
+               if (!atomic_dec_and_test(&name->refcnt))
+                       return;
+       }
 
        if (name->name != name->iname) {
                __putname(name->name);