Received: by 2002:a05:7412:a9a2:b0:e2:908c:2ebd with SMTP id o34csp1243891rdh; Fri, 27 Oct 2023 08:31:53 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHnkWe5v1XhZHvqF5jGfai7wTjsTmsUSqCRZ606qPN+ngIwmT+Oo3OIxWJcgNLCSOfarv3u X-Received: by 2002:a81:d40b:0:b0:5a7:be33:8bd4 with SMTP id z11-20020a81d40b000000b005a7be338bd4mr3385086ywi.2.1698420713527; Fri, 27 Oct 2023 08:31:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698420713; cv=none; d=google.com; s=arc-20160816; b=JxQzHaORiUN/vEuSSRmlBWcn4g17rLz6ljZLb7YngUu9CtBIe/oVgFW5VBXoOy/HeP f4cuOcn6lvwAP0IHNHEVAiihEX6TirPdnzF/PUhY+AoKYh7FCVC5+szXv8qx8GG57yUl 843nReE3IY1EwqLG5r5Ndm01vywpqN72fhNLgN2mjA9NWXPZmwef3RtYHRHw4pUTjEva g4O/Ww+YnmHSyyGTa81VLufR/AwLVMmSNX22KcfKP86HyCj84rUqadsqhM3L94kXLyU1 aUTBe9CElUoE9cq8CC+M6YUSLa6yZl+LLTq327VVzNnoYxXMptCd+q9kbXEdlu+e43CA JsRA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=J/UFA7u/XXiPmDgo1cL607vFxp7ihV1o+SEyqiKodo8=; fh=AxiQz74Slas/KjyK+kq2DxLIEWe+Aiabdv5iDlk7FXI=; b=Gi+0qOaU6s8BCOyYUc79y84wWZNFVMZhyihQz1UDlCiiicnEIBQ27Z7svfV296Tlao Jq9DiJ0vB/1GEwq+/dXReMRgLlRc72oYozbgusx7+QlHPFVAVWXnutkdiklOPtd0vQBi y8TGx6O858FLe9oTvLki2PagSl0bPChpN6i3W2eDS44/aE7+jF36o3q90Os+q8ooNcG8 qgHr25icEsG2YJ1cK+WIg8ntpKUA4GNAWfDpKgP349NcZol6a+7CSbl/isAdVed8L39+ K3fCzTjRN09jIdMLxPS4GER+k9mJLsZmB8jsbn2LOJNX1zUiIXExhT8l0vQy5qsBDpcC e4Vw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=WLcdGcDU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id bt20-20020a05690c073400b005afd11670a4si2040003ywb.479.2023.10.27.08.31.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Oct 2023 08:31:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=WLcdGcDU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (Postfix) with ESMTP id 4CC14837341C; Fri, 27 Oct 2023 08:31:49 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346257AbjJ0Pbj (ORCPT + 99 others); Fri, 27 Oct 2023 11:31:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231793AbjJ0Pbi (ORCPT ); Fri, 27 Oct 2023 11:31:38 -0400 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 3C90AAF for ; Fri, 27 Oct 2023 08:30:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698420651; 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; bh=J/UFA7u/XXiPmDgo1cL607vFxp7ihV1o+SEyqiKodo8=; b=WLcdGcDUpKaw83tVy5nN3xK55KgJow/yxVoBmapeybme0VfmIZ+gcLp8uRbKR76tB4MwcP IzDHTd06vJ5bJRtp+BcrfAnprESxcMRGdYvzo1LE3+AGX3c2owo9O5X7NcplKOGHw9zqOi yzGJIVILevlBojAUVTIO1Jwlesw3zJo= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-2-mX5ZNU_FMF2HoJCh-ZiGYw-1; Fri, 27 Oct 2023 11:29:42 -0400 X-MC-Unique: mX5ZNU_FMF2HoJCh-ZiGYw-1 Received: by mail-qv1-f69.google.com with SMTP id 6a1803df08f44-66cfd45de06so27848536d6.0 for ; Fri, 27 Oct 2023 08:29:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698420582; x=1699025382; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=J/UFA7u/XXiPmDgo1cL607vFxp7ihV1o+SEyqiKodo8=; b=BU3SAtpKCT46jP9kERqs1mGUvefnSuIi2gA11obWYHqC+BRkPIy6iks2L2fXj8LWLn PzVMjlTaydlQwafEiJG4g8Cfs8I8dC4jB8BvSMAK6yK3ouUYe27R/fXEOwWBYbqSKxvs g5nXgZTITlpLEHJdeDN1kiJqKqGXkAUI2KKvL/sV0i5O2GxcfhRKNvqgBsLEzqGDc8x9 ScJ1/sREK5uGN65d0a6ti2N6CTRG5wYLaes/dyEnB5J/g9h4JeSyhjk8RSLVnd96UaqP 56QOKhM4iuN4Lrl113lJLIahakw7tI9aQM8U3gA8YDotMzjmcyZ622fK9rSiBs8gvvxA hdvQ== X-Gm-Message-State: AOJu0YwSJcQrsZNLJCF/gIthsNoTATDOo+WWm9w9Ep2Oh6b3qJza44Dr T5MzGQ5Bi8GmyPHOI6z4CScGtnGHIf/2xZuo6pqP9dMKLkFkC9wKZNSZS3SuPqUvKp31x7AqQuv 1QaPHhM+WDN8edU4ugI+Q8BM= X-Received: by 2002:ad4:5967:0:b0:66d:6406:1301 with SMTP id eq7-20020ad45967000000b0066d64061301mr3464104qvb.20.1698420581921; Fri, 27 Oct 2023 08:29:41 -0700 (PDT) X-Received: by 2002:ad4:5967:0:b0:66d:6406:1301 with SMTP id eq7-20020ad45967000000b0066d64061301mr3464084qvb.20.1698420581621; Fri, 27 Oct 2023 08:29:41 -0700 (PDT) Received: from klayman.redhat.com (net-2-34-31-107.cust.vodafonedsl.it. [2.34.31.107]) by smtp.gmail.com with ESMTPSA id fc8-20020ad44f28000000b0066d32666a27sm691316qvb.66.2023.10.27.08.29.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Oct 2023 08:29:41 -0700 (PDT) From: Marco Pagani To: Moritz Fischer , Wu Hao , Xu Yilun , Tom Rix , Alan Tull , Greg Kroah-Hartman Cc: Marco Pagani , linux-fpga@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH] fpga: remove module reference counting from core components Date: Fri, 27 Oct 2023 17:29:27 +0200 Message-ID: <20231027152928.184012-1-marpagan@redhat.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.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 (howler.vger.email [0.0.0.0]); Fri, 27 Oct 2023 08:31:49 -0700 (PDT) Remove unnecessary module reference counting from the core components of the subsystem. Low-level driver modules cannot be removed before core modules since they use their exported symbols. For more context, refer to this thread: https://lore.kernel.org/linux-fpga/ZS6hhlvjUcqyv8zL@yilunxu-OptiPlex-7050 Other changes: In __fpga_bridge_get(): do a (missing ?) get_device() and bind the image to the bridge only after the mutex has been acquired. In __fpga_mgr_get(): do a get_device(). Currently, get_device() is called when allocating an image in fpga_image_info_alloc(). However, since there are still two (of_)fpga_mgr_get() functions exposed by the core, I think they should behave as expected. In fpga_region_get() / fpga_region_put(): call get_device() before acquiring the mutex and put_device() after having released the mutex to avoid races. Fixes: 654ba4cc0f3e ("fpga manager: ensure lifetime with of_fpga_mgr_get") Signed-off-by: Marco Pagani --- drivers/fpga/fpga-bridge.c | 24 +++++++----------------- drivers/fpga/fpga-mgr.c | 8 +------- drivers/fpga/fpga-region.c | 14 ++++---------- 3 files changed, 12 insertions(+), 34 deletions(-) diff --git a/drivers/fpga/fpga-bridge.c b/drivers/fpga/fpga-bridge.c index a024be2b84e2..3bcc9c9849c5 100644 --- a/drivers/fpga/fpga-bridge.c +++ b/drivers/fpga/fpga-bridge.c @@ -58,30 +58,21 @@ EXPORT_SYMBOL_GPL(fpga_bridge_disable); static struct fpga_bridge *__fpga_bridge_get(struct device *dev, struct fpga_image_info *info) { - struct fpga_bridge *bridge; - int ret = -ENODEV; - - bridge = to_fpga_bridge(dev); + struct fpga_bridge *bridge = to_fpga_bridge(dev); - bridge->info = info; + get_device(dev); if (!mutex_trylock(&bridge->mutex)) { - ret = -EBUSY; - goto err_dev; + dev_dbg(dev, "%s: FPGA Bridge already in use\n", __func__); + put_device(dev); + return ERR_PTR(-EBUSY); } - if (!try_module_get(dev->parent->driver->owner)) - goto err_ll_mod; + bridge->info = info; dev_dbg(&bridge->dev, "get\n"); return bridge; - -err_ll_mod: - mutex_unlock(&bridge->mutex); -err_dev: - put_device(dev); - return ERR_PTR(ret); } /** @@ -93,7 +84,7 @@ static struct fpga_bridge *__fpga_bridge_get(struct device *dev, * Return: * * fpga_bridge struct pointer if successful. * * -EBUSY if someone already has a reference to the bridge. - * * -ENODEV if @np is not an FPGA Bridge or can't take parent driver refcount. + * * -ENODEV if @np is not an FPGA Bridge. */ struct fpga_bridge *of_fpga_bridge_get(struct device_node *np, struct fpga_image_info *info) @@ -146,7 +137,6 @@ void fpga_bridge_put(struct fpga_bridge *bridge) dev_dbg(&bridge->dev, "put\n"); bridge->info = NULL; - module_put(bridge->dev.parent->driver->owner); mutex_unlock(&bridge->mutex); put_device(&bridge->dev); } diff --git a/drivers/fpga/fpga-mgr.c b/drivers/fpga/fpga-mgr.c index 06651389c592..6c355eafd18f 100644 --- a/drivers/fpga/fpga-mgr.c +++ b/drivers/fpga/fpga-mgr.c @@ -670,14 +670,9 @@ static struct fpga_manager *__fpga_mgr_get(struct device *dev) mgr = to_fpga_manager(dev); - if (!try_module_get(dev->parent->driver->owner)) - goto err_dev; + get_device(&mgr->dev); return mgr; - -err_dev: - put_device(dev); - return ERR_PTR(-ENODEV); } static int fpga_mgr_dev_match(struct device *dev, const void *data) @@ -727,7 +722,6 @@ EXPORT_SYMBOL_GPL(of_fpga_mgr_get); */ void fpga_mgr_put(struct fpga_manager *mgr) { - module_put(mgr->dev.parent->driver->owner); put_device(&mgr->dev); } EXPORT_SYMBOL_GPL(fpga_mgr_put); diff --git a/drivers/fpga/fpga-region.c b/drivers/fpga/fpga-region.c index b364a929425c..c299956cafdc 100644 --- a/drivers/fpga/fpga-region.c +++ b/drivers/fpga/fpga-region.c @@ -41,22 +41,17 @@ EXPORT_SYMBOL_GPL(fpga_region_class_find); * Return: * * fpga_region struct if successful. * * -EBUSY if someone already has a reference to the region. - * * -ENODEV if can't take parent driver module refcount. */ static struct fpga_region *fpga_region_get(struct fpga_region *region) { struct device *dev = ®ion->dev; + get_device(dev); + if (!mutex_trylock(®ion->mutex)) { dev_dbg(dev, "%s: FPGA Region already in use\n", __func__); - return ERR_PTR(-EBUSY); - } - - get_device(dev); - if (!try_module_get(dev->parent->driver->owner)) { put_device(dev); - mutex_unlock(®ion->mutex); - return ERR_PTR(-ENODEV); + return ERR_PTR(-EBUSY); } dev_dbg(dev, "get\n"); @@ -75,9 +70,8 @@ static void fpga_region_put(struct fpga_region *region) dev_dbg(dev, "put\n"); - module_put(dev->parent->driver->owner); - put_device(dev); mutex_unlock(®ion->mutex); + put_device(dev); } /** -- 2.41.0