From patchwork Thu Feb 7 00:03:33 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: NeilBrown X-Patchwork-Id: 10800335 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8771B13B4 for ; Thu, 7 Feb 2019 00:07:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 743812D348 for ; Thu, 7 Feb 2019 00:07:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 68FE92D62D; Thu, 7 Feb 2019 00:07:44 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from pdx1-mailman02.dreamhost.com (pdx1-mailman02.dreamhost.com [64.90.62.194]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 0C90E2D348 for ; Thu, 7 Feb 2019 00:07:44 +0000 (UTC) Received: from pdx1-mailman02.dreamhost.com (localhost [IPv6:::1]) by pdx1-mailman02.dreamhost.com (Postfix) with ESMTP id BED334C3D77; Wed, 6 Feb 2019 16:07:43 -0800 (PST) X-Original-To: lustre-devel@lists.lustre.org Delivered-To: lustre-devel-lustre.org@pdx1-mailman02.dreamhost.com Received: from mx1.suse.de (mx2.suse.de [195.135.220.15]) by pdx1-mailman02.dreamhost.com (Postfix) with ESMTP id 2F95D4C35A7 for ; Wed, 6 Feb 2019 16:07:41 -0800 (PST) X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 6F7EAADE5; Thu, 7 Feb 2019 00:07:40 +0000 (UTC) From: NeilBrown To: Andreas Dilger , James Simmons , Oleg Drokin Date: Thu, 07 Feb 2019 11:03:33 +1100 Message-ID: <154949781345.10620.8278067573811049130.stgit@noble.brown> In-Reply-To: <154949776249.10620.1215070753973826063.stgit@noble.brown> References: <154949776249.10620.1215070753973826063.stgit@noble.brown> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Subject: [lustre-devel] [PATCH 20/21] lustre: obdclass: fix module load locking. X-BeenThere: lustre-devel@lists.lustre.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "For discussing Lustre software development." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Lustre Development List Errors-To: lustre-devel-bounces@lists.lustre.org Sender: "lustre-devel" X-Virus-Scanned: ClamAV using ClamSMTP Safe module loading requires that we try_module_get() in a context where the module cannot be unloaded, typically prodected by a spinlock that module-unload has to take. This doesn't currently happen in class_get_type(). There is a per-type spinlock, but it is almost entirely unused, and cannot be used to protect the module from being unloaded. So discard ->obd_type_lock, and ensure that __class_search_type() and try_module_get() are both called while obd_types_lock is held - this prevent class_unregister_type() from completing (so the 'type' won't get freed. That is always called from a module-unload function - if it has got that far, try_module_get() will fail. So also check the return status of try_module_get(). Signed-off-by: NeilBrown Reviewed-by: James Simmons --- drivers/staging/lustre/lustre/include/obd.h | 1 - drivers/staging/lustre/lustre/obdclass/genops.c | 24 ++++++++++++++--------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/drivers/staging/lustre/lustre/include/obd.h b/drivers/staging/lustre/lustre/include/obd.h index 171d2c214be6..463ab680b524 100644 --- a/drivers/staging/lustre/lustre/include/obd.h +++ b/drivers/staging/lustre/lustre/include/obd.h @@ -106,7 +106,6 @@ struct obd_type { char *typ_name; int typ_refcnt; struct lu_device_type *typ_lu; - spinlock_t obd_type_lock; struct kobject *typ_kobj; }; diff --git a/drivers/staging/lustre/lustre/obdclass/genops.c b/drivers/staging/lustre/lustre/obdclass/genops.c index a174f538dd0d..dad21d9fa328 100644 --- a/drivers/staging/lustre/lustre/obdclass/genops.c +++ b/drivers/staging/lustre/lustre/obdclass/genops.c @@ -109,35 +109,42 @@ static struct obd_type *class_search_type(const char *name) static struct obd_type *class_get_type(const char *name) { - struct obd_type *type = class_search_type(name); + struct obd_type *type; + + spin_lock(&obd_types_lock); + type = __class_search_type(name); if (!type) { const char *modname = name; + spin_unlock(&obd_types_lock); if (!request_module("%s", modname)) { CDEBUG(D_INFO, "Loaded module '%s'\n", modname); - type = class_search_type(name); } else { LCONSOLE_ERROR_MSG(0x158, "Can't load module '%s'\n", modname); } + spin_lock(&obd_types_lock); + type = __class_search_type(name); } if (type) { - spin_lock(&type->obd_type_lock); - type->typ_refcnt++; - try_module_get(type->typ_dt_ops->owner); - spin_unlock(&type->obd_type_lock); + if (try_module_get(type->typ_dt_ops->owner)) + type->typ_refcnt++; + else + type = NULL; } + spin_unlock(&obd_types_lock); return type; } void class_put_type(struct obd_type *type) { LASSERT(type); - spin_lock(&type->obd_type_lock); + spin_lock(&obd_types_lock); + LASSERT(type->typ_refcnt > 0); type->typ_refcnt--; module_put(type->typ_dt_ops->owner); - spin_unlock(&type->obd_type_lock); + spin_unlock(&obd_types_lock); } static void class_sysfs_release(struct kobject *kobj) @@ -206,7 +213,6 @@ int class_register_type(struct obd_ops *dt_ops, struct md_ops *md_ops, if (md_ops) *type->typ_md_ops = *md_ops; strcpy(type->typ_name, name); - spin_lock_init(&type->obd_type_lock); type->typ_debugfs_entry = debugfs_create_dir(type->typ_name, debugfs_lustre_root);