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: 10800333 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 90341746 for ; Thu, 7 Feb 2019 00:07:42 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7BD682D348 for ; Thu, 7 Feb 2019 00:07:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7069F2D62D; Thu, 7 Feb 2019 00:07:42 +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 295E22D348 for ; Thu, 7 Feb 2019 00:07:42 +0000 (UTC) Received: from pdx1-mailman02.dreamhost.com (localhost [IPv6:::1]) by pdx1-mailman02.dreamhost.com (Postfix) with ESMTP id EC3FE4C3D91; Wed, 6 Feb 2019 16:07:36 -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 D4BE84C3D5D for ; Wed, 6 Feb 2019 16:07:35 -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 1BF37ADE5; Thu, 7 Feb 2019 00:07:35 +0000 (UTC) From: NeilBrown To: Andreas Dilger , James Simmons , Oleg Drokin Date: Thu, 07 Feb 2019 11:03:33 +1100 Message-ID: <154949781342.10620.11880180011933935219.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 19/21] lustre: obdclass: avoid races in class_register_type() 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 If there are two parallel attempts to register the same class name, it could get registered twice. So re-check after allocation to make sure the name is still unique. Signed-off-by: NeilBrown --- drivers/staging/lustre/lustre/obdclass/genops.c | 29 +++++++++++++++-------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/drivers/staging/lustre/lustre/obdclass/genops.c b/drivers/staging/lustre/lustre/obdclass/genops.c index 382eaf519a79..a174f538dd0d 100644 --- a/drivers/staging/lustre/lustre/obdclass/genops.c +++ b/drivers/staging/lustre/lustre/obdclass/genops.c @@ -86,17 +86,23 @@ static void obd_device_free(struct obd_device *obd) kmem_cache_free(obd_device_cachep, obd); } -static struct obd_type *class_search_type(const char *name) +static struct obd_type *__class_search_type(const char *name) { struct obd_type *type; - spin_lock(&obd_types_lock); list_for_each_entry(type, &obd_types, typ_chain) { - if (strcmp(type->typ_name, name) == 0) { - spin_unlock(&obd_types_lock); + if (strcmp(type->typ_name, name) == 0) return type; - } } + return NULL; +} + +static struct obd_type *class_search_type(const char *name) +{ + struct obd_type *type; + + spin_lock(&obd_types_lock); + type = __class_search_type(name); spin_unlock(&obd_types_lock); return NULL; } @@ -214,19 +220,22 @@ int class_register_type(struct obd_ops *dt_ops, struct md_ops *md_ops, if (ldt) { type->typ_lu = ldt; rc = lu_device_type_init(ldt); - if (rc != 0) { - kobject_put(type->typ_kobj); + if (rc != 0) goto failed; - } } + INIT_LIST_HEAD(&type->typ_chain); spin_lock(&obd_types_lock); - list_add(&type->typ_chain, &obd_types); + if (__class_search_type(name) == NULL) + list_add(&type->typ_chain, &obd_types); spin_unlock(&obd_types_lock); - return 0; + if (!list_empty(&type->typ_chain)) + return 0; failed: + if (!IS_ERR_OR_NULL(type->typ_kobj)) + kobject_put(type->typ_kobj); kfree(type->typ_name); kfree(type->typ_md_ops); kfree(type->typ_dt_ops);