Skip to contentSkip to conversation

btrfs: Use exclusive lock for file_remove_privs

PATCH In review Bernd Schubert 4 participants 5 messages Sep 6, 2023 – Sep 8, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v3 cover btrfs: Use exclusive lock for file_remove_privs

While adding shared direct IO write locks to fuse Miklos noticed that file_remove_privs() needs an exclusive lock. I then noticed that btrfs actually has the same issue as I had in my patch, it was calling into that function with a shared lock. This series adds a new exported function file_needs_remove_privs(), which used by the follow up btrfs patch and will be used by the DIO code path in fuse as well. If that function returns any mask the shared lock needs to be dropped and replaced by the exclusive variant.

Note: Compilation tested only.

v3: Removed file_needs_remove_privs, btrfs can check for S_NOSEC. Christoph had suggested to benchmark if using file_remove_privs has any performance improvement before using it, but I'm not sure what exactly to run and actually I think IS_NOSEC should be fine for local block device file systems. The actual patch got also easier to read with that.

v2:
Already check for IS_NOSEC in btrfs_direct_write before the first lock is taken.
Slight modification to make the code easier to read (boolean pointer is passed to btrfs_write_check, instead of flags).

Cc Christoph Hellwig <[email protected]>
Cc Miklos Szeredi <[email protected]>
Cc Dharmendra Singh <[email protected]>
Cc David Sterba <[email protected]>
Cc Christian Brauner <[email protected]>

Bernd Schubert (1):
btrfs: file_remove_privs needs an exclusive lock

 fs/btrfs/file.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
Signature · 1 line
2.39.2
#1 lore raw

PATCH v3 1/1 btrfs: file_remove_privs needs an exclusive lock

file_remove_privs might call into notify_change(), which requires to hold an exclusive lock.

Fixes e9adabb9712e ("btrfs: use shared lock for direct writes within EOF")
Cc Christoph Hellwig <[email protected]>
Cc Goldwyn Rodrigues <[email protected]>
Cc Miklos Szeredi <[email protected]>
Cc Dharmendra Singh <[email protected]>
Cc David Sterba <[email protected]>
---
 fs/btrfs/file.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
fs/btrfs/file.c +12 -2
index fd03e689a6be..c4b304a2948e 100644
@@ -1466,8 +1466,12 @@ static ssize_t btrfs_direct_write(struct kiocb *iocb, struct iov_iter *from)
 	if (iocb->ki_flags & IOCB_NOWAIT)
 		ilock_flags |= BTRFS_ILOCK_TRY;
 
-	/* If the write DIO is within EOF, use a shared lock */
-	if (iocb->ki_pos + iov_iter_count(from) <= i_size_read(inode))
+	/* If the write DIO is within EOF, use a shared lock and also only
+	 * if security bits will likely not be dropped. Either will need
+	 * to be rechecked after the lock was acquired.
+	 */
+	if (iocb->ki_pos + iov_iter_count(from) <= i_size_read(inode) &&
+	    IS_NOSEC(inode))
 		ilock_flags |= BTRFS_ILOCK_SHARED;
 
 relock:
@@ -1475,6 +1479,12 @@ static ssize_t btrfs_direct_write(struct kiocb *iocb, struct iov_iter *from)
 	if (err < 0)
 		return err;
 
+	if (ilock_flags & BTRFS_ILOCK_SHARED && !IS_NOSEC(inode)) {
+		btrfs_inode_unlock(BTRFS_I(inode), ilock_flags);
+		ilock_flags &= ~BTRFS_ILOCK_SHARED;
+		goto relock;
+	}
+
 	err = generic_write_checks(iocb, from);
 	if (err <= 0) {
 		btrfs_inode_unlock(BTRFS_I(inode), ilock_flags);
Signature · 1 line
2.39.2
Reviewed-by on v3 1/1
#2 lore raw

Re: [PATCH 1/1] btrfs: file_remove_privs needs an exclusive lock

Quoted 2 lines · Bernd Schubert

file_remove_privs might call into notify_change(), which requires to hold an exclusive lock.

Looks good:

FYI, I'd be really curious about benchmarking this against you version that checks xattrs for shared locked writes on files that have xattrs but not security ones or setuid bits. On the one hand being able to do the shared lock sounds nice, on the other hand even just looking up the xattrs will probably make it slower at least for smaller I/O.

#3 lore raw

Re: [PATCH 1/1] btrfs: file_remove_privs needs an exclusive lock

Quoted 6 lines · Christoph Hellwig
Quoted 2 lines · Bernd Schubert

file_remove_privs might call into notify_change(), which requires to hold an exclusive lock.

Looks good:

Quoted 7 lines

FYI, I'd be really curious about benchmarking this against you version that checks xattrs for shared locked writes on files that have xattrs but not security ones or setuid bits. On the one hand being able to do the shared lock sounds nice, on the other hand even just looking up the xattrs will probably make it slower at least for smaller I/O.

I had checked the history of S_NOSEC and I guess that already tells that the xattr lookup is too slow (commit 69b4573296469fd3f70cf7044693074980517067) I don't promise that I benchmark it today, but I can
try to find some time in the next week or the week after. Although I guess there won't be any difference with my initial patch, as dentry_needs_remove_privs() also checks for IS_NOSEC(inode) - overhead was just the additional non inlined function call to file_needs_remove_privs(). And if the flag was not set, overhead was looking up xattr two times.

Bernd

#4 lore raw

Re: [PATCH 1/1] btrfs: file_remove_privs needs an exclusive lock

In reply to Bernd Schubert
Quoted 13 lines · Bernd Schubert

file_remove_privs might call into notify_change(), which requires to hold an exclusive lock.

Fixes e9adabb9712e ("btrfs: use shared lock for direct writes within EOF")
Cc Christoph Hellwig <[email protected]>
Cc Goldwyn Rodrigues <[email protected]>
Cc Miklos Szeredi <[email protected]>
Cc Dharmendra Singh <[email protected]>
Cc David Sterba <[email protected]>

Added to btrfs tree, with slightly reformatted comments, some minor coding style changes and updated changelog. Thanks.