Skip to contentSkip to conversation

include/linux/fs.h: add inode_lock_killable()

PATCH Superseded Max Kellermann 3 participants 8 messages Apr 29, 2025 – May 13, 2025 VFS

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 include/linux/fs.h: add inode_lock_killable()

Prepare for making inode operations killable while they're waiting for the lock.

---
 include/linux/fs.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
include/linux/fs.h +10 -0
index 016b0fe1536e..5e4ac873228d 100644
@@ -867,6 +867,11 @@ static inline void inode_lock(struct inode *inode)
 	down_write(&inode->i_rwsem);
 }
 
+static inline __must_check int inode_lock_killable(struct inode *inode)
+{
+	return down_write_killable(&inode->i_rwsem);
+}
+
 static inline void inode_unlock(struct inode *inode)
 {
 	up_write(&inode->i_rwsem);
@@ -877,6 +882,11 @@ static inline void inode_lock_shared(struct inode *inode)
 	down_read(&inode->i_rwsem);
 }
 
+static inline __must_check int inode_lock_shared_killable(struct inode *inode)
+{
+	return down_read_killable(&inode->i_rwsem);
+}
+
 static inline void inode_unlock_shared(struct inode *inode)
 {
 	up_read(&inode->i_rwsem);
Signature · 1 line
2.47.2
#1 lore raw

PATCH 2/2 fs: make several inode lock operations killable

Allows killing processes that are waiting for the inode lock.

---
 fs/open.c       | 14 +++++++++++---
 fs/read_write.c |  4 +++-
 2 files changed, 14 insertions(+), 4 deletions(-)
fs/open.c +11 -3
index a9063cca9911..7828234a7caa 100644
@@ -60,7 +60,10 @@ int do_truncate(struct mnt_idmap *idmap, struct dentry *dentry,
 	if (ret)
 		newattrs.ia_valid |= ret | ATTR_FORCE;
 
-	inode_lock(dentry->d_inode);
+	ret = inode_lock_killable(dentry->d_inode);
+	if (ret)
+		return ret;
+
 	/* Note any delegations or leases have already been broken: */
 	ret = notify_change(idmap, dentry, &newattrs, NULL);
 	inode_unlock(dentry->d_inode);
@@ -635,7 +638,9 @@ int chmod_common(const struct path *path, umode_t mode)
 	if (error)
 		return error;
 retry_deleg:
-	inode_lock(inode);
+	error = inode_lock_killable(inode);
+	if (error)
+		goto out_mnt_unlock;
 	error = security_path_chmod(path, mode);
 	if (error)
 		goto out_unlock;
@@ -650,6 +655,7 @@ int chmod_common(const struct path *path, umode_t mode)
 		if (!error)
 			goto retry_deleg;
 	}
+out_mnt_unlock:
 	mnt_drop_write(path->mnt);
 	return error;
 }
@@ -769,7 +775,9 @@ int chown_common(const struct path *path, uid_t user, gid_t group)
 		return -EINVAL;
 	if ((group != (gid_t)-1) && !setattr_vfsgid(&newattrs, gid))
 		return -EINVAL;
-	inode_lock(inode);
+	error = inode_lock_killable(inode);
+	if (error)
+		return error;
 	if (!S_ISDIR(inode->i_mode))
 		newattrs.ia_valid |= ATTR_KILL_SUID | ATTR_KILL_PRIV |
 				     setattr_should_drop_sgid(idmap, inode);
fs/read_write.c +3 -1
index bb0ed26a0b3a..0ef70e128c4a 100644
@@ -332,7 +332,9 @@ loff_t default_llseek(struct file *file, loff_t offset, int whence)
 	struct inode *inode = file_inode(file);
 	loff_t retval;
 
