Skip to contentSkip to conversation

per-inode locks in FUSE (kernel vs userspace)

DISCUSSION Eric Wong 3 participants 5 messages Dec 3, 2021 – Dec 7, 2021 VFS

lore
Share & tools
lore

Eric Wong

author
#0 lore raw

Hi all, I'm working on a new multi-threaded FS using the libfuse3 fuse_lowlevel.h API. It looks to me like the kernel already performs the necessary locking on a per-inode basis to save me some work in userspace.

In particular, I originally thought I'd need pthreads mutexes on a per-inode (fuse_ino_t) basis to protect userspace data structures between the .setattr (truncate), .fsync, and .write_buf userspace callbacks.

However upon reading the kernel, I can see fuse_fsync, fuse_{cache,direct}_write_iter in fs/fuse/file.c all use inode_lock. do_truncate also uses inode_lock in fs/open.c.

So it's look like implementing extra locking in userspace would do nothing useful in my case, right?

Thanks.

#1 lore raw
Quoted 10 lines · Eric Wong

Hi all, I'm working on a new multi-threaded FS using the libfuse3 fuse_lowlevel.h API. It looks to me like the kernel already performs the necessary locking on a per-inode basis to save me some work in userspace.

In particular, I originally thought I'd need pthreads mutexes on a per-inode (fuse_ino_t) basis to protect userspace data structures between the .setattr (truncate), .fsync, and .write_buf userspace callbacks.

Quoted 6 lines

However upon reading the kernel, I can see fuse_fsync, fuse_{cache,direct}_write_iter in fs/fuse/file.c all use inode_lock. do_truncate also uses inode_lock in fs/open.c.

So it's look like implementing extra locking in userspace would do nothing useful in my case, right?

I guess it probably is a good idea to implement proper locking in multi-threaded fs and not rely on what kind of locking kernel is doing. If kernel locking changes down the line, your implementation will be broken.

Vivek

#2 lore raw
Quoted 16 lines · Vivek Goyal
Quoted 14 lines · Eric Wong

Hi all, I'm working on a new multi-threaded FS using the libfuse3 fuse_lowlevel.h API. It looks to me like the kernel already performs the necessary locking on a per-inode basis to save me some work in userspace.

In particular, I originally thought I'd need pthreads mutexes on a per-inode (fuse_ino_t) basis to protect userspace data structures between the .setattr (truncate), .fsync, and .write_buf userspace callbacks.

However upon reading the kernel, I can see fuse_fsync, fuse_{cache,direct}_write_iter in fs/fuse/file.c all use inode_lock. do_truncate also uses inode_lock in fs/open.c.

Quoted 7 lines
Quoted 2 lines

So it's look like implementing extra locking in userspace would do nothing useful in my case, right?

I guess it probably is a good idea to implement proper locking in multi-threaded fs and not rely on what kind of locking kernel is doing. If kernel locking changes down the line, your implementation will be broken.

Thing is, some fuse filesystem implementations already do rely on
kernel locking. So while it shouldn't hurt to have an extra layer of
locking (except complexity and performance) it's not necessary.

See for example FUSE_PARALLEL_DIROPS which was added due to kernel locking changes to avoid breaking backward compatibility.

#3 lore raw
Quoted 20 lines · Miklos Szeredi
Quoted 19 lines · Vivek Goyal
Quoted 16 lines · Eric Wong

Hi all, I'm working on a new multi-threaded FS using the libfuse3 fuse_lowlevel.h API. It looks to me like the kernel already performs the necessary locking on a per-inode basis to save me some work in userspace.

In particular, I originally thought I'd need pthreads mutexes on a per-inode (fuse_ino_t) basis to protect userspace data structures between the .setattr (truncate), .fsync, and .write_buf userspace callbacks.

However upon reading the kernel, I can see fuse_fsync, fuse_{cache,direct}_write_iter in fs/fuse/file.c all use inode_lock. do_truncate also uses inode_lock in fs/open.c.

So it's look like implementing extra locking in userspace would do nothing useful in my case, right?

Quoted 8 lines
Quoted 4 lines

I guess it probably is a good idea to implement proper locking in multi-threaded fs and not rely on what kind of locking kernel is doing. If kernel locking changes down the line, your implementation will be broken.

Thing is, some fuse filesystem implementations already do rely on
kernel locking. So while it shouldn't hurt to have an extra layer of
locking (except complexity and performance) it's not necessary.

I am wondering if same applies to virtiofs. In that case guest kernel is untrusted entity. So we don't want to run into a situation where guest kernel can somehow corrupt shared data structures of virtiofsd and that somehow opens the door for some other bad outcome. May be in that case it is safer to not rely on guest kernel locking.

Quoted 3 lines

See for example FUSE_PARALLEL_DIROPS which was added due to kernel locking changes to avoid breaking backward compatibility.

Good to know about this option. I checked that fuse_lowlevel.c enables it by default. So I should be fine from virtiofsd point of view.

Thanks
Vivek

#4 lore raw
Quoted 31 lines · Vivek Goyal
Quoted 20 lines · Miklos Szeredi
Quoted 19 lines · Vivek Goyal
Quoted 16 lines · Eric Wong

Hi all, I'm working on a new multi-threaded FS using the libfuse3 fuse_lowlevel.h API. It looks to me like the kernel already performs the necessary locking on a per-inode basis to save me some work in userspace.

In particular, I originally thought I'd need pthreads mutexes on a per-inode (fuse_ino_t) basis to protect userspace data structures between the .setattr (truncate), .fsync, and .write_buf userspace callbacks.

However upon reading the kernel, I can see fuse_fsync, fuse_{cache,direct}_write_iter in fs/fuse/file.c all use inode_lock. do_truncate also uses inode_lock in fs/open.c.

So it's look like implementing extra locking in userspace would do nothing useful in my case, right?

Quoted 8 lines
Quoted 4 lines

I guess it probably is a good idea to implement proper locking in multi-threaded fs and not rely on what kind of locking kernel is doing. If kernel locking changes down the line, your implementation will be broken.

Thing is, some fuse filesystem implementations already do rely on
kernel locking. So while it shouldn't hurt to have an extra layer of
locking (except complexity and performance) it's not necessary.

Quoted 5 lines

I am wondering if same applies to virtiofs. In that case guest kernel is untrusted entity. So we don't want to run into a situation where guest kernel can somehow corrupt shared data structures of virtiofsd and that somehow opens the door for some other bad outcome. May be in that case it is safer to not rely on guest kernel locking.

That's true, virtiofs has inverted trust model, so the server must not assume anything from the client.

Thanks,
Miklos