From patchwork Wed Nov 22 14:32:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Pagani X-Patchwork-Id: 168398 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2b07:b0:403:3b70:6f57 with SMTP id io7csp1365930vqb; Wed, 22 Nov 2023 06:33:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IG1gydt6msODAmiRNajmRK0ZsbPWIAd373sN7/VHrBfsPKqe9MT5iI6VUlqS8T7q3zPpC60 X-Received: by 2002:a05:6a21:300a:b0:189:c551:8905 with SMTP id yd10-20020a056a21300a00b00189c5518905mr2012391pzb.32.1700663605411; Wed, 22 Nov 2023 06:33:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700663605; cv=none; d=google.com; s=arc-20160816; b=ysGrcG004YT0RzG/hqO0ExpYt3YkjvHgSUEXetrEE3H8Eyp7tWIq53wYiT4zmuY6Lg ZT6m6ewu6Jwsp5I2FInZq9q8fR2Si2Iqa9QRdW0qu4k3lDD6E1mY7zOCEOiTg9MDkxRt 7cTXo5G0GROk04YheX5Kng6LZI9VYiivBpdVd+IaWjovG2vP8YMcNBtvrl5YQdGKqPIE Cslp8A8nN7s2MHJIGL0XyguZK2H2xZm/dyEhM4/PRtHtcp41YvtgIu62e+FnpMUwaORj UilumyIYQXfW+Radftvijsi7GQ+2GxQ5sIwv06I727oK6UsRdII04DHfCib5EH5CjRRO leKA== 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=DZFOSfIM7TPtd3vStgatz2Vc2HN+6585nrKoz3NT+MI=; fh=RI0mNHrSGBk/0gJ3tsBPdYn+81ROXQTqubQc1unKFUU=; b=PQvjcqJX+rUVEXSxEcru/aaiJbZVeC91Ja4lWRIMieJA+TlGciiytODHxQA5GPB9b+ un2wSoVqJIAybeKsYHJh2YIdY+dJpFujPqLv+oDiGBaElyIz7/RFhBvcnAJ1ityGFH2W inEZQiJIuqBvaDGLk6JhzVQnEUO8pvK5brIqI6jjHES7xsgCVpiXZwmUDtzgoN0jG2bw mv8q5ZWXFvSi2YgKV/fEDUqYpcSxGpYNYqbvmQ9IRQoVR1WfUMTvMnYQA0kRmtgMTUQG hFtFWdPr+BFk8YNXoyfVoHJJ2twGwdFsz2FLIyVUBlPS4cS2cuYVFWsOPJRif+pnwNk7 z5FA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=aXQIxUQl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id w13-20020a170902e88d00b001cc76bea504si12966033plg.345.2023.11.22.06.33.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Nov 2023 06:33:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=aXQIxUQl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (Postfix) with ESMTP id 531DD8244E41; Wed, 22 Nov 2023 06:33:18 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231720AbjKVOdS (ORCPT + 99 others); Wed, 22 Nov 2023 09:33:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48096 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232207AbjKVOdQ (ORCPT ); Wed, 22 Nov 2023 09:33:16 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B42C5D5C for ; Wed, 22 Nov 2023 06:33:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700663590; 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=DZFOSfIM7TPtd3vStgatz2Vc2HN+6585nrKoz3NT+MI=; b=aXQIxUQl3wlQ3yhNsUi5kn+bYU1p9O/DfUvxmVo3xjMvUCoJuZvS32RW1sIiwvSe1iBxsG DgguowqbDYEXO2Gjm8UPqttF+TSgDQoSjV8mNS0BOI1ww/cZo4hdnaCa1eGhjC2xx0HPwh 4UUvHLT+snJxeWFuKQTE+IoyX+kCj2U= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-317-RUJ8uCMUNjOpuZUNnHVBxg-1; Wed, 22 Nov 2023 09:33:08 -0500 X-MC-Unique: RUJ8uCMUNjOpuZUNnHVBxg-1 Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-421b20c9893so94839691cf.0 for ; Wed, 22 Nov 2023 06:33:08 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700663588; x=1701268388; 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=DZFOSfIM7TPtd3vStgatz2Vc2HN+6585nrKoz3NT+MI=; b=AqvCynrQozKLZVpEdpU5GScwEhv4V9phVkD+098mHHKavEanMNgU8msJPQw5tlRPJf /l/GBT3pWo1BPRLsgASX+vthsLxRsyBpTL+Ic/HgqYIQGIrA6cAdIYkNAHiaaEQtzA2Z GF/ZFMQx3HU2QsC0yCd10Dkpv5wq2F/LRSqIuaULlsxip+D/aCu9ayucfXXFP1BpRXCu J1QtTmAWE4YzXCQbWS275Y/L40aShinAGl6JRDhk12KDHiShKPHBGQyvAKwpfK+WfOie 68z6QbcRQ2+5tezWjzWrqaZuNR3sOOuJS5lexDW3UhOy8MtGR/8LsMP9X9o8uhJOZz65 nxHQ== X-Gm-Message-State: AOJu0Yx4SZRZfB2d00DM5AxPNNWXKv4f+x/wsqmogC4+6tsLv/fG8euT y5O5ydW8zPfGpOo6snFagNECvr1e9izoOwOhr835pRgyZX+FVU/ebYtgt0oqX5Hd6/LxeJcrEpR DQpfjAcBtP3tx3AcxInz/mrk= X-Received: by 2002:ac8:7f82:0:b0:417:d340:c426 with SMTP id z2-20020ac87f82000000b00417d340c426mr2710695qtj.9.1700663587712; Wed, 22 Nov 2023 06:33:07 -0800 (PST) X-Received: by 2002:ac8:7f82:0:b0:417:d340:c426 with SMTP id z2-20020ac87f82000000b00417d340c426mr2710678qtj.9.1700663587434; Wed, 22 Nov 2023 06:33:07 -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 v7-20020ac87487000000b0041950c7f6d8sm4430101qtq.60.2023.11.22.06.33.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Nov 2023 06:33:07 -0800 (PST) From: Marco Pagani To: Moritz Fischer , Wu Hao , Xu Yilun , Tom Rix , Greg Kroah-Hartman Cc: Marco Pagani , linux-kernel@vger.kernel.org, linux-fpga@vger.kernel.org Subject: [RFC PATCH 1/2] fpga: add a module owner field to fpga_manager and fpga_manager_ops Date: Wed, 22 Nov 2023 15:32:51 +0100 Message-ID: <20231122143252.181680-2-marpagan@redhat.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231122143252.181680-1-marpagan@redhat.com> References: <20231122143252.181680-1-marpagan@redhat.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Wed, 22 Nov 2023 06:33:18 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783275040856749809 X-GMAIL-MSGID: 1783275040856749809 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, the fpga_mgr_get() and fpga_mgr_put() methods are protected with a mutex. Other changes: moved put_device() form __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 --- 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..f76be7e2f722 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,8 +746,12 @@ EXPORT_SYMBOL_GPL(of_fpga_mgr_get); */ void fpga_mgr_put(struct fpga_manager *mgr) { - module_put(mgr->dev.parent->driver->owner); + mutex_lock(&mgr_lock); + + module_put(mgr->owner); put_device(&mgr->dev); + + mutex_unlock(&mgr_lock); } EXPORT_SYMBOL_GPL(fpga_mgr_put); @@ -806,6 +829,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; 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; };