Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp2713043lqz; Wed, 3 Apr 2024 06:39:03 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWc29RGxbt69dAsQOfLSi0iz+Y0bUK5NLZQjv6crEnBDFUzlgX7pH11Ar9WlDr/keXPrgoAadQJ08Sm7RemV2MMTt4eUq0ucnBvHuEXcg== X-Google-Smtp-Source: AGHT+IFVuoCK9q6fqCC4zxI9MbvKpaNhnIq3vUmYs472Gzwnxo+6OFJFyDATsPZ2KnWw0BfPJ+EK X-Received: by 2002:a17:90a:de94:b0:2a1:f586:d203 with SMTP id n20-20020a17090ade9400b002a1f586d203mr2474840pjv.41.1712151542739; Wed, 03 Apr 2024 06:39:02 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712151542; cv=pass; d=google.com; s=arc-20160816; b=gwwGuawp6TxwEjiK19cHnWnlR7aLlqeFSKD6XJ/7/Bc+4A/5ta1x2S7o73FCsEXnpV /z0/IXXe8uC4YtQ63b8JLPP2MQ90oQuv6TUEJZKepWwePZt+exUBhPEGXJPC9zI1/A1m 6NTmcXgZk+CuiFULmoWLaSe6UighEtS5Tze2YwjQ2/xrbxs6CY1gopZDB3YBZ8x9l8uK xUKvCnnnPMVWUZPEQ5ClVnqFSECDNPerR7zFFr6A4mgJwd6hDCfLhdg/JdoP5m2UqRgc V3/pxm+P/NzsHBP0Iv2HnyNYprvAtdpkqFu7wfma2nJHCMc2fRb3cpkgPLA36nhV3OJt ADKw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=ZF+AJqNgdEEhHyUGebHGFGETeFDomSQcf3CdIhYx3HE=; fh=juLyn4/cJswvynskLsiehvF38SaqgqlqenMumd1rMO4=; b=DmCNymrGkQ9kc/kyuIyNmwv90l3kpMFt4UGzyWd0by+Gu/xjz8el4cjWKJrmVhkvWG QLUiWzverDCbMsaRr7nvibztScydU90kiVAVlhwOWsZ0CmWG4NL+7hh4YK0UIZfSTQM4 rY/PLJuFFzaGR+/EWbTyhHZ6lKHfV8s0UNz9JOG0zF/P9l5Ed+hyi0H/KG5hmykdnXaS U9XydxC2ZXsUtA1un89GwHs0k3vghRFbzwvvW6hQ376OhnMOQvnGqG5TBBja1RN92Sn+ DPvgPt+D9cetWqfVYFWgouc3Z4I60rC6wE7SbmNW3nJq8jb/o7GSOi8pAjbFIO/4IHWJ 4g+A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=EZrwjHom; 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-129850-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-129850-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. [139.178.88.99]) by mx.google.com with ESMTPS id hg6-20020a17090b300600b002a2a2fbd745si902591pjb.109.2024.04.03.06.39.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 06:39:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-129850-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=EZrwjHom; 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-129850-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-129850-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 97AE528C46E for ; Wed, 3 Apr 2024 13:35:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3C3221487C4; Wed, 3 Apr 2024 13:34:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="EZrwjHom" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 0DFD6148303 for ; Wed, 3 Apr 2024 13:34:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712151270; cv=none; b=Y9KY71ZH+iMoZkUeXuwNerqeuIApw37JlHTt7eJjZ1UolXdHJJKKeMOPiWIYM/2z5E7adu8BP4WXdg5V5iwFUwYpNxiujjtbbeO6Dy1s6YPlBGZ3PM4RdUVuxfIZOOdzkPtIW0cCsZUww5bjELGS+8NkDMQDeWI6DWhpIeg088E= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712151270; c=relaxed/simple; bh=d0DirP2/DcnMHmap5JRHS10akoeyY4HwoPDoR/NLxSo=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=nrnteYmYOGG+HbnVEED1cWYKJDWa4G0ke8Ga65Apimu/dCXMAWrWu67dcGOvWIH2COPk9q6zyLD53lOScf/MCsbshqMJ5mKlPWN/zEdDgZ6T9shDwFhFiNihPOnCKIl/6LhTrYXPYz9JgX0s+KgjpqtBrbs1XDhVtBpd2HaCZXU= 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=EZrwjHom; arc=none smtp.client-ip=170.10.129.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=1712151267; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ZF+AJqNgdEEhHyUGebHGFGETeFDomSQcf3CdIhYx3HE=; b=EZrwjHomwpWBlET0jhs2ZESpyvDqetDc9rby5ypIrDgFHTur4Yo4g1WZ4FLyP2uX+H9NhI sPuATJ2OWy96nMsCVwcuP6MKLUREWE54ureuDWi6+tsFTy1QHUL0a2QgfkTPRloXTS1Yt6 d6nCqxh+RyW9UzOIjm+QMyG3yxadgr8= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-245-l7Y8UzK3MjKvjDyeYTiJLA-1; Wed, 03 Apr 2024 09:34:25 -0400 X-MC-Unique: l7Y8UzK3MjKvjDyeYTiJLA-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-4161b5d077aso9522465e9.0 for ; Wed, 03 Apr 2024 06:34:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712151264; x=1712756064; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ZF+AJqNgdEEhHyUGebHGFGETeFDomSQcf3CdIhYx3HE=; b=YU36EK1wWrTIA7r4s/ur5yUAWMVsosu/3tfCgN/H9QZJMef00FpM9wfSdZx1O1TIMo Gd2nQWYuTW5LiUEDiKQ3OSl7um/3PzVL+k5ifpu8vEhhT82FLzfGF3n0/6epSxNr47is Cjs6mOaaMNXi7NEJuM/BBVTNLnCqE17TJKu0kfOsWxiNJIKmQDMOO4mCF1prCnHV5B1o LViXM76jAJOT2Jyn40cmsRfj9YFb9Np6nN3QizW99DfSekHqn6pCDHN67MdAczFVVDco iT19+OsEga316znJ9Z8QeQgXHFgA4xSG8gfkCYD+N0vRYFUsVRGkmR72zgCYPAujoEey 2Dew== X-Forwarded-Encrypted: i=1; AJvYcCUlUI1R0DPWtM9nhCIQtMdboanRrvSIXk86OKqZ4dblpBRe6PlUSZ1DoRwYFNKmznP4fKoCREFlXMRVUnIrYHWX7e1P0TktLC96ZXRg X-Gm-Message-State: AOJu0Yzo3+n349346O1owaB+FTQZdBma58w3sM6j801rv7scfPyuOZvx YVsueoq8AChTUtcXg7F6s9N/64J4C7ujYidJ/M991r+BgPZO5G/s1wUhwAV2c8mLcOxvCcXMbN9 D2NMSGyCqECAVcIL6IjhnvMXm14YlpXuUR4i/nlV+vfkEByujVGMLDAs/mMI7 X-Received: by 2002:a05:600c:35d6:b0:415:46be:6239 with SMTP id r22-20020a05600c35d600b0041546be6239mr10742068wmq.36.1712151264679; Wed, 03 Apr 2024 06:34:24 -0700 (PDT) X-Received: by 2002:a05:600c:35d6:b0:415:46be:6239 with SMTP id r22-20020a05600c35d600b0041546be6239mr10742054wmq.36.1712151264329; Wed, 03 Apr 2024 06:34:24 -0700 (PDT) Received: from [192.168.9.34] (net-2-34-25-239.cust.vodafonedsl.it. [2.34.25.239]) by smtp.gmail.com with ESMTPSA id n18-20020a05600c4f9200b004148c3685ffsm21867330wmq.3.2024.04.03.06.34.23 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 03 Apr 2024 06:34:23 -0700 (PDT) Message-ID: <64c1685a-b544-408e-97e4-8c3cff6aca6c@redhat.com> Date: Wed, 3 Apr 2024 15:34:22 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] fpga: region: add owner module and take its refcount To: Xu Yilun Cc: Moritz Fischer , Wu Hao , Xu Yilun , Tom Rix , Jonathan Corbet , Greg Kroah-Hartman , Alan Tull , linux-fpga@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org References: <20240327160022.202934-1-marpagan@redhat.com> Content-Language: en-US From: Marco Pagani In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 2024-04-01 11:34, Xu Yilun wrote: > On Wed, Mar 27, 2024 at 05:00:20PM +0100, Marco Pagani wrote: >> 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. >> >> Other changes: unlock the mutex before calling put_device() in >> fpga_region_put() to avoid potential use after release issues. > > Please try not to mix different changes in one patch, especially for > a "bug fix" as you said. You are right. I'll split out the change and eventually send it as a separate patch. > And I do have concern about the fix, see below. > > [...] > >> @@ -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->br_owner)) { >> put_device(dev); >> mutex_unlock(®ion->mutex); >> return ERR_PTR(-ENODEV); >> @@ -75,9 +75,9 @@ static void fpga_region_put(struct fpga_region *region) >> >> dev_dbg(dev, "put\n"); >> >> - module_put(dev->parent->driver->owner); >> - put_device(dev); >> + module_put(region->br_owner); >> mutex_unlock(®ion->mutex); > > If there is concern the region would be freed after put_device(), then > why still keep the sequence in fpga_region_get()? Ouch, sorry, I forgot to make the change also in fpga_region_get(). > And is it possible region is freed before get_device() in > fpga_region_get()? If the user follows the usual pattern (i.e., waiting for fpga_region_program_fpga() to complete before calling fpga_region_unregister()) there should be no problem. However, I think releasing the device before unlocking the mutex contained in the context associated with the device makes the code brittle and more prone to problems. > Or we should clearly document how/when to use these functions? I think it is not necessary to change the documentation since the in-kernel programming API will not be affected by the change. Thanks, Marco