From: Fan Yong Date: Mon, 5 Mar 2018 11:35:02 +0000 (+0800) Subject: LU-10769 osd-zfs: fix deadlock on osd_object::oo_guard X-Git-Tag: 2.10.59~15 X-Git-Url: https://git.whamcloud.com/?p=fs%2Flustre-release.git;a=commitdiff_plain;h=095643bbd1bb7c70061eaddf7b6413c429322b31 LU-10769 osd-zfs: fix deadlock on osd_object::oo_guard There is race condition inside osd-zfs, it may cause deadlock. Consider the following scenarios: 1) The Thread1 calls osd_attr_set() to set flags on the object. The osd_attr_set() will call the osd_xattr_get() with holding the read mode semaphore on the object::oo_guard. 2) The Thread2 calls the osd_declare_destroy() to destroy such object, it will down_write() on the object::oo_gurad, but be blocked by the Thread1's granted read mode semaphore. 3) The osd_xattr_get() triggered by the osd_xattr_set() will also down_read() on the object::oo_guard. But it will be blocked by the Thread2's pending down_write() request. Then the Thread1 and the Thread2 deadlock. This patch makes the osd_attr_set() to call the lockless version xattr_get osd_xattr_get_internal() to avoid such deadlock. Signed-off-by: Fan Yong Change-Id: Iaac2e414b5f1fd197303bb7ec7d5e2763b6f3e9a Reviewed-on: https://review.whamcloud.com/31511 Reviewed-by: Andreas Dilger Reviewed-by: Nathaniel Clark Tested-by: Jenkins Tested-by: Maloo --- diff --git a/lustre/osd-zfs/osd_internal.h b/lustre/osd-zfs/osd_internal.h index 5c72d58..4877600 100644 --- a/lustre/osd-zfs/osd_internal.h +++ b/lustre/osd-zfs/osd_internal.h @@ -609,7 +609,6 @@ int __osd_attr_init(const struct lu_env *env, struct osd_device *osd, struct lu_attr *la, uint64_t parent, nvlist_t *); int osd_find_new_dnode(const struct lu_env *env, dmu_tx_t *tx, uint64_t oid, dnode_t **dnp); -int osd_object_init0(const struct lu_env *env, struct osd_object *obj); /* osd_oi.c */ int osd_oi_init(const struct lu_env *env, struct osd_device *o); diff --git a/lustre/osd-zfs/osd_object.c b/lustre/osd-zfs/osd_object.c index c835e92..0734da2 100644 --- a/lustre/osd-zfs/osd_object.c +++ b/lustre/osd-zfs/osd_object.c @@ -186,8 +186,8 @@ osd_object_sa_bulk_update(struct osd_object *obj, sa_bulk_attr_t *attrs, /* * Retrieve the attributes of a DMU object */ -int __osd_object_attr_get(const struct lu_env *env, struct osd_device *o, - struct osd_object *obj, struct lu_attr *la) +static int __osd_object_attr_get(const struct lu_env *env, struct osd_device *o, + struct osd_object *obj, struct lu_attr *la) { struct osa_attr *osa = &osd_oti_get(env)->oti_osa; sa_bulk_attr_t *bulk = osd_oti_get(env)->oti_attr_bulk; @@ -406,7 +406,7 @@ out: /* * Concurrency: shouldn't matter. */ -int osd_object_init0(const struct lu_env *env, struct osd_object *obj) +static int osd_object_init0(const struct lu_env *env, struct osd_object *obj) { struct osd_device *osd = osd_obj2dev(obj); const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu); @@ -1264,9 +1264,9 @@ static int osd_attr_set(const struct lu_env *env, struct dt_object *dt, rc = -zap_update(osd->od_os, zapid, buf, 8, sizeof(*zde) / 8, zde, oh->ot_tx); } - up_read(&obj->oo_guard); - - RETURN(rc > 0 ? 0 : rc); + if (rc > 0) + rc = 0; + GOTO(out, rc); } /* Only allow set size for regular file */ @@ -1288,6 +1288,7 @@ static int osd_attr_set(const struct lu_env *env, struct dt_object *dt, if (valid & LA_FLAGS) { struct lustre_mdt_attrs *lma; struct lu_buf buf; + int size = 0; if (la->la_flags & LUSTRE_LMA_FL_MASKS) { LASSERT(!obj->oo_pfid_in_lma); @@ -1295,9 +1296,14 @@ static int osd_attr_set(const struct lu_env *env, struct dt_object *dt, lma = (struct lustre_mdt_attrs *)&info->oti_buf; buf.lb_buf = lma; buf.lb_len = sizeof(info->oti_buf); - rc = osd_xattr_get(env, &obj->oo_dt, &buf, - XATTR_NAME_LMA); - if (rc > 0) { + + /* Please do NOT call osd_xattr_get() directly, that + * will cause recursive down_read() on oo_guard. */ + rc = osd_xattr_get_internal(env, obj, &buf, + XATTR_NAME_LMA, &size); + if (!rc && unlikely(size < sizeof(*lma))) { + rc = -EINVAL; + } else if (!rc) { lma->lma_incompat = le32_to_cpu(lma->lma_incompat); lma->lma_incompat |=