-	inode_lock(inode);
+	retval = inode_lock_killable(inode);
+	if (retval)
+		return retval;
 	switch (whence) {
 		case SEEK_END:
 			offset += i_size_read(inode);
Signature · 1 line
2.47.2
#2 lore raw

Re: [PATCH 2/2] fs: make several inode lock operations killable

Quoted 4 lines · Max Kellermann

Allows killing processes that are waiting for the inode lock.

---

Oha, that's interesting.

Quoted 21 lines
 fs/open.c       | 14 +++++++++++---
 fs/read_write.c |  4 +++-
 2 files changed, 14 insertions(+), 4 deletions(-)
fs/open.c +4 -1
index a9063cca9911..7828234a7caa 100644
@@ -60,7 +60,10 @@ int do_truncate(struct mnt_idmap *idmap, struct dentry *dentry,
 	if (ret)
 		newattrs.ia_valid |= ret | ATTR_FORCE;
 
-	inode_lock(dentry->d_inode);
+	ret = inode_lock_killable(dentry->d_inode);
+	if (ret)
+		return ret;
+
 	/* Note any delegations or leases have already been broken: */
 	ret = notify_change(idmap, dentry, &newattrs, NULL);
 	inode_unlock(dentry->d_inode);

fs/open.c int chmod_common(const struct path *path, umode_t mode) · line 641

Quoted 5 lines
fs/open.c +1 -1
@@ -635,7 +638,9 @@ int chmod_common(const struct path *path, umode_t mode)
 	if (error)
 		return error;
 retry_deleg:
-	inode_lock(inode);
+	error = inode_lock_killable(inode);

That's probably fine.

Quoted 14 lines
fs/open.c +3 -0
+	if (error)
+		goto out_mnt_unlock;
 	error = security_path_chmod(path, mode);
 	if (error)
 		goto out_unlock;
@@ -650,6 +655,7 @@ int chmod_common(const struct path *path, umode_t mode)
 		if (!error)
 			goto retry_deleg;
 	}
+out_mnt_unlock:
 	mnt_drop_write(path->mnt);
 	return error;
 }

fs/open.c int chown_common(const struct path *path, uid_t user, gid_t group) · line 780

Quoted 7 lines
fs/open.c +3 -1
@@ -769,7 +775,9 @@ int chown_common(const struct path *path, uid_t user, gid_t group)
 		return -EINVAL;
 	if ((group != (gid_t)-1) && !setattr_vfsgid(&newattrs, gid))
 		return -EINVAL;
-	inode_lock(inode);
+	error = inode_lock_killable(inode);
+	if (error)
+		return error;

That too.

fs/read_write.c loff_t default_llseek(struct file *file, loff_t offset, int whence) · line 335

Quoted 13 lines
fs/open.c +0 -0
 	if (!S_ISDIR(inode->i_mode))
 		newattrs.ia_valid |= ATTR_KILL_SUID | ATTR_KILL_PRIV |
 				     setattr_should_drop_sgid(idmap, inode);
fs/read_write.c +1 -1
index bb0ed26a0b3a..0ef70e128c4a 100644
@@ -332,7 +332,9 @@ loff_t default_llseek(struct file *file, loff_t offset, int whence)
 	struct inode *inode = file_inode(file);
 	loff_t retval;
 
-	inode_lock(inode);
+	retval = inode_lock_killable(inode);

That change doesn't seem so obviously fine to me. Either way I'd like to see this split in three patches and some reasoning why it's safe and some justification why it's wanted...

#3 lore raw

Re: [PATCH 2/2] fs: make several inode lock operations killable

Quoted 10 lines · Christian Brauner

fs/read_write.c loff_t default_llseek(struct file *file, loff_t offset, int whence) · line 335

Quoted 8 lines
fs/read_write.c +1 -1
@@ -332,7 +332,9 @@ loff_t default_llseek(struct file *file, loff_t offset, int whence)
      struct inode *inode = file_inode(file);
      loff_t retval;
 
-     inode_lock(inode);
+     retval = inode_lock_killable(inode);

That change doesn't seem so obviously fine to me.

Why do you think so? And how is this different than the other two.

Quoted 2 lines

Either way I'd like to see this split in three patches and some reasoning why it's safe and some justification why it's wanted...

Sure I can split this patch, but before I spend the time, I'd like us first to agree that the patch is useful.

