Skip to contentSkip to conversation

don't bother with path_get()/path_put() in unix_open_file()

RFC PATCH Applied Al Viro 3 participants 6 messages Jul 12, 2025 – Jul 15, 2025

Subsystems (4)
lore
Share & tools
lore

Al Viro

author maintainer
#0 lore raw

RFC PATCH 1/1 don't bother with path_get()/path_put() in unix_open_file()

Once unix_sock ->path is set, we are guaranteed that its ->path will remain unchanged (and pinned) until the socket is closed. OTOH, dentry_open() does not modify the path passed to it.

IOW, there's no need to copy unix_sk(sk)->path in unix_open_file() - we can just pass it to dentry_open() and be done with that.

net/unix/af_unix.c +4 -12
index 52b155123985..019ba2609b66 100644
@@ -3191,7 +3191,6 @@ EXPORT_SYMBOL_GPL(unix_outq_len);
 
 static int unix_open_file(struct sock *sk)
 {
-	struct path path;
 	struct file *f;
 	int fd;
 
@@ -3201,27 +3200,20 @@ static int unix_open_file(struct sock *sk)
 	if (!smp_load_acquire(&unix_sk(sk)->addr))
 		return -ENOENT;
 
-	path = unix_sk(sk)->path;
-	if (!path.dentry)
+	if (!unix_sk(sk)->path.dentry)
 		return -ENOENT;
 
-	path_get(&path);
-
 	fd = get_unused_fd_flags(O_CLOEXEC);
 	if (fd < 0)
-		goto out;
+		return fd;
 
-	f = dentry_open(&path, O_PATH, current_cred());
+	f = dentry_open(&unix_sk(sk)->path, O_PATH, current_cred());
 	if (IS_ERR(f)) {
 		put_unused_fd(fd);
-		fd = PTR_ERR(f);
-		goto out;
+		return PTR_ERR(f);
 	}
 
 	fd_install(fd, f);
-out:
-	path_put(&path);
-
 	return fd;
 }
 
maintainer Reviewed-by on v1 1/1
#1 lore raw

From: Al Viro <[email protected]>
Date: Sat, 12 Jul 2025 06:41:57 +0100

Quoted 8 lines

Once unix_sock ->path is set, we are guaranteed that its ->path will remain unchanged (and pinned) until the socket is closed. OTOH, dentry_open() does not modify the path passed to it.

IOW, there's no need to copy unix_sk(sk)->path in unix_open_file() - we can just pass it to dentry_open() and be done with that.

Sounds good. I confirmed vfs_open() copies the passed const path ptr.

Quoted 43 lines
net/unix/af_unix.c +4 -12
index 52b155123985..019ba2609b66 100644
@@ -3191,7 +3191,6 @@ EXPORT_SYMBOL_GPL(unix_outq_len);
 
 static int unix_open_file(struct sock *sk)
 {
-	struct path path;
 	struct file *f;
 	int fd;
 
@@ -3201,27 +3200,20 @@ static int unix_open_file(struct sock *sk)
 	if (!smp_load_acquire(&unix_sk(sk)->addr))
 		return -ENOENT;
 
-	path = unix_sk(sk)->path;
-	if (!path.dentry)
+	if (!unix_sk(sk)->path.dentry)
 		return -ENOENT;
 
-	path_get(&path);
-
 	fd = get_unused_fd_flags(O_CLOEXEC);
 	if (fd < 0)
-		goto out;
+		return fd;
 
-	f = dentry_open(&path, O_PATH, current_cred());
+	f = dentry_open(&unix_sk(sk)->path, O_PATH, current_cred());
 	if (IS_ERR(f)) {
 		put_unused_fd(fd);
-		fd = PTR_ERR(f);
-		goto out;
+		return PTR_ERR(f);
 	}
 
 	fd_install(fd, f);
-out:
-	path_put(&path);
-
 	return fd;
 }
#2 lore raw
Quoted 6 lines · Kuniyuki Iwashima

From: Al Viro <[email protected]>
Date: Sat, 12 Jul 2025 06:41:57 +0100

Quoted 4 lines

Once unix_sock ->path is set, we are guaranteed that its ->path will remain unchanged (and pinned) until the socket is closed. OTOH, dentry_open() does not modify the path passed to it.

Quoted 8 lines
Quoted 4 lines

IOW, there's no need to copy unix_sk(sk)->path in unix_open_file() - we can just pass it to dentry_open() and be done with that.

Sounds good. I confirmed vfs_open() copies the passed const path ptr.

I can just throw that into the SCM_PIDFD branch?

author maintainer
#3 lore raw
Quoted 10 lines · Christian Brauner
Quoted 9 lines · Kuniyuki Iwashima

From: Al Viro <[email protected]>
Date: Sat, 12 Jul 2025 06:41:57 +0100

Quoted 7 lines

Once unix_sock ->path is set, we are guaranteed that its ->path will remain unchanged (and pinned) until the socket is closed. OTOH, dentry_open() does not modify the path passed to it.

IOW, there's no need to copy unix_sk(sk)->path in unix_open_file() - we can just pass it to dentry_open() and be done with that.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Sounds good. I confirmed vfs_open() copies the passed const path ptr.

I can just throw that into the SCM_PIDFD branch?

Fine by me; the thing is, I don't have anything else in the area at the moment (and won't until -rc1 - CLASS(get_unused_fd) series will stray there, but it's not settled enough yet, so it's definitely the next cycle fodder).

So if you (or netdev folks) already have anything going on in the af_unix.c, I've no problem with that thing going there.

#4 lore raw
Quoted 18 lines · Al Viro
Quoted 16 lines · Christian Brauner
Quoted 14 lines · Kuniyuki Iwashima

From: Al Viro <[email protected]>
Date: Sat, 12 Jul 2025 06:41:57 +0100

Quoted 8 lines

Once unix_sock ->path is set, we are guaranteed that its ->path will remain unchanged (and pinned) until the socket is closed. OTOH, dentry_open() does not modify the path passed to it.

IOW, there's no need to copy unix_sk(sk)->path in unix_open_file() - we can just pass it to dentry_open() and be done with that.

Sounds good. I confirmed vfs_open() copies the passed const path ptr.

Quoted 8 lines
Quoted 1 line

I can just throw that into the SCM_PIDFD branch?

Fine by me; the thing is, I don't have anything else in the area at the moment (and won't until -rc1 - CLASS(get_unused_fd) series will stray there, but it's not settled enough yet, so it's definitely the next cycle fodder).

So if you (or netdev folks) already have anything going on in the af_unix.c, I've no problem with that thing going there.

AFAIK, there's no conflicting changes around unix_open_file() in net-next, and this is more of vfs stuff, so whichever is fine to me.

maintainer Applied
#5 lore raw
In reply to Al Viro
Quoted 8 lines · Al Viro

Once unix_sock ->path is set, we are guaranteed that its ->path will remain unchanged (and pinned) until the socket is closed. OTOH, dentry_open() does not modify the path passed to it.

IOW, there's no need to copy unix_sk(sk)->path in unix_open_file() - we can just pass it to dentry_open() and be done with that.

[...]

Applied to the vfs-6.17.pidfs branch of the vfs/vfs.git tree. Patches in the vfs-6.17.pidfs branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs-6.17.pidfs

[1/1] don't bother with path_get()/path_put() in unix_open_file()

      https://git.kernel.org/vfs/vfs/c/1f531e35c146