linux-kernel-mentees.lists.linuxfoundation.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] driver: gpu: Fix warning directly dereferencing a rcu pointer
@ 2023-11-26 14:57 Abhinav Singh
  2023-11-29 23:52 ` Danilo Krummrich
  0 siblings, 1 reply; 3+ messages in thread
From: Abhinav Singh @ 2023-11-26 14:57 UTC (permalink / raw)
  To: kherbst, lyude, dakr, airlied, daniel
  Cc: dri-devel, nouveau, linux-kernel-mentees, Abhinav Singh

Fix a sparse warning with this message
"warning:dereference of noderef expression". In this context it means we
are dereferencing a __rcu tagged pointer directly.

We should not be directly dereferencing a rcu pointer. To get a normal
(non __rcu tagged pointer) from a __rcu tagged pointer we are using the
function unrcu_pointer(...). The non __rcu tagged pointer then can be
dereferenced just like a normal pointer.

I tested with qemu with this command 
qemu-system-x86_64 \
	-m 2G \
	-smp 2 \
	-kernel bzImage \
	-append "console=ttyS0 root=/dev/sda earlyprintk=serial net.ifnames=0" \
	-drive file=bullseye.img,format=raw \
	-net user,host=10.0.2.10,hostfwd=tcp:127.0.0.1:10021-:22 \
	-net nic,model=e1000 \
	-enable-kvm \
	-nographic \
	-pidfile vm.pid \
	2>&1 | tee vm.log
with lockdep enabled.

Signed-off-by: Abhinav Singh <singhabhinav9051571833@gmail.com>
---
 drivers/gpu/drm/nouveau/nv10_fence.c | 2 +-
 drivers/gpu/drm/nouveau/nv84_fence.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/nouveau/nv10_fence.c b/drivers/gpu/drm/nouveau/nv10_fence.c
index c6a0db5b9e21..845b64c079ed 100644
--- a/drivers/gpu/drm/nouveau/nv10_fence.c
+++ b/drivers/gpu/drm/nouveau/nv10_fence.c
@@ -32,7 +32,7 @@
 int
 nv10_fence_emit(struct nouveau_fence *fence)
 {
-	struct nvif_push *push = fence->channel->chan.push;
+	struct nvif_push *push = unrcu_pointer(fence->channel)->chan.push;
 	int ret = PUSH_WAIT(push, 2);
 	if (ret == 0) {
 		PUSH_MTHD(push, NV06E, SET_REFERENCE, fence->base.seqno);
diff --git a/drivers/gpu/drm/nouveau/nv84_fence.c b/drivers/gpu/drm/nouveau/nv84_fence.c
index 812b8c62eeba..d42e72e23dec 100644
--- a/drivers/gpu/drm/nouveau/nv84_fence.c
+++ b/drivers/gpu/drm/nouveau/nv84_fence.c
@@ -85,7 +85,7 @@ nv84_fence_chid(struct nouveau_channel *chan)
 static int
 nv84_fence_emit(struct nouveau_fence *fence)
 {
-	struct nouveau_channel *chan = fence->channel;
+	struct nouveau_channel *chan = unrcu_pointer(fence->channel);
 	struct nv84_fence_chan *fctx = chan->fence;
 	u64 addr = fctx->vma->addr + nv84_fence_chid(chan) * 16;
 
-- 
2.39.2


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

* Re: [PATCH] driver: gpu: Fix warning directly dereferencing a rcu pointer
  2023-11-26 14:57 [PATCH] driver: gpu: Fix warning directly dereferencing a rcu pointer Abhinav Singh
@ 2023-11-29 23:52 ` Danilo Krummrich
  2023-12-03  8:11   ` Abhinav Singh
  0 siblings, 1 reply; 3+ messages in thread
From: Danilo Krummrich @ 2023-11-29 23:52 UTC (permalink / raw)
  To: Abhinav Singh
  Cc: kherbst, lyude, airlied, daniel, dri-devel, nouveau,
	linux-kernel-mentees

Hi Abhinav,

Thanks for sending this follow-up patch.

On 11/26/23 15:57, Abhinav Singh wrote:
> Fix a sparse warning with this message
> "warning:dereference of noderef expression". In this context it means we
> are dereferencing a __rcu tagged pointer directly.
> 
> We should not be directly dereferencing a rcu pointer. To get a normal
> (non __rcu tagged pointer) from a __rcu tagged pointer we are using the
> function unrcu_pointer(...). The non __rcu tagged pointer then can be
> dereferenced just like a normal pointer.

Can you please add a brief explanation why unrcu_pointer() is fine here?

> 
> I tested with qemu with this command
> qemu-system-x86_64 \
> 	-m 2G \
> 	-smp 2 \
> 	-kernel bzImage \
> 	-append "console=ttyS0 root=/dev/sda earlyprintk=serial net.ifnames=0" \
> 	-drive file=bullseye.img,format=raw \
> 	-net user,host=10.0.2.10,hostfwd=tcp:127.0.0.1:10021-:22 \
> 	-net nic,model=e1000 \
> 	-enable-kvm \
> 	-nographic \
> 	-pidfile vm.pid \
> 	2>&1 | tee vm.log
> with lockdep enabled.

How is that relevant for this patch?

- Danilo

> 
> Signed-off-by: Abhinav Singh <singhabhinav9051571833@gmail.com>
> ---
>   drivers/gpu/drm/nouveau/nv10_fence.c | 2 +-
>   drivers/gpu/drm/nouveau/nv84_fence.c | 2 +-
>   2 files changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/gpu/drm/nouveau/nv10_fence.c b/drivers/gpu/drm/nouveau/nv10_fence.c
> index c6a0db5b9e21..845b64c079ed 100644
> --- a/drivers/gpu/drm/nouveau/nv10_fence.c
> +++ b/drivers/gpu/drm/nouveau/nv10_fence.c
> @@ -32,7 +32,7 @@
>   int
>   nv10_fence_emit(struct nouveau_fence *fence)
>   {
> -	struct nvif_push *push = fence->channel->chan.push;
> +	struct nvif_push *push = unrcu_pointer(fence->channel)->chan.push;
>   	int ret = PUSH_WAIT(push, 2);
>   	if (ret == 0) {
>   		PUSH_MTHD(push, NV06E, SET_REFERENCE, fence->base.seqno);
> diff --git a/drivers/gpu/drm/nouveau/nv84_fence.c b/drivers/gpu/drm/nouveau/nv84_fence.c
> index 812b8c62eeba..d42e72e23dec 100644
> --- a/drivers/gpu/drm/nouveau/nv84_fence.c
> +++ b/drivers/gpu/drm/nouveau/nv84_fence.c
> @@ -85,7 +85,7 @@ nv84_fence_chid(struct nouveau_channel *chan)
>   static int
>   nv84_fence_emit(struct nouveau_fence *fence)
>   {
> -	struct nouveau_channel *chan = fence->channel;
> +	struct nouveau_channel *chan = unrcu_pointer(fence->channel);
>   	struct nv84_fence_chan *fctx = chan->fence;
>   	u64 addr = fctx->vma->addr + nv84_fence_chid(chan) * 16;
>   


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

* Re: [PATCH] driver: gpu: Fix warning directly dereferencing a rcu pointer
  2023-11-29 23:52 ` Danilo Krummrich
