Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp486217lqt; Fri, 19 Apr 2024 01:36:19 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCU7FyrIb3uZm7+X6NJR+M/dmkEOUZKALm2hK43E31Z6eC+gO7qWUWbVgTqMPTbSVRaXeWBpYYmUimoIF65Wp+0wWJxdGkroug0MlyJEEg== X-Google-Smtp-Source: AGHT+IF6aRw9KycFVT2Z1pKRCmSQ5bUbjcySuz6GzGGnBvi/gUYvbwLLDrbpbHvNJ/ax3/rid4h9 X-Received: by 2002:a05:6870:6241:b0:22e:f3cd:b6b7 with SMTP id r1-20020a056870624100b0022ef3cdb6b7mr1597259oak.27.1713515778811; Fri, 19 Apr 2024 01:36:18 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713515778; cv=pass; d=google.com; s=arc-20160816; b=AQdXMFLy58kPbKVNfGmv7MZET37tUTW63zf3Cmq+9U8cUKl2vv6EdHx11m1Ezo7hXi bN5pKLVE5QwmVHZB5ZebNG4Jewb6CK/8X72j+Wv0uxXLR2mzIbeJscSPW0nN6+G8Dke+ S2qBrNTYvd/KrRtH1xrt7F6EHoPa8iATLuOr/jDUM+8UpXu1vyYmG1Uaz20lYyWbBy6a 0/HD/JnfgE47Hci1BmI3XWyl22Dmnl+6ksOjlsUlU16GmhqK7f5olyVvAH0Mgamzhyxo WaTdpHGaaaAEQdW3WNd2YE2rW3wU2/hUGQ9PjcsPWZrOat4qpURkDLfgi8tcENKDHVvi y+7g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=m8Z4JwBEgcNs/PB0AnWXpJ2AEsgxb+gwBzYIddtlxkc=; fh=2UCR8V1dto8ECtG9cP+ZVCIreY+NJe6kwcEZvdRQzTU=; b=IhQ98bDmXfk4XjpYlLrgmHjWJz3CPBa793gzdvm3muN+49dQ9ieldctQxGBNgBcLON P7kVjnrbpGHVcXSmuGlzYTucpsQc6S0k+z156O8gfFn1VP3/qgkZfLXqSeymyjjq6b4t g/qt0rJ2DAKpdNzhyp/YsZ2a8OKdFaVmwysmBZwzrvrFWHZaLc016lyhaEs8x6aEHCj0 Dg8GwnlUo6tG8FeWnj75A2H+plpEI62r+0rTzOuCK/VbSeSLAVN/++AnE60Kh5S0trpK UL5KTPo/Nwrzz7/pIp8vVOnbZAHUxSVlEkeoFMZh/h1f2J8AbyQMOS9y0rU/Uh4sdikr 63ag==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=iwh3bAjA; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-151175-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-151175-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id kp7-20020a056a00464700b006ece3da5268si2999470pfb.391.2024.04.19.01.36.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Apr 2024 01:36:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-151175-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=iwh3bAjA; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-151175-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-151175-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 5ED84281346 for ; Fri, 19 Apr 2024 08:36:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 23F6C5FB8C; Fri, 19 Apr 2024 08:36:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="iwh3bAjA" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3E498405EC for ; Fri, 19 Apr 2024 08:36:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713515772; cv=none; b=AQHvNeXZczeeUs3l0D9yhcYjwkXw86u1JcR/lNwt8naCKPwiSmRBnIiKfIbT/8SGEC7Mp/7fKA6074rrftomqUkOosDe9RA3z2hx4rVEcCt5XgJvNmEUk0Mbibuljpw6X9JwBefUxfGbSvJbuxf6jW7GYThCfj2aOatVuwWgSuI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713515772; c=relaxed/simple; bh=ki32pQWZPdeOnqu6wy6iaFLnakDcjYIYqsw/JYzRlS0=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=bLpr9q6hm8NbqB64b6T85kokHbUtRBfgVQlxTASJMpi3/iJxh0QruDnxMltlR3tqmWHRBzAMGFCp4QN24MMbmZbBOkHTQLsgmPxfeKxszXZrbRk2WZLVtyIX+t6yuyzz2lxm/tSkN5pROj395U6nlbwQzBlDrxePCRB+AxjLm7c= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=iwh3bAjA; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1713515769; 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=m8Z4JwBEgcNs/PB0AnWXpJ2AEsgxb+gwBzYIddtlxkc=; b=iwh3bAjAv9C/g9W9LmObq1/+7OUuOmZc1joXM3KnSXFC4oRTYlNuILel3PoU9OtTxK6ZVx OAASKRNjafzcc5/6uzxLdkJJamuduIo5LkFxWoYHIfqnhQsS8AKn9LOpoNf0h+b5FB19O8 3CXSn1PnEN/PK4Le1ncSlhWrz0clEZE= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-286-QYApYOt_NyKSwxW-nIT9hQ-1; Fri, 19 Apr 2024 04:36:08 -0400 X-MC-Unique: QYApYOt_NyKSwxW-nIT9hQ-1 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-69b631714d4so27876836d6.3 for ; Fri, 19 Apr 2024 01:36:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713515767; x=1714120567; 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=m8Z4JwBEgcNs/PB0AnWXpJ2AEsgxb+gwBzYIddtlxkc=; b=UhEvc8fK7aT4ZLRJsnjjpgyk9afGDG9hujMqiPdi3qNlQ/xOcDeBbmAy/z0r+Qr7IH Nj2DAgD1deconXQjIt6S4PhNRCoz4RekCLut+SQpCyLY83mpxQ07Pa/8DTyaavhX1CqJ vQcNE++XNK27LP4lrD4PnEsGMo3uLvIDd7e/fEkkXhl4C4mNzw7YezYxaRAXSbA4Gdle WCOao+zP5Vy9ZsRyJ+NV2yDm15Vm9JDE6/QZagNWY9YrU4SLYa22uGFQYiRVSRbbdBbl nSzlVhha3lIA1M4qIgCCMnL3DbLr0DVLY/oEVKlcKNHtUcBNh3w8IArHEglzUrm6TW0i qjpQ== X-Forwarded-Encrypted: i=1; AJvYcCUad4O126BfBupciTuWqlfZIMVgtHLFKHdVVhlBA+PEzqRI4sAC+ha7zdVK2Yy1shX+XtLdG1itdE0GfyCHowiG7BTPd1dZAPlFUGc4 X-Gm-Message-State: AOJu0Yx6UJpTZ+akzdElIZhaiD63B6bPKFC3gq6SLTyb7I+esrcZRers agag4cRNLYo+M2VgWAASJcNSshr5ty2rCN8++O1jRtkGZV5d1U2wRUJiGs0eETxizxwwsnZlSBt nbajEEvPqd01v9u1lllyfqDdnbvErbFTDSnUNtTuF7dlnXizgl1f+N4AW9Nbh X-Received: by 2002:a0c:f583:0:b0:69b:695e:bde1 with SMTP id k3-20020a0cf583000000b0069b695ebde1mr1579693qvm.5.1713515767576; Fri, 19 Apr 2024 01:36:07 -0700 (PDT) X-Received: by 2002:a0c:f583:0:b0:69b:695e:bde1 with SMTP id k3-20020a0cf583000000b0069b695ebde1mr1579679qvm.5.1713515767287; Fri, 19 Apr 2024 01:36:07 -0700 (PDT) Received: from klayman.redhat.com (net-2-34-26-208.cust.vodafonedsl.it. [2.34.26.208]) by smtp.gmail.com with ESMTPSA id n11-20020a0ce48b000000b0069b6c831e86sm1368482qvl.97.2024.04.19.01.36.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Apr 2024 01:36:07 -0700 (PDT) From: Marco Pagani To: Moritz Fischer , Wu Hao , Xu Yilun , Tom Rix , Jonathan Corbet , Alan Tull , Greg Kroah-Hartman Cc: Marco Pagani , Russ Weight , linux-fpga@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v6] fpga: region: add owner module and take its refcount Date: Fri, 19 Apr 2024 10:35:59 +0200 Message-ID: <20240419083601.77403-1-marpagan@redhat.com> X-Mailer: git-send-email 2.44.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The current implementation of the fpga region assumes that the low-level module registers a driver for the parent device and uses its owner pointer to take the module's refcount. This approach is problematic since it can lead to a null pointer dereference while attempting to get the region during programming if the parent device does not have a driver. To address this problem, add a module owner pointer to the fpga_region struct and use it to take the module's refcount. Modify the functions for registering a region to take an additional owner module parameter and rename them to avoid conflicts. Use the old function names for helper macros that automatically set the module that registers the region as the owner. This ensures compatibility with existing low-level control modules and reduces the chances of registering a region without setting the owner. Also, update the documentation to keep it consistent with the new interface for registering an fpga region. Fixes: 0fa20cdfcc1f ("fpga: fpga-region: device tree control for FPGA") Suggested-by: Greg Kroah-Hartman Suggested-by: Xu Yilun Reviewed-by: Russ Weight Signed-off-by: Marco Pagani Acked-by: Xu Yilun --- v6: - Fixed typos in the @owner parameter description v5: - Reverted function names swap in the documentation - Renamed owner module pointer br_owner -> ops_owner v4: - Split out the swap between put_device() and mutex_unlock() while releasing the region to avoid potential use after release issues v3: - Add reviewed-by Russ Weight v2: - Fixed typo in the documentation sets -> set - Renamed owner module pointer get_br_owner -> br_owner --- Documentation/driver-api/fpga/fpga-region.rst | 13 ++++++---- drivers/fpga/fpga-region.c | 24 +++++++++++-------- include/linux/fpga/fpga-region.h | 13 +++++++--- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/Documentation/driver-api/fpga/fpga-region.rst b/Documentation/driver-api/fpga/fpga-region.rst index dc55d60a0b4a..2d03b5fb7657 100644 --- a/Documentation/driver-api/fpga/fpga-region.rst +++ b/Documentation/driver-api/fpga/fpga-region.rst @@ -46,13 +46,16 @@ API to add a new FPGA region ---------------------------- * struct fpga_region - The FPGA region struct -* struct fpga_region_info - Parameter structure for fpga_region_register_full() -* fpga_region_register_full() - Create and register an FPGA region using the +* struct fpga_region_info - Parameter structure for __fpga_region_register_full() +* __fpga_region_register_full() - Create and register an FPGA region using the fpga_region_info structure to provide the full flexibility of options -* fpga_region_register() - Create and register an FPGA region using standard +* __fpga_region_register() - Create and register an FPGA region using standard arguments * fpga_region_unregister() - Unregister an FPGA region +Helper macros ``fpga_region_register()`` and ``fpga_region_register_full()`` +automatically set the module that registers the FPGA region as the owner. + The FPGA region's probe function will need to get a reference to the FPGA Manager it will be using to do the programming. This usually would happen during the region's probe function. @@ -82,10 +85,10 @@ following APIs to handle building or tearing down that list. :functions: fpga_region_info .. kernel-doc:: drivers/fpga/fpga-region.c - :functions: fpga_region_register_full + :functions: __fpga_region_register_full .. kernel-doc:: drivers/fpga/fpga-region.c - :functions: fpga_region_register + :functions: __fpga_region_register .. kernel-doc:: drivers/fpga/fpga-region.c :functions: fpga_region_unregister diff --git a/drivers/fpga/fpga-region.c b/drivers/fpga/fpga-region.c index b364a929425c..753cd142503e 100644 --- a/drivers/fpga/fpga-region.c +++ b/drivers/fpga/fpga-region.c @@ -53,7 +53,7 @@ static struct fpga_region *fpga_region_get(struct fpga_region *region) } get_device(dev); - if (!try_module_get(dev->parent->driver->owner)) { + if (!try_module_get(region->ops_owner)) { put_device(dev); mutex_unlock(®ion->mutex); return ERR_PTR(-ENODEV); @@ -75,7 +75,7 @@ static void fpga_region_put(struct fpga_region *region) dev_dbg(dev, "put\n"); - module_put(dev->parent->driver->owner); + module_put(region->ops_owner); put_device(dev); mutex_unlock(®ion->mutex); } @@ -181,14 +181,16 @@ static struct attribute *fpga_region_attrs[] = { ATTRIBUTE_GROUPS(fpga_region); /** - * fpga_region_register_full - create and register an FPGA Region device + * __fpga_region_register_full - create and register an FPGA Region device * @parent: device parent * @info: parameters for FPGA Region + * @owner: module containing the get_bridges function * * Return: struct fpga_region or ERR_PTR() */ struct fpga_region * -fpga_region_register_full(struct device *parent, const struct fpga_region_info *info) +__fpga_region_register_full(struct device *parent, const struct fpga_region_info *info, + struct module *owner) { struct fpga_region *region; int id, ret = 0; @@ -213,6 +215,7 @@ fpga_region_register_full(struct device *parent, const struct fpga_region_info * region->compat_id = info->compat_id; region->priv = info->priv; region->get_bridges = info->get_bridges; + region->ops_owner = owner; mutex_init(®ion->mutex); INIT_LIST_HEAD(®ion->bridge_list); @@ -241,13 +244,14 @@ fpga_region_register_full(struct device *parent, const struct fpga_region_info * return ERR_PTR(ret); } -EXPORT_SYMBOL_GPL(fpga_region_register_full); +EXPORT_SYMBOL_GPL(__fpga_region_register_full); /** - * fpga_region_register - create and register an FPGA Region device + * __fpga_region_register - create and register an FPGA Region device * @parent: device parent * @mgr: manager that programs this region * @get_bridges: optional function to get bridges to a list + * @owner: module containing the get_bridges function * * This simple version of the register function should be sufficient for most users. * The fpga_region_register_full() function is available for users that need to @@ -256,17 +260,17 @@ EXPORT_SYMBOL_GPL(fpga_region_register_full); * Return: struct fpga_region or ERR_PTR() */ struct fpga_region * -fpga_region_register(struct device *parent, struct fpga_manager *mgr, - int (*get_bridges)(struct fpga_region *)) +__fpga_region_register(struct device *parent, struct fpga_manager *mgr, + int (*get_bridges)(struct fpga_region *), struct module *owner) { struct fpga_region_info info = { 0 }; info.mgr = mgr; info.get_bridges = get_bridges; - return fpga_region_register_full(parent, &info); + return __fpga_region_register_full(parent, &info, owner); } -EXPORT_SYMBOL_GPL(fpga_region_register); +EXPORT_SYMBOL_GPL(__fpga_region_register); /** * fpga_region_unregister - unregister an FPGA region diff --git a/include/linux/fpga/fpga-region.h b/include/linux/fpga/fpga-region.h index 9d4d32909340..5fbc05fe70a6 100644 --- a/include/linux/fpga/fpga-region.h +++ b/include/linux/fpga/fpga-region.h @@ -36,6 +36,7 @@ struct fpga_region_info { * @mgr: FPGA manager * @info: FPGA image info * @compat_id: FPGA region id for compatibility check. + * @ops_owner: module containing the get_bridges function * @priv: private data * @get_bridges: optional function to get bridges to a list */ @@ -46,6 +47,7 @@ struct fpga_region { struct fpga_manager *mgr; struct fpga_image_info *info; struct fpga_compat_id *compat_id; + struct module *ops_owner; void *priv; int (*get_bridges)(struct fpga_region *region); }; @@ -58,12 +60,17 @@ fpga_region_class_find(struct device *start, const void *data, int fpga_region_program_fpga(struct fpga_region *region); +#define fpga_region_register_full(parent, info) \ + __fpga_region_register_full(parent, info, THIS_MODULE) struct fpga_region * -fpga_region_register_full(struct device *parent, const struct fpga_region_info *info); +__fpga_region_register_full(struct device *parent, const struct fpga_region_info *info, + struct module *owner); +#define fpga_region_register(parent, mgr, get_bridges) \ + __fpga_region_register(parent, mgr, get_bridges, THIS_MODULE) struct fpga_region * -fpga_region_register(struct device *parent, struct fpga_manager *mgr, - int (*get_bridges)(struct fpga_region *)); +__fpga_region_register(struct device *parent, struct fpga_manager *mgr, + int (*get_bridges)(struct fpga_region *), struct module *owner); void fpga_region_unregister(struct fpga_region *region); #endif /* _FPGA_REGION_H */ base-commit: 5d04660b29fb31e88e945c8b3eb658976824d0fa -- 2.44.0