I wrote this while debugging lots of netfs/nfs/ceph bugs; even without these bugs, I/O operations on netfs can take a looong time (if the server is slow) and the inode is locked during the whole operation. That can cause lots of other processes to go stuck, and my patch allows these operations to be canceled. Without this, the processes not only remain stuck until the inode is unlocked, but all stuck processes have to finish all their I/O before anything can continue. I'd like to be able to "kill -9" stuck processes.

A similar NFS-specific patch I wrote was merged last year:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=38a125b31504f91bf6fdd3cfc3a3e9a721e6c97a
The same patch for Ceph was never merged (but not explicitly
rejected): https://lore.kernel.org/lkml/[email protected]/
Prior to my work, several NFS operations were already killable.

#4 lore raw

Re: [PATCH 2/2] fs: make several inode lock operations killable

Sorry, coming back to this now. I lost sight of this patch.

Quoted 7 lines · Max Kellermann
Quoted 6 lines · Christian Brauner
Quoted 6 lines
fs/read_write.c +0 -0
@@ -332,7 +332,9 @@ loff_t default_llseek(struct file *file, loff_t offset, int whence)
      struct inode *inode = file_inode(file);
      loff_t retval;
 
Quoted 6 lines
Quoted 4 lines

fs/read_write.c loff_t default_llseek(struct file *file, loff_t offset, int whence) · line 335

Quoted 2 lines
fs/read_write.c +1 -1
-     inode_lock(inode);
+     retval = inode_lock_killable(inode);

That change doesn't seem so obviously fine to me.

Why do you think so? And how is this different than the other two.

chown_common() and chmod_common() are very close to the syscall boundary so it's very unlikely that we run into weird issues apart from userspace regression when they suddenly fail a change for new unexpected reasons.

But just look at default_llseek():

Quoted 1 line

git grep default_llseek | wc -l

    461

That is a lot of stuff and it's not immediately clear how deeply or nested they are called. For example from overlayfs in stacked callchains. Who knows what strange assumptions some of the callers have including the possible return values from that helper.

Quoted 6 lines
Quoted 3 lines

Either way I'd like to see this split in three patches and some reasoning why it's safe and some justification why it's wanted...

Sure I can split this patch, but before I spend the time, I'd like us first to agree that the patch is useful.

This is difficult to answer. Yes, on the face of it it seems useful to be able to kill various operations that sleep on inode lock but who knows what implicit guarantees/expectations we're going to break if we do it. Maybe @Jan has some thoughts here as well.

Quoted 14 lines

I wrote this while debugging lots of netfs/nfs/ceph bugs; even without these bugs, I/O operations on netfs can take a looong time (if the server is slow) and the inode is locked during the whole operation. That can cause lots of other processes to go stuck, and my patch allows these operations to be canceled. Without this, the processes not only remain stuck until the inode is unlocked, but all stuck processes have to finish all their I/O before anything can continue. I'd like to be able to "kill -9" stuck processes.

A similar NFS-specific patch I wrote was merged last year:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=38a125b31504f91bf6fdd3cfc3a3e9a721e6c97a
The same patch for Ceph was never merged (but not explicitly
rejected): https://lore.kernel.org/lkml/[email protected]/
Prior to my work, several NFS operations were already killable.

reviewer
#5 lore raw

Re: [PATCH 2/2] fs: make several inode lock operations killable

Quoted 33 lines · Christian Brauner
Quoted 7 lines · Max Kellermann
Quoted 6 lines · Christian Brauner
Quoted 6 lines
fs/read_write.c +0 -0
@@ -332,7 +332,9 @@ loff_t default_llseek(struct file *file, loff_t offset, int whence)
      struct inode *inode = file_inode(file);
      loff_t retval;
 
Quoted 6 lines
Quoted 4 lines

fs/read_write.c loff_t default_llseek(struct file *file, loff_t offset, int whence) · line 335

Quoted 2 lines
fs/read_write.c +1 -1
-     inode_lock(inode);
+     retval = inode_lock_killable(inode);

