Re: [PATCH v5 02/17] dmaengine: sh: rz-dmac: Fix incorrect NULL check on list_first_entry()
From: Frank Li
Date: Tue May 12 2026 - 16:36:28 EST
On Tue, May 12, 2026 at 03:12:03PM +0300, Claudiu Beznea wrote:
> The list passed as argument to list_first_entry() is expected to be not
> empty.
Little confused,
#define list_first_entry_or_null(ptr, type, member) ({ \
struct list_head *head__ = (ptr); \
struct list_head *pos__ = READ_ONCE(head__->next); \
pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
})
both list passed to list_first_entry() or list_first_entry_or_null() must
be not NULL.
The return value is difference.
Fix incorrect NULL check for list_first_entry()
list_first_entry() does not return NULL when the list is empty,
making the existing NULL check invalid. Use list_first_entry_or_null()
instead.
Frank
> Use list_first_entry_or_null() to avoid dereferencing invalid
> memory.
>
> Fixes: 21323b118c16 ("dmaengine: sh: rz-dmac: Add device_tx_status() callback")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Claudiu Beznea <claudiu.beznea.uj@xxxxxxxxxxxxxx>
> ---
>
> Changes in v5:
> - none
>
> Changes in v4:
> - none, this patch is new
>
> drivers/dma/sh/rz-dmac.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/dma/sh/rz-dmac.c b/drivers/dma/sh/rz-dmac.c
> index 9f206a33dcc6..6d80cb668957 100644
> --- a/drivers/dma/sh/rz-dmac.c
> +++ b/drivers/dma/sh/rz-dmac.c
> @@ -723,8 +723,8 @@ static u32 rz_dmac_chan_get_residue(struct rz_dmac_chan *channel,
> u32 crla, crtb, i;
>
> /* Get current processing virtual descriptor */
> - current_desc = list_first_entry(&channel->ld_active,
> - struct rz_dmac_desc, node);
> + current_desc = list_first_entry_or_null(&channel->ld_active,
> + struct rz_dmac_desc, node);
> if (!current_desc)
> return 0;
>
> --
> 2.43.0
>