Skip to contentSkip to conversation

nfsd: Prevent truncation of an unlinked inode from blocking access to its directory

PATCH In review Nick Huang 4 participants 11 messages May 14, 2021 – May 18, 2021

Subsystems (3)
lore
Share & tools
lore

Nick Huang

author
#0 lore raw

PATCH 1/1 nfsd: Prevent truncation of an unlinked inode from blocking access to its directory

From: Yu Hsiang Huang <[email protected]>

Truncation of an unlinked inode may take a long time for I/O waiting, and it doesn't have to prevent access to the directory. Thus, let truncation occur outside the directory's mutex, just like do_unlinkat() does.

---
 fs/nfsd/vfs.c | 5 +++++
 1 file changed, 5 insertions(+)
fs/nfsd/vfs.c +5 -0
index 15adf1f6ab21..39948f130712 100644
@@ -1859,6 +1859,7 @@ nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
 {
 	struct dentry	*dentry, *rdentry;
 	struct inode	*dirp;
+	struct inode	*rinode;
 	__be32		err;
 	int		host_err;
 
@@ -1887,6 +1888,8 @@ nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
 		host_err = -ENOENT;
 		goto out_drop_write;
 	}
+	rinode = d_inode(rdentry);
+	ihold(rinode);
 
 	if (!type)
 		type = d_inode(rdentry)->i_mode & S_IFMT;
@@ -1902,6 +1905,8 @@ nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
 	if (!host_err)
 		host_err = commit_metadata(fhp);
 	dput(rdentry);
+	fh_unlock(fhp);
+	iput(rinode);    /* truncate the inode here */
 
 out_drop_write:
 	fh_drop_write(fhp);
Signature · 1 line
2.25.1
#1 lore raw
Quoted 5 lines · Nick Huang

From: Yu Hsiang Huang <[email protected]>

Truncation of an unlinked inode may take a long time for I/O waiting, and it doesn't have to prevent access to the directory. Thus, let truncation occur outside the directory's mutex, just like do_unlinkat() does.

Makes sense to me, thanks. I'll queue it up for 5.14.

Just out of curiosity: was this found just by inspection, or were you hitting this in a real workload? I'd be interested in what it took to reproduce if so.

--b.

Quoted 40 lines
---
 fs/nfsd/vfs.c | 5 +++++
 1 file changed, 5 insertions(+)
fs/nfsd/vfs.c +5 -0
index 15adf1f6ab21..39948f130712 100644
@@ -1859,6 +1859,7 @@ nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
 {
 	struct dentry	*dentry, *rdentry;
 	struct inode	*dirp;
+	struct inode	*rinode;
 	__be32		err;
 	int		host_err;
 
@@ -1887,6 +1888,8 @@ nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
 		host_err = -ENOENT;
 		goto out_drop_write;
 	}
+	rinode = d_inode(rdentry);
+	ihold(rinode);
 
 	if (!type)
 		type = d_inode(rdentry)->i_mode & S_IFMT;
@@ -1902,6 +1905,8 @@ nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
 	if (!host_err)
 		host_err = commit_metadata(fhp);
 	dput(rdentry);
+	fh_unlock(fhp);
+	iput(rinode);    /* truncate the inode here */
 
 out_drop_write:
 	fh_drop_write(fhp);
Signature · 1 line
2.25.1
#2 lore raw
In reply to Nick Huang
Quoted 39 lines · Nick Huang

From: Yu Hsiang Huang <[email protected]>

Truncation of an unlinked inode may take a long time for I/O waiting, and
it doesn't have to prevent access to the directory. Thus, let truncation
occur outside the directory's mutex, just like do_unlinkat() does.

---
 fs/nfsd/vfs.c | 5 +++++

 1 file changed, 5 insertions(+)
