Skip to content

Commit 12fe3dd

Browse files
Luis Henriquesidryomov
authored andcommitted
ceph: fix buffer free while holding i_ceph_lock in __ceph_build_xattrs_blob()
Calling ceph_buffer_put() in __ceph_build_xattrs_blob() may result in freeing the i_xattrs.blob buffer while holding the i_ceph_lock. This can be fixed by having this function returning the old blob buffer and have the callers of this function freeing it when the lock is released. The following backtrace was triggered by fstests generic/117. BUG: sleeping function called from invalid context at mm/vmalloc.c:2283 in_atomic(): 1, irqs_disabled(): 0, pid: 649, name: fsstress 4 locks held by fsstress/649: #0: 00000000a7478e7e (&type->s_umount_key#19){++++}, at: iterate_supers+0x77/0xf0 #1: 00000000f8de1423 (&(&ci->i_ceph_lock)->rlock){+.+.}, at: ceph_check_caps+0x7b/0xc60 #2: 00000000562f2b27 (&s->s_mutex){+.+.}, at: ceph_check_caps+0x3bd/0xc60 #3: 00000000f83ce16a (&mdsc->snap_rwsem){++++}, at: ceph_check_caps+0x3ed/0xc60 CPU: 1 PID: 649 Comm: fsstress Not tainted 5.2.0+ #439 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.12.1-0-ga5cab58-prebuilt.qemu.org 04/01/2014 Call Trace: dump_stack+0x67/0x90 ___might_sleep.cold+0x9f/0xb1 vfree+0x4b/0x60 ceph_buffer_release+0x1b/0x60 __ceph_build_xattrs_blob+0x12b/0x170 __send_cap+0x302/0x540 ? __lock_acquire+0x23c/0x1e40 ? __mark_caps_flushing+0x15c/0x280 ? _raw_spin_unlock+0x24/0x30 ceph_check_caps+0x5f0/0xc60 ceph_flush_dirty_caps+0x7c/0x150 ? __ia32_sys_fdatasync+0x20/0x20 ceph_sync_fs+0x5a/0x130 iterate_supers+0x8f/0xf0 ksys_sync+0x4f/0xb0 __ia32_sys_sync+0xa/0x10 do_syscall_64+0x50/0x1c0 entry_SYSCALL_64_after_hwframe+0x49/0xbe RIP: 0033:0x7fc6409ab617 Signed-off-by: Luis Henriques <[email protected]> Reviewed-by: Jeff Layton <[email protected]> Signed-off-by: Ilya Dryomov <[email protected]>
1 parent 86968ef commit 12fe3dd

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

fs/ceph/caps.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,7 @@ static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
13011301
{
13021302
struct ceph_inode_info *ci = cap->ci;
13031303
struct inode *inode = &ci->vfs_inode;
1304+
struct ceph_buffer *old_blob = NULL;
13041305
struct cap_msg_args arg;
13051306
int held, revoking;
13061307
int wake = 0;
@@ -1365,7 +1366,7 @@ static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
13651366
ci->i_requested_max_size = arg.max_size;
13661367

13671368
if (flushing & CEPH_CAP_XATTR_EXCL) {
1368-
__ceph_build_xattrs_blob(ci);
1369+
old_blob = __ceph_build_xattrs_blob(ci);
13691370
arg.xattr_version = ci->i_xattrs.version;
13701371
arg.xattr_buf = ci->i_xattrs.blob;
13711372
} else {
@@ -1409,6 +1410,8 @@ static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
14091410

14101411
spin_unlock(&ci->i_ceph_lock);
14111412

1413+
ceph_buffer_put(old_blob);
1414+
14121415
ret = send_cap_msg(&arg);
14131416
if (ret < 0) {
14141417
dout("error sending cap msg, must requeue %p\n", inode);

fs/ceph/snap.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,7 @@ void ceph_queue_cap_snap(struct ceph_inode_info *ci)
465465
struct inode *inode = &ci->vfs_inode;
466466
struct ceph_cap_snap *capsnap;
467467
struct ceph_snap_context *old_snapc, *new_snapc;
468+
struct ceph_buffer *old_blob = NULL;
468469
int used, dirty;
469470

470471
capsnap = kzalloc(sizeof(*capsnap), GFP_NOFS);
@@ -541,7 +542,7 @@ void ceph_queue_cap_snap(struct ceph_inode_info *ci)
541542
capsnap->gid = inode->i_gid;
542543

543544
if (dirty & CEPH_CAP_XATTR_EXCL) {
544-
__ceph_build_xattrs_blob(ci);
545+
old_blob = __ceph_build_xattrs_blob(ci);
545546
capsnap->xattr_blob =
546547
ceph_buffer_get(ci->i_xattrs.blob);
547548
capsnap->xattr_version = ci->i_xattrs.version;
@@ -584,6 +585,7 @@ void ceph_queue_cap_snap(struct ceph_inode_info *ci)
584585
}
585586
spin_unlock(&ci->i_ceph_lock);
586587

588+
ceph_buffer_put(old_blob);
587589
kfree(capsnap);
588590
ceph_put_snap_context(old_snapc);
589591
}

fs/ceph/super.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ extern int ceph_getattr(const struct path *path, struct kstat *stat,
926926
int __ceph_setxattr(struct inode *, const char *, const void *, size_t, int);
927927
ssize_t __ceph_getxattr(struct inode *, const char *, void *, size_t);
928928
extern ssize_t ceph_listxattr(struct dentry *, char *, size_t);
929-
extern void __ceph_build_xattrs_blob(struct ceph_inode_info *ci);
929+
extern struct ceph_buffer *__ceph_build_xattrs_blob(struct ceph_inode_info *ci);
930930
extern void __ceph_destroy_xattrs(struct ceph_inode_info *ci);
931931
extern const struct xattr_handler *ceph_xattr_handlers[];
932932

fs/ceph/xattr.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -754,12 +754,15 @@ static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
754754

755755
/*
756756
* If there are dirty xattrs, reencode xattrs into the prealloc_blob
757-
* and swap into place.
757+
* and swap into place. It returns the old i_xattrs.blob (or NULL) so
758+
* that it can be freed by the caller as the i_ceph_lock is likely to be
759+
* held.
758760
*/
759-
void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
761+
struct ceph_buffer *__ceph_build_xattrs_blob(struct ceph_inode_info *ci)
760762
{
761763
struct rb_node *p;
762764
struct ceph_inode_xattr *xattr = NULL;
765+
struct ceph_buffer *old_blob = NULL;
763766
void *dest;
764767

765768
dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
@@ -790,12 +793,14 @@ void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
790793
dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
791794

792795
if (ci->i_xattrs.blob)
793-
ceph_buffer_put(ci->i_xattrs.blob);
796+
old_blob = ci->i_xattrs.blob;
794797
ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
795798
ci->i_xattrs.prealloc_blob = NULL;
796799
ci->i_xattrs.dirty = false;
797800
ci->i_xattrs.version++;
798801
}
802+
803+
return old_blob;
799804
}
800805

801806
static inline int __get_request_mask(struct inode *in) {

0 commit comments

Comments
 (0)