media: venus: don't de-reference NULL pointers at IRQ time
authorMauro Carvalho Chehab <mchehab+huawei@kernel.org>
Thu, 8 Apr 2021 06:49:12 +0000 (08:49 +0200)
committerMauro Carvalho Chehab <mchehab+huawei@kernel.org>
Thu, 8 Apr 2021 08:04:20 +0000 (10:04 +0200)
commit686ee9b6253f9b6d7f1151e73114698940cc0894
treedda8df182732fa1c21fadee1a7405ef586263f20
parentb6f139947e93fec1ade5faf3517dfb2b3b9bcd41
media: venus: don't de-reference NULL pointers at IRQ time

Smatch is warning that:
drivers/media/platform/qcom/venus/hfi_venus.c:1100 venus_isr() warn: variable dereferenced before check 'hdev' (see line 1097)

The logic basically does:
hdev = to_hfi_priv(core);

with is translated to:
hdev = core->priv;

If the IRQ code can receive a NULL pointer for hdev, there's
a bug there, as it will first try to de-reference the pointer,
and then check if it is null.

After looking at the code, it seems that this indeed can happen:
Basically, the venus IRQ thread is started with:
devm_request_threaded_irq()
So, it will only be freed after the driver unbinds.

In order to prevent the IRQ code to work with freed data,
the logic at venus_hfi_destroy() sets core->priv to NULL,
which would make the IRQ code to ignore any pending IRQs.

There is, however a race condition, as core->priv is set
to NULL only after being freed. So, we need also to move the
core->priv = NULL to happen earlier.

Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
drivers/media/platform/qcom/venus/hfi_venus.c