That change doesn't seem so obviously fine to me.

Why do you think so? And how is this different than the other two.

chown_common() and chmod_common() are very close to the syscall boundary so it's very unlikely that we run into weird issues apart from userspace regression when they suddenly fail a change for new unexpected reasons.

But just look at default_llseek():

Quoted 1 line

git grep default_llseek | wc -l

    461

That is a lot of stuff and it's not immediately clear how deeply or nested they are called. For example from overlayfs in stacked callchains. Who knows what strange assumptions some of the callers have including the possible return values from that helper.

Quoted 4 lines
Quoted 3 lines

Either way I'd like to see this split in three patches and some reasoning why it's safe and some justification why it's wanted...

Quoted 7 lines
Quoted 2 lines

Sure I can split this patch, but before I spend the time, I'd like us first to agree that the patch is useful.

This is difficult to answer. Yes, on the face of it it seems useful to be able to kill various operations that sleep on inode lock but who knows what implicit guarantees/expectations we're going to break if we do it. Maybe @Jan has some thoughts here as well.

So I think having lock killable is useful and generally this should be safe wrt userspace (the process will get killed before it can notice the difference anyway). The concern about guarantees / expectations is still valid for the *kernel* code (which is I think what you meant above). So I guess some audit of kernel paths that can end up calling ->llseek handler and whether they are OK with the handler returning EINTR is needed. I expect this will be fine but I would not bet too much on it :)

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#6 lore raw

Re: [PATCH 2/2] fs: make several inode lock operations killable

Quoted 42 lines · Jan Kara
Quoted 33 lines · Christian Brauner
Quoted 7 lines · Max Kellermann
Quoted 6 lines · Christian Brauner
Quoted 6 lines
fs/read_write.c +0 -0
@@ -332,7 +332,9 @@ loff_t default_llseek(struct file *file, loff_t offset, int whence)
      struct inode *inode = file_inode(file);
      loff_t retval;
 
Quoted 6 lines
Quoted 4 lines

fs/read_write.c loff_t default_llseek(struct file *file, loff_t offset, int whence) · line 335

Quoted 2 lines
fs/read_write.c +1 -1
-     inode_lock(inode);
+     retval = inode_lock_killable(inode);

That change doesn't seem so obviously fine to me.

Why do you think so? And how is this different than the other two.

chown_common() and chmod_common() are very close to the syscall boundary so it's very unlikely that we run into weird issues apart from userspace regression when they suddenly fail a change for new unexpected reasons.

But just look at default_llseek():

Quoted 1 line

git grep default_llseek | wc -l

    461

That is a lot of stuff and it's not immediately clear how deeply or nested they are called. For example from overlayfs in stacked callchains. Who knows what strange assumptions some of the callers have including the possible return values from that helper.

Quoted 4 lines
Quoted 3 lines

Either way I'd like to see this split in three patches and some reasoning why it's safe and some justification why it's wanted...

Quoted 7 lines
Quoted 2 lines

Sure I can split this patch, but before I spend the time, I'd like us first to agree that the patch is useful.

This is difficult to answer. Yes, on the face of it it seems useful to be able to kill various operations that sleep on inode lock but who knows what implicit guarantees/expectations we're going to break if we do it. Maybe @Jan has some thoughts here as well.

Quoted 7 lines

So I think having lock killable is useful and generally this should be safe wrt userspace (the process will get killed before it can notice the difference anyway). The concern about guarantees / expectations is still valid for the *kernel* code (which is I think what you meant above). So I guess some audit of kernel paths that can end up calling ->llseek handler and whether they are OK with the handler returning EINTR is needed. I expect this will be fine but I would not bet too much on it :)

Great. @Max you want to send an updated version where split into separate patches?

#7 lore raw

Re: [PATCH 2/2] fs: make several inode lock operations killable

Quoted 2 lines · Christian Brauner

Great. @Max you want to send an updated version where split into separate patches?

So this is about fear of not-so-perfect existing code calling this. Yes, will post splitted v2 today.