linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] PCI/P2PDMA: Annotate RCU dereference
@ 2023-02-09 17:29 Logan Gunthorpe
  2023-02-09 17:53 ` Chaitanya Kulkarni
  2023-02-14 22:08 ` Bjorn Helgaas
  0 siblings, 2 replies; 3+ messages in thread
From: Logan Gunthorpe @ 2023-02-09 17:29 UTC (permalink / raw)
  To: linux-pci, linux-kernel, Bjorn Helgaas
  Cc: Chaitanya Kulkarni, Logan Gunthorpe, kernel test robot

A dereference of the __rcu pointer was noticed by sparse:

  drivers/pci/p2pdma.c:199:44: sparse: sparse: dereference of noderef expression

The __rcu pointer should be dereferenced using
rcu_dereference_protected() instead of accessed directly. Its safe
to use rcu_derference_protected() seeing a reference is held on
the pgmap's percpu reference counter and thus it cannot disappear.

Reported-by: kernel test robot <lkp@intel.com>
Signed-off-by: Logan Gunthorpe <logang@deltatee.com>
---

Based on v6.2-rc7

 drivers/pci/p2pdma.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/pci/p2pdma.c b/drivers/pci/p2pdma.c
index 86812d2073ea..9e8205572830 100644
--- a/drivers/pci/p2pdma.c
+++ b/drivers/pci/p2pdma.c
@@ -194,11 +194,13 @@ static const struct attribute_group p2pmem_group = {
 static void p2pdma_page_free(struct page *page)
 {
 	struct pci_p2pdma_pagemap *pgmap = to_p2p_pgmap(page->pgmap);
+	/* safe to dereference while a reference is held to the percpu ref */
+	struct pci_p2pdma *p2pdma =
+		rcu_dereference_protected(pgmap->provider->p2pdma, 1);
 	struct percpu_ref *ref;

-	gen_pool_free_owner(pgmap->provider->p2pdma->pool,
-			    (uintptr_t)page_to_virt(page), PAGE_SIZE,
-			    (void **)&ref);
+	gen_pool_free_owner(p2pdma->pool, (uintptr_t)page_to_virt(page),
+			    PAGE_SIZE, (void **)&ref);
 	percpu_ref_put(ref);
 }


base-commit: 4ec5183ec48656cec489c49f989c508b68b518e3
--
2.30.2

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] PCI/P2PDMA: Annotate RCU dereference
  2023-02-09 17:29 [PATCH] PCI/P2PDMA: Annotate RCU dereference Logan Gunthorpe
@ 2023-02-09 17:53 ` Chaitanya Kulkarni
  2023-02-14 22:08 ` Bjorn Helgaas
  1 sibling, 0 replies; 3+ messages in thread
From: Chaitanya Kulkarni @ 2023-02-09 17:53 UTC (permalink / raw)
  To: Logan Gunthorpe, linux-pci, linux-kernel, Bjorn Helgaas; +Cc: kernel test robot

On 2/9/23 09:29, Logan Gunthorpe wrote:
> A dereference of the __rcu pointer was noticed by sparse:
> 
>    drivers/pci/p2pdma.c:199:44: sparse: sparse: dereference of noderef expression
> 
> The __rcu pointer should be dereferenced using
> rcu_dereference_protected() instead of accessed directly. Its safe
> to use rcu_derference_protected() seeing a reference is held on
> the pgmap's percpu reference counter and thus it cannot disappear.
> 
> Reported-by: kernel test robot <lkp@intel.com>
> Signed-off-by: Logan Gunthorpe <logang@deltatee.com>
> ---

I believe that this change removes the warning is question.

With that said, looks good.

Reviewed-by: Chaitanya Kulkarni <kch@nvidia.com>

-ck

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] PCI/P2PDMA: Annotate RCU dereference
  2023-02-09 17:29 [PATCH] PCI/P2PDMA: Annotate RCU dereference Logan Gunthorpe
  2023-02-09 17:53 ` Chaitanya Kulkarni
@ 2023-02-14 22:08 ` Bjorn Helgaas
  1 sibling, 0 replies; 3+ messages in thread
From: Bjorn Helgaas @ 2023-02-14 22:08 UTC (permalink / raw)
  To: Logan Gunthorpe
  Cc: linux-pci, linux-kernel, Bjorn Helgaas, Chaitanya Kulkarni,
	kernel test robot

On Thu, Feb 09, 2023 at 10:29:53AM -0700, Logan Gunthorpe wrote:
> A dereference of the __rcu pointer was noticed by sparse:
> 
>   drivers/pci/p2pdma.c:199:44: sparse: sparse: dereference of noderef expression
> 
> The __rcu pointer should be dereferenced using
> rcu_dereference_protected() instead of accessed directly. Its safe
> to use rcu_derference_protected() seeing a reference is held on
> the pgmap's percpu reference counter and thus it cannot disappear.
> 
> Reported-by: kernel test robot <lkp@intel.com>
> Signed-off-by: Logan Gunthorpe <logang@deltatee.com>

Applied with Chaitanya's reviewed-by to pci/p2pdma for v6.3, thanks!

> ---
> 
> Based on v6.2-rc7
> 
>  drivers/pci/p2pdma.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/pci/p2pdma.c b/drivers/pci/p2pdma.c
> index 86812d2073ea..9e8205572830 100644
> --- a/drivers/pci/p2pdma.c
> +++ b/drivers/pci/p2pdma.c
> @@ -194,11 +194,13 @@ static const struct attribute_group p2pmem_group = {
>  static void p2pdma_page_free(struct page *page)
>  {
>  	struct pci_p2pdma_pagemap *pgmap = to_p2p_pgmap(page->pgmap);
> +	/* safe to dereference while a reference is held to the percpu ref */
> +	struct pci_p2pdma *p2pdma =
> +		rcu_dereference_protected(pgmap->provider->p2pdma, 1);
>  	struct percpu_ref *ref;
> 
> -	gen_pool_free_owner(pgmap->provider->p2pdma->pool,
> -			    (uintptr_t)page_to_virt(page), PAGE_SIZE,
> -			    (void **)&ref);
> +	gen_pool_free_owner(p2pdma->pool, (uintptr_t)page_to_virt(page),
> +			    PAGE_SIZE, (void **)&ref);
>  	percpu_ref_put(ref);
>  }
> 
> 
> base-commit: 4ec5183ec48656cec489c49f989c508b68b518e3
> --
> 2.30.2

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-02-14 22:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-09 17:29 [PATCH] PCI/P2PDMA: Annotate RCU dereference Logan Gunthorpe
2023-02-09 17:53 ` Chaitanya Kulkarni
2023-02-14 22:08 ` Bjorn Helgaas

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).