fs/nfsd/vfs.c +1 -0
index 15adf1f6ab21..39948f130712 100644
@@ -1859,6 +1859,7 @@ nfsd_unlink(struct svc_rqst *rqstp, struct
 svc_fh *fhp, int type,
  {
         struct dentry   *dentry, *rdentry;
         struct inode    *dirp;
+       struct inode    *rinode;
         __be32          err;
         int             host_err;

fs/nfsd/vfs.c +2 -0
@@ -1887,6 +1888,8 @@ nfsd_unlink(struct svc_rqst *rqstp, struct
 svc_fh *fhp, int type,
                 host_err = -ENOENT;
                 goto out_drop_write;
         }
+       rinode = d_inode(rdentry);
+       ihold(rinode);


        if (!type)
                type = d_inode(rdentry)->i_mode & S_IFMT;

fs/nfsd/vfs.c nfsd_unlink(struct svc_rqst *rqstp, struct · line 1907

Quoted 3 lines
fs/nfsd/vfs.c +0 -0
@@ -1902,6 +1905,8 @@ nfsd_unlink(struct svc_rqst *rqstp, struct
 svc_fh *fhp, int type,
         if (!host_err)
                 host_err = commit_metadata(fhp);

Why leave the commit_metadata() call under the lock? If you're concerned about latency, then it makes more sense to call fh_unlock() before flushing those metadata updates to disk.

This is, BTW, an optimisation that appears to be possible in several other cases in fs/nfsd/vfs.c.

Quoted 6 lines
fs/nfsd/vfs.c +2 -0
         dput(rdentry);
+       fh_unlock(fhp);
+       iput(rinode);    /* truncate the inode here */


 out_drop_write:
        fh_drop_write(fhp);

Signature · 3 lines
Trond Myklebust
Linux NFS client maintainer, Hammerspace
[email protected]
#3 lore raw
Quoted 3 lines · Trond Myklebust

Why leave the commit_metadata() call under the lock? If you're concerned about latency, then it makes more sense to call fh_unlock() before flushing those metadata updates to disk.

Also I'm not sure why the extra inode reference is needed. What speaks against just moving the dput out of the locked section?

#4 lore raw
Quoted 7 lines · Christoph Hellwig
Quoted 3 lines · Trond Myklebust

Why leave the commit_metadata() call under the lock? If you're concerned about latency, then it makes more sense to call fh_unlock() before flushing those metadata updates to disk.

Also I'm not sure why the extra inode reference is needed. What speaks against just moving the dput out of the locked section?

I don't know. Do you know why do_unlinkat() is doing the same thing?

--b.

#5 lore raw
Quoted 10 lines · On Mon, May 17, 2021 at 02:56:59PM -0400, [email protected] wrote:
Quoted 7 lines · Christoph Hellwig
Quoted 3 lines · Trond Myklebust

Why leave the commit_metadata() call under the lock? If you're concerned about latency, then it makes more sense to call fh_unlock() before flushing those metadata updates to disk.

Also I'm not sure why the extra inode reference is needed. What speaks against just moving the dput out of the locked section?

I don't know. Do you know why do_unlinkat() is doing the same thing?

No. Al, any idea why unlink does the final dput under i_rwsem?

#6 lore raw
In reply to Christoph Hellwig
Quoted 9 lines · Christoph Hellwig
Quoted 4 lines · Trond Myklebust

Why leave the commit_metadata() call under the lock? If you're concerned about latency, then it makes more sense to call fh_unlock()
before flushing those metadata updates to disk.

Also I'm not sure why the extra inode reference is needed.  What speaks
against just moving the dput out of the locked section?

Isn't the inode reference taken just in order to ensure that the call to iput_final() (and in particular the call to truncate_inode_pages_final()) is performed outside the lock?

The dput() is presumably usually not particularly expensive, since the dentry is just a completely ordinary negative dentry at this point.

Signature · 3 lines
Trond Myklebust
Linux NFS client maintainer, Hammerspace
[email protected]
#7 lore raw
Quoted 11 lines · Trond Myklebust
Quoted 9 lines · Christoph Hellwig
Quoted 4 lines · Trond Myklebust

Why leave the commit_metadata() call under the lock? If you're concerned about latency, then it makes more sense to call fh_unlock()
before flushing those metadata updates to disk.

Also I'm not sure why the extra inode reference is needed.  What speaks
against just moving the dput out of the locked section?

Quoted 6 lines

Isn't the inode reference taken just in order to ensure that the call to iput_final() (and in particular the call to truncate_inode_pages_final()) is performed outside the lock?

The dput() is presumably usually not particularly expensive, since the dentry is just a completely ordinary negative dentry at this point.

Right, but why bother with the extra ihold/iput instead of just postponing the dput?

fs/namei.c +2 -4
index 79b0ff9b151e..aeed93c9874c 100644
@@ -4084,7 +4084,6 @@ long do_unlinkat(int dfd, struct filename *name)
 		inode = dentry->d_inode;
 		if (d_is_negative(dentry))
 			goto slashes;
-		ihold(inode);
 		error = security_path_unlink(&path, dentry);
 		if (error)
 			goto exit2;
@@ -4092,11 +4091,10 @@ long do_unlinkat(int dfd, struct filename *name)
 		error = vfs_unlink(mnt_userns, path.dentry->d_inode, dentry,
 				   &delegated_inode);
 exit2:
-		dput(dentry);
 	}
 	inode_unlock(path.dentry->d_inode);
-	if (inode)
-		iput(inode);	/* truncate the inode here */
+	if (!IS_ERR(dentry))
+		dput(dentry);	/* truncate the inode here */
 	inode = NULL;
 	if (delegated_inode) {
 		error = break_deleg_wait(&delegated_inode);

?

--b.

#8 lore raw
Quoted 17 lines · On Tue, 2021-05-18 at 14:21 -0400, [email protected] wrote:
Quoted 16 lines · Trond Myklebust
Quoted 9 lines · Christoph Hellwig
Quoted 4 lines · Trond Myklebust

Why leave the commit_metadata() call under the lock? If you're concerned about latency, then it makes more sense to call fh_unlock()
before flushing those metadata updates to disk.

Also I'm not sure why the extra inode reference is needed.  What speaks
against just moving the dput out of the locked section?

Isn't the inode reference taken just in order to ensure that the call
to iput_final() (and in particular the call to truncate_inode_pages_final()) is performed outside the lock?

Quoted 6 lines
Quoted 3 lines

The dput() is presumably usually not particularly expensive, since the
dentry is just a completely ordinary negative dentry at this point.

Right, but why bother with the extra ihold/iput instead of just postponing the dput?

As I said above, the dentry is negative at this point. It doesn't carry a reference to the inode any more, so that wouldn't defer the inode truncation.

Quoted 34 lines
fs/namei.c +0 -1
index 79b0ff9b151e..aeed93c9874c 100644
@@ -4084,7 +4084,6 @@ long do_unlinkat(int dfd, struct filename
 *name)
                 inode = dentry->d_inode;
                 if (d_is_negative(dentry))
                         goto slashes;
-               ihold(inode);
                 error = security_path_unlink(&path, dentry);
                 if (error)

                        goto exit2;

fs/namei.c +0 -0
@@ -4092,11 +4091,10 @@ long do_unlinkat(int dfd, struct filename
 *name)
                 error = vfs_unlink(mnt_userns, path.dentry->d_inode,
 dentry,
                                    &delegated_inode);

 exit2:
-               dput(dentry);
        }
        inode_unlock(path.dentry->d_inode);
-       if (inode)
-               iput(inode);    /* truncate the inode here */ +       if (!IS_ERR(dentry))
+               dput(dentry);   /* truncate the inode here */ inode = NULL;
        if (delegated_inode) {
                error = break_deleg_wait(&delegated_inode);

?

--b.

Signature · 3 lines
Trond Myklebust
Linux NFS client maintainer, Hammerspace
[email protected]
#9 lore raw
Quoted 22 lines · Trond Myklebust
Quoted 21 lines · On Tue, 2021-05-18 at 14:21 -0400, [email protected] wrote:
Quoted 19 lines · Trond Myklebust
Quoted 9 lines · Christoph Hellwig
Quoted 4 lines · Trond Myklebust

Why leave the commit_metadata() call under the lock? If you're concerned about latency, then it makes more sense to call fh_unlock()
before flushing those metadata updates to disk.

Also I'm not sure why the extra inode reference is needed.  What speaks
against just moving the dput out of the locked section?

Isn't the inode reference taken just in order to ensure that the call
to iput_final() (and in particular the call to truncate_inode_pages_final()) is performed outside the lock?

The dput() is presumably usually not particularly expensive, since the
dentry is just a completely ordinary negative dentry at this point.

Quoted 6 lines
Quoted 2 lines

Right, but why bother with the extra ihold/iput instead of just postponing the dput?

As I said above, the dentry is negative at this point. It doesn't carry a reference to the inode any more, so that wouldn't defer the inode truncation.

Oh, of course, thanks!

--b.

Quoted 42 lines
Quoted 35 lines
fs/namei.c +0 -1
index 79b0ff9b151e..aeed93c9874c 100644
@@ -4084,7 +4084,6 @@ long do_unlinkat(int dfd, struct filename
 *name)
                 inode = dentry->d_inode;
                 if (d_is_negative(dentry))
                         goto slashes;
-               ihold(inode);
                 error = security_path_unlink(&path, dentry);
                 if (error)

                        goto exit2;

fs/namei.c +0 -0
@@ -4092,11 +4091,10 @@ long do_unlinkat(int dfd, struct filename
 *name)
                 error = vfs_unlink(mnt_userns, path.dentry->d_inode,
 dentry,
                                    &delegated_inode);

 exit2:
-               dput(dentry);
        }
        inode_unlock(path.dentry->d_inode);
-       if (inode)
-               iput(inode);    /* truncate the inode here */ +       if (!IS_ERR(dentry))
+               dput(dentry);   /* truncate the inode here */ inode = NULL;
        if (delegated_inode) {
                error = break_deleg_wait(&delegated_inode);

?

--b.

Signature · 3 lines
Trond Myklebust
Linux NFS client maintainer, Hammerspace
[email protected]
#10 lore raw
In reply to Trond Myklebust
Quoted 48 lines · Trond Myklebust
Quoted 39 lines · Nick Huang

From: Yu Hsiang Huang <[email protected]>

Truncation of an unlinked inode may take a long time for I/O waiting, and
it doesn't have to prevent access to the directory. Thus, let truncation
occur outside the directory's mutex, just like do_unlinkat() does.

---
 fs/nfsd/vfs.c | 5 +++++

 1 file changed, 5 insertions(+)
fs/nfsd/vfs.c +1 -0
index 15adf1f6ab21..39948f130712 100644
@@ -1859,6 +1859,7 @@ nfsd_unlink(struct svc_rqst *rqstp, struct
 svc_fh *fhp, int type,
  {
         struct dentry   *dentry, *rdentry;
         struct inode    *dirp;
+       struct inode    *rinode;
         __be32          err;
         int             host_err;

fs/nfsd/vfs.c +2 -0
@@ -1887,6 +1888,8 @@ nfsd_unlink(struct svc_rqst *rqstp, struct
 svc_fh *fhp, int type,
                 host_err = -ENOENT;
                 goto out_drop_write;
         }
+       rinode = d_inode(rdentry);
+       ihold(rinode);


        if (!type)
                type = d_inode(rdentry)->i_mode & S_IFMT;

fs/nfsd/vfs.c nfsd_unlink(struct svc_rqst *rqstp, struct · line 1907

Quoted 3 lines
fs/nfsd/vfs.c +0 -0
@@ -1902,6 +1905,8 @@ nfsd_unlink(struct svc_rqst *rqstp, struct
 svc_fh *fhp, int type,
         if (!host_err)
                 host_err = commit_metadata(fhp);

Why leave the commit_metadata() call under the lock? If you're concerned about latency, then it makes more sense to call fh_unlock() before flushing those metadata updates to disk.

Quoted 2 lines

This is, BTW, an optimisation that appears to be possible in several other cases in fs/nfsd/vfs.c.

I'm tentatively applying the original patch plus the following.

Create and rename code are two other places where we have commit_metadata() calls that could probably be moved out from under locks but they looked slightly more complicated.

--b.

commit ec79990df716

Author: J. Bruce Fields <[email protected]>
Date:   Fri May 14 18:21:37 2021 -0400

nfsd: move some commit_metadata()s outside the inode lock

The commit may be time-consuming and there's no need to hold the lock for it.

More of these are possible, these were just some easy ones.

fs/nfsd/vfs.c +3 -2
index 39948f130712..d73d3c9126fc 100644
@@ -1613,9 +1613,9 @@ nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
 
 	host_err = vfs_symlink(&init_user_ns, d_inode(dentry), dnew, path);
 	err = nfserrno(host_err);
+	fh_unlock(fhp);
 	if (!err)
 		err = nfserrno(commit_metadata(fhp));
-	fh_unlock(fhp);
 
 	fh_drop_write(fhp);
 
@@ -1680,6 +1680,7 @@ nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
 	if (d_really_is_negative(dold))
 		goto out_dput;
 	host_err = vfs_link(dold, &init_user_ns, dirp, dnew, NULL);
+	fh_unlock(ffhp);
 	if (!host_err) {
 		err = nfserrno(commit_metadata(ffhp));
 		if (!err)
@@ -1902,10 +1903,10 @@ nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
 		host_err = vfs_rmdir(&init_user_ns, dirp, rdentry);
 	}
 
+	fh_unlock(fhp);
 	if (!host_err)
 		host_err = commit_metadata(fhp);
 	dput(rdentry);
-	fh_unlock(fhp);
 	iput(rinode);    /* truncate the inode here */
 
 out_drop_write: