Skip to content

Commit

Permalink
ceph: use a xarray to record all the opened files for each inode
Browse files Browse the repository at this point in the history
When releasing the file locks the fl->fl_file memory could be
already released by another thread in filp_close(), so we couldn't
depend on fl->fl_file to get the inode. Just use a xarray to record
the opened files for each inode.

Cc: stable@vger.kernel.org
URL: https://tracker.ceph.com/issues/57986
Signed-off-by: Xiubo Li <xiubli@redhat.com>
  • Loading branch information
lxbsz authored and intel-lab-lkp committed Nov 14, 2022
1 parent 96ae91c commit 232cc8f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
9 changes: 9 additions & 0 deletions fs/ceph/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,13 @@ static int ceph_init_file_info(struct inode *inode, struct file *file,
fi->flags |= CEPH_F_SYNC;

file->private_data = fi;

ret = xa_insert(&ci->i_opened_files, (unsigned long)file,
CEPH_FILP_AVAILABLE, GFP_KERNEL);
if (ret) {
kmem_cache_free(ceph_file_cachep, fi);
return ret;
}
}

ceph_get_fmode(ci, fmode, 1);
Expand Down Expand Up @@ -932,6 +939,8 @@ int ceph_release(struct inode *inode, struct file *file)
dout("release inode %p regular file %p\n", inode, file);
WARN_ON(!list_empty(&fi->rw_contexts));

xa_erase(&ci->i_opened_files, (unsigned long)file);

ceph_fscache_unuse_cookie(inode, file->f_mode & FMODE_WRITE);
ceph_put_fmode(ci, fi->fmode, 1);

Expand Down
4 changes: 4 additions & 0 deletions fs/ceph/inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,8 @@ struct inode *ceph_alloc_inode(struct super_block *sb)
INIT_LIST_HEAD(&ci->i_unsafe_iops);
spin_lock_init(&ci->i_unsafe_lock);

xa_init(&ci->i_opened_files);

ci->i_snap_realm = NULL;
INIT_LIST_HEAD(&ci->i_snap_realm_item);
INIT_LIST_HEAD(&ci->i_snap_flush_item);
Expand All @@ -637,6 +639,8 @@ void ceph_free_inode(struct inode *inode)
{
struct ceph_inode_info *ci = ceph_inode(inode);

xa_destroy(&ci->i_opened_files);

kfree(ci->i_symlink);
#ifdef CONFIG_FS_ENCRYPTION
kfree(ci->fscrypt_auth);
Expand Down
17 changes: 16 additions & 1 deletion fs/ceph/locks.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ static void ceph_fl_copy_lock(struct file_lock *dst, struct file_lock *src)

static void ceph_fl_release_lock(struct file_lock *fl)
{
struct ceph_file_info *fi = fl->fl_file->private_data;
struct inode *inode = fl->fl_u.ceph_fl.fl_inode;
struct ceph_inode_info *ci;
struct ceph_file_info *fi;
void *val;

/*
* If inode is NULL it should be a request file_lock,
Expand All @@ -54,6 +55,20 @@ static void ceph_fl_release_lock(struct file_lock *fl)
return;

ci = ceph_inode(inode);

/*
* For Posix-style locks, it may race between filp_close()s,
* and it's possible that the 'file' memory pointed by
* 'fl->fl_file' has been released. If so just skip it.
*/
rcu_read_lock();
val = xa_load(&ci->i_opened_files, (unsigned long)fl->fl_file);
if (val == CEPH_FILP_AVAILABLE) {
fi = fl->fl_file->private_data;
atomic_dec(&fi->num_locks);
}
rcu_read_unlock();

if (atomic_dec_and_test(&ci->i_filelock_ref)) {
/* clear error when all locks are released */
spin_lock(&ci->i_ceph_lock);
Expand Down
4 changes: 4 additions & 0 deletions fs/ceph/super.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ struct ceph_inode_xattrs_info {
u64 version, index_version;
};

#define CEPH_FILP_AVAILABLE xa_mk_value(1)

/*
* Ceph inode.
*/
Expand Down Expand Up @@ -434,6 +436,8 @@ struct ceph_inode_info {
struct list_head i_unsafe_iops; /* uncommitted mds inode ops */
spinlock_t i_unsafe_lock;

struct xarray i_opened_files;

union {
struct ceph_snap_realm *i_snap_realm; /* snap realm (if caps) */
struct ceph_snapid_map *i_snapid_map; /* snapid -> dev_t */
Expand Down

0 comments on commit 232cc8f

Please sign in to comment.