@ 2023-12-03  8:11   ` Abhinav Singh
  0 siblings, 0 replies; 3+ messages in thread
From: Abhinav Singh @ 2023-12-03  8:11 UTC (permalink / raw)
  To: Danilo Krummrich
  Cc: kherbst, lyude, airlied, daniel, dri-devel, nouveau,
	linux-kernel-mentees

On 11/30/23 05:22, Danilo Krummrich wrote:
> Hi Abhinav,
> 
> Thanks for sending this follow-up patch.
> 
> On 11/26/23 15:57, Abhinav Singh wrote:
>> Fix a sparse warning with this message
>> "warning:dereference of noderef expression". In this context it means we
>> are dereferencing a __rcu tagged pointer directly.
>>
>> We should not be directly dereferencing a rcu pointer. To get a normal
>> (non __rcu tagged pointer) from a __rcu tagged pointer we are using the
>> function unrcu_pointer(...). The non __rcu tagged pointer then can be
>> dereferenced just like a normal pointer.
> 
> Can you please add a brief explanation why unrcu_pointer() is fine here?
Is this description okay
"The reason for using unrcu_pointer(...) instead of rcu_dereference(...)
or rcu_dereference_protected(...) is because, before nv10_fence_emit() 
and nv_04_fence_emit() did not add this fence to the fence context's
pending list, thus channel doesn't need any protection" ?
> 
>>
>> I tested with qemu with this command
>> qemu-system-x86_64 \
>>     -m 2G \
>>     -smp 2 \
>>     -kernel bzImage \
>>     -append "console=ttyS0 root=/dev/sda earlyprintk=serial 
>> net.ifnames=0" \
>>     -drive file=bullseye.img,format=raw \
>>     -net user,host=10.0.2.10,hostfwd=tcp:127.0.0.1:10021-:22 \
>>     -net nic,model=e1000 \
>>     -enable-kvm \
>>     -nographic \
>>     -pidfile vm.pid \
>>     2>&1 | tee vm.log
>> with lockdep enabled.
> 
> How is that relevant for this patch?
> 
> - Danilo
To test rcu related code lockdep must be enabled, it gives any warning 
or error message if we are dealing inappropriately with rcu pointers. So 
I tested this lockdep enabled. I added the test description in this 
patch as well 
https://lore.kernel.org/all/0754e669-8b00-461c-b6fe-79c659bf59a3@redhat.com/ 
which is very similar to this patch so I thought I should here as well. 
Is it not relevant here?

Thank You,
Abhinav Singh
> 
>>
>> Signed-off-by: Abhinav Singh <singhabhinav9051571833@gmail.com>
>> ---
>>   drivers/gpu/drm/nouveau/nv10_fence.c | 2 +-
>>   drivers/gpu/drm/nouveau/nv84_fence.c | 2 +-
>>   2 files changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/gpu/drm/nouveau/nv10_fence.c 
>> b/drivers/gpu/drm/nouveau/nv10_fence.c
>> index c6a0db5b9e21..845b64c079ed 100644
>> --- a/drivers/gpu/drm/nouveau/nv10_fence.c
>> +++ b/drivers/gpu/drm/nouveau/nv10_fence.c
>> @@ -32,7 +32,7 @@
>>   int
>>   nv10_fence_emit(struct nouveau_fence *fence)
>>   {
>> -    struct nvif_push *push = fence->channel->chan.push;
>> +    struct nvif_push *push = unrcu_pointer(fence->channel)->chan.push;
>>       int ret = PUSH_WAIT(push, 2);
>>       if (ret == 0) {
>>           PUSH_MTHD(push, NV06E, SET_REFERENCE, fence->base.seqno);
>> diff --git a/drivers/gpu/drm/nouveau/nv84_fence.c 
>> b/drivers/gpu/drm/nouveau/nv84_fence.c
>> index 812b8c62eeba..d42e72e23dec 100644
>> --- a/drivers/gpu/drm/nouveau/nv84_fence.c
>> +++ b/drivers/gpu/drm/nouveau/nv84_fence.c
>> @@ -85,7 +85,7 @@ nv84_fence_chid(struct nouveau_channel *chan)
>>   static int
>>   nv84_fence_emit(struct nouveau_fence *fence)
>>   {
>> -    struct nouveau_channel *chan = fence->channel;
>> +    struct nouveau_channel *chan = unrcu_pointer(fence->channel);
>>       struct nv84_fence_chan *fctx = chan->fence;
>>       u64 addr = fctx->vma->addr + nv84_fence_chid(chan) * 16;
> 


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

end of thread, other threads:[~2023-12-03  8:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-26 14:57 [PATCH] driver: gpu: Fix warning directly dereferencing a rcu pointer Abhinav Singh
2023-11-29 23:52 ` Danilo Krummrich
2023-12-03  8:11   ` Abhinav Singh

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).