]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
struct filename ->refcnt doesn't need to be atomic
authorAl Viro <viro@zeniv.linux.org.uk>
Sun, 2 Nov 2025 05:01:47 +0000 (01:01 -0400)
committerAl Viro <viro@zeniv.linux.org.uk>
Tue, 13 Jan 2026 20:18:07 +0000 (15:18 -0500)
... or visible outside of audit, really.  Note that references
held in delayed_filename always have refcount 1, and from the
moment of complete_getname() or equivalent point in getname...()
there won't be any references to struct filename instance left
in places visible to other threads.

Acked-by: Paul Moore <paul@paul-moore.com>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
fs/namei.c
include/linux/fs.h
kernel/auditsc.c

index b76cc43fe89ddbfb596f0c22976b3098d985b77e..f4359825ba4832f27a1b86657c46fc9f628d9983 100644 (file)
@@ -150,7 +150,7 @@ static inline void free_filename(struct filename *p)
 static inline void initname(struct filename *name)
 {
        name->aname = NULL;
-       atomic_set(&name->refcnt, 1);
+       name->refcnt = 1;
 }
 
 static int getname_long(struct filename *name, const char __user *filename)
@@ -292,13 +292,13 @@ void putname(struct filename *name)
        if (IS_ERR_OR_NULL(name))
                return;
 
-       refcnt = atomic_read(&name->refcnt);
+       refcnt = name->refcnt;
        if (unlikely(refcnt != 1)) {
                if (WARN_ON_ONCE(!refcnt))
                        return;
 
-               if (!atomic_dec_and_test(&name->refcnt))
-                       return;
+               name->refcnt--;
+               return;
        }
 
        if (unlikely(name->name != name->iname))
@@ -328,12 +328,12 @@ int delayed_getname_uflags(struct delayed_filename *v, const char __user *string
 
 int putname_to_delayed(struct delayed_filename *v, struct filename *name)
 {
-       if (likely(atomic_read(&name->refcnt) == 1)) {
+       if (likely(name->refcnt == 1)) {
                v->__incomplete_filename = name;
                return 0;
        }
+       name->refcnt--;
        v->__incomplete_filename = do_getname_kernel(name->name, true);
-       putname(name);
        return PTR_ERR_OR_ZERO(v->__incomplete_filename);
 }
 
index f1612a7dffd00559f7fa2f4a974df76d2950529d..6a26ee347517dff6ccd6c5665b3b4a81ad198db9 100644 (file)
@@ -2412,7 +2412,7 @@ struct audit_names;
 
 struct __filename_head {
        const char              *name;  /* pointer to actual string */
-       atomic_t                refcnt;
+       int                     refcnt;
        struct audit_names      *aname;
 };
 #define EMBEDDED_NAME_MAX      (192 - sizeof(struct __filename_head))
@@ -2527,12 +2527,6 @@ void dismiss_delayed_filename(struct delayed_filename *);
 int putname_to_delayed(struct delayed_filename *, struct filename *);
 struct filename *complete_getname(struct delayed_filename *);
 
-static inline struct filename *refname(struct filename *name)
-{
-       atomic_inc(&name->refcnt);
-       return name;
-}
-
 DEFINE_CLASS(filename, struct filename *, putname(_T), getname(p), const char __user *p)
 EXTEND_CLASS(filename, _kernel, getname_kernel(p), const char *p)
 EXTEND_CLASS(filename, _flags, getname_flags(p, f), const char __user *p, unsigned int f)
index 67d8da92738100fcc25a87788f9982c72a87c109..86a44b162a8762e4f78b04a55a4a8bb1576cacc4 100644 (file)
@@ -2191,7 +2191,7 @@ void __audit_getname(struct filename *name)
        n->name = name;
        n->name_len = AUDIT_NAME_FULL;
        name->aname = n;
-       refname(name);
+       name->refcnt++;
 }
 
 static inline int audit_copy_fcaps(struct audit_names *name,
@@ -2323,7 +2323,7 @@ out_alloc:
                return;
        if (name) {
                n->name = name;
-               refname(name);
+               name->refcnt++;
        }
 
 out:
@@ -2445,7 +2445,7 @@ void __audit_inode_child(struct inode *parent,
                if (found_parent) {
                        found_child->name = found_parent->name;
                        found_child->name_len = AUDIT_NAME_FULL;
-                       refname(found_child->name);
+                       found_child->name->refcnt++;
                }
        }