From patchwork Thu Sep 20 11:21:58 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leon Romanovsky X-Patchwork-Id: 10607469 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 15219157B for ; Thu, 20 Sep 2018 11:22:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 02C152D19E for ; Thu, 20 Sep 2018 11:22:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EAB282D1A6; Thu, 20 Sep 2018 11:22:12 +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=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8D7BB2D19E for ; Thu, 20 Sep 2018 11:22:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732592AbeITRFL (ORCPT ); Thu, 20 Sep 2018 13:05:11 -0400 Received: from mail.kernel.org ([198.145.29.99]:58548 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730984AbeITRFL (ORCPT ); Thu, 20 Sep 2018 13:05:11 -0400 Received: from localhost (unknown [193.47.165.251]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 48FFB2152F; Thu, 20 Sep 2018 11:22:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1537442530; bh=8t1tEpu+xwAlPV+XcidS+RgX3xmu7FwZ9h4YQU8N9oI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=q1G4sRF3N1Wm3pcfFpmGnTtzZ3+xfJBrhA2Hn9Md62L0xCX0783Hw/88VAHn2D7qg BuKhtmu9AlBF9tlPhOO7tnlreXdmZf0mp5fvY6wTjEvRLTBZRpFeJhot+GhDyusTLR RpYsv0oEIgPGdkMT1ZOkNySHymAJnySAT5zZoVWw= From: Leon Romanovsky To: Doug Ledford , Jason Gunthorpe Cc: Leon Romanovsky , RDMA mailing list , linux-s390@vger.kernel.org, Ursula Braun , "David S. Miller" , netdev@vger.kernel.org, Selvin Xavier , Steve Wise , Lijun Ou , Shiraz Saleem , Ariel Elior , Christian Benvenuti , Adit Ranadive , Dennis Dalessandro Subject: [PATCH rdma-next 1/5] RDMA/core: Provide getter and setter to access IB device name Date: Thu, 20 Sep 2018 14:21:58 +0300 Message-Id: <20180920112202.9181-2-leon@kernel.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180920112202.9181-1-leon@kernel.org> References: <20180920112202.9181-1-leon@kernel.org> Sender: linux-rdma-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Leon Romanovsky Prepare IB device name field to rename operation by ensuring that all accesses to it are protected with lock and users don't see part of name. The protection is done with global device_lock because it is used in allocation and deallocation phases. At this stage, this lock is not busy and easily can be moved to be per-device, once it will be needed. Signed-off-by: Leon Romanovsky --- drivers/infiniband/core/device.c | 24 +++++++++++++++++++++++- include/rdma/ib_verbs.h | 8 +++++++- 2 files changed, 30 insertions(+), 2 deletions(-) -- 2.14.4 diff --git a/drivers/infiniband/core/device.c b/drivers/infiniband/core/device.c index 5a680a88aa87..3270cde6d806 100644 --- a/drivers/infiniband/core/device.c +++ b/drivers/infiniband/core/device.c @@ -170,6 +170,14 @@ static struct ib_device *__ib_device_get_by_name(const char *name) return NULL; } +void ib_device_get_name(struct ib_device *ibdev, char *name) +{ + down_read(&lists_rwsem); + strlcpy(name, ibdev->name, IB_DEVICE_NAME_MAX); + up_read(&lists_rwsem); +} +EXPORT_SYMBOL(ib_device_get_name); + static int alloc_name(char *name) { unsigned long *inuse; @@ -202,6 +210,21 @@ static int alloc_name(char *name) return 0; } +int ib_device_alloc_name(struct ib_device *ibdev, const char *pattern) +{ + int ret = 0; + + mutex_lock(&device_mutex); + strlcpy(ibdev->name, pattern, IB_DEVICE_NAME_MAX); + if (strchr(ibdev->name, '%')) + ret = alloc_name(ibdev->name); + + mutex_unlock(&device_mutex); + + return ret; +} +EXPORT_SYMBOL(ib_device_alloc_name); + static void ib_device_release(struct device *device) { struct ib_device *dev = container_of(device, struct ib_device, dev); @@ -499,7 +522,6 @@ int ib_register_device(struct ib_device *device, ret = alloc_name(device->name); if (ret) goto out; - } if (ib_device_check_mandatory(device)) { ret = -EINVAL; diff --git a/include/rdma/ib_verbs.h b/include/rdma/ib_verbs.h index e764ed1f6025..66660e7b9854 100644 --- a/include/rdma/ib_verbs.h +++ b/include/rdma/ib_verbs.h @@ -2260,6 +2260,11 @@ struct ib_device { /* Do not access @dma_device directly from ULP nor from HW drivers. */ struct device *dma_device; + /* + * Do not access @name directly, + * use ib_device_get_name()/ib_device_alloc_name() + * and don't assume that it can't change after access. + */ char name[IB_DEVICE_NAME_MAX]; struct list_head event_handler_list; @@ -2638,7 +2643,8 @@ struct ib_device *ib_alloc_device(size_t size); void ib_dealloc_device(struct ib_device *device); void ib_get_device_fw_str(struct ib_device *device, char *str); - +int ib_device_alloc_name(struct ib_device *ibdev, const char *pattern); +void ib_device_get_name(struct ib_device *ibdev, char *name); int ib_register_device(struct ib_device *device, int (*port_callback)(struct ib_device *, u8, struct kobject *));