Skip to content

Commit 3c0f0f5

Browse files
Trond Myklebustgregkh
authored andcommitted
NFSv4: Fix a pNFS layout related use-after-free race when freeing the inode
[ Upstream commit b6d49ec ] When returning the layout in nfs4_evict_inode(), we need to ensure that the layout is actually done being freed before we can proceed to free the inode itself. Signed-off-by: Trond Myklebust <trond.myklebust@hammerspace.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 06ac2ca commit 3c0f0f5

3 files changed

Lines changed: 37 additions & 3 deletions

File tree

‎fs/nfs/nfs4super.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static void nfs4_evict_inode(struct inode *inode)
6767
nfs_inode_evict_delegation(inode);
6868
/* Note that above delegreturn would trigger pnfs return-on-close */
6969
pnfs_return_layout(inode);
70-
pnfs_destroy_layout(NFS_I(inode));
70+
pnfs_destroy_layout_final(NFS_I(inode));
7171
/* First call standard NFS clear_inode() code */
7272
nfs_clear_inode(inode);
7373
nfs4_xattr_cache_zap(inode);

‎fs/nfs/pnfs.c‎

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ void
294294
pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo)
295295
{
296296
struct inode *inode;
297+
unsigned long i_state;
297298

298299
if (!lo)
299300
return;
@@ -304,8 +305,12 @@ pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo)
304305
if (!list_empty(&lo->plh_segs))
305306
WARN_ONCE(1, "NFS: BUG unfreed layout segments.\n");
306307
pnfs_detach_layout_hdr(lo);
308+
i_state = inode->i_state;
307309
spin_unlock(&inode->i_lock);
308310
pnfs_free_layout_hdr(lo);
311+
/* Notify pnfs_destroy_layout_final() that we're done */
312+
if (i_state & (I_FREEING | I_CLEAR))
313+
wake_up_var(lo);
309314
}
310315
}
311316

@@ -734,8 +739,7 @@ pnfs_free_lseg_list(struct list_head *free_me)
734739
}
735740
}
736741

737-
void
738-
pnfs_destroy_layout(struct nfs_inode *nfsi)
742+
static struct pnfs_layout_hdr *__pnfs_destroy_layout(struct nfs_inode *nfsi)
739743
{
740744
struct pnfs_layout_hdr *lo;
741745
LIST_HEAD(tmp_list);
@@ -753,9 +757,34 @@ pnfs_destroy_layout(struct nfs_inode *nfsi)
753757
pnfs_put_layout_hdr(lo);
754758
} else
755759
spin_unlock(&nfsi->vfs_inode.i_lock);
760+
return lo;
761+
}
762+
763+
void pnfs_destroy_layout(struct nfs_inode *nfsi)
764+
{
765+
__pnfs_destroy_layout(nfsi);
756766
}
757767
EXPORT_SYMBOL_GPL(pnfs_destroy_layout);
758768

769+
static bool pnfs_layout_removed(struct nfs_inode *nfsi,
770+
struct pnfs_layout_hdr *lo)
771+
{
772+
bool ret;
773+
774+
spin_lock(&nfsi->vfs_inode.i_lock);
775+
ret = nfsi->layout != lo;
776+
spin_unlock(&nfsi->vfs_inode.i_lock);
777+
return ret;
778+
}
779+
780+
void pnfs_destroy_layout_final(struct nfs_inode *nfsi)
781+
{
782+
struct pnfs_layout_hdr *lo = __pnfs_destroy_layout(nfsi);
783+
784+
if (lo)
785+
wait_var_event(lo, pnfs_layout_removed(nfsi, lo));
786+
}
787+
759788
static bool
760789
pnfs_layout_add_bulk_destroy_list(struct inode *inode,
761790
struct list_head *layout_list)

‎fs/nfs/pnfs.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ struct pnfs_layout_segment *pnfs_layout_process(struct nfs4_layoutget *lgp);
266266
void pnfs_layoutget_free(struct nfs4_layoutget *lgp);
267267
void pnfs_free_lseg_list(struct list_head *tmp_list);
268268
void pnfs_destroy_layout(struct nfs_inode *);
269+
void pnfs_destroy_layout_final(struct nfs_inode *);
269270
void pnfs_destroy_all_layouts(struct nfs_client *);
270271
int pnfs_destroy_layouts_byfsid(struct nfs_client *clp,
271272
struct nfs_fsid *fsid,
@@ -710,6 +711,10 @@ static inline void pnfs_destroy_layout(struct nfs_inode *nfsi)
710711
{
711712
}
712713

714+
static inline void pnfs_destroy_layout_final(struct nfs_inode *nfsi)
715+
{
716+
}
717+
713718
static inline struct pnfs_layout_segment *
714719
pnfs_get_lseg(struct pnfs_layout_segment *lseg)
715720
{

0 commit comments

Comments
 (0)