Maarten Lankhorst
2023-Nov-13 08:24 UTC
[Nouveau] [PATCH] driver: gpu: Fixing warning directly dereferencing a rcu pointer
Hey, Den 2023-11-13 kl. 09:10, skrev Abhinav Singh:> This patch fixes 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, rather we should > be using rcu helper function rcu_dereferece() inside rcu read critical > section to get a normal pointer which can be dereferenced. > > 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 at gmail.com> > --- > drivers/gpu/drm/nouveau/nv04_fence.c | 4 +++- > 1 file changed, 3 insertions(+), 1 deletion(-) > > diff --git a/drivers/gpu/drm/nouveau/nv04_fence.c b/drivers/gpu/drm/nouveau/nv04_fence.c > index 5b71a5a5cd85..e62bad1ac720 100644 > --- a/drivers/gpu/drm/nouveau/nv04_fence.c > +++ b/drivers/gpu/drm/nouveau/nv04_fence.c > @@ -39,7 +39,9 @@ struct nv04_fence_priv { > static int > nv04_fence_emit(struct nouveau_fence *fence) > { > - struct nvif_push *push = fence->channel->chan.push; > + rcu_read_lock(); > + struct nvif_push *push = rcu_dereference(fence->channel)->chan.push; > + rcu_read_unlock(); > int ret = PUSH_WAIT(push, 2); > if (ret == 0) { > PUSH_NVSQ(push, NV_SW, 0x0150, fence->base.seqno);I'm not an expert in nouveau fence channel lifetime, but I'm pretty sure this should probably be a rcu_dereference_protected, since a fence likely can't lose its channel before its command to signal is submitted. But in case it's not, I would at least advise to check for fence->channel lifetime before submitting a patch like this. At least the original code warned about it not being 100% correct. Cheers, ~Maarten
Danilo Krummrich
2023-Nov-13 17:10 UTC
[Nouveau] [PATCH] driver: gpu: Fixing warning directly dereferencing a rcu pointer
On 11/13/23 09:24, Maarten Lankhorst wrote:> Hey, > > Den 2023-11-13 kl. 09:10, skrev Abhinav Singh: >> This patch fixes 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, rather we should >> be using rcu helper function rcu_dereferece() inside rcu read critical >> section to get a normal pointer which can be dereferenced. >> >> 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 at gmail.com> >> --- >> ? drivers/gpu/drm/nouveau/nv04_fence.c | 4 +++- >> ? 1 file changed, 3 insertions(+), 1 deletion(-) >> >> diff --git a/drivers/gpu/drm/nouveau/nv04_fence.c b/drivers/gpu/drm/nouveau/nv04_fence.c >> index 5b71a5a5cd85..e62bad1ac720 100644 >> --- a/drivers/gpu/drm/nouveau/nv04_fence.c >> +++ b/drivers/gpu/drm/nouveau/nv04_fence.c >> @@ -39,7 +39,9 @@ struct nv04_fence_priv { >> ? static int >> ? nv04_fence_emit(struct nouveau_fence *fence) >> ? { >> -??? struct nvif_push *push = fence->channel->chan.push; >> +??? rcu_read_lock(); >> +??? struct nvif_push *push = rcu_dereference(fence->channel)->chan.push; >> +??? rcu_read_unlock(); >> ????? int ret = PUSH_WAIT(push, 2); >> ????? if (ret == 0) { >> ????????? PUSH_NVSQ(push, NV_SW, 0x0150, fence->base.seqno); > > I'm not an expert in nouveau fence channel lifetime, but I'm pretty sure this should probably be a rcu_dereference_protected, since a fence likely can't lose its channel before its command to signal is submitted.Yes, before nouveau_fence_emit() did not add this fence to the fence context's pending list ->channel doesn't need any protection. We can probably just use unrcu_pointer(), as in [1]. [1] https://elixir.bootlin.com/linux/latest/source/drivers/gpu/drm/nouveau/nouveau_fence.c#L210> > But in case it's not, I would at least advise to check for fence->channel lifetime before submitting a patch like this. At least the original code warned about it not being 100% correct. > > Cheers, > > ~Maarten >