From patchwork Sat Jan 12 02:31:23 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 10760873 X-Patchwork-Delegate: jgg@ziepe.ca 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 7706791E for ; Sat, 12 Jan 2019 02:31:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 68C0F2A0EE for ; Sat, 12 Jan 2019 02:31:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5D1672A134; Sat, 12 Jan 2019 02:31:38 +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 DF1102A136 for ; Sat, 12 Jan 2019 02:31:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726510AbfALCbh (ORCPT ); Fri, 11 Jan 2019 21:31:37 -0500 Received: from mail-pl1-f196.google.com ([209.85.214.196]:36756 "EHLO mail-pl1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726475AbfALCbh (ORCPT ); Fri, 11 Jan 2019 21:31:37 -0500 Received: by mail-pl1-f196.google.com with SMTP id g9so7575088plo.3 for ; Fri, 11 Jan 2019 18:31:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=5A5ZGVUSGiACercxL3tv37T8y+xlAQ71pu1GHtg85RE=; b=GROT3Cz789+GnJWBFMUcLV87X8g7c8itFD7/EWeA/faeWgRmx7hpGKiusNnw3JEJay AAcHexaCb2LqMEBItfHWUrRTeiWJzZiwV+vL5gF9kgMPbrCSmfoLJ2UQqjfJP5CXVRnR bc1gc2lw/rair3Wgy+k/SgXPEQWqjFMZZFjU7SbLmDXoRjVmPcoqaCesVLZjIBtEKrMc cW7fCIOVycfL7KFbkudmSKKWIseUif964nP3ichooT461tA008nM5hzmd5lDCqMBQrAa 72xzczhfAUz6qX9/AGR+A977vbjfpDxcdAeuHbgSae4/pl/dvkCIv7zaV4wDFdTVHYGA /4sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=5A5ZGVUSGiACercxL3tv37T8y+xlAQ71pu1GHtg85RE=; b=UtiZIuAJREiPgSbYFIoXH4DPBjfGqddFJP6DtkSuHPDK8Svi9vz0ogfJw900Mzb6xY khFmyCYjxtAcB+nbmSAZ0KWIoc+Bb0HIApgXvanwo6nsfTp9p4Dtl1NlEOOXlWfiJlz/ eKiwBZLcEouKyFGdzMclgF8+9iYoyetMxZ2qjVeB7vJHTCyktpv2nZw1HBno9/pEFOyg TSga1Mb7XlRihw6ms0uvs50dIwg8vAQton+3T27UL+3t3b8+6v1WkKfMv20vMpgo3Mua +UZbEalMulNDwmHnZCqz3bY8iFOmOyVeiYHHrkMTsrRH/5FDHdm4ACQuukAXQM5zBJNr ejrA== X-Gm-Message-State: AJcUukepFoUSbTlZNoPMCk4vuaG93GYVNHHp7mEpQYxA1+9qlKG0MYEK a8ORabj4Ws95NxRzMIDItHuxKB+QLnc= X-Google-Smtp-Source: ALg8bN4KtN4/xqJ9BheO48FL+iGAA7y5xQsK5m6HYYThNUrXzXAgtISXULSmP3zlN7kf1urhMqjvMA== X-Received: by 2002:a17:902:6b49:: with SMTP id g9mr16699431plt.98.1547260295575; Fri, 11 Jan 2019 18:31:35 -0800 (PST) Received: from ziepe.ca (S010614cc2056d97f.ed.shawcable.net. [174.3.196.123]) by smtp.gmail.com with ESMTPSA id 125sm141522024pfx.159.2019.01.11.18.31.33 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 11 Jan 2019 18:31:33 -0800 (PST) Received: from jgg by mlx.ziepe.ca with local (Exim 4.90_1) (envelope-from ) id 1gi95A-0006NA-VQ; Fri, 11 Jan 2019 19:31:32 -0700 From: Jason Gunthorpe To: linux-rdma@vger.kernel.org, Moni Shoua Cc: Jason Gunthorpe Subject: [PATCH for-rc 1/2] RDMA/device: Expose ib_device_try_get(() Date: Fri, 11 Jan 2019 19:31:23 -0700 Message-Id: <20190112023124.21647-2-jgg@ziepe.ca> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190112023124.21647-1-jgg@ziepe.ca> References: <20190112023124.21647-1-jgg@ziepe.ca> MIME-Version: 1.0 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: Jason Gunthorpe It turns out future patches need this capability quite widely now, not just for netlink, so provide two global functions to manage the registration lock refcount. This also moves the point the lock becomes 1 to within ib_register_device() so that the semantics of the public API are very sane and clear. Calling ib_device_try_get() will fail on devices that are only allocated but not yet registered. Signed-off-by: Jason Gunthorpe Signed-off-by: Steve Wise Reviewed-by: Parav Pandit --- drivers/infiniband/core/core_priv.h | 1 - drivers/infiniband/core/device.c | 6 +++--- include/rdma/ib_verbs.h | 24 ++++++++++++++++++++++-- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/drivers/infiniband/core/core_priv.h b/drivers/infiniband/core/core_priv.h index 3cd830d52967eb..616734313f0c69 100644 --- a/drivers/infiniband/core/core_priv.h +++ b/drivers/infiniband/core/core_priv.h @@ -267,7 +267,6 @@ static inline int ib_mad_enforce_security(struct ib_mad_agent_private *map, #endif struct ib_device *ib_device_get_by_index(u32 ifindex); -void ib_device_put(struct ib_device *device); /* RDMA device netlink */ void nldev_init(void); void nldev_exit(void); diff --git a/drivers/infiniband/core/device.c b/drivers/infiniband/core/device.c index 8872453e26c07c..9b5c72d3c85a88 100644 --- a/drivers/infiniband/core/device.c +++ b/drivers/infiniband/core/device.c @@ -156,8 +156,7 @@ struct ib_device *ib_device_get_by_index(u32 index) down_read(&lists_rwsem); device = __ib_device_get_by_index(index); if (device) { - /* Do not return a device if unregistration has started. */ - if (!refcount_inc_not_zero(&device->refcount)) + if (!ib_device_try_get(device)) device = NULL; } up_read(&lists_rwsem); @@ -169,6 +168,7 @@ void ib_device_put(struct ib_device *device) if (refcount_dec_and_test(&device->refcount)) complete(&device->unreg_completion); } +EXPORT_SYMBOL(ib_device_put); static struct ib_device *__ib_device_get_by_name(const char *name) { @@ -303,7 +303,6 @@ struct ib_device *ib_alloc_device(size_t size) rwlock_init(&device->client_data_lock); INIT_LIST_HEAD(&device->client_data_list); INIT_LIST_HEAD(&device->port_list); - refcount_set(&device->refcount, 1); init_completion(&device->unreg_completion); return device; @@ -620,6 +619,7 @@ int ib_register_device(struct ib_device *device, const char *name, goto cg_cleanup; } + refcount_set(&device->refcount, 1); device->reg_state = IB_DEV_REGISTERED; list_for_each_entry(client, &client_list, list) diff --git a/include/rdma/ib_verbs.h b/include/rdma/ib_verbs.h index aa1f126d338350..85e9dab17b9b92 100644 --- a/include/rdma/ib_verbs.h +++ b/include/rdma/ib_verbs.h @@ -2577,9 +2577,10 @@ struct ib_device { const struct uapi_definition *driver_def; enum rdma_driver_id driver_id; + /* - * Provides synchronization between device unregistration and netlink - * commands on a device. To be used only by core. + * If positive refcount indicates that the device is currently + * registered and cannot be unregistered. */ refcount_t refcount; struct completion unreg_completion; @@ -3924,6 +3925,25 @@ static inline bool ib_access_writable(int access_flags) int ib_check_mr_status(struct ib_mr *mr, u32 check_mask, struct ib_mr_status *mr_status); +/** + * ib_device_try_get: Hold a registration lock + * device: The device to lock + * + * A device under an active registration lock cannot become unregistered. It + * is only possible to obtain a registration lock on a device that is fully + * registered, otherwise this function returns false. + * + * The registration lock is only necessary for actions which require the + * device to still be registered. Uses that only require the device pointer to + * be valid should use get_device(&ibdev->dev) to hold the memory. + * + */ +static inline bool ib_device_try_get(struct ib_device *dev) +{ + return refcount_inc_not_zero(&dev->refcount); +} + +void ib_device_put(struct ib_device *device); struct net_device *ib_get_net_dev_by_params(struct ib_device *dev, u8 port, u16 pkey, const union ib_gid *gid, const struct sockaddr *addr); From patchwork Sat Jan 12 02:31:24 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 10760871 X-Patchwork-Delegate: jgg@ziepe.ca 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 0D55A1869 for ; Sat, 12 Jan 2019 02:31:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F2AAC2A0EE for ; Sat, 12 Jan 2019 02:31:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E65162A138; Sat, 12 Jan 2019 02:31:37 +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 8F24D2A0EE for ; Sat, 12 Jan 2019 02:31:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726509AbfALCbg (ORCPT ); Fri, 11 Jan 2019 21:31:36 -0500 Received: from mail-pl1-f194.google.com ([209.85.214.194]:32787 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726433AbfALCbg (ORCPT ); Fri, 11 Jan 2019 21:31:36 -0500 Received: by mail-pl1-f194.google.com with SMTP id z23so7595563plo.0 for ; Fri, 11 Jan 2019 18:31:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=MBhsHDl3upbG5mylYHP2a8ceMQtU1CZGZn+5VM/LlVA=; b=iiM7BpYQnxdNoWoCtTQ5BJzSda1datMDb5w5UKs9xkFQT1IXBmtuq3B5PNCU2TvAfq JlD7VxRb8fkjhTsXizTEGxHQrnBrWqDuOl27Gjha2OqG4DRUZHi0l3NXsrzLRraAcNHG e8cklbqOgAnmWv1bA+rNYrlCJQ5KyzH8tpFGs89/IWMZgB27PRLLwz9qw1uNnPFMnmzN j5/ReNd3VhtPD4zN8503n2mZHsv3WlWI5UvbcJQlWJl0ygE4l0E+7vRpLsYR6hYOlRxi AD6I4ruAMOpS9SQSFNeHyaRrHBilkv2m8crQPbAdmMKSXojG9X1NMISlVc0s97yiBnZM r3/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=MBhsHDl3upbG5mylYHP2a8ceMQtU1CZGZn+5VM/LlVA=; b=bgAO5I5UI4u+Scqhr0fKtATjYuO/a5nK89C02rWO4O72r19mAJRH/SO9FDg+bl1sqG YhGkpuWsEsv8UFl2sO3kfpgfPsGiAecQDRdRMQHPze9M6RI+9JBoiYiL/euGKG7yn2w7 KWq1/gJIEVD2hjqTZhhYhwsmnj8r32TQVexQ5Y0mKb2vC2VJ2UYGTeQxho40qkVsOZj2 FZ0rgf58vsOZVJlnIRo69+t95wC6epwmI/z5JNwd7TdPlRmIHtgP3J0Vr3PINARs607v sG0lQ+fCp0MgdzTiIq60xA/MGvk1H+umfMA5+3pWFi76geDnTwLu31C7GKGe1tceyUEa RRag== X-Gm-Message-State: AJcUukch68rhy+u7WFWXydLJKnpOw05dwOCoz2nzvMYc+kCTXCfIlA6L DzWva+4nPbMss1VO003YKHmPVsHAQVY= X-Google-Smtp-Source: ALg8bN6BrYYcX4GaLuaEPRoWoLBAA60nLBMoywDDRqogLey+qWEppuqT7t6tmobcZ3eLYRQs5hZuZg== X-Received: by 2002:a17:902:bd4a:: with SMTP id b10mr17104231plx.232.1547260294830; Fri, 11 Jan 2019 18:31:34 -0800 (PST) Received: from ziepe.ca (S010614cc2056d97f.ed.shawcable.net. [174.3.196.123]) by smtp.gmail.com with ESMTPSA id e128sm116177039pfe.67.2019.01.11.18.31.33 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 11 Jan 2019 18:31:33 -0800 (PST) Received: from jgg by mlx.ziepe.ca with local (Exim 4.90_1) (envelope-from ) id 1gi95B-0006NG-0G; Fri, 11 Jan 2019 19:31:33 -0700 From: Jason Gunthorpe To: linux-rdma@vger.kernel.org, Moni Shoua Cc: Jason Gunthorpe Subject: [PATCH for-rc 2/2] IB/mlx5: Fix how advise_mr() launches async work Date: Fri, 11 Jan 2019 19:31:24 -0700 Message-Id: <20190112023124.21647-3-jgg@ziepe.ca> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190112023124.21647-1-jgg@ziepe.ca> References: <20190112023124.21647-1-jgg@ziepe.ca> MIME-Version: 1.0 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: Jason Gunthorpe Work must hold a kref on the ib_device otherwise the dev pointer can become free before the work runs. Remove the bogus use of 'reg_state': - While in uverbs the reg_state is guaranteed to always be REGISTERED - Testing reg_state with no locking is bogus. Use ib_device_try_get() to get back into a region that prevents unregistration. Fixes: 813e90b1aeaa ("IB/mlx5: Add advise_mr() support") Signed-off-by: Jason Gunthorpe Reviewed-by: Moni Shoua --- drivers/infiniband/hw/mlx5/odp.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/drivers/infiniband/hw/mlx5/odp.c b/drivers/infiniband/hw/mlx5/odp.c index 8d46b1dc56580f..a241a65834d4ca 100644 --- a/drivers/infiniband/hw/mlx5/odp.c +++ b/drivers/infiniband/hw/mlx5/odp.c @@ -1595,10 +1595,12 @@ static void mlx5_ib_prefetch_mr_work(struct work_struct *work) struct prefetch_mr_work *w = container_of(work, struct prefetch_mr_work, work); - if (w->dev->ib_dev.reg_state == IB_DEV_REGISTERED) + if (ib_device_try_get(&w->dev->ib_dev)) { mlx5_ib_prefetch_sg_list(w->dev, w->pf_flags, w->sg_list, w->num_sge); - + ib_device_put(&w->dev->ib_dev); + } + put_device(&w->dev->ib_dev.dev); kfree(w); } @@ -1617,15 +1619,13 @@ int mlx5_ib_advise_mr_prefetch(struct ib_pd *pd, return mlx5_ib_prefetch_sg_list(dev, pf_flags, sg_list, num_sge); - if (dev->ib_dev.reg_state != IB_DEV_REGISTERED) - return -ENODEV; - work = kvzalloc(struct_size(work, sg_list, num_sge), GFP_KERNEL); if (!work) return -ENOMEM; memcpy(work->sg_list, sg_list, num_sge * sizeof(struct ib_sge)); + get_device(&dev->ib_dev.dev); work->dev = dev; work->pf_flags = pf_flags; work->num_sge = num_sge;