Message ID | 20231124162807.238724-2-marpagan@redhat.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce62:0:b0:403:3b70:6f57 with SMTP id o2csp1347686vqx; Fri, 24 Nov 2023 08:29:22 -0800 (PST) X-Google-Smtp-Source: AGHT+IHbY4jDg49InHvNMgtcfoVXD/BS9hpWs+mnGygMnWfyM0D+sLk1o7Esv56LVVSJuBWiEANO X-Received: by 2002:a17:90b:1e51:b0:27d:4ede:75b0 with SMTP id pi17-20020a17090b1e5100b0027d4ede75b0mr3992532pjb.16.1700843362282; Fri, 24 Nov 2023 08:29:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700843362; cv=none; d=google.com; s=arc-20160816; b=ueJiQOeLPTUKZIAp7mbHRK+jQKnLyhHOr7O1+6GMGjfjB9XwwDRrdC3cM90g5vZmCK QzDpNB6icwNRZU/v0yPjBBOncshPW7UJ2atuJjFqlT367iQvDXt3cwfwKJai7z6fFn31 Qoy3xwL7ZFPet7IaT/41TOWsxATH/N26vcT3fFq72ymULmr3z+pvmdAlKgH93MbqGV/k OQxzQ9yZk5L/YM5vq6KfmESMI6QTMcCkxJh774jQcxVLHG7TN6TC5tHGfME3m/PX7802 vP2OqmhCowowOEJDy25EJ0bDJfBPJzIBb1XpXgdle1prGL1VWij8f3ByLnx1PrsPO3u8 O8zA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=g//rHl/LxpwQpXoYOYg3bTjSPJ3DCs9GAcBjIJBfkM0=; fh=RI0mNHrSGBk/0gJ3tsBPdYn+81ROXQTqubQc1unKFUU=; b=U0Z4KT8O/xBzJcQKOQdZIqcut4D/P5aIHEuYHDWhoFu4gJUNQQGnOGfmbMiSMjFVVJ l7khZDl1k1I2DHsF6UtNtV6TCCavWI0RSjZQ14JKsqc9W4MUJdZCWm9FUtp1EDhwWqyy +Wa/1jZz/PwYUdMJQimQhB/UAcktyQt5jkOXkUUIYztFTTcQpW4i1m5tg8DqLXAGsjRe mukCoZ+vXe7HZPZ6PV7c69fheKv98SbbXCTxLTw+VX68aimhnnd1t4qePlp0gdneIOAI shyc+VKT/uKVFZLFdZ7mYLUC5WFodLdh5AvSU3cSsFCKa5cGGjCBsK6jXEDSuegK9C5j PxMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=YtaAeyYz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id h3-20020a17090a130300b002806cdeecc6si4414733pja.35.2023.11.24.08.29.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Nov 2023 08:29:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=YtaAeyYz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 9A1C080C0347; Fri, 24 Nov 2023 08:28:48 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231192AbjKXQ2b (ORCPT <rfc822;ouuuleilei@gmail.com> + 99 others); Fri, 24 Nov 2023 11:28:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44766 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229715AbjKXQ23 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 24 Nov 2023 11:28:29 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 196E6D41 for <linux-kernel@vger.kernel.org>; Fri, 24 Nov 2023 08:28:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700843315; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=g//rHl/LxpwQpXoYOYg3bTjSPJ3DCs9GAcBjIJBfkM0=; b=YtaAeyYzEveB5AO62HFun3cRg8YNwl7kTuJRwYDq9vtjAuNbddy22LquGxu+Te4Sh3L+dh WAS0ElKrYuw5edQdqjx4DBwSGvHoiNOib8OiCL813kGUa0wRJsMLejKL34MiV0IiIXiBL9 VBBMRXC/s2XXf3vODsNbAJpR+df6/Mk= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-256-yR1BhhoiMjCB5r_REKE94g-1; Fri, 24 Nov 2023 11:28:34 -0500 X-MC-Unique: yR1BhhoiMjCB5r_REKE94g-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-778915a0c73so185527385a.1 for <linux-kernel@vger.kernel.org>; Fri, 24 Nov 2023 08:28:34 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700843314; x=1701448114; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=g//rHl/LxpwQpXoYOYg3bTjSPJ3DCs9GAcBjIJBfkM0=; b=JLjG/YDNtJHls4ZxeSP8+8cIdqaTPXkrrjA0Vyp3oB/u5kSwXd2K+rmcKtN10ExT13 MJTvwJxgiKq1kjXBpZ1dF0Z8tWeTdMghFc7zYUSDlVvQ2rkZ/ODniYuKhlNh8F5o2O2s tJe31y1Bh1/VzPYf21tajfdsy9nJraZmgV7o4PTfuw8hMpENBjxih7coX5SJndsteZRa KPzumU3peC8wetd4vATqObWueE2NE8IsqGf/833E/NgHMMJ6M0eVV7C6B4QA5tRaW70m WZ3qgpQ6oXib8SXfB2UO5EqyaQiCatZoSmcgHCnyC05YpMCTXimrdoHTkK006WM35atK AfGQ== X-Gm-Message-State: AOJu0YwzSMDO9HG5/JXU9ofYlSxwZ3j7ZmyjLAaRdIfthJJuNqWtFOvZ znPYp43OfpsRUxoAwdTXfEkZwF63yBilqxOUfJSYavFWQlZrN5WW18CaNsYm1k3Yu1T+rOEfFpb dL0SMqsWp6lg1HBkhFtCwwms= X-Received: by 2002:a05:620a:4711:b0:77d:855c:a6e6 with SMTP id bs17-20020a05620a471100b0077d855ca6e6mr1495287qkb.71.1700843313781; Fri, 24 Nov 2023 08:28:33 -0800 (PST) X-Received: by 2002:a05:620a:4711:b0:77d:855c:a6e6 with SMTP id bs17-20020a05620a471100b0077d855ca6e6mr1495254qkb.71.1700843313472; Fri, 24 Nov 2023 08:28:33 -0800 (PST) Received: from klayman.redhat.com (net-2-34-24-178.cust.vodafonedsl.it. [2.34.24.178]) by smtp.gmail.com with ESMTPSA id rk4-20020a05620a900400b007676f3859fasm1327608qkn.30.2023.11.24.08.28.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Nov 2023 08:28:33 -0800 (PST) From: Marco Pagani <marpagan@redhat.com> To: Moritz Fischer <mdf@kernel.org>, Wu Hao <hao.wu@intel.com>, Xu Yilun <yilun.xu@intel.com>, Tom Rix <trix@redhat.com>, Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: Marco Pagani <marpagan@redhat.com>, linux-kernel@vger.kernel.org, linux-fpga@vger.kernel.org Subject: [RFC PATCH v2 1/2] fpga: add a module owner field to fpga_manager and fpga_manager_ops Date: Fri, 24 Nov 2023 17:28:06 +0100 Message-ID: <20231124162807.238724-2-marpagan@redhat.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231124162807.238724-1-marpagan@redhat.com> References: <20231124162807.238724-1-marpagan@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Fri, 24 Nov 2023 08:28:48 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783463529033587736 X-GMAIL-MSGID: 1783463529033587736 |
Series |
fpga: improve protection against low-level modules unloading
|
|
Commit Message
Marco Pagani
Nov. 24, 2023, 4:28 p.m. UTC
Add a module *owner field to the fpga_manager_ops and fpga_manager
structs to protect the fpga manager against the unloading of the
low-level control module while someone is holding a reference to the
manager device. Low-level control modules should statically set the
owner field of the fpga_manager_ops struct to THIS_MODULE. Then, when
the manager is registered using fpga_mgr_register(), the value is copied
into the owner field of the fpga_manager struct (that contains the
device context). In this way, the manager can later use it in
fpga_mgr_get() to take the low-level module's refcount. To prevent races
while unloading the low-level control module, fpga_mgr_get() and part of
the fpga_mgr_unregister() methods are protected with a mutex.
Other changes: move put_device() from __fpga_mgr_get() to fpga_mgr_get()
and of_fpga_mgr_get() to improve code clarity.
Fixes: 654ba4cc0f3e ("fpga manager: ensure lifetime with of_fpga_mgr_get")
Signed-off-by: Marco Pagani <marpagan@redhat.com>
---
drivers/fpga/fpga-mgr.c | 56 +++++++++++++++++++++++++----------
include/linux/fpga/fpga-mgr.h | 4 +++
2 files changed, 44 insertions(+), 16 deletions(-)
Comments
On Fri, Nov 24, 2023 at 05:28:06PM +0100, Marco Pagani wrote: > Add a module *owner field to the fpga_manager_ops and fpga_manager > structs to protect the fpga manager against the unloading of the > low-level control module while someone is holding a reference to the > manager device. Low-level control modules should statically set the > owner field of the fpga_manager_ops struct to THIS_MODULE. Then, when > the manager is registered using fpga_mgr_register(), the value is copied > into the owner field of the fpga_manager struct (that contains the > device context). In this way, the manager can later use it in > fpga_mgr_get() to take the low-level module's refcount. To prevent races > while unloading the low-level control module, fpga_mgr_get() and part of > the fpga_mgr_unregister() methods are protected with a mutex. > > Other changes: move put_device() from __fpga_mgr_get() to fpga_mgr_get() > and of_fpga_mgr_get() to improve code clarity. > > Fixes: 654ba4cc0f3e ("fpga manager: ensure lifetime with of_fpga_mgr_get") > Signed-off-by: Marco Pagani <marpagan@redhat.com> > --- > drivers/fpga/fpga-mgr.c | 56 +++++++++++++++++++++++++---------- > include/linux/fpga/fpga-mgr.h | 4 +++ > 2 files changed, 44 insertions(+), 16 deletions(-) > > diff --git a/drivers/fpga/fpga-mgr.c b/drivers/fpga/fpga-mgr.c > index 06651389c592..608605d59860 100644 > --- a/drivers/fpga/fpga-mgr.c > +++ b/drivers/fpga/fpga-mgr.c > @@ -21,6 +21,8 @@ > static DEFINE_IDA(fpga_mgr_ida); > static const struct class fpga_mgr_class; > > +static DEFINE_MUTEX(mgr_lock); > + > struct fpga_mgr_devres { > struct fpga_manager *mgr; > }; > @@ -667,17 +669,15 @@ ATTRIBUTE_GROUPS(fpga_mgr); > static struct fpga_manager *__fpga_mgr_get(struct device *dev) > { > struct fpga_manager *mgr; > + struct module *owner; > > mgr = to_fpga_manager(dev); > + owner = mgr->owner; > > - if (!try_module_get(dev->parent->driver->owner)) > - goto err_dev; > + if (owner && !try_module_get(owner)) No need to test owner == NULL, try_module_get() does this. > + mgr = ERR_PTR(-ENODEV); > > return mgr; > - > -err_dev: > - put_device(dev); > - return ERR_PTR(-ENODEV); > } > > static int fpga_mgr_dev_match(struct device *dev, const void *data) > @@ -693,12 +693,22 @@ static int fpga_mgr_dev_match(struct device *dev, const void *data) > */ > struct fpga_manager *fpga_mgr_get(struct device *dev) > { > - struct device *mgr_dev = class_find_device(&fpga_mgr_class, NULL, dev, > - fpga_mgr_dev_match); > + struct fpga_manager *mgr = ERR_PTR(-ENODEV); > + struct device *mgr_dev; > + > + mutex_lock(&mgr_lock); > + > + mgr_dev = class_find_device(&fpga_mgr_class, NULL, dev, fpga_mgr_dev_match); > if (!mgr_dev) > - return ERR_PTR(-ENODEV); > + goto out; > + > + mgr = __fpga_mgr_get(mgr_dev); > + if (IS_ERR(mgr)) > + put_device(mgr_dev); > > - return __fpga_mgr_get(mgr_dev); > +out: > + mutex_unlock(&mgr_lock); > + return mgr; > } > EXPORT_SYMBOL_GPL(fpga_mgr_get); > > @@ -711,13 +721,22 @@ EXPORT_SYMBOL_GPL(fpga_mgr_get); > */ > struct fpga_manager *of_fpga_mgr_get(struct device_node *node) > { > - struct device *dev; > + struct fpga_manager *mgr = ERR_PTR(-ENODEV); > + struct device *mgr_dev; > + > + mutex_lock(&mgr_lock); > + > + mgr_dev = class_find_device_by_of_node(&fpga_mgr_class, node); > + if (!mgr_dev) > + goto out; > > - dev = class_find_device_by_of_node(&fpga_mgr_class, node); > - if (!dev) > - return ERR_PTR(-ENODEV); > + mgr = __fpga_mgr_get(mgr_dev); > + if (IS_ERR(mgr)) > + put_device(mgr_dev); > > - return __fpga_mgr_get(dev); > +out: > + mutex_unlock(&mgr_lock); > + return mgr; > } > EXPORT_SYMBOL_GPL(of_fpga_mgr_get); > > @@ -727,7 +746,7 @@ EXPORT_SYMBOL_GPL(of_fpga_mgr_get); > */ > void fpga_mgr_put(struct fpga_manager *mgr) > { > - module_put(mgr->dev.parent->driver->owner); > + module_put(mgr->owner); > put_device(&mgr->dev); > } > EXPORT_SYMBOL_GPL(fpga_mgr_put); > @@ -806,6 +825,7 @@ fpga_mgr_register_full(struct device *parent, const struct fpga_manager_info *in > > mgr->name = info->name; > mgr->mops = info->mops; > + mgr->owner = info->mops->owner; > mgr->priv = info->priv; > mgr->compat_id = info->compat_id; > > @@ -888,7 +908,11 @@ void fpga_mgr_unregister(struct fpga_manager *mgr) > */ > fpga_mgr_fpga_remove(mgr); > > + mutex_lock(&mgr_lock); > + > device_unregister(&mgr->dev); > + > + mutex_unlock(&mgr_lock); Why this part should be protected rather than the whole fpga_mgr_unregister()? I feel the scope of the protection is unclear to me in this patch. What data should be protected from concurrent access by this mutex? From the code seems the racing of mgr dev should be protected but apparently it doesn't have to. And with this mutex, the get/put/unregister() for one mgr should be exclusive with another mgr, but that also seems not necessary. I think the mgr->owner & mgr->ops should be protected from concurrent access of delete_module & fpga_mgr_get/put(), so how about: struct fpga_manager_ops { struct module *owner; ... }; struct fpga_manager { ... struct mutex mops_lock; const struct fpga_manager_ops *mops; ... }; static struct fpga_manager *__fpga_mgr_get(struct device *dev) { struct fpga_manager *mgr; mgr = to_fpga_manager(dev); mutex_lock(&mgr->mops_lock); if (!mgr->mops || !try_module_get(mgr->mops->owner)) mgr = ERR_PTR(-ENODEV); mutex_unlock(&mgr->mops_lock); return mgr; } void fpga_mgr_unregister(struct fpga_manager *mgr) { fpga_mgr_fpga_remove(mgr); mutex_lock(&mgr->ops_lock); mgr->mops = NULL; mutex_unlock(&mgr->ops_lock); device_unregister(&mgr->dev); } Not actually tested. Thanks, Yilun > } > EXPORT_SYMBOL_GPL(fpga_mgr_unregister); > > diff --git a/include/linux/fpga/fpga-mgr.h b/include/linux/fpga/fpga-mgr.h > index 54f63459efd6..eaf6e072dbc0 100644 > --- a/include/linux/fpga/fpga-mgr.h > +++ b/include/linux/fpga/fpga-mgr.h > @@ -162,6 +162,7 @@ struct fpga_manager_info { > * @write_complete: set FPGA to operating state after writing is done > * @fpga_remove: optional: Set FPGA into a specific state during driver remove > * @groups: optional attribute groups. > + * @owner: owner module. > * > * fpga_manager_ops are the low level functions implemented by a specific > * fpga manager driver. The optional ones are tested for NULL before being > @@ -184,6 +185,7 @@ struct fpga_manager_ops { > struct fpga_image_info *info); > void (*fpga_remove)(struct fpga_manager *mgr); > const struct attribute_group **groups; > + struct module *owner; > }; > > /* FPGA manager status: Partial/Full Reconfiguration errors */ > @@ -201,6 +203,7 @@ struct fpga_manager_ops { > * @state: state of fpga manager > * @compat_id: FPGA manager id for compatibility check. > * @mops: pointer to struct of fpga manager ops > + * @owner: owner module. > * @priv: low level driver private date > */ > struct fpga_manager { > @@ -210,6 +213,7 @@ struct fpga_manager { > enum fpga_mgr_states state; > struct fpga_compat_id *compat_id; > const struct fpga_manager_ops *mops; > + struct module *owner; > void *priv; > }; > > -- > 2.42.0 > >
On 2023-11-25 10:11, Xu Yilun wrote: > On Fri, Nov 24, 2023 at 05:28:06PM +0100, Marco Pagani wrote: >> Add a module *owner field to the fpga_manager_ops and fpga_manager >> structs to protect the fpga manager against the unloading of the >> low-level control module while someone is holding a reference to the >> manager device. Low-level control modules should statically set the >> owner field of the fpga_manager_ops struct to THIS_MODULE. Then, when >> the manager is registered using fpga_mgr_register(), the value is copied >> into the owner field of the fpga_manager struct (that contains the >> device context). In this way, the manager can later use it in >> fpga_mgr_get() to take the low-level module's refcount. To prevent races >> while unloading the low-level control module, fpga_mgr_get() and part of >> the fpga_mgr_unregister() methods are protected with a mutex. >> >> Other changes: move put_device() from __fpga_mgr_get() to fpga_mgr_get() >> and of_fpga_mgr_get() to improve code clarity. >> >> Fixes: 654ba4cc0f3e ("fpga manager: ensure lifetime with of_fpga_mgr_get") >> Signed-off-by: Marco Pagani <marpagan@redhat.com> >> --- >> drivers/fpga/fpga-mgr.c | 56 +++++++++++++++++++++++++---------- >> include/linux/fpga/fpga-mgr.h | 4 +++ >> 2 files changed, 44 insertions(+), 16 deletions(-) >> >> diff --git a/drivers/fpga/fpga-mgr.c b/drivers/fpga/fpga-mgr.c >> index 06651389c592..608605d59860 100644 >> --- a/drivers/fpga/fpga-mgr.c >> +++ b/drivers/fpga/fpga-mgr.c >> @@ -21,6 +21,8 @@ >> static DEFINE_IDA(fpga_mgr_ida); >> static const struct class fpga_mgr_class; >> >> +static DEFINE_MUTEX(mgr_lock); >> + >> struct fpga_mgr_devres { >> struct fpga_manager *mgr; >> }; >> @@ -667,17 +669,15 @@ ATTRIBUTE_GROUPS(fpga_mgr); >> static struct fpga_manager *__fpga_mgr_get(struct device *dev) >> { >> struct fpga_manager *mgr; >> + struct module *owner; >> >> mgr = to_fpga_manager(dev); >> + owner = mgr->owner; >> >> - if (!try_module_get(dev->parent->driver->owner)) >> - goto err_dev; >> + if (owner && !try_module_get(owner)) > > No need to test owner == NULL, try_module_get() does this. You are right. I'll remove it in the next version. > >> + mgr = ERR_PTR(-ENODEV); >> >> return mgr; >> - >> -err_dev: >> - put_device(dev); >> - return ERR_PTR(-ENODEV); >> } >> >> static int fpga_mgr_dev_match(struct device *dev, const void *data) >> @@ -693,12 +693,22 @@ static int fpga_mgr_dev_match(struct device *dev, const void *data) >> */ >> struct fpga_manager *fpga_mgr_get(struct device *dev) >> { >> - struct device *mgr_dev = class_find_device(&fpga_mgr_class, NULL, dev, >> - fpga_mgr_dev_match); >> + struct fpga_manager *mgr = ERR_PTR(-ENODEV); >> + struct device *mgr_dev; >> + >> + mutex_lock(&mgr_lock); >> + >> + mgr_dev = class_find_device(&fpga_mgr_class, NULL, dev, fpga_mgr_dev_match); >> if (!mgr_dev) >> - return ERR_PTR(-ENODEV); >> + goto out; >> + >> + mgr = __fpga_mgr_get(mgr_dev); >> + if (IS_ERR(mgr)) >> + put_device(mgr_dev); >> >> - return __fpga_mgr_get(mgr_dev); >> +out: >> + mutex_unlock(&mgr_lock); >> + return mgr; >> } >> EXPORT_SYMBOL_GPL(fpga_mgr_get); >> >> @@ -711,13 +721,22 @@ EXPORT_SYMBOL_GPL(fpga_mgr_get); >> */ >> struct fpga_manager *of_fpga_mgr_get(struct device_node *node) >> { >> - struct device *dev; >> + struct fpga_manager *mgr = ERR_PTR(-ENODEV); >> + struct device *mgr_dev; >> + >> + mutex_lock(&mgr_lock); >> + >> + mgr_dev = class_find_device_by_of_node(&fpga_mgr_class, node); >> + if (!mgr_dev) >> + goto out; >> >> - dev = class_find_device_by_of_node(&fpga_mgr_class, node); >> - if (!dev) >> - return ERR_PTR(-ENODEV); >> + mgr = __fpga_mgr_get(mgr_dev); >> + if (IS_ERR(mgr)) >> + put_device(mgr_dev); >> >> - return __fpga_mgr_get(dev); >> +out: >> + mutex_unlock(&mgr_lock); >> + return mgr; >> } >> EXPORT_SYMBOL_GPL(of_fpga_mgr_get); >> >> @@ -727,7 +746,7 @@ EXPORT_SYMBOL_GPL(of_fpga_mgr_get); >> */ >> void fpga_mgr_put(struct fpga_manager *mgr) >> { >> - module_put(mgr->dev.parent->driver->owner); >> + module_put(mgr->owner); >> put_device(&mgr->dev); >> } >> EXPORT_SYMBOL_GPL(fpga_mgr_put); >> @@ -806,6 +825,7 @@ fpga_mgr_register_full(struct device *parent, const struct fpga_manager_info *in >> >> mgr->name = info->name; >> mgr->mops = info->mops; >> + mgr->owner = info->mops->owner; >> mgr->priv = info->priv; >> mgr->compat_id = info->compat_id; >> >> @@ -888,7 +908,11 @@ void fpga_mgr_unregister(struct fpga_manager *mgr) >> */ >> fpga_mgr_fpga_remove(mgr); >> >> + mutex_lock(&mgr_lock); >> + >> device_unregister(&mgr->dev); >> + >> + mutex_unlock(&mgr_lock); > > Why this part should be protected rather than the whole > fpga_mgr_unregister()? > Protecting the fpga_remove() op seems unnecessary to me because it does not affect the manager device's lifetime. Moreover, it may hold the mutex for a long time since it was intended to interact with the hardware to put it in a specific state before removing the driver. > I feel the scope of the protection is unclear to me in this patch. What > data should be protected from concurrent access by this mutex? From the > code seems the racing of mgr dev should be protected but apparently it > doesn't have to. The mutex is there to ensure the lifetime of the manager device and its context (struct fpga_manager) if fpga_mgr_get() happens to run concurrently with the removal of the low-level module. > > And with this mutex, the get/put/unregister() for one mgr should be > exclusive with another mgr, but that also seems not necessary. > I decided to use a static mutex because I thought putting the mutex in the manager's context would be unsafe since its life would be tied to the manager's life. For instance, consider the following sequence: - A removes the low-level control module, and delete_module progresses up to the point when it calls the module's exit function, which in turn calls fpga_mgr_unregister(). - fpga_mgr_unregister() takes the mutex but gets descheduled before completing the unregistering of the manager device. - Meanwhile, B wants to get the manager (it is still there) and calls fpga_mgr_get(), which tries to take the mutex but gets suspended since it is held by A. - A resumes and fpga_mgr_unregister() releases the manager device and its context, including the mutex on which B is suspended. We could mitigate this specific case using mutex_trylock(). However, there will still be other problematic cases, like if fpga_mgr_get() gets suspended right before taking the mutex and then delete_module proceeds up to when fpga_mgr_unregister() frees the manager device and its context. > I think the mgr->owner & mgr->ops should be protected from concurrent > access of delete_module & fpga_mgr_get/put(), so how about: > > struct fpga_manager_ops { > struct module *owner; > ... > }; > > struct fpga_manager { > ... > struct mutex mops_lock; > const struct fpga_manager_ops *mops; > ... > }; > > > static struct fpga_manager *__fpga_mgr_get(struct device *dev) > { > struct fpga_manager *mgr; > > mgr = to_fpga_manager(dev); > > mutex_lock(&mgr->mops_lock); > > if (!mgr->mops || !try_module_get(mgr->mops->owner)) > mgr = ERR_PTR(-ENODEV); > > mutex_unlock(&mgr->mops_lock); > > return mgr; > } > > void fpga_mgr_unregister(struct fpga_manager *mgr) > { > fpga_mgr_fpga_remove(mgr); > > mutex_lock(&mgr->ops_lock); > mgr->mops = NULL; > mutex_unlock(&mgr->ops_lock); > > device_unregister(&mgr->dev); > } > > Not actually tested. > I think protecting the only the ops is not enough for the same reasons. If fpga_mgr_get() gets suspended right after class_find_device(),and meanwhile the low-level module is removed, it resumes with a reference to a manager device that no longer exists. In a certain sense, however, using a mutex seems like a mitigation that does not solve the problem at its root. I honestly still think that taking the module's refcount right when registering the manager is the only way that is both safe and robust against code changes. However, my proposal was rejected. So, if you prefer, I can drop the mutex entirely in the next version, and we leave the responsibility of keeping all kernel pieces to the user. It will still be an improvement over taking the low-level module's refcount through the parent device's driver pointer. Thanks, Marco
On Thu, Nov 30, 2023 at 11:42:36AM +0100, Marco Pagani wrote: > > > On 2023-11-25 10:11, Xu Yilun wrote: > > On Fri, Nov 24, 2023 at 05:28:06PM +0100, Marco Pagani wrote: > >> Add a module *owner field to the fpga_manager_ops and fpga_manager > >> structs to protect the fpga manager against the unloading of the > >> low-level control module while someone is holding a reference to the > >> manager device. Low-level control modules should statically set the > >> owner field of the fpga_manager_ops struct to THIS_MODULE. Then, when > >> the manager is registered using fpga_mgr_register(), the value is copied > >> into the owner field of the fpga_manager struct (that contains the > >> device context). In this way, the manager can later use it in > >> fpga_mgr_get() to take the low-level module's refcount. To prevent races > >> while unloading the low-level control module, fpga_mgr_get() and part of > >> the fpga_mgr_unregister() methods are protected with a mutex. > >> > >> Other changes: move put_device() from __fpga_mgr_get() to fpga_mgr_get() > >> and of_fpga_mgr_get() to improve code clarity. > >> > >> Fixes: 654ba4cc0f3e ("fpga manager: ensure lifetime with of_fpga_mgr_get") > >> Signed-off-by: Marco Pagani <marpagan@redhat.com> > >> --- > >> drivers/fpga/fpga-mgr.c | 56 +++++++++++++++++++++++++---------- > >> include/linux/fpga/fpga-mgr.h | 4 +++ > >> 2 files changed, 44 insertions(+), 16 deletions(-) > >> > >> diff --git a/drivers/fpga/fpga-mgr.c b/drivers/fpga/fpga-mgr.c > >> index 06651389c592..608605d59860 100644 > >> --- a/drivers/fpga/fpga-mgr.c > >> +++ b/drivers/fpga/fpga-mgr.c > >> @@ -21,6 +21,8 @@ > >> static DEFINE_IDA(fpga_mgr_ida); > >> static const struct class fpga_mgr_class; > >> > >> +static DEFINE_MUTEX(mgr_lock); > >> + > >> struct fpga_mgr_devres { > >> struct fpga_manager *mgr; > >> }; > >> @@ -667,17 +669,15 @@ ATTRIBUTE_GROUPS(fpga_mgr); > >> static struct fpga_manager *__fpga_mgr_get(struct device *dev) > >> { > >> struct fpga_manager *mgr; > >> + struct module *owner; > >> > >> mgr = to_fpga_manager(dev); > >> + owner = mgr->owner; > >> > >> - if (!try_module_get(dev->parent->driver->owner)) > >> - goto err_dev; > >> + if (owner && !try_module_get(owner)) > > > > No need to test owner == NULL, try_module_get() does this. > > You are right. I'll remove it in the next version. > > > > >> + mgr = ERR_PTR(-ENODEV); > >> > >> return mgr; > >> - > >> -err_dev: > >> - put_device(dev); > >> - return ERR_PTR(-ENODEV); > >> } > >> > >> static int fpga_mgr_dev_match(struct device *dev, const void *data) > >> @@ -693,12 +693,22 @@ static int fpga_mgr_dev_match(struct device *dev, const void *data) > >> */ > >> struct fpga_manager *fpga_mgr_get(struct device *dev) > >> { > >> - struct device *mgr_dev = class_find_device(&fpga_mgr_class, NULL, dev, > >> - fpga_mgr_dev_match); > >> + struct fpga_manager *mgr = ERR_PTR(-ENODEV); > >> + struct device *mgr_dev; > >> + > >> + mutex_lock(&mgr_lock); > >> + > >> + mgr_dev = class_find_device(&fpga_mgr_class, NULL, dev, fpga_mgr_dev_match); > >> if (!mgr_dev) > >> - return ERR_PTR(-ENODEV); > >> + goto out; > >> + > >> + mgr = __fpga_mgr_get(mgr_dev); > >> + if (IS_ERR(mgr)) > >> + put_device(mgr_dev); > >> > >> - return __fpga_mgr_get(mgr_dev); > >> +out: > >> + mutex_unlock(&mgr_lock); > >> + return mgr; > >> } > >> EXPORT_SYMBOL_GPL(fpga_mgr_get); > >> > >> @@ -711,13 +721,22 @@ EXPORT_SYMBOL_GPL(fpga_mgr_get); > >> */ > >> struct fpga_manager *of_fpga_mgr_get(struct device_node *node) > >> { > >> - struct device *dev; > >> + struct fpga_manager *mgr = ERR_PTR(-ENODEV); > >> + struct device *mgr_dev; > >> + > >> + mutex_lock(&mgr_lock); > >> + > >> + mgr_dev = class_find_device_by_of_node(&fpga_mgr_class, node); > >> + if (!mgr_dev) > >> + goto out; > >> > >> - dev = class_find_device_by_of_node(&fpga_mgr_class, node); > >> - if (!dev) > >> - return ERR_PTR(-ENODEV); > >> + mgr = __fpga_mgr_get(mgr_dev); > >> + if (IS_ERR(mgr)) > >> + put_device(mgr_dev); > >> > >> - return __fpga_mgr_get(dev); > >> +out: > >> + mutex_unlock(&mgr_lock); > >> + return mgr; > >> } > >> EXPORT_SYMBOL_GPL(of_fpga_mgr_get); > >> > >> @@ -727,7 +746,7 @@ EXPORT_SYMBOL_GPL(of_fpga_mgr_get); > >> */ > >> void fpga_mgr_put(struct fpga_manager *mgr) > >> { > >> - module_put(mgr->dev.parent->driver->owner); > >> + module_put(mgr->owner); > >> put_device(&mgr->dev); > >> } > >> EXPORT_SYMBOL_GPL(fpga_mgr_put); > >> @@ -806,6 +825,7 @@ fpga_mgr_register_full(struct device *parent, const struct fpga_manager_info *in > >> > >> mgr->name = info->name; > >> mgr->mops = info->mops; > >> + mgr->owner = info->mops->owner; > >> mgr->priv = info->priv; > >> mgr->compat_id = info->compat_id; > >> > >> @@ -888,7 +908,11 @@ void fpga_mgr_unregister(struct fpga_manager *mgr) > >> */ > >> fpga_mgr_fpga_remove(mgr); > >> > >> + mutex_lock(&mgr_lock); > >> + > >> device_unregister(&mgr->dev); > >> + > >> + mutex_unlock(&mgr_lock); > > > > Why this part should be protected rather than the whole > > fpga_mgr_unregister()? > > > > Protecting the fpga_remove() op seems unnecessary to me because it > does not affect the manager device's lifetime. Moreover, it may hold > the mutex for a long time since it was intended to interact with the > hardware to put it in a specific state before removing the driver. > > > I feel the scope of the protection is unclear to me in this patch. What > > data should be protected from concurrent access by this mutex? From the > > code seems the racing of mgr dev should be protected but apparently it > > doesn't have to. > > The mutex is there to ensure the lifetime of the manager device and > its context (struct fpga_manager) if fpga_mgr_get() happens to run > concurrently with the removal of the low-level module. > > > > > And with this mutex, the get/put/unregister() for one mgr should be > > exclusive with another mgr, but that also seems not necessary. > > > > I decided to use a static mutex because I thought putting the mutex > in the manager's context would be unsafe since its life would be tied > to the manager's life. For instance, consider the following sequence: > > - A removes the low-level control module, and delete_module progresses > up to the point when it calls the module's exit function, which in turn > calls fpga_mgr_unregister(). > > - fpga_mgr_unregister() takes the mutex but gets descheduled before > completing the unregistering of the manager device. > > - Meanwhile, B wants to get the manager (it is still there) and calls > fpga_mgr_get(), which tries to take the mutex but gets suspended since > it is held by A. > > - A resumes and fpga_mgr_unregister() releases the manager device and The lifecycle of the manager device is not entirely decided by fpga_mgr_unregister(), this func just puts/decreases the device refcount. Remember fpga_mgr_get() gets the device via class_find_device()->get_device(). I assume if the valid device pointer could be returned by class_find_device(), it would never be released by other nice players. So I have no worry about the per manager mutex. > its context, including the mutex on which B is suspended. > > We could mitigate this specific case using mutex_trylock(). However, > there will still be other problematic cases, like if fpga_mgr_get() > gets suspended right before taking the mutex and then delete_module > proceeds up to when fpga_mgr_unregister() frees the manager device > and its context. > > > I think the mgr->owner & mgr->ops should be protected from concurrent > > access of delete_module & fpga_mgr_get/put(), so how about: > > > > struct fpga_manager_ops { > > struct module *owner; > > ... > > }; > > > > struct fpga_manager { > > ... > > struct mutex mops_lock; > > const struct fpga_manager_ops *mops; > > ... > > }; > > > > > > static struct fpga_manager *__fpga_mgr_get(struct device *dev) > > { > > struct fpga_manager *mgr; > > > > mgr = to_fpga_manager(dev); > > > > mutex_lock(&mgr->mops_lock); > > > > if (!mgr->mops || !try_module_get(mgr->mops->owner)) > > mgr = ERR_PTR(-ENODEV); > > > > mutex_unlock(&mgr->mops_lock); > > > > return mgr; > > } > > > > void fpga_mgr_unregister(struct fpga_manager *mgr) > > { > > fpga_mgr_fpga_remove(mgr); > > > > mutex_lock(&mgr->ops_lock); > > mgr->mops = NULL; > > mutex_unlock(&mgr->ops_lock); > > > > device_unregister(&mgr->dev); > > } > > > > Not actually tested. > > > > I think protecting the only the ops is not enough for the same reasons. > If fpga_mgr_get() gets suspended right after class_find_device(),and > meanwhile the low-level module is removed, it resumes with a reference > to a manager device that no longer exists. > > In a certain sense, however, using a mutex seems like a mitigation > that does not solve the problem at its root. I honestly still think > that taking the module's refcount right when registering the manager > is the only way that is both safe and robust against code changes. I would nak either. As mentioned above, that makes rmmod vendor module impossible. Introducing another user interface to release the module's refcount is also a bad idea. Who decides to take the ref, who releases it. A user has no knowledge of what is happening inside and should not enforce. > However, my proposal was rejected. > > So, if you prefer, I can drop the mutex entirely in the next version, > and we leave the responsibility of keeping all kernel pieces to the No, please try to fix it. Could you please reconsider my proposal? Thanks, Yilun > user. It will still be an improvement over taking the low-level > module's refcount through the parent device's driver pointer. > > Thanks, > Marco > >
On 2023-12-02 13:16, Xu Yilun wrote: > On Thu, Nov 30, 2023 at 11:42:36AM +0100, Marco Pagani wrote: >> >> >> On 2023-11-25 10:11, Xu Yilun wrote: >>> On Fri, Nov 24, 2023 at 05:28:06PM +0100, Marco Pagani wrote: >>>> Add a module *owner field to the fpga_manager_ops and fpga_manager >>>> structs to protect the fpga manager against the unloading of the >>>> low-level control module while someone is holding a reference to the >>>> manager device. Low-level control modules should statically set the >>>> owner field of the fpga_manager_ops struct to THIS_MODULE. Then, when >>>> the manager is registered using fpga_mgr_register(), the value is copied >>>> into the owner field of the fpga_manager struct (that contains the >>>> device context). In this way, the manager can later use it in >>>> fpga_mgr_get() to take the low-level module's refcount. To prevent races >>>> while unloading the low-level control module, fpga_mgr_get() and part of >>>> the fpga_mgr_unregister() methods are protected with a mutex. >>>> >>>> Other changes: move put_device() from __fpga_mgr_get() to fpga_mgr_get() >>>> and of_fpga_mgr_get() to improve code clarity. >>>> >>>> Fixes: 654ba4cc0f3e ("fpga manager: ensure lifetime with of_fpga_mgr_get") >>>> Signed-off-by: Marco Pagani <marpagan@redhat.com> >>>> --- >>>> drivers/fpga/fpga-mgr.c | 56 +++++++++++++++++++++++++---------- >>>> include/linux/fpga/fpga-mgr.h | 4 +++ >>>> 2 files changed, 44 insertions(+), 16 deletions(-) >>>> >>>> diff --git a/drivers/fpga/fpga-mgr.c b/drivers/fpga/fpga-mgr.c >>>> index 06651389c592..608605d59860 100644 >>>> --- a/drivers/fpga/fpga-mgr.c >>>> +++ b/drivers/fpga/fpga-mgr.c >>>> @@ -21,6 +21,8 @@ >>>> static DEFINE_IDA(fpga_mgr_ida); >>>> static const struct class fpga_mgr_class; >>>> >>>> +static DEFINE_MUTEX(mgr_lock); >>>> + >>>> struct fpga_mgr_devres { >>>> struct fpga_manager *mgr; >>>> }; >>>> @@ -667,17 +669,15 @@ ATTRIBUTE_GROUPS(fpga_mgr); >>>> static struct fpga_manager *__fpga_mgr_get(struct device *dev) >>>> { >>>> struct fpga_manager *mgr; >>>> + struct module *owner; >>>> >>>> mgr = to_fpga_manager(dev); >>>> + owner = mgr->owner; >>>> >>>> - if (!try_module_get(dev->parent->driver->owner)) >>>> - goto err_dev; >>>> + if (owner && !try_module_get(owner)) >>> >>> No need to test owner == NULL, try_module_get() does this. >> >> You are right. I'll remove it in the next version. >> >>> >>>> + mgr = ERR_PTR(-ENODEV); >>>> >>>> return mgr; >>>> - >>>> -err_dev: >>>> - put_device(dev); >>>> - return ERR_PTR(-ENODEV); >>>> } >>>> >>>> static int fpga_mgr_dev_match(struct device *dev, const void *data) >>>> @@ -693,12 +693,22 @@ static int fpga_mgr_dev_match(struct device *dev, const void *data) >>>> */ >>>> struct fpga_manager *fpga_mgr_get(struct device *dev) >>>> { >>>> - struct device *mgr_dev = class_find_device(&fpga_mgr_class, NULL, dev, >>>> - fpga_mgr_dev_match); >>>> + struct fpga_manager *mgr = ERR_PTR(-ENODEV); >>>> + struct device *mgr_dev; >>>> + >>>> + mutex_lock(&mgr_lock); >>>> + >>>> + mgr_dev = class_find_device(&fpga_mgr_class, NULL, dev, fpga_mgr_dev_match); >>>> if (!mgr_dev) >>>> - return ERR_PTR(-ENODEV); >>>> + goto out; >>>> + >>>> + mgr = __fpga_mgr_get(mgr_dev); >>>> + if (IS_ERR(mgr)) >>>> + put_device(mgr_dev); >>>> >>>> - return __fpga_mgr_get(mgr_dev); >>>> +out: >>>> + mutex_unlock(&mgr_lock); >>>> + return mgr; >>>> } >>>> EXPORT_SYMBOL_GPL(fpga_mgr_get); >>>> >>>> @@ -711,13 +721,22 @@ EXPORT_SYMBOL_GPL(fpga_mgr_get); >>>> */ >>>> struct fpga_manager *of_fpga_mgr_get(struct device_node *node) >>>> { >>>> - struct device *dev; >>>> + struct fpga_manager *mgr = ERR_PTR(-ENODEV); >>>> + struct device *mgr_dev; >>>> + >>>> + mutex_lock(&mgr_lock); >>>> + >>>> + mgr_dev = class_find_device_by_of_node(&fpga_mgr_class, node); >>>> + if (!mgr_dev) >>>> + goto out; >>>> >>>> - dev = class_find_device_by_of_node(&fpga_mgr_class, node); >>>> - if (!dev) >>>> - return ERR_PTR(-ENODEV); >>>> + mgr = __fpga_mgr_get(mgr_dev); >>>> + if (IS_ERR(mgr)) >>>> + put_device(mgr_dev); >>>> >>>> - return __fpga_mgr_get(dev); >>>> +out: >>>> + mutex_unlock(&mgr_lock); >>>> + return mgr; >>>> } >>>> EXPORT_SYMBOL_GPL(of_fpga_mgr_get); >>>> >>>> @@ -727,7 +746,7 @@ EXPORT_SYMBOL_GPL(of_fpga_mgr_get); >>>> */ >>>> void fpga_mgr_put(struct fpga_manager *mgr) >>>> { >>>> - module_put(mgr->dev.parent->driver->owner); >>>> + module_put(mgr->owner); >>>> put_device(&mgr->dev); >>>> } >>>> EXPORT_SYMBOL_GPL(fpga_mgr_put); >>>> @@ -806,6 +825,7 @@ fpga_mgr_register_full(struct device *parent, const struct fpga_manager_info *in >>>> >>>> mgr->name = info->name; >>>> mgr->mops = info->mops; >>>> + mgr->owner = info->mops->owner; >>>> mgr->priv = info->priv; >>>> mgr->compat_id = info->compat_id; >>>> >>>> @@ -888,7 +908,11 @@ void fpga_mgr_unregister(struct fpga_manager *mgr) >>>> */ >>>> fpga_mgr_fpga_remove(mgr); >>>> >>>> + mutex_lock(&mgr_lock); >>>> + >>>> device_unregister(&mgr->dev); >>>> + >>>> + mutex_unlock(&mgr_lock); >>> >>> Why this part should be protected rather than the whole >>> fpga_mgr_unregister()? >>> >> >> Protecting the fpga_remove() op seems unnecessary to me because it >> does not affect the manager device's lifetime. Moreover, it may hold >> the mutex for a long time since it was intended to interact with the >> hardware to put it in a specific state before removing the driver. >> >>> I feel the scope of the protection is unclear to me in this patch. What >>> data should be protected from concurrent access by this mutex? From the >>> code seems the racing of mgr dev should be protected but apparently it >>> doesn't have to. >> >> The mutex is there to ensure the lifetime of the manager device and >> its context (struct fpga_manager) if fpga_mgr_get() happens to run >> concurrently with the removal of the low-level module. >> >>> >>> And with this mutex, the get/put/unregister() for one mgr should be >>> exclusive with another mgr, but that also seems not necessary. >>> >> >> I decided to use a static mutex because I thought putting the mutex >> in the manager's context would be unsafe since its life would be tied >> to the manager's life. For instance, consider the following sequence: >> >> - A removes the low-level control module, and delete_module progresses >> up to the point when it calls the module's exit function, which in turn >> calls fpga_mgr_unregister(). >> >> - fpga_mgr_unregister() takes the mutex but gets descheduled before >> completing the unregistering of the manager device. >> >> - Meanwhile, B wants to get the manager (it is still there) and calls >> fpga_mgr_get(), which tries to take the mutex but gets suspended since >> it is held by A. >> >> - A resumes and fpga_mgr_unregister() releases the manager device and > > The lifecycle of the manager device is not entirely decided by > fpga_mgr_unregister(), this func just puts/decreases the device > refcount. Right, but here I'm considering the case where no one has previously taken the manager device. In that specific case, the refcount will be decremented to zero, and the device (with its context) will be released. However, you got me thinking about how the mutex is causing the problem in this case. > > Remember fpga_mgr_get() gets the device via > class_find_device()->get_device(). I assume if the valid device pointer > could be returned by class_find_device(), it would never be released by > other nice players. So I have no worry about the per manager mutex. > >> its context, including the mutex on which B is suspended. >> >> We could mitigate this specific case using mutex_trylock(). However, >> there will still be other problematic cases, like if fpga_mgr_get() >> gets suspended right before taking the mutex and then delete_module >> proceeds up to when fpga_mgr_unregister() frees the manager device >> and its context. >> >>> I think the mgr->owner & mgr->ops should be protected from concurrent >>> access of delete_module & fpga_mgr_get/put(), so how about: >>> >>> struct fpga_manager_ops { >>> struct module *owner; >>> ... >>> }; >>> >>> struct fpga_manager { >>> ... >>> struct mutex mops_lock; >>> const struct fpga_manager_ops *mops; >>> ... >>> }; >>> >>> >>> static struct fpga_manager *__fpga_mgr_get(struct device *dev) >>> { >>> struct fpga_manager *mgr; >>> >>> mgr = to_fpga_manager(dev); >>> >>> mutex_lock(&mgr->mops_lock); >>> >>> if (!mgr->mops || !try_module_get(mgr->mops->owner)) >>> mgr = ERR_PTR(-ENODEV); >>> >>> mutex_unlock(&mgr->mops_lock); >>> >>> return mgr; >>> } >>> >>> void fpga_mgr_unregister(struct fpga_manager *mgr) >>> { >>> fpga_mgr_fpga_remove(mgr); >>> >>> mutex_lock(&mgr->ops_lock); >>> mgr->mops = NULL; >>> mutex_unlock(&mgr->ops_lock); >>> >>> device_unregister(&mgr->dev); >>> } >>> >>> Not actually tested. >>> >> >> I think protecting the only the ops is not enough for the same reasons. >> If fpga_mgr_get() gets suspended right after class_find_device(),and >> meanwhile the low-level module is removed, it resumes with a reference >> to a manager device that no longer exists. >> >> In a certain sense, however, using a mutex seems like a mitigation >> that does not solve the problem at its root. I honestly still think >> that taking the module's refcount right when registering the manager >> is the only way that is both safe and robust against code changes. > > I would nak either. As mentioned above, that makes rmmod vendor module > impossible. Introducing another user interface to release the module's > refcount is also a bad idea. Who decides to take the ref, who releases > it. A user has no knowledge of what is happening inside and should not > enforce. > >> However, my proposal was rejected. >> >> So, if you prefer, I can drop the mutex entirely in the next version, >> and we leave the responsibility of keeping all kernel pieces to the > > No, please try to fix it. Could you please reconsider my proposal? > I have considered it and thought about it. However, I don't think we need a mutex to protect mgr->mops. This is because the low-level module's refcount has already been decremented when fpga_mgr_unregister() is called by the module's exit function. So, when we get to the point of executing fpga_mgr_unregister(), any concurrent call to try_module_get() will fail (if no one has taken the module before) without the need to check mops first. If we assume (as you pointed out) that class_find_device() can be safely executed concurrently with device_unregister() (returning either a valid dev pointer or null) and, consequently, the manager context is guaranteed to exist after that point. Then, we should be good without the mutex if we call try_module_get() on a copy of the owner pointer stored in the manager context. >> user. It will still be an improvement over taking the low-level >> module's refcount through the parent device's driver pointer. >> Thanks, Marco
> >>> I feel the scope of the protection is unclear to me in this patch. What > >>> data should be protected from concurrent access by this mutex? From the > >>> code seems the racing of mgr dev should be protected but apparently it > >>> doesn't have to. > >> > >> The mutex is there to ensure the lifetime of the manager device and > >> its context (struct fpga_manager) if fpga_mgr_get() happens to run > >> concurrently with the removal of the low-level module. > >> > >>> > >>> And with this mutex, the get/put/unregister() for one mgr should be > >>> exclusive with another mgr, but that also seems not necessary. > >>> > >> > >> I decided to use a static mutex because I thought putting the mutex > >> in the manager's context would be unsafe since its life would be tied > >> to the manager's life. For instance, consider the following sequence: > >> > >> - A removes the low-level control module, and delete_module progresses > >> up to the point when it calls the module's exit function, which in turn > >> calls fpga_mgr_unregister(). > >> > >> - fpga_mgr_unregister() takes the mutex but gets descheduled before > >> completing the unregistering of the manager device. > >> > >> - Meanwhile, B wants to get the manager (it is still there) and calls > >> fpga_mgr_get(), which tries to take the mutex but gets suspended since > >> it is held by A. > >> > >> - A resumes and fpga_mgr_unregister() releases the manager device and > > > > The lifecycle of the manager device is not entirely decided by > > fpga_mgr_unregister(), this func just puts/decreases the device > > refcount. > > Right, but here I'm considering the case where no one has previously > taken the manager device. In that specific case, the refcount will be I don't think this is valid, anyone should firstly get the manager device via get_device() then try to access its context. > decremented to zero, and the device (with its context) will be released. If no one has taken the manager device, the device & its context are safe to be released. > > However, you got me thinking about how the mutex is causing the problem > in this case. > > > > > Remember fpga_mgr_get() gets the device via > > class_find_device()->get_device(). I assume if the valid device pointer > > could be returned by class_find_device(), it would never be released by > > other nice players. So I have no worry about the per manager mutex. > > > >> its context, including the mutex on which B is suspended. > >> > >> We could mitigate this specific case using mutex_trylock(). However, > >> there will still be other problematic cases, like if fpga_mgr_get() > >> gets suspended right before taking the mutex and then delete_module > >> proceeds up to when fpga_mgr_unregister() frees the manager device > >> and its context. > >> > >>> I think the mgr->owner & mgr->ops should be protected from concurrent > >>> access of delete_module & fpga_mgr_get/put(), so how about: > >>> > >>> struct fpga_manager_ops { > >>> struct module *owner; > >>> ... > >>> }; > >>> > >>> struct fpga_manager { > >>> ... > >>> struct mutex mops_lock; > >>> const struct fpga_manager_ops *mops; > >>> ... > >>> }; > >>> > >>> > >>> static struct fpga_manager *__fpga_mgr_get(struct device *dev) > >>> { > >>> struct fpga_manager *mgr; > >>> > >>> mgr = to_fpga_manager(dev); > >>> > >>> mutex_lock(&mgr->mops_lock); > >>> > >>> if (!mgr->mops || !try_module_get(mgr->mops->owner)) > >>> mgr = ERR_PTR(-ENODEV); > >>> > >>> mutex_unlock(&mgr->mops_lock); > >>> > >>> return mgr; > >>> } > >>> > >>> void fpga_mgr_unregister(struct fpga_manager *mgr) > >>> { > >>> fpga_mgr_fpga_remove(mgr); > >>> > >>> mutex_lock(&mgr->ops_lock); > >>> mgr->mops = NULL; > >>> mutex_unlock(&mgr->ops_lock); > >>> > >>> device_unregister(&mgr->dev); > >>> } > >>> > >>> Not actually tested. > >>> > >> > >> I think protecting the only the ops is not enough for the same reasons. > >> If fpga_mgr_get() gets suspended right after class_find_device(),and > >> meanwhile the low-level module is removed, it resumes with a reference > >> to a manager device that no longer exists. > >> > >> In a certain sense, however, using a mutex seems like a mitigation > >> that does not solve the problem at its root. I honestly still think > >> that taking the module's refcount right when registering the manager > >> is the only way that is both safe and robust against code changes. > > > > I would nak either. As mentioned above, that makes rmmod vendor module > > impossible. Introducing another user interface to release the module's > > refcount is also a bad idea. Who decides to take the ref, who releases > > it. A user has no knowledge of what is happening inside and should not > > enforce. > > > >> However, my proposal was rejected. > >> > >> So, if you prefer, I can drop the mutex entirely in the next version, > >> and we leave the responsibility of keeping all kernel pieces to the > > > > No, please try to fix it. Could you please reconsider my proposal? > > > > I have considered it and thought about it. However, I don't think we need a > mutex to protect mgr->mops. This is because the low-level module's refcount has > already been decremented when fpga_mgr_unregister() is called by the module's > exit function. So, when we get to the point of executing fpga_mgr_unregister(), > any concurrent call to try_module_get() will fail (if no one has taken the Are you still taking care of your previous finding [1]? It says: To be clear, you should only use try_module_get() *iff* you are 100% sure the module already does exist ... IIUC, if you do nothing on fpga_mgr_unregister(), the low-level module may just disappear and any copy of the owner pointer became invalid. Then try_module_get() would not fail but panic. [1] 557aafac1153 ("kernel/module: add documentation for try_module_get()") Thanks, Yilun > module before) without the need to check mops first. > > If we assume (as you pointed out) that class_find_device() can be safely > executed concurrently with device_unregister() (returning either a valid dev > pointer or null) and, consequently, the manager context is guaranteed to exist > after that point. Then, we should be good without the mutex if we call > try_module_get() on a copy of the owner pointer stored in the manager context. > > >> user. It will still be an improvement over taking the low-level > >> module's refcount through the parent device's driver pointer. > >> > > Thanks, > Marco > >
On 2023-12-09 16:27, Xu Yilun wrote: >>>>> I feel the scope of the protection is unclear to me in this patch. What >>>>> data should be protected from concurrent access by this mutex? From the >>>>> code seems the racing of mgr dev should be protected but apparently it >>>>> doesn't have to. >>>> >>>> The mutex is there to ensure the lifetime of the manager device and >>>> its context (struct fpga_manager) if fpga_mgr_get() happens to run >>>> concurrently with the removal of the low-level module. >>>> >>>>> >>>>> And with this mutex, the get/put/unregister() for one mgr should be >>>>> exclusive with another mgr, but that also seems not necessary. >>>>> >>>> >>>> I decided to use a static mutex because I thought putting the mutex >>>> in the manager's context would be unsafe since its life would be tied >>>> to the manager's life. For instance, consider the following sequence: >>>> >>>> - A removes the low-level control module, and delete_module progresses >>>> up to the point when it calls the module's exit function, which in turn >>>> calls fpga_mgr_unregister(). >>>> >>>> - fpga_mgr_unregister() takes the mutex but gets descheduled before >>>> completing the unregistering of the manager device. >>>> >>>> - Meanwhile, B wants to get the manager (it is still there) and calls >>>> fpga_mgr_get(), which tries to take the mutex but gets suspended since >>>> it is held by A. >>>> >>>> - A resumes and fpga_mgr_unregister() releases the manager device and >>> >>> The lifecycle of the manager device is not entirely decided by >>> fpga_mgr_unregister(), this func just puts/decreases the device >>> refcount. >> >> Right, but here I'm considering the case where no one has previously >> taken the manager device. In that specific case, the refcount will be > > I don't think this is valid, anyone should firstly get the manager > device via get_device() then try to access its context. > >> decremented to zero, and the device (with its context) will be released. > > If no one has taken the manager device, the device & its context are > safe to be released. > >> >> However, you got me thinking about how the mutex is causing the problem >> in this case. >> >>> >>> Remember fpga_mgr_get() gets the device via >>> class_find_device()->get_device(). I assume if the valid device pointer >>> could be returned by class_find_device(), it would never be released by >>> other nice players. So I have no worry about the per manager mutex. >>> >>>> its context, including the mutex on which B is suspended. >>>> >>>> We could mitigate this specific case using mutex_trylock(). However, >>>> there will still be other problematic cases, like if fpga_mgr_get() >>>> gets suspended right before taking the mutex and then delete_module >>>> proceeds up to when fpga_mgr_unregister() frees the manager device >>>> and its context. >>>> >>>>> I think the mgr->owner & mgr->ops should be protected from concurrent >>>>> access of delete_module & fpga_mgr_get/put(), so how about: >>>>> >>>>> struct fpga_manager_ops { >>>>> struct module *owner; >>>>> ... >>>>> }; >>>>> >>>>> struct fpga_manager { >>>>> ... >>>>> struct mutex mops_lock; >>>>> const struct fpga_manager_ops *mops; >>>>> ... >>>>> }; >>>>> >>>>> >>>>> static struct fpga_manager *__fpga_mgr_get(struct device *dev) >>>>> { >>>>> struct fpga_manager *mgr; >>>>> >>>>> mgr = to_fpga_manager(dev); >>>>> >>>>> mutex_lock(&mgr->mops_lock); >>>>> >>>>> if (!mgr->mops || !try_module_get(mgr->mops->owner)) >>>>> mgr = ERR_PTR(-ENODEV); >>>>> >>>>> mutex_unlock(&mgr->mops_lock); >>>>> >>>>> return mgr; >>>>> } >>>>> >>>>> void fpga_mgr_unregister(struct fpga_manager *mgr) >>>>> { >>>>> fpga_mgr_fpga_remove(mgr); >>>>> >>>>> mutex_lock(&mgr->ops_lock); >>>>> mgr->mops = NULL; >>>>> mutex_unlock(&mgr->ops_lock); >>>>> >>>>> device_unregister(&mgr->dev); >>>>> } >>>>> >>>>> Not actually tested. >>>>> >>>> >>>> I think protecting the only the ops is not enough for the same reasons. >>>> If fpga_mgr_get() gets suspended right after class_find_device(),and >>>> meanwhile the low-level module is removed, it resumes with a reference >>>> to a manager device that no longer exists. >>>> >>>> In a certain sense, however, using a mutex seems like a mitigation >>>> that does not solve the problem at its root. I honestly still think >>>> that taking the module's refcount right when registering the manager >>>> is the only way that is both safe and robust against code changes. >>> >>> I would nak either. As mentioned above, that makes rmmod vendor module >>> impossible. Introducing another user interface to release the module's >>> refcount is also a bad idea. Who decides to take the ref, who releases >>> it. A user has no knowledge of what is happening inside and should not >>> enforce. >>> >>>> However, my proposal was rejected. >>>> >>>> So, if you prefer, I can drop the mutex entirely in the next version, >>>> and we leave the responsibility of keeping all kernel pieces to the >>> >>> No, please try to fix it. Could you please reconsider my proposal? >>> >> >> I have considered it and thought about it. However, I don't think we need a >> mutex to protect mgr->mops. This is because the low-level module's refcount has >> already been decremented when fpga_mgr_unregister() is called by the module's >> exit function. So, when we get to the point of executing fpga_mgr_unregister(), >> any concurrent call to try_module_get() will fail (if no one has taken the > > Are you still taking care of your previous finding [1]? It says: > > To be clear, you should only use try_module_get() *iff* you are 100% sure > the module already does exist ... > > IIUC, if you do nothing on fpga_mgr_unregister(), the low-level module may > just disappear and any copy of the owner pointer became invalid. Then > try_module_get() would not fail but panic. > > [1] 557aafac1153 ("kernel/module: add documentation for try_module_get()") > You are right. I'll follow your proposal for locking. I would have preferred a solution that kept the module locked until it was safe to remove it, but I cannot come up with anything reasonable at the moment. > >> module before) without the need to check mops first. >> >> If we assume (as you pointed out) that class_find_device() can be safely >> executed concurrently with device_unregister() (returning either a valid dev >> pointer or null) and, consequently, the manager context is guaranteed to exist >> after that point. Then, we should be good without the mutex if we call >> try_module_get() on a copy of the owner pointer stored in the manager context. >> >>>> user. It will still be an improvement over taking the low-level >>>> module's refcount through the parent device's driver pointer. >>>> Thanks, Marco
diff --git a/drivers/fpga/fpga-mgr.c b/drivers/fpga/fpga-mgr.c index 06651389c592..608605d59860 100644 --- a/drivers/fpga/fpga-mgr.c +++ b/drivers/fpga/fpga-mgr.c @@ -21,6 +21,8 @@ static DEFINE_IDA(fpga_mgr_ida); static const struct class fpga_mgr_class; +static DEFINE_MUTEX(mgr_lock); + struct fpga_mgr_devres { struct fpga_manager *mgr; }; @@ -667,17 +669,15 @@ ATTRIBUTE_GROUPS(fpga_mgr); static struct fpga_manager *__fpga_mgr_get(struct device *dev) { struct fpga_manager *mgr; + struct module *owner; mgr = to_fpga_manager(dev); + owner = mgr->owner; - if (!try_module_get(dev->parent->driver->owner)) - goto err_dev; + if (owner && !try_module_get(owner)) + mgr = ERR_PTR(-ENODEV); return mgr; - -err_dev: - put_device(dev); - return ERR_PTR(-ENODEV); } static int fpga_mgr_dev_match(struct device *dev, const void *data) @@ -693,12 +693,22 @@ static int fpga_mgr_dev_match(struct device *dev, const void *data) */ struct fpga_manager *fpga_mgr_get(struct device *dev) { - struct device *mgr_dev = class_find_device(&fpga_mgr_class, NULL, dev, - fpga_mgr_dev_match); + struct fpga_manager *mgr = ERR_PTR(-ENODEV); + struct device *mgr_dev; + + mutex_lock(&mgr_lock); + + mgr_dev = class_find_device(&fpga_mgr_class, NULL, dev, fpga_mgr_dev_match); if (!mgr_dev) - return ERR_PTR(-ENODEV); + goto out; + + mgr = __fpga_mgr_get(mgr_dev); + if (IS_ERR(mgr)) + put_device(mgr_dev); - return __fpga_mgr_get(mgr_dev); +out: + mutex_unlock(&mgr_lock); + return mgr; } EXPORT_SYMBOL_GPL(fpga_mgr_get); @@ -711,13 +721,22 @@ EXPORT_SYMBOL_GPL(fpga_mgr_get); */ struct fpga_manager *of_fpga_mgr_get(struct device_node *node) { - struct device *dev; + struct fpga_manager *mgr = ERR_PTR(-ENODEV); + struct device *mgr_dev; + + mutex_lock(&mgr_lock); + + mgr_dev = class_find_device_by_of_node(&fpga_mgr_class, node); + if (!mgr_dev) + goto out; - dev = class_find_device_by_of_node(&fpga_mgr_class, node); - if (!dev) - return ERR_PTR(-ENODEV); + mgr = __fpga_mgr_get(mgr_dev); + if (IS_ERR(mgr)) + put_device(mgr_dev); - return __fpga_mgr_get(dev); +out: + mutex_unlock(&mgr_lock); + return mgr; } EXPORT_SYMBOL_GPL(of_fpga_mgr_get); @@ -727,7 +746,7 @@ EXPORT_SYMBOL_GPL(of_fpga_mgr_get); */ void fpga_mgr_put(struct fpga_manager *mgr) { - module_put(mgr->dev.parent->driver->owner); + module_put(mgr->owner); put_device(&mgr->dev); } EXPORT_SYMBOL_GPL(fpga_mgr_put); @@ -806,6 +825,7 @@ fpga_mgr_register_full(struct device *parent, const struct fpga_manager_info *in mgr->name = info->name; mgr->mops = info->mops; + mgr->owner = info->mops->owner; mgr->priv = info->priv; mgr->compat_id = info->compat_id; @@ -888,7 +908,11 @@ void fpga_mgr_unregister(struct fpga_manager *mgr) */ fpga_mgr_fpga_remove(mgr); + mutex_lock(&mgr_lock); + device_unregister(&mgr->dev); + + mutex_unlock(&mgr_lock); } EXPORT_SYMBOL_GPL(fpga_mgr_unregister); diff --git a/include/linux/fpga/fpga-mgr.h b/include/linux/fpga/fpga-mgr.h index 54f63459efd6..eaf6e072dbc0 100644 --- a/include/linux/fpga/fpga-mgr.h +++ b/include/linux/fpga/fpga-mgr.h @@ -162,6 +162,7 @@ struct fpga_manager_info { * @write_complete: set FPGA to operating state after writing is done * @fpga_remove: optional: Set FPGA into a specific state during driver remove * @groups: optional attribute groups. + * @owner: owner module. * * fpga_manager_ops are the low level functions implemented by a specific * fpga manager driver. The optional ones are tested for NULL before being @@ -184,6 +185,7 @@ struct fpga_manager_ops { struct fpga_image_info *info); void (*fpga_remove)(struct fpga_manager *mgr); const struct attribute_group **groups; + struct module *owner; }; /* FPGA manager status: Partial/Full Reconfiguration errors */ @@ -201,6 +203,7 @@ struct fpga_manager_ops { * @state: state of fpga manager * @compat_id: FPGA manager id for compatibility check. * @mops: pointer to struct of fpga manager ops + * @owner: owner module. * @priv: low level driver private date */ struct fpga_manager { @@ -210,6 +213,7 @@ struct fpga_manager { enum fpga_mgr_states state; struct fpga_compat_id *compat_id; const struct fpga_manager_ops *mops; + struct module *owner; void *priv; };