Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] media: imagination: e5010: uninitialized hardware after e5010_reset()
--
commit 17ce3535d8dd09d68990c2b4330437290166712c
Author: Brandon Brnich <b-brnich(a)ti.com>
media: imagination: e5010: Enable autosuspend for runtime PM
This commit enables runtime PM autosuspend for the e5010 JPEG encoder,
moving the runtime PM get and put calls from start_streaming and
stop_streaming into e5010_device_run and e5010_irq, and clearing
last_context_run in the runtime resume callback.
> diff --git a/drivers/media/platform/imagination/e5010-jpeg-enc.c b/drivers/media/platform/imagination/e5010-jpeg-enc.c
> index c25ab9627518e..228137243f749 100644
> --- a/drivers/media/platform/imagination/e5010-jpeg-enc.c
> +++ b/drivers/media/platform/imagination/e5010-jpeg-enc.c
[ ... ]
> @@ -1304,31 +1315,13 @@ static int e5010_encoder_cmd(struct file *file, void *priv,
> static int e5010_start_streaming(struct vb2_queue *q, unsigned int count)
> {
> struct e5010_context *ctx = vb2_get_drv_priv(q);
> - int ret;
>
> struct e5010_q_data *queue = get_queue(ctx, q->type);
>
> v4l2_m2m_update_start_streaming_state(ctx->fh.m2m_ctx, q);
> queue->sequence = 0;
>
> - ret = pm_runtime_resume_and_get(ctx->e5010->dev);
> - if (ret < 0) {
> - v4l2_err(&ctx->e5010->v4l2_dev, "failed to power up jpeg\n");
> - goto fail;
> - }
> -
> - ret = e5010_init_device(ctx->e5010);
> - if (ret) {
> - v4l2_err(&ctx->e5010->v4l2_dev, "failed to Enable e5010 device\n");
> - goto fail;
> - }
> -
> return 0;
> -
> -fail:
> - e5010_vb2_buffers_return(q, VB2_BUF_STATE_QUEUED);
> -
> - return ret;
> }
[ ... ]
> @@ -1487,7 +1485,8 @@ static void e5010_device_run(void *priv)
>
> device_busy_err:
> e5010_reset(e5010->dev, e5010->core_base, e5010->mmu_base);
> -
> + spin_unlock_irqrestore(&e5010->hw_lock, flags);
> + pm_runtime_put_autosuspend(e5010->dev);
> no_ready_buf_err:
[Severity: High]
What happens to subsequent jobs after e5010_device_run() takes the
device_busy_err error path?
When a register write fails in e5010_device_run(), it jumps to
device_busy_err and calls e5010_reset(), which resets the Jasper core, MMU,
and system registers:
drivers/media/platform/imagination/e5010-jpeg-enc-hw.c:e5010_reset() {
write_reg_field(core_base, JASPER_RESET_OFFSET,
JASPER_RESET_CR_CORE_RESET_MASK,
JASPER_RESET_CR_CORE_RESET_SHIFT, 1);
write_reg_field(mmu_base, MMU_MMU_CONTROL1_OFFSET,
MMU_MMU_CONTROL1_MMU_SOFT_RESET_MASK,
MMU_MMU_CONTROL1_MMU_SOFT_RESET_SHIFT, 1);
...
write_reg_field(core_base, JASPER_RESET_OFFSET,
JASPER_RESET_CR_SYS_RESET_MASK,
JASPER_RESET_CR_SYS_RESET_SHIFT, 1);
}
This wipes the hardware configuration set up by e5010_init_device() (MMU
bypass, picture done and output address error interrupts, and input source
selection) as well as the quantization tables programmed by
update_qp_tables().
Earlier in e5010_device_run(), before the register writes that can jump to
device_busy_err, the driver already updated the context tracking state:
} else {
e5010->last_context_run = ctx;
ctx->update_qp = false;
}
In device_busy_err, the driver calls pm_runtime_put_autosuspend(e5010->dev)
and then v4l2_m2m_job_finish(). Because autosuspend delays suspension by
JPEG_AUTOSUSPEND_DELAY_MS (100 ms), and because this patch removes
e5010_init_device() from e5010_start_streaming() and pm_runtime_put_sync()
from e5010_stop_streaming(), the device stays active across an immediate
next M2M job or a stream stop/start cycle within the autosuspend window.
Similarly, when !pm_runtime_enabled(dev), e5010_init_device() is only called
once in e5010_probe() and e5010_runtime_resume() never runs.
Without a runtime suspend and resume cycle occurring in between, won't the
next call to e5010_device_run() run with e5010_init_device() unconfigured
(interrupts disabled and MMU bypass cleared) and e5010->last_context_run
still equal to ctx, causing e5010_hw_encode_start() to never generate an
interrupt, hanging the M2M queue, and leaking the runtime PM reference?
Should device_busy_err re-initialize the device with e5010_init_device() and
reset e5010->last_context_run = NULL after calling e5010_reset()?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261023144618.1652390-1-b-brnich@ti.com?par…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] media: rkvdec: early return on !ctx in IRQ handlers skips rkvdec_iommu_restore()
- [High] media: rkvdec: unclocked MMIO access in IRQ handlers when ctx is NULL
--
commit c53a89dfd843b63318ca630daf3670e4a02c2e82
Author: Alex Kazinsky <a.kazinsky(a)ideco.ru>
media: rkvdec: Handle interrupts without a current context
This commit passes struct rkvdec_dev directly to the variant IRQ handlers and
returns early after clearing interrupt status registers when the current M2M
context is NULL, preventing a NULL pointer dereference if the watchdog
finishes the job before the threaded interrupt handler runs.
> diff --git a/drivers/media/platform/rockchip/rkvdec/rkvdec.c b/drivers/media/platform/rockchip/rkvdec/rkvdec.c
> index fbcd0f175ad3f..0adae52b9c40f 100644
> --- a/drivers/media/platform/rockchip/rkvdec/rkvdec.c
> +++ b/drivers/media/platform/rockchip/rkvdec/rkvdec.c
> @@ -1434,15 +1434,18 @@ static void rkvdec_iommu_restore(struct rkvdec_dev *rkvdec)
> }
> }
>
> -static irqreturn_t rk3399_irq_handler(struct rkvdec_ctx *ctx)
> +static irqreturn_t rk3399_irq_handler(struct rkvdec_dev *rkvdec,
> + struct rkvdec_ctx *ctx)
> {
> - struct rkvdec_dev *rkvdec = ctx->dev;
> enum vb2_buffer_state state;
> u32 status;
>
> status = readl(rkvdec->regs + RKVDEC_REG_INTERRUPT);
> writel(0, rkvdec->regs + RKVDEC_REG_INTERRUPT);
[Severity: High]
Is it safe for rk3399_irq_handler(), vdpu381_irq_handler(), and
vdpu383_irq_handler() to access rkvdec->regs or rkvdec->link when ctx is NULL
without first checking runtime PM state via pm_runtime_get_if_active()?
When a decode job times out, rkvdec_watchdog_func() calls rkvdec_job_finish(),
which releases the device's runtime PM reference before clearing the current
M2M context:
drivers/media/platform/rockchip/rkvdec/rkvdec.c:rkvdec_job_finish() {
struct rkvdec_dev *rkvdec = ctx->dev;
pm_runtime_put_autosuspend(rkvdec->dev);
rkvdec_job_finish_no_pm(ctx, result);
}
Whenever v4l2_m2m_get_curr_priv(rkvdec->m2m_dev) returns NULL in
rkvdec_irq_handler(), no job holds a runtime PM reference on rkvdec->dev, and
rkvdec_runtime_suspend() disables all device clocks without synchronizing the
threaded IRQ:
drivers/media/platform/rockchip/rkvdec/rkvdec.c:rkvdec_runtime_suspend() {
struct rkvdec_dev *rkvdec = dev_get_drvdata(dev);
clk_bulk_disable_unprepare(rkvdec->num_clocks, rkvdec->clocks);
return 0;
}
If the threaded interrupt handler runs after or concurrently with runtime
suspend (for example, if the IRQ thread is delayed past the autosuspend
window, autosuspend_delay_ms is set to 0, or system suspend invokes
pm_runtime_force_suspend()), won't these readl() and writel() calls access
unclocked and power-gated registers?
>
> + if (!ctx)
> + return IRQ_HANDLED;
> +
> if (status & RKVDEC_RDY_STA) {
> state = VB2_BUF_STATE_DONE;
> } else {
> @@ -1457,9 +1460,9 @@ static irqreturn_t rk3399_irq_handler(struct rkvdec_ctx *ctx)
> return IRQ_HANDLED;
> }
>
> -static irqreturn_t vdpu381_irq_handler(struct rkvdec_ctx *ctx)
> +static irqreturn_t vdpu381_irq_handler(struct rkvdec_dev *rkvdec,
> + struct rkvdec_ctx *ctx)
> {
> - struct rkvdec_dev *rkvdec = ctx->dev;
> enum vb2_buffer_state state;
> bool need_reset = 0;
> u32 status;
> @@ -1467,6 +1470,9 @@ static irqreturn_t vdpu381_irq_handler(struct rkvdec_ctx *ctx)
> status = readl(rkvdec->regs + VDPU381_REG_STA_INT);
> writel(0, rkvdec->regs + VDPU381_REG_STA_INT);
>
> + if (!ctx)
> + return IRQ_HANDLED;
> +
> if (status & VDPU381_STA_INT_DEC_RDY_STA) {
> state = VB2_BUF_STATE_DONE;
> } else {
> @@ -1486,9 +1492,9 @@ static irqreturn_t vdpu381_irq_handler(struct rkvdec_ctx *ctx)
> return IRQ_HANDLED;
> }
>
> -static irqreturn_t vdpu383_irq_handler(struct rkvdec_ctx *ctx)
> +static irqreturn_t vdpu383_irq_handler(struct rkvdec_dev *rkvdec,
> + struct rkvdec_ctx *ctx)
> {
> - struct rkvdec_dev *rkvdec = ctx->dev;
> enum vb2_buffer_state state;
> bool need_reset = 0;
> u32 status;
> @@ -1499,6 +1505,9 @@ static irqreturn_t vdpu383_irq_handler(struct rkvdec_ctx *ctx)
> writel(FIELD_PREP_WM16(VDPU383_INT_EN_IRQ | VDPU383_INT_EN_LINE_IRQ, 0),
> rkvdec->link + VDPU383_LINK_INT_EN);
>
> + if (!ctx)
> + return IRQ_HANDLED;
[Severity: High]
Does returning early on !ctx in rk3399_irq_handler(), vdpu381_irq_handler(),
and vdpu383_irq_handler() skip calling rkvdec_iommu_restore(rkvdec) after a
hardware error or self-reset?
When a decode job encounters an error or timeout, the hardware self-resets
(wiping its IOMMU mappings) and asserts an interrupt with RKVDEC_SOFTRESET_RDY
in rk3399_irq_handler(), VDPU381_STA_INT_SOFTRESET_RDY |
VDPU381_STA_INT_TIMEOUT | VDPU381_STA_INT_ERROR in vdpu381_irq_handler(), or
without VDPU383_STA_INT_DEC_RDY_STA in vdpu383_irq_handler().
If rkvdec_watchdog_func() finishes the job before rkvdec_irq_handler() calls
v4l2_m2m_get_curr_priv(rkvdec->m2m_dev), ctx is NULL because
rkvdec_watchdog_func() only calls rkvdec_job_finish() and does not call
rkvdec_iommu_restore(). By clearing the interrupt status register and
returning before checking status, the IOMMU mappings are never restored,
leaving the IOMMU unprogrammed for subsequent decode jobs.
Since rkvdec_iommu_restore() only requires rkvdec and ctx is only needed by
rkvdec_job_finish(ctx, state), should the !ctx check or guard be placed around
rkvdec_job_finish() after the status check and rkvdec_iommu_restore(rkvdec)
instead?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261023131124.94116-1-a.kazinsky@ideco.ru?p…
Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider:
- [Critical] media: amlogic: vdec: unchecked ref_list bounds and canvas lifecycle in h264
- [High] media: amlogic: vdec: aml_vdec_init_dec_inst() returns -1 on output restart
- [High] media: amlogic: vdec: check DPB entry flags in poc_is_in_dpb() and get_dpb_by_poc()
- [High] media: amlogic: vdec: out-of-bounds SPS workspace write in h264_config_params()
- [High] media: amlogic: vdec: ignored pm-domain error, unenabled clk, early vdev reg
- [High] media: amlogic: vdec: wrong IS_ERR() check on meson_sm_get() return
- [Medium] media: amlogic: vdec: uninitialized pic_info, prematuresizeimage calc, and split locks
- [Medium] media: amlogic: vdec: missing error unwind and propagation in vdec_poweron()
- [Low] media: amlogic: vdec: missing select REGMAP_MMIO in Kconfig
--
commit 05491c1c72b85040b071b152b79a908d403d95f0
Author: Zhentao Guo <zhentao.guo(a)amlogic.com>
arm64: defconfig: Enable CONFIG_VIDEO_AMLOGIC_VDEC
This commit enables the Amlogic V4L2 stateless video decoder driver
(CONFIG_VIDEO_AMLOGIC_VDEC=m) as a module in the arm64 defconfig for
Amlogic SoCs such as the S4 (S805X2) platform.
> diff --git a/arch/arm64/configs/defconfig b/arch/arm64/configs/defconfig
> index 7b4985b29b21c..f00c8a418a464 100644
> --- a/arch/arm64/configs/defconfig
> +++ b/arch/arm64/configs/defconfig
> @@ -929,6 +929,7 @@ CONFIG_USB_VIDEO_CLASS=m
> CONFIG_V4L_PLATFORM_DRIVERS=y
> CONFIG_SDR_PLATFORM_DRIVERS=y
> CONFIG_V4L_MEM2MEM_DRIVERS=y
> +CONFIG_VIDEO_AMLOGIC_VDEC=m
[Severity: Critical]
Can h264_config_ref_lists() and h264_reorder_reflists() in
drivers/media/platform/amlogic/vdec/h264.c overflow ref_list0 and ref_list1
and leak meson_canvas indices?
First, in struct aml_h264_ctx, ref_list0 and ref_list1 are sized to
V4L2_H264_NUM_DPB_ENTRIES + 1 (17 elements). However, in
h264_config_ref_lists(), v4l2_h264_init_reflist_builder() sets
builder.num_valid up to V4L2_H264_REF_LIST_LEN (32) when
V4L2_H264_DECODE_PARAM_FLAG_FIELD_PIC is set in decode->flags, and passes
builder.num_valid as list_size to config_p_reflist() and config_b_reflist():
drivers/media/platform/amlogic/vdec/h264.c:config_p_reflist() {
for (i = 0; i < list_size; i++) {
index = v4l2_p0_reflist[i].index;
h264_ctx->ref_list0[i].used = 1;
...
}
}
Won't list_size > 17 write past the end of ref_list0 and ref_list1 in
config_p_reflist() and config_b_reflist(), as well as in the loops using
h264_ctx->list_size[] in h264_config_decode_spec(), get_canvas_index(), and
h264_release_decode_spec()?
Second, when all 16 dpb entries are active and num_ref_idx_lx_active is 16,
reorder_short_term() and reorder_long_term() shift ref_list_reordered[15] into
the sentinel slot ref_list_reordered[16] without clearing the sentinel
afterward:
drivers/media/platform/amlogic/vdec/h264.c:reorder_short_term() {
for (c_idx = num_ref_idx_lx_active; c_idx > *ref_idx_lx; c_idx--)
memcpy(&ref_list_reordered[c_idx], &ref_list_reordered[c_idx - 1],
sizeof(struct h264_decode_buf_spec));
...
h264_ctx->list_size[cur_list] = num_ref_idx_lx_active;
}
Since get_refidx_by_picnum(), get_st_refpic_by_num(), and
get_lt_refpic_by_num() iterate until ref_list[i].dpb is NULL with no upper
bound on i:
drivers/media/platform/amlogic/vdec/h264.c:get_refidx_by_picnum() {
for (i = 0; ref_list[i].dpb; i++) {
if (pic_num == ref_list[i].dpb->pic_num)
return i;
}
}
can overwriting ref_list[16].dpb cause these helpers to read past the end of
ref_list0 and ref_list1 on subsequent reordering commands?
Third, reorder_short_term() and reorder_long_term() overwrite
h264_ctx->list_size[cur_list] with num_ref_idx_lx_active. In
get_canvas_index(), config_decode_canvas() is called for every entry below
list_size without checking buf->used or buf->valid, while
h264_release_decode_spec() only calls release_canvas_index() when buf->used is
non-zero:
drivers/media/platform/amlogic/vdec/h264.c:get_canvas_index() {
if (h264_ctx->list_size[0] > 0) {
for (i = 0; i < h264_ctx->list_size[0]; i++) {
buf = &h264_ctx->ref_list0[i];
ret = config_decode_canvas(hw, buf, h264_ctx->mb_width,
h264_ctx->mb_height);
...
}
}
}
drivers/media/platform/amlogic/vdec/h264.c:h264_release_decode_spec() {
if (h264_ctx->list_size[0] > 0) {
for (i = 0; i < h264_ctx->list_size[0]; i++) {
buf = &h264_ctx->ref_list0[i];
buf->valid = 0;
if (buf->used) {
buf->dpb = NULL;
release_canvas_index(hw, buf);
buf->used = 0;
}
}
h264_ctx->list_size[0] = 0;
}
}
If num_ref_idx_lx_active is larger than the initial list_size, won't
get_canvas_index() allocate meson canvases for entries with buf->used == 0
that h264_release_decode_spec() never frees, and if num_ref_idx_lx_active is
smaller, won't entries above the new list_size retain stale state?
[Severity: High]
What happens in drivers/media/platform/amlogic/vdec/aml_vdec.c when the output
queue is stopped and restarted while the capture queue remains streaming?
When a client stops the output queue via vb2ops_vdec_stop_streaming() while
ctx->is_cap_streamon is still set, aml_vdec_release_instance() is not called
and ctx->codec_priv remains non-NULL:
drivers/media/platform/amlogic/vdec/aml_vdec.c:vb2ops_vdec_stop_streaming() {
if (!ctx->is_output_streamon && !ctx->is_cap_streamon)
aml_vdec_release_instance(ctx);
}
When the output queue is subsequently restarted, vb2ops_vdec_start_streaming()
calls aml_vdec_init_dec_inst(), which initializes ret to -1 and skips
ctx->codec_ops->init(ctx) because ctx->codec_priv is already allocated:
drivers/media/platform/amlogic/vdec/aml_vdec.c:aml_vdec_init_dec_inst() {
int ret = -1;
...
if (ctx->codec_ops->init && !ctx->codec_priv) {
ret = ctx->codec_ops->init(ctx);
if (ret < 0)
return ret;
}
ctx->curr_dec_type = fmt_out->codec_type;
...
return ret;
}
Because ret stays -1, vb2ops_vdec_start_streaming() treats the call as a
failure, returns all queued source buffers via v4l2_m2m_src_buf_remove() and
v4l2_m2m_buf_done(), and fails VIDIOC_STREAMON with -EINVAL:
drivers/media/platform/amlogic/vdec/aml_vdec.c:vb2ops_vdec_start_streaming() {
if (aml_vdec_init_dec_inst(ctx) < 0) {
while ((src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx)))
v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_QUEUED);
return -EINVAL;
}
}
Should ret be initialized to 0 in aml_vdec_init_dec_inst() so restarting the
output queue succeeds when ctx->codec_priv is already present?
[Severity: High]
Should poc_is_in_dpb() and get_dpb_by_poc() in
drivers/media/platform/amlogic/vdec/h264.c check whether a dpb entry is active
before matching top_field_order_cnt?
During H264_SLICE_HEADER_DONE handling in
h264_threaded_isr_func()->h264_config_decode_spec()->clear_unused_col_buf(),
poc_is_in_dpb() is called to decide whether to release colocated_buf and
canvas_pos slots:
drivers/media/platform/amlogic/vdec/h264.c:poc_is_in_dpb() {
for (i = 0; i < V4L2_H264_NUM_DPB_ENTRIES; i++) {
if (poc == dpb[i].top_field_order_cnt) {
ret = 1;
break;
}
}
}
Because decode->dpb has 16 fixed entries and poc_is_in_dpb() does not check
dpb[i].flags for V4L2_H264_DPB_ENTRY_FLAG_ACTIVE (or
V4L2_H264_DPB_ENTRY_FLAG_VALID), any unused zeroed dpb entry has
top_field_order_cnt == 0, causing poc_is_in_dpb(0, decode->dpb) to always
return 1 and leak the colocated_buf and canvas_pos slots for poc 0 in
clear_unused_col_buf():
drivers/media/platform/amlogic/vdec/h264.c:clear_unused_col_buf() {
for (i = 0; i < h264_ctx->colocated_buf_num; i++) {
if (!((h264_ctx->colocated_buf_map >> i) & 0x1))
continue;
col_poc = h264_ctx->colocated_buf_poc[i];
if (poc_is_in_dpb(col_poc, decode->dpb) != 1)
release_colocate_buf(h264_ctx, i);
}
for (i = 0; i < (V4L2_H264_NUM_DPB_ENTRIES + 1); i++) {
if (!((h264_ctx->canvas_pos_map >> i) & 0x1))
continue;
col_poc = h264_ctx->ref_canvas[i].poc;
if (poc_is_in_dpb(col_poc, decode->dpb) != 1)
release_canvas_pos(h264_ctx, i);
}
}
Similarly, if userspace clears flags on retired dpb entries without zeroing
top_field_order_cnt, their colocated_buf and canvas_pos slots are never freed
until all slots are exhausted.
In addition, h264_config_decode_buf() looks up reference entries with
get_dpb_by_poc():
drivers/media/platform/amlogic/vdec/h264.c:get_dpb_by_poc() {
for (i = 0; i < V4L2_H264_NUM_DPB_ENTRIES; i++) {
if (decode->dpb[i].top_field_order_cnt == poc)
return &decode->dpb[i];
}
return NULL;
}
drivers/media/platform/amlogic/vdec/h264.c:h264_config_decode_buf() {
dpb = get_dpb_by_poc(decode, poc);
if (dpb && (dpb->flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE)) {
}
If an earlier inactive dpb slot has the same top_field_order_cnt as a later
active dpb slot (for instance, an unused zeroed slot before an active poc 0
entry), won't get_dpb_by_poc() return the inactive slot and cause
h264_config_decode_buf() to skip writing the active reference frame's info
into H264_BUFFER_INFO_DATA?
[Severity: High]
Can a userspace V4L2_CID_STATELESS_H264_SPS control with
sps->seq_parameter_set_id >= 32 cause an out-of-bounds write in
h264_config_params() in drivers/media/platform/amlogic/vdec/h264.c?
In drivers/media/v4l2-core/v4l2-ctrls-core.c, std_validate_compound() does not
validate that p_h264_sps->seq_parameter_set_id is within 0..31, and
aml_vdec_ctrls_setup() in drivers/media/platform/amlogic/vdec/aml_vdec.c does
not provide custom validation. When aml_h264_dec_run() calls
h264_config_params()->config_sps_params(), p_sps_base is calculated directly
from sps->seq_parameter_set_id (a u8 up to 255):
drivers/media/platform/amlogic/vdec/h264.c:h264_config_params() {
p_sps_base = (unsigned short *)(h264_ctx->workspace_vaddr +
MEM_SPS_BASE + sps->seq_parameter_set_id * 0x400);
p_pps_base = (unsigned short *)(h264_ctx->workspace_vaddr +
MEM_PPS_BASE + pps->pic_parameter_set_id * 0x200);
}
drivers/media/platform/amlogic/vdec/h264.c:config_sps_params() {
memset(sps_base, 0, 0x100);
memset(data_tmp, 0, sizeof(data_tmp));
}
Since MEM_SPS_BASE (0x01c3c00) to MEM_PPS_BASE (0x01cbc00) only has room for
32 entries of 0x400 bytes, won't seq_parameter_set_id >= 32 cause
config_sps_params() to overwrite the MEM_PPS_BASE table and adjacent regions
of h264_ctx->workspace_vaddr?
[Severity: High]
Could drivers/media/platform/amlogic/vdec/aml_vdec_hw.c and
drivers/media/platform/amlogic/vdec/aml_vdec_drv.c access unclocked or
unpowered DOS_BUS registers if power domain attachment fails or before
vdec_enable() runs?
In pm_vdec_power_domain_init(), errors from dev_pm_domain_attach_by_name()
(including -EPROBE_DEFER) are ignored and pd[i].dev is set to NULL:
drivers/media/platform/amlogic/vdec/aml_vdec_hw.c:pm_vdec_power_domain_init() {
pd[i].dev = dev_pm_domain_attach_by_name(hw->dev, pd[i].name);
if (IS_ERR_OR_NULL(pd[i].dev)) {
err = PTR_ERR(pd[i].dev);
dev_dbg(hw->dev, "Get %s failed, pm-domain: %d\n",
pd[i].name, err);
pd[i].dev = NULL;
continue;
}
}
When pd_data[core].dev is NULL, vdec_poweron() skips hw->pm->power_on(hw, core)
and leaves both the vdec power domain and vdec clock disabled, so subsequent
DOS_BUS register accesses in m2mops_vdec_device_run()->dos_enable() can
trigger a synchronous external abort:
drivers/media/platform/amlogic/vdec/aml_vdec_hw.c:vdec_poweron() {
mutex_lock(&hw->pm_mutex);
if (!hw->pm->pd_data[core].dev)
goto out;
...
hw->pm->power_on(hw, core);
}
In addition, vdec_clock_gate_init() acquires the dos clock in
hw->gates[CLK_DOS]:
drivers/media/platform/amlogic/vdec/aml_vdec_hw.c:vdec_clock_gate_init() {
hw->gates[CLK_DOS].id = "dos";
hw->gates[CLK_VDEC].id = "vdec";
hw->gates[CLK_HVDEC].id = "hvdec";
return devm_clk_bulk_get(hw->dev, CLK_MAX, hw->gates);
}
Yet pm_vdec_power_domain_power_on() only enables vdec and hvdec and never
enables hw->gates[CLK_DOS].clk.
Furthermore, in aml_vdec_drv_probe() in
drivers/media/platform/amlogic/vdec/aml_vdec_drv.c, video_register_device() is
called before dev->v4l2_dev.mdev is initialized and before
vdec_enable(dev->dec_hw) is called:
drivers/media/platform/amlogic/vdec/aml_vdec_drv.c:aml_vdec_drv_probe() {
ret = video_register_device(vfd_dec, VFL_TYPE_VIDEO, -1);
if (ret) {
v4l2_err(&dev->v4l2_dev, "Failed to register video device");
goto err_vid_dev_register;
}
dev->mdev.dev = &pdev->dev;
strscpy(dev->mdev.model, AML_VDEC_DRV_NAME, sizeof(dev->mdev.model));
media_device_init(&dev->mdev);
dev->mdev.ops = &aml_m2m_media_ops;
dev->v4l2_dev.mdev = &dev->mdev;
...
vdec_enable(dev->dec_hw);
}
Should pm_vdec_power_domain_init() propagate errors from
dev_pm_domain_attach_by_name(), should CLK_DOS be enabled before accessing
DOS_BUS registers, and should vdec_enable() and media_device_init() run before
video_register_device() publishes the device node?
[Severity: High]
Why does dev_request_hw_resources() in
drivers/media/platform/amlogic/vdec/aml_vdec_hw.c check meson_sm_get() with
IS_ERR() instead of checking for NULL?
In drivers/firmware/meson/meson_sm.c, meson_sm_get() returns NULL when the
secure-monitor device is missing or has not finished probing:
drivers/firmware/meson/meson_sm.c:meson_sm_get() {
if (!pdev)
return NULL;
fw = platform_get_drvdata(pdev);
put_device(&pdev->dev);
return fw;
}
However, dev_request_hw_resources() only checks IS_ERR(hw->sec_fw):
drivers/media/platform/amlogic/vdec/aml_vdec_hw.c:dev_request_hw_resources() {
hw->sec_fw = meson_sm_get(sm_np);
of_node_put(sm_np);
if (IS_ERR(hw->sec_fw))
return PTR_ERR(hw->sec_fw);
}
If meson_sm_get() returns NULL, probe succeeds with hw->sec_fw == NULL, and
when a decode job runs m2mops_vdec_device_run()->load_firmware() in
drivers/media/platform/amlogic/vdec/aml_vdec_tee_fw.c:
drivers/media/platform/amlogic/vdec/aml_vdec_tee_fw.c:load_firmware() {
meson_sm_call(hw->sec_fw, SM_LOAD_VIDEO_FW, &ret,
video_fw->fw_format, video_fw->core,
video_fw->is_swap, 0, 0);
}
won't meson_sm_call() dereference a NULL hw->sec_fw pointer instead of probe
returning -EPROBE_DEFER?
[Severity: Medium]
Are ctx->pic_info, plane_fmt[0].sizeimage, and the format/queue locks in
drivers/media/platform/amlogic/vdec/aml_vdec.c initialized in the right order?
First, when a file handle is opened, fops_vcodec_open()->aml_vdec_reset_fmts()
calls reset_output_fmts(), which sets ctx->pix_fmt[AML_FMT_SRC] without
calling set_pic_info():
drivers/media/platform/amlogic/vdec/aml_vdec.c:reset_output_fmts() {
vdec_try_fmt_mp(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, &fmt,
out_fmt);
ctx->pix_fmt[AML_FMT_SRC] = fmt;
}
If userspace starts streaming with the default format without calling
VIDIOC_S_FMT on V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, ctx->pic_info.fb_size[0]
remains 0, so h264_config_decode_spec() and h264_config_ref_spec() in
drivers/media/platform/amlogic/vdec/h264.c set c_dma_addr equal to y_dma_addr
for single-planar capture formats:
drivers/media/platform/amlogic/vdec/h264.c:h264_config_decode_spec() {
h264_ctx->curr_spec.y_dma_addr = vb2_dma_contig_plane_dma_addr(vb, 0);
if (ctx->pix_fmt[AML_FMT_DST].num_planes > 1)
h264_ctx->curr_spec.c_dma_addr =
vb2_dma_contig_plane_dma_addr(vb, 1);
else
h264_ctx->curr_spec.c_dma_addr =
h264_ctx->curr_spec.y_dma_addr + ctx->pic_info.fb_size[0];
}
Second, in vdec_try_fmt_mp(), plane_fmt[0].sizeimage for the output queue is
computed from pix_mp->height and pix_mp->width before ALIGN() and
v4l2_apply_frmsize_constraints() clamp and align them:
drivers/media/platform/amlogic/vdec/aml_vdec.c:vdec_try_fmt_mp() {
if (V4L2_TYPE_IS_OUTPUT(type)) {
pix_mp->num_planes = dec_fmt->num_planes;
pix_mp->pixelformat = dec_fmt->fourcc;
if (!pix_mp->plane_fmt[0].sizeimage)
pix_mp->plane_fmt[0].sizeimage =
(pix_mp->height * pix_mp->width * 3) / 2;
pix_mp->height = ALIGN(pix_mp->height, align);
pix_mp->width = ALIGN(pix_mp->width, align);
} else {
...
}
v4l2_apply_frmsize_constraints(&pix_mp->width, &pix_mp->height,
&dec_fmt->stepwise);
}
If userspace passes width == 0 or height == 0 with sizeimage == 0, sizeimage
remains 0 even after width and height are clamped to the minimum frame size.
Third, aml_vdec_reset_fmts() and aml_vdec_queue_init() set
ctx->m2m_ctx->q_lock, src_vq->lock, and dst_vq->lock to &ctx->v4l2_intf_lock,
whereas aml_vdec_drv_probe() sets vfd_dec->lock to &dev->dev_mutex:
drivers/media/platform/amlogic/vdec/aml_vdec.c:aml_vdec_reset_fmts() {
ctx->m2m_ctx->q_lock = &ctx->v4l2_intf_lock;
reset_output_fmts(ctx);
reset_capture_fmts(ctx);
}
Doesn't using two different mutexes allow VIDIOC_S_FMT and VIDIOC_G_FMT to
race with queue ioctls such as VIDIOC_REQBUFS, VIDIOC_QBUF, and
VIDIOC_STREAMON?
[Severity: Medium]
What happens in drivers/media/platform/amlogic/vdec/aml_vdec_hw.c when
pm_runtime_resume_and_get() or clk_prepare_enable() fails during
vdec_poweron()?
In pm_vdec_power_domain_power_on(), if clk_prepare_enable(gate_node->clk)
succeeds and pm_runtime_resume_and_get(dev) fails, gate_node->clk is not
disabled and unprepared, and the error is not returned to the caller:
drivers/media/platform/amlogic/vdec/aml_vdec_hw.c:pm_vdec_power_domain_power_on() {
if (gate_node) {
ret = clk_prepare_enable(gate_node->clk);
...
if (!ret && dev) {
ret = pm_runtime_resume_and_get(dev);
if (ret < 0)
dev_err(hw->dev, "failed to power on %s domain: %d\n",
pm->pd_data[id].name, ret);
...
if (!ret)
dos_local_config(hw, 1, id);
}
Because vdec_poweron() increments hw->pm->pd_data[core].ref_count before
calling hw->pm->power_on(hw, core) and vdec_enable() always returns 0 to
aml_vdec_drv_probe():
drivers/media/platform/amlogic/vdec/aml_vdec_hw.c:vdec_poweron() {
hw->pm->pd_data[core].ref_count++;
if (hw->pm->pd_data[core].ref_count > 1)
goto out;
hw->pm->power_on(hw, core);
}
when aml_vdec_drv_remove() later calls
vdec_disable()->vdec_poweroff()->pm_vdec_power_domain_power_off():
drivers/media/platform/amlogic/vdec/aml_vdec_hw.c:pm_vdec_power_domain_power_off() {
dos_local_config(hw, 0, id);
if (dev) {
pm_runtime_put_sync(dev);
}
won't pm_vdec_power_domain_power_off() access unpowered DOS_BUS registers in
dos_local_config(hw, 0, id) and cause unbalanced pm_runtime_put_sync() and
clk_disable_unprepare() calls?
[Severity: Low]
Should VIDEO_AMLOGIC_VDEC in drivers/media/platform/amlogic/vdec/Kconfig
select REGMAP_MMIO?
In drivers/media/platform/amlogic/vdec/aml_vdec_hw.c,
dev_request_hw_resources() initializes regmaps via devm_regmap_init_mmio():
drivers/media/platform/amlogic/vdec/aml_vdec_hw.c:dev_request_hw_resources() {
if (i == DOS_BUS) {
hw->map[i] = devm_regmap_init_mmio(&pdev->dev, reg_base[i],
&dos_regmap_conf);
} else if (i == DMC_BUS) {
hw->map[i] = devm_regmap_init_mmio(&pdev->dev, reg_base[i],
&dmc_regmap_conf);
}
}
However, VIDEO_AMLOGIC_VDEC in drivers/media/platform/amlogic/vdec/Kconfig
does not select REGMAP_MMIO:
drivers/media/platform/amlogic/vdec/Kconfig: {
config VIDEO_AMLOGIC_VDEC
tristate "Amlogic Video Decoder Driver"
...
select VIDEOBUF2_DMA_CONTIG
select V4L2_H264
select V4L2_MEM2MEM_DEV
select MESON_CANVAS
}
Could configurations enabling CONFIG_VIDEO_AMLOGIC_VDEC without another driver
selecting CONFIG_REGMAP_MMIO fail to build or link?
> CONFIG_VIDEO_AMPHION_VPU=m
> CONFIG_VIDEO_CADENCE_CSI2RX=m
> CONFIG_VIDEO_WAVE_VPU=m
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261022-b4-s4-vdec-upstream-v12-0-616240978…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] media: wave5: wave5_return_bufs() leaves buffers in inst->avail_src_bufs
- [Medium] media: wave5: DMA buffer leak in wave5_vpu_dec_stop_streaming() error path
- [Medium] media: wave5: incomplete buffer list drain in wave5_vpu_dec_stop_streaming()
--
commit a83d9d8ae18ec13f7f5a3bc1c69ae74370785681
Author: Jiale Yao <yaojiale02(a)163.com>
media: wave5: handle remaining decoder runtime resume failures
This commit checks the return value of pm_runtime_resume_and_get() across
the remaining decoder callbacks in wave5-vpu-dec.c to prevent register
accesses while VPU clocks are disabled and avoid unmatched runtime PM puts.
> diff --git a/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c b/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c
> index 86bc36ee17fd6..0088501f93345 100644
> --- a/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c
> +++ b/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c
[ ... ]
> @@ -1392,7 +1395,11 @@ static int wave5_vpu_dec_start_streaming(struct vb2_queue *q, unsigned int count
> int ret = 0;
>
> dev_dbg(inst->dev->dev, "%s: type: %u\n", __func__, q->type);
> - pm_runtime_resume_and_get(inst->dev->dev);
> + ret = pm_runtime_resume_and_get(inst->dev->dev);
> + if (ret < 0) {
> + wave5_return_bufs(q, VB2_BUF_STATE_QUEUED);
> + return ret;
> + }
[Severity: High]
Can calling wave5_return_bufs() here when q->type is
V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE leave buffers linked in
inst->avail_src_bufs, leading to list corruption or a use-after-free?
Before vb2_start_streaming() calls wave5_vpu_dec_start_streaming(), it
enqueues pre-queued buffers into the driver via
wave5_vpu_dec_buf_queue() -> wave5_vpu_dec_buf_queue_src():
drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c:wave5_vpu_dec_buf_queue_src() {
...
INIT_LIST_HEAD(&vpu_buf->list);
list_add_tail(&vpu_buf->list, &inst->avail_src_bufs);
mutex_unlock(&inst->feed_lock);
v4l2_m2m_buf_queue(m2m_ctx, vbuf);
...
}
However, wave5_return_bufs() in drivers/media/platform/chips-media/wave5/wave5-helper.c
only removes buffers from m2m_ctx via v4l2_m2m_src_buf_remove() and does
not unlink vpu_buf->list from inst->avail_src_bufs:
drivers/media/platform/chips-media/wave5/wave5-helper.c:wave5_return_bufs() {
for (;;) {
if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
vbuf = v4l2_m2m_src_buf_remove(m2m_ctx);
else
vbuf = v4l2_m2m_dst_buf_remove(m2m_ctx);
if (!vbuf)
return;
v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req, &v4l2_ctrl_hdl);
v4l2_m2m_buf_done(vbuf, state);
}
}
When wave5_vpu_dec_start_streaming() returns an error, vb2_start_streaming()
resets q->start_streaming_called to 0.
If userspace retries VIDIOC_STREAMON, vb2_start_streaming() calls
wave5_vpu_dec_buf_queue_src() again on the same buffers while their
vpu_buf->list nodes are still linked in inst->avail_src_bufs, reinitializing
the active list nodes with INIT_LIST_HEAD() and corrupting
inst->avail_src_bufs.
Alternatively, if userspace frees the buffers via VIDIOC_REQBUFS(0) or
VIDIOC_REMOVE_BUFS after the failed VIDIOC_STREAMON, __vb2_queue_cancel()
does not call wave5_vpu_dec_stop_streaming() because
q->start_streaming_called is 0, and __vb2_queue_free() frees the
struct vpu_src_buffer objects while they remain linked in
inst->avail_src_bufs.
The same issue also affects the error path in wave5_vpu_dec_device_run()
below, which calls wave5_return_bufs(src_vq, VB2_BUF_STATE_ERROR) before
fill_ringbuffer() runs without draining inst->avail_src_bufs or resetting
inst->retry and inst->queuing_num.
>
> v4l2_m2m_update_start_streaming_state(m2m_ctx, q);
>
> @@ -1556,9 +1563,28 @@ static void wave5_vpu_dec_stop_streaming(struct vb2_queue *q)
> struct vpu_instance *inst = vb2_get_drv_priv(q);
> struct v4l2_m2m_ctx *m2m_ctx = inst->v4l2_fh.m2m_ctx;
> unsigned long timeout;
> + int ret;
>
> dev_dbg(inst->dev->dev, "%s: type: %u\n", __func__, q->type);
> - pm_runtime_resume_and_get(inst->dev->dev);
> + ret = pm_runtime_resume_and_get(inst->dev->dev);
> + if (ret < 0) {
> + if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
> + struct vpu_src_buffer *vpu_buf;
> +
> + inst->retry = false;
> + inst->queuing_num = 0;
> + while ((vpu_buf = inst_src_buf_remove(inst)) != NULL)
> + ;
[Severity: Medium]
What happens if a pending signal interrupts inst_src_buf_remove() while
draining inst->avail_src_bufs in wave5_vpu_dec_stop_streaming()?
In drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c,
inst_src_buf_remove() uses mutex_lock_interruptible():
drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c:inst_src_buf_remove() {
...
ret = mutex_lock_interruptible(&inst->feed_lock);
if (ret)
return NULL;
...
}
During STREAMOFF, v4l2_m2m_cancel_job() calls wave5_vpu_dec_job_abort(),
which calls v4l2_m2m_job_finish() synchronously before a concurrent
wave5_vpu_dec_device_run() -> fill_ringbuffer() -> inst_src_buf_remove() on
the m2m workqueue has necessarily released inst->feed_lock.
If the task in wave5_vpu_dec_stop_streaming() has a pending signal and
contends on inst->feed_lock, inst_src_buf_remove() returns NULL on -EINTR.
The while loop treats NULL as an empty list and stops draining early,
leaving the remaining vpu_src_buffer entries linked on inst->avail_src_bufs
while wave5_return_bufs() completes them back to vb2.
> + inst->eos = false;
> + }
> +
> + v4l2_m2m_update_stop_streaming_state(m2m_ctx, q);
> + wave5_return_bufs(q, VB2_BUF_STATE_ERROR);
> + inst->empty_queue = false;
> + inst->sent_eos = false;
> + return;
> + }
[Severity: Medium]
When stopping the capture queue after a dynamic resolution change, can
returning early here in wave5_vpu_dec_stop_streaming() leak coherent DMA
allocations in inst->frame_vbuf[]?
During a dynamic resolution change, handle_dynamic_resolution_change() in
drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c updates
inst->fbc_buf_count to the new sequence's count:
drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c:handle_dynamic_resolution_change() {
...
inst->needs_reallocation = true;
inst->fbc_buf_count = initial_info->min_frame_buffer_count + 1;
...
}
Normally, streamoff_capture() runs DEC_RESET_FRAMEBUF_INFO when
inst->needs_reallocation is set, which frees all MAX_REG_FRAME framebuffer
and auxiliary DMA buffers in host memory without accessing hardware
registers:
drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c:streamoff_capture() {
...
if (inst->needs_reallocation) {
wave5_vpu_dec_give_command(inst, DEC_RESET_FRAMEBUF_INFO, NULL);
inst->needs_reallocation = false;
}
}
If pm_runtime_resume_and_get() fails while stopping the capture queue and
the new inst->fbc_buf_count is smaller than the previous sequence's buffer
count, skipping DEC_RESET_FRAMEBUF_INFO causes wave5_cleanup_instance() in
drivers/media/platform/chips-media/wave5/wave5-helper.c to only free up to
the new inst->fbc_buf_count:
drivers/media/platform/chips-media/wave5/wave5-helper.c:wave5_cleanup_instance() {
...
for (i = 0; i < inst->fbc_buf_count; i++)
wave5_vpu_dec_reset_framebuffer(inst, i);
...
}
Should the capture queue error path still issue DEC_RESET_FRAMEBUF_INFO when
inst->needs_reallocation is true so the buffers above the new
inst->fbc_buf_count are not leaked?
[ ... ]
> @@ -1675,7 +1701,21 @@ static void wave5_vpu_dec_device_run(void *priv)
> bool cmd_issued = false;
>
> dev_dbg(inst->dev->dev, "%s: Fill the ring buffer with new bitstream data", __func__);
> - pm_runtime_resume_and_get(inst->dev->dev);
> + ret = pm_runtime_resume_and_get(inst->dev->dev);
> + if (ret < 0) {
> + struct vb2_queue *src_vq = v4l2_m2m_get_src_vq(m2m_ctx);
> + struct vb2_queue *dst_vq = v4l2_m2m_get_dst_vq(m2m_ctx);
> +
> + dev_err(inst->dev->dev, "Failed to resume VPU: %d\n", ret);
> + set_instance_state(inst, VPU_INST_STATE_STOP);
> + vb2_queue_error(src_vq);
> + vb2_queue_error(dst_vq);
> + wave5_return_bufs(src_vq, VB2_BUF_STATE_ERROR);
> + wave5_return_bufs(dst_vq, VB2_BUF_STATE_ERROR);
> + v4l2_m2m_job_finish(inst->v4l2_m2m_dev, m2m_ctx);
> + return;
> + }
[Severity: High]
As noted in wave5_vpu_dec_start_streaming() above, does calling
wave5_return_bufs(src_vq, VB2_BUF_STATE_ERROR) here before
fill_ringbuffer() runs leave those output buffers on inst->avail_src_bufs?
Because wave5_vpu_dec_buf_queue_src() already added the output buffers to
inst->avail_src_bufs before wave5_vpu_dec_device_run() was scheduled,
completing them via wave5_return_bufs() allows userspace to dequeue and
free or re-queue them while their struct vpu_src_buffer entries remain on
inst->avail_src_bufs. Should this path also drain inst->avail_src_bufs and
reset inst->retry and inst->queuing_num?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261023092804.673980-1-yaojiale02@163.com?p…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] media: wave5: false bookkeeping fix claim in wave5_vpu_dec_buf_queue_dst()
--
commit 6d1e8ae87720c994aa464b924f1cca34970cf828
Author: Jiale Yao <yaojiale02(a)163.com>
media: wave5: use m2m buffer completion helper
This commit replaces vb2_buffer_done() with v4l2_m2m_buf_done() in
wave5_vpu_dec_buf_queue_dst() when pm_runtime_resume_and_get() fails.
> media: wave5: use m2m buffer completion helper
>
> The decoder destination queue handles a runtime resume failure by
> completing the queued buffer. The driver is a mem2mem driver, so use
> v4l2_m2m_buf_done() to preserve mem2mem queue bookkeeping instead of
> calling vb2_buffer_done() directly.
>
> Fixes: 158b39a0f7d1 ("media: chips-media: wave5: Check decoder qbuf runtime resume")
> Cc: stable(a)vger.kernel.org
> Tested-by: Brandon Brnich <b-brnich(a)ti.com>
> Signed-off-by: Jiale Yao <yaojiale02(a)163.com>
>
> diff --git a/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c b/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c
> index 0088501f93345..928f6d93de49e 100644
> --- a/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c
> +++ b/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c
> @@ -1309,7 +1309,7 @@ static void wave5_vpu_dec_buf_queue_dst(struct vb2_buffer *vb)
>
> ret = pm_runtime_resume_and_get(inst->dev->dev);
> if (ret < 0) {
> - vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
> + v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
> return;
> }
[Severity: Medium]
Does replacing vb2_buffer_done() with v4l2_m2m_buf_done() here actually change
any mem2mem queue bookkeeping?
In drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c,
wave5_vpu_dec_buf_queue_dst() derives vbuf from vb via to_vb2_v4l2_buffer(vb),
so &vbuf->vb2_buf and vb point to the exact same struct vb2_buffer:
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
...
ret = pm_runtime_resume_and_get(inst->dev->dev);
if (ret < 0) {
v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
return;
}
In include/media/v4l2-mem2mem.h, v4l2_m2m_buf_done() is a direct wrapper around
vb2_buffer_done() that does not touch struct v4l2_m2m_ctx or perform any
mem2mem queue bookkeeping:
static inline void
v4l2_m2m_buf_done(struct vb2_v4l2_buffer *buf, enum vb2_buffer_state state)
{
vb2_buffer_done(&buf->vb2_buf, state);
}
Furthermore, when pm_runtime_resume_and_get() fails in
wave5_vpu_dec_buf_queue_dst(), vbuf has not yet been queued into m2m_ctx, as
v4l2_m2m_buf_queue(m2m_ctx, vbuf) is only reached at the end of the function
after runtime resume succeeds.
Since this change produces identical code and does not fix a runtime bug or
alter mem2mem bookkeeping, should the commit message be updated to describe it
as a cleanup and the Fixes: and Cc: stable(a)vger.kernel.org tags be dropped?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261023092804.673980-1-yaojiale02@163.com?p…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] media: pxa_camera: unlocked capture list access in pxa_camera_eof_bh_work()
--
commit 8723c21016890e361d61ee299976abccfb5e21c5
Author: Wang Haoran <haoranwangsec(a)gmail.com>
media: pxa_camera: guard list_first_entry() against empty capture list in EOF bottom half
This commit adds a list_empty(&pcdev->capture) check in
pxa_camera_eof_bh_work() before calling list_first_entry(), stopping capture
and returning early if the capture list has been emptied.
> diff --git a/drivers/media/platform/intel/pxa_camera.c b/drivers/media/platform/intel/pxa_camera.c
> index 9e1fdd963b559..99a0c00951e88 100644
> --- a/drivers/media/platform/intel/pxa_camera.c
> +++ b/drivers/media/platform/intel/pxa_camera.c
> @@ -1161,6 +1161,11 @@ static void pxa_camera_eof_bh_work(struct work_struct *t)
> cifr = __raw_readl(pcdev->base + CIFR) | CIFR_RESET_F;
> __raw_writel(cifr, pcdev->base + CIFR);
>
> + if (list_empty(&pcdev->capture)) {
> + pxa_camera_stop_capture(pcdev);
> + return;
> + }
> +
> pcdev->active = list_first_entry(&pcdev->capture,
> struct pxa_buffer, queue);
> buf = pcdev->active;
[Severity: Critical]
Can this list_empty(&pcdev->capture) check still race with
pxa_camera_dma_irq() -> pxa_camera_wakeup() because pxa_camera_eof_bh_work()
does not hold pcdev->lock?
In drivers/media/platform/intel/pxa_camera.c, pxa_camera_dma_irq() acquires
pcdev->lock before calling pxa_camera_wakeup(), which deletes the buffer from
pcdev->capture, completes it via vb2_buffer_done(), and updates pcdev->active
or stops capture:
drivers/media/platform/intel/pxa_camera.c:pxa_camera_dma_irq() {
spin_lock_irqsave(&pcdev->lock, flags);
...
buf->active_dma &= ~act_dma;
if (!buf->active_dma) {
pxa_camera_wakeup(pcdev, buf, VB2_BUF_STATE_DONE);
pxa_camera_check_link_miss(pcdev, last_buf->cookie[chan],
last_issued);
}
out:
spin_unlock_irqrestore(&pcdev->lock, flags);
}
drivers/media/platform/intel/pxa_camera.c:pxa_camera_wakeup() {
...
list_del_init(&buf->queue);
...
vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
...
if (list_empty(&pcdev->capture)) {
pxa_camera_stop_capture(pcdev);
return;
}
pcdev->active = list_entry(pcdev->capture.next,
struct pxa_buffer, queue);
}
Because pxa_camera_eof_bh_work() runs on system_bh_wq without holding
pcdev->lock, if pxa_camera_wakeup() empties pcdev->capture right after
list_empty(&pcdev->capture) returns false in pxa_camera_eof_bh_work(),
list_first_entry(&pcdev->capture, struct pxa_buffer, queue) will still compute
the bogus container_of() pointer from &pcdev->capture and write to
buf->active_dma in pxa_video_buf_set_actdma().
Similarly, if pxa_camera_wakeup() runs right after list_first_entry() fetches
the last buffer, pxa_camera_eof_bh_work() can reassign the already-completed
buffer to pcdev->active, modify buf->active_dma after vb2_buffer_done(), and
restart the DMA channels after pxa_camera_stop_capture() stopped them.
Should pxa_camera_eof_bh_work() serialize access to pcdev->capture,
pcdev->active, and the DMA state under pcdev->lock (along with the other
pcdev->capture paths in pxac_vb2_queue() and pxac_vb2_stop_streaming(), plus
cancelling eof_bh_work on stream stop)?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261023073437.1517305-1-lixicaicai@gmail.co…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Critical] accel/thames: use-after-free of bo in thames_ioctl_bo_create()
- [High] accel/thames: missing TLB flush when thames_bo_bind() replaces L3 table
- [Low] accel/thames: Kconfig selects DRM_GEM_SHMEM_HELPER without MMU dependency
- [Low] accel/thames: missing MMU, IOMMU_SUPPORT, and 64BIT dependencies in Kconfig
--
commit e7110c2d02687ac644a69317b2f94c6b4caee594
Author: Tomeu Vizoso <tomeu.vizoso(a)gmail.com>
accel/thames: Add IOCTLs for BO creation and mapping
This commit adds the DRM_IOCTL_THAMES_BO_CREATE,
DRM_IOCTL_THAMES_BO_MMAP_OFFSET, and DRM_IOCTL_THAMES_BO_BIND ioctls to
create SHMEM or contiguous DMA buffer objects and map or unmap them in
the DSP page tables of each core.
> diff --git a/drivers/accel/thames/Kconfig b/drivers/accel/thames/Kconfig
> index 9362b92740751..47a2986fcabdc 100644
> --- a/drivers/accel/thames/Kconfig
> +++ b/drivers/accel/thames/Kconfig
> @@ -6,6 +6,8 @@ config DRM_ACCEL_THAMES
> depends on TI_K3_DSP_REMOTEPROC
> depends on REMOTEPROC
> depends on MAILBOX
> + select DRM_GEM_DMA_HELPER
> + select DRM_GEM_SHMEM_HELPER
> select IOMMU_IO_PGTABLE_LPAE
[Severity: Low]
Can selecting DRM_GEM_SHMEM_HELPER and IOMMU_IO_PGTABLE_LPAE without
inheriting their dependencies cause Kconfig unmet dependency warnings,
build failures, or 32-bit address truncation when COMPILE_TEST is enabled?
In drivers/remoteproc/Kconfig, TI_K3_DSP_REMOTEPROC has:
config TI_K3_DSP_REMOTEPROC
tristate "TI K3 DSP remoteproc support"
depends on ARCH_K3 || COMPILE_TEST
However, DRM_GEM_SHMEM_HELPER in drivers/gpu/drm/Kconfig depends on MMU:
config DRM_GEM_SHMEM_HELPER
tristate
depends on DRM && MMU
And IOMMU_IO_PGTABLE_LPAE in drivers/iommu/Kconfig is guarded by
if IOMMU_SUPPORT (which depends on MMU) and depends on !GENERIC_ATOMIC64,
with drivers/iommu/io-pgtable-arm.c also calling iommu_alloc_pages_node_sz()
and iommu_free_pages() from CONFIG_IOMMU_SUPPORT:
if IOMMU_SUPPORT
...
config IOMMU_IO_PGTABLE_LPAE
bool "ARMv7/v8 Long Descriptor Format"
select IOMMU_IO_PGTABLE
depends on ARM || ARM64 || COMPILE_TEST
depends on !GENERIC_ATOMIC64 # for cmpxchg64()
In addition, thames_bo_bind() requires dsp_va >= THAMES_BIND_VA_START
(SZ_4G) and passes dsp_va through thames_context_map() ->
thames_pgtable_map() -> io_pgtable_ops->map_pages() and ->unmap_pages(),
where the iova parameter is unsigned long and would truncate 64-bit DSP
virtual addresses on 32-bit COMPILE_TEST builds.
Should DRM_ACCEL_THAMES depend on MMU, IOMMU_SUPPORT (or select it as
DRM_PANFROST and DRM_PANTHOR do), !GENERIC_ATOMIC64, and 64BIT?
[ ... ]
> diff --git a/drivers/accel/thames/thames_gem.c b/drivers/accel/thames/thames_gem.c
> index b98e0fe203b08..c9fd08c7aaf4d 100644
> --- a/drivers/accel/thames/thames_gem.c
> +++ b/drivers/accel/thames/thames_gem.c
[ ... ]
> +static int thames_bo_bind(struct thames_file_priv *priv, struct thames_gem_object *bo,
> + u64 dsp_va)
> +{
> + size_t size = bo->base.base.size;
> + int prot = thames_bo_flags_to_prot(bo->flags);
> + struct thames_binding *binding;
> + int ret;
> +
> + if (!IS_ALIGNED(dsp_va, PAGE_SIZE))
> + return -EINVAL;
> +
> + if (dsp_va < THAMES_BIND_VA_START || dsp_va > THAMES_BIND_VA_END ||
> + size > THAMES_BIND_VA_END - dsp_va)
> + return -EINVAL;
> +
> + binding = kzalloc_obj(*binding);
> + if (!binding)
> + return -ENOMEM;
> +
> + binding->bo = bo;
> + binding->priv = priv;
> + binding->mm.start = dsp_va;
> + binding->mm.size = size;
> +
> + ret = mutex_lock_interruptible(&priv->tdev->bindings_lock);
> + if (ret)
> + goto err_free;
> +
> + if (thames_bo_find_binding(bo, priv)) {
> + ret = -EBUSY;
> + goto err_unlock;
> + }
> +
> + scoped_guard(mutex, &priv->mm_lock)
> + ret = drm_mm_reserve_node(&priv->mm, &binding->mm);
> + if (ret)
> + goto err_unlock;
> +
> + if (bo->flags & DRM_THAMES_BO_CONTIGUOUS)
> + ret = thames_context_map(priv, dsp_va, prot, bo->base.dma_addr, size);
> + else
> + ret = thames_context_map_sgt(priv, dsp_va, prot, bo->shmem.sgt);
> + if (ret) {
> + scoped_guard(mutex, &priv->mm_lock)
> + drm_mm_remove_node(&binding->mm);
> + goto err_unlock;
> + }
> +
> + list_add_tail(&binding->bo_link, &bo->bindings);
> + list_add_tail(&binding->context_link, &priv->bindings);
> + mutex_unlock(&priv->tdev->bindings_lock);
[Severity: High]
Can thames_bo_bind() leave stale walk-cache entries on the DSP cores when a
2 MiB block mapping replaces an existing Level 3 page table?
In thames_context_pgtable_init(), the ARM_64_LPAE_S1 page tables are
allocated with cfg.tlb left as NULL:
struct io_pgtable_cfg cfg = {
.pgsize_bitmap = SZ_4K | SZ_2M,
.ias = 48,
.oas = 40,
.coherent_walk = false,
.iommu_dev = tdev->ddev.dev,
};
Because cfg.tlb is NULL, io_pgtable_tlb_flush_walk() is a no-op and TLB
invalidation relies on thames_context_bump_generation(priv).
If a 4 KiB buffer is bound inside a 2 MiB region and later unbound via
thames_bo_unbind(), __arm_lpae_unmap() clears the Level 3 leaf PTE while
leaving the Level 2 ARM_LPAE_PTE_TYPE_TABLE descriptor in place. If a job
then runs on the DSP, the unbind's mapping_generation is consumed while the
Level 2 table descriptor remains valid and cacheable by the MMU walk cache.
When a 2 MiB buffer is subsequently bound at that 2 MiB-aligned dsp_va via
thames_bo_bind(), arm_lpae_init_pte() in drivers/iommu/io-pgtable-arm.c
unmaps and frees the old Level 3 page table page before installing the
block entry:
} else if (iopte_type(ptep[i]) == ARM_LPAE_PTE_TYPE_TABLE) {
/*
* We need to unmap and free the old table before
* overwriting it with a block entry.
*/
arm_lpae_iopte *tblp;
size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data);
tblp = ptep - ARM_LPAE_LVL_IDX(iova, lvl, data);
if (__arm_lpae_unmap(data, NULL, iova + i * sz, sz, 1,
lvl, tblp) != sz) {
WARN_ON(1);
return -EINVAL;
}
}
Since neither cfg.tlb->tlb_flush_walk nor thames_bo_bind() calls
thames_context_bump_generation(priv), won't the DSP MMU walk cache retain
entries pointing to the freed Level 3 page table page?
[ ... ]
> +int thames_ioctl_bo_create(struct drm_device *ddev, void *data, struct drm_file *file)
> +{
> + struct drm_thames_bo_create *args = data;
> + struct thames_gem_object *bo;
> + struct drm_gem_dma_object *mem;
> + int ret;
> +
> + if (args->handle)
> + return -EINVAL;
> +
> + if (!args->size)
> + return -EINVAL;
> +
> + if (args->flags & ~(DRM_THAMES_BO_READ_ONLY | DRM_THAMES_BO_EXECUTABLE |
> + DRM_THAMES_BO_CONTIGUOUS))
> + return -EINVAL;
> +
> + if (args->flags & DRM_THAMES_BO_CONTIGUOUS) {
> + mem = thames_gem_create(ddev, args->size);
> + if (IS_ERR(mem))
> + return PTR_ERR(mem);
> + bo = to_thames_bo(&mem->base);
> + } else {
> + bo = thames_gem_create_shmem(ddev, args->size);
> + if (IS_ERR(bo))
> + return PTR_ERR(bo);
> + }
> +
> + bo->flags = args->flags;
> +
> + ret = drm_gem_handle_create(file, &bo->base.base, &args->handle);
> + drm_gem_object_put(&bo->base.base);
> + if (ret) {
> + dev_err(ddev->dev, "Failed to create gem handle: %d", ret);
> + return ret;
> + }
> +
> + args->size = bo->base.base.size;
[Severity: Critical]
Does reading bo->base.base.size after drm_gem_object_put(&bo->base.base)
cause a use-after-free of bo?
Once drm_gem_handle_create() publishes the handle in file->object_idr and
drm_gem_object_put() drops the local reference, a concurrent thread sharing
the same drm_file can call DRM_IOCTL_GEM_CLOSE on the new handle and free
bo via thames_gem_bo_free() or thames_gem_shmem_bo_free() before
args->size = bo->base.base.size is reached.
Should args->size = bo->base.base.size be set before calling
drm_gem_handle_create() or before dropping the local reference?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261023-thames-v3-0-3ab40aa17157@tomeuvizos…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] accel/thames: persistent device link to rejected core in thames_core_init()
- [High] accel/thames: embedded mbox_dev lifetime and reuse in thames_platform_probe()
- [High] accel/thames: broken rproc auto_boot check and power leak in thames_core_init()
- [Medium] accel/thames: unconditional platform device creation in thames_register()
--
commit 33330c246e61374c538a20616d02df3f30a783af
Author: Tomeu Vizoso <tomeu.vizoso(a)gmail.com>
accel/thames: Add driver for the C7x DSPs in TI SoCs
This commit adds the initial DRM compute accelerator driver for Texas
Instruments C7x DSP cores on the J722S SoC. It discovers available DSP
cores via device tree, attaches to their remoteproc instances, performs a
shared-memory and mailbox doorbell handshake to validate firmware ABI and
capabilities, and exposes a device capability query ioctl to userspace.
> diff --git a/drivers/accel/thames/thames_core.c b/drivers/accel/thames/thames_core.c
> new file mode 100644
> index 0000000000000..d74c74b8f8ebf
> --- /dev/null
> +++ b/drivers/accel/thames/thames_core.c
[ ... ]
> +/* Embedded in struct thames_core: nothing to free. */
> +static void thames_core_mbox_dev_release(struct device *dev)
> +{
> +}
> +
> +/*
> + * A device of the driver's own carrying the DSP node, so the mailbox core finds
> + * the node's mboxes without borrowing the K3 driver's device.
> + */
> +static int thames_core_request_doorbell(struct thames_core *core)
> +{
> + struct mbox_client *cl = &core->mbox_client;
> + struct device *mbox_dev = &core->mbox_dev;
> + int err;
> +
> + device_initialize(mbox_dev);
> + mbox_dev->parent = core->tdev->ddev.dev;
> + mbox_dev->release = thames_core_mbox_dev_release;
> + device_set_node(mbox_dev, of_fwnode_handle(core->of_node));
> + dev_set_name(mbox_dev, "thames-mbox.%u", core->index);
> +
> + err = device_add(mbox_dev);
> + if (err) {
> + put_device(mbox_dev);
> + return err;
> + }
> +
> + cl->dev = mbox_dev;
> + cl->rx_callback = thames_mbox_rx;
> + cl->tx_block = false;
> + cl->knows_txdone = false;
> +
> + core->mbox_chan = mbox_request_channel(cl, 1);
> + if (IS_ERR(core->mbox_chan)) {
> + err = PTR_ERR(core->mbox_chan);
> + core->mbox_chan = NULL;
> + device_del(mbox_dev);
> + put_device(mbox_dev);
> + return err;
> + }
> +
> + return 0;
> +}
[Severity: High]
Can embedding struct device mbox_dev inside struct thames_core with an empty
thames_core_mbox_dev_release() callback corrupt kobject state or cause a
use-after-free?
In thames_core_init(), thames_core_request_doorbell() registers core->mbox_dev
via device_initialize() and device_add() before rproc_boot(),
thames_core_wait_handshake(), and thames_core_read_handshake(). If core 0
fails any of those steps (for example, when its firmware handshake is
refused), thames_core_free_doorbell() calls device_del() and put_device(), and
thames_platform_probe() in drivers/accel/thames/thames_drv.c immediately does:
if (err) {
dev_warn(&pdev->dev, "Leaving out the core at %pOF: %d\n", np, err);
of_node_put(np);
memset(&tdev->cores[core], 0, sizeof(tdev->cores[core]));
continue;
}
Without incrementing core, the next loop iteration zeroes and re-registers the
same tdev->cores[0].mbox_dev memory in-place via device_initialize() and
device_add(). Furthermore, tdev is allocated with devm_drm_dev_alloc(), so its
lifetime is tied to tdev->ddev rather than mbox_dev's kobject refcount. If a
sysfs reader, uevent handler, or CONFIG_DEBUG_KOBJECT_RELEASE still holds a
reference to mbox_dev->kobj after put_device(), zeroing and re-initializing
tdev->cores[0] or freeing tdev on unbind will access or overwrite live kobject
memory.
[ ... ]
> +int thames_core_init(struct thames_core *core)
> +{
> + struct thames_caps caps = {};
> + struct rproc *rproc;
> + int err;
> +
> + /* Without a phandle the lookup would defer forever. */
> + if (!core->of_node->phandle) {
> + dev_err(core->tdev->ddev.dev, "%pOF has no phandle to find its remoteproc by\n",
> + core->of_node);
> + return -EINVAL;
> + }
> +
> + rproc = rproc_get_by_phandle(core->of_node->phandle);
> + if (!rproc)
> + return dev_err_probe(core->tdev->ddev.dev, -EPROBE_DEFER,
> + "%pOF: no remoteproc registered for it yet\n", core->of_node);
> +
> + /*
> + * remoteproc's own boot is still to come: booting the core here as well
> + * would leave two power counts, and a reset or a suspend could not stop
> + * it.
> + */
> + if (rproc->state == RPROC_OFFLINE && rproc->auto_boot) {
> + rproc_put(rproc);
> + return dev_err_probe(core->tdev->ddev.dev, -EPROBE_DEFER,
> + "%pOF: remoteproc has not booted it yet (auto-boot pending)\n",
> + core->of_node);
> + }
> +
> + core->rproc = rproc;
> + core->dev = rproc->dev.parent;
> + snprintf(core->name, sizeof(core->name), "DSP core %s (index %u)", dev_name(core->dev),
> + core->index);
> +
> + /*
> + * The PM core then suspends this driver before the DSP, and an unbind
> + * of the K3 driver unbinds this one first.
> + */
> + if (!device_link_add(core->tdev->ddev.dev, core->dev, DL_FLAG_AUTOPROBE_CONSUMER)) {
> + dev_err(core->tdev->ddev.dev, "Failed to link to %s\n", dev_name(core->dev));
> + err = -EINVAL;
> + goto err_put;
> + }
> +
> + err = thames_core_read_windows(core);
> + if (err)
> + goto err_put;
> +
> + err = thames_core_get_timers(core);
> + if (err)
> + goto err_put;
> +
> + err = thames_core_map_ipc(core);
> + if (err)
> + goto err_timers;
> +
> + err = thames_core_request_doorbell(core);
> + if (err)
> + goto err_unmap;
> +
> + if (rproc->state == RPROC_OFFLINE) {
> + err = rproc_boot(rproc);
> + if (err)
> + goto err_doorbell;
> + }
> +
> + err = thames_core_wait_handshake(core);
> + if (err)
> + goto err_doorbell;
> +
> + err = thames_core_read_handshake(core, &caps);
> + if (err)
> + goto err_doorbell;
> +
> + if (!core->tdev->num_cores)
> + core->tdev->caps = caps;
> +
> + return 0;
> +
> +err_doorbell:
> + thames_core_free_doorbell(core);
> +err_unmap:
> + thames_core_unmap_ipc(core);
> +err_timers:
> + thames_core_put_timers(core);
> +err_put:
> + rproc_put(rproc);
> + core->rproc = NULL;
> + return err;
> +}
[Severity: High]
Does adding the managed device link via device_link_add() before validating
the core's resources and firmware handshake leave an active supplier link to a
rejected DSP core?
In thames_core_init(), device_link_add(core->tdev->ddev.dev, core->dev,
DL_FLAG_AUTOPROBE_CONSUMER) is called before thames_core_read_windows(),
thames_core_get_timers(), thames_core_map_ipc(),
thames_core_request_doorbell(), rproc_boot(), thames_core_wait_handshake(),
and thames_core_read_handshake(). If any of those steps fail (for instance,
when one of the two DSP cores runs non-Thames firmware and is refused at
handshake), the error path unwinds without removing the managed device link,
and thames_platform_probe() continues probing with the remaining DSP core.
When thames_platform_probe() succeeds using the other core,
device_links_driver_bound() activates all managed links on
core->tdev->ddev.dev, including the link to the refused core->dev. If the
user later unbinds the remoteproc driver from the refused DSP core,
device_links_unbind_consumers() will forcefully unbind the thames driver and
unplug the active DRM device, and device_links_check_suppliers() will block
thames from re-probing with -EPROBE_DEFER while that core's remoteproc remains
unbound.
[Severity: High]
In addition, how can the offline boot path in thames_core_init() be reached
given that rproc->auto_boot remains true?
In rproc_alloc(), rproc->auto_boot is initialized to true, and neither
k3_dsp_rproc_probe() nor the remoteproc core clears rproc->auto_boot after
rproc_trigger_auto_boot() completes, fails, or when a core is stopped via
sysfs. Because of the earlier check:
if (rproc->state == RPROC_OFFLINE && rproc->auto_boot) {
rproc_put(rproc);
return dev_err_probe(core->tdev->ddev.dev, -EPROBE_DEFER, ...);
}
any core in RPROC_OFFLINE causes thames_core_init() to return -EPROBE_DEFER
(which aborts thames_platform_probe() for all cores), making the subsequent
if (rproc->state == RPROC_OFFLINE) rproc_boot(rproc) block unreachable unless
rproc->state changes concurrently without rproc->lock. Even when
rproc_auto_boot_callback() is genuinely in flight, returning -EPROBE_DEFER
relies on driver_deferred_probe_trigger(), which is only invoked when a device
binds to a driver; since this firmware creates no virtio or remoteproc
subdevices, completing rproc_boot() will not re-trigger deferred probing.
Also, if rproc_boot(rproc) in thames_core_init() is reached via a race on
rproc->state and succeeds, but thames_core_wait_handshake() or
thames_core_read_handshake() then fails, jumping to err_doorbell drops the
rproc reference via rproc_put() without calling rproc_shutdown(), leaking the
rproc->power count. And in both thames_core_init() and thames_core_suspend(),
rproc->state is read without holding rproc->lock.
> diff --git a/drivers/accel/thames/thames_drv.c b/drivers/accel/thames/thames_drv.c
> new file mode 100644
> index 0000000000000..de93bc87756c3
> --- /dev/null
> +++ b/drivers/accel/thames/thames_drv.c
[ ... ]
> +static int __init thames_register(void)
> +{
> + int ret;
> +
> + drm_dev = platform_device_register_simple("thames", -1, NULL, 0);
> + if (IS_ERR(drm_dev))
> + return PTR_ERR(drm_dev);
> +
> + ret = platform_driver_register(&thames_platform_driver);
> + if (ret)
> + platform_device_unregister(drm_dev);
> +
> + return ret;
> +}
[Severity: Medium]
Should thames_register() check whether a compatible DSP node is available in
the device tree before calling platform_device_register_simple()?
On multiplatform kernels where CONFIG_DRM_ACCEL_THAMES is built-in or loaded
on a SoC without an available "ti,am62a-c7xv-dsp" node, thames_register()
unconditionally creates the "thames" platform device. This triggers
thames_platform_probe(), which logs an error ("No DSP core attached") and
fails with -ENODEV, while platform_driver_register() still returns 0 and
leaves the unbound platform device registered in sysfs.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261023-thames-v3-0-3ab40aa17157@tomeuvizos…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Critical] accel/thames: double put and NULL deref on error in thames_gem_mmap()
- [Critical] accel/thames: COW bypass in thames_gem_mmap() on MAP_PRIVATE mappings
- [Low] accel/thames: MMU-only PTE helpers in thames_gem.c break !MMU COMPILE_TEST builds
--
commit c4d46a848bca800b97f80e3d5bd5a0d1cb370cf4
Author: Tomeu Vizoso <tomeu.vizoso(a)gmail.com>
accel/thames: Install memory mappings populated and dirty
This commit replaces the default GEM DMA and GEM shmem mmap helpers in the
thames accelerator driver with custom mmap callbacks that pre-populate page
table entries as writable and dirty up front.
> diff --git a/drivers/accel/thames/thames_gem.c b/drivers/accel/thames/thames_gem.c
> index 5a4dc497768f8..9f55169c6370b 100644
> --- a/drivers/accel/thames/thames_gem.c
> +++ b/drivers/accel/thames/thames_gem.c
> @@ -808,12 +808,82 @@ static void thames_gem_shmem_bo_free(struct drm_gem_object *obj)
> drm_gem_shmem_free(&bo->shmem);
> }
>
> +/*
> + * Installed dirty and writable: the A53s have no hardware dirty-bit management,
> + * so every first write would fault.
> + */
> +static int thames_gem_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
> +{
> + struct drm_gem_dma_object *dma_obj = to_drm_gem_dma_obj(obj);
> + pgprot_t prot;
> + int ret;
> +
> + vma->vm_pgoff -= drm_vma_node_start(&obj->vma_node);
> + vm_flags_mod(vma, VM_DONTDUMP | VM_DONTEXPAND, VM_PFNMAP);
> +
> + prot = vm_get_page_prot(vma->vm_flags);
> + if (vma->vm_flags & VM_WRITE)
> + prot = pte_pgprot(pte_mkdirty(pte_mkwrite_novma(pfn_pte(0, prot))));
> + vma->vm_page_prot = prot;
[Severity: Critical]
What happens here if userspace calls mmap() with PROT_WRITE and MAP_PRIVATE
on a contiguous buffer object?
In that case, vma_is_cow_mapping(vma) is true (VM_WRITE is set while
VM_SHARED is clear), so vm_get_page_prot(vma->vm_flags) returns read-only
page protection to fault on writes for copy-on-write.
Because thames_gem_mmap() does not reject vma_is_cow_mapping(vma) (unlike
thames_gem_shmem_mmap() below) and applies pte_mkwrite_novma() whenever
VM_WRITE is set, dma_mmap_pages() -> remap_pfn_range() will install
hardware-writable PTEs pointing directly to dma_obj->vaddr in a private
mapping, allowing writes to bypass copy-on-write and mutate the shared DMA
buffer.
Should thames_gem_mmap() reject vma_is_cow_mapping(vma) with -EINVAL as well?
> +
> + ret = dma_mmap_pages(drm_dev_dma_dev(obj->dev), vma, vma->vm_end - vma->vm_start,
> + virt_to_page(dma_obj->vaddr));
> + if (ret)
> + drm_gem_vm_close(vma);
> +
> + return ret;
> +}
[Severity: Critical]
Does calling drm_gem_vm_close(vma) when dma_mmap_pages() fails cause a
double put or a NULL pointer dereference?
thames_gem_mmap() is wired up as the drm_gem_object_funcs.mmap callback in
thames_gem_funcs.
When called via drm_gem_mmap() -> drm_gem_mmap_obj(), drm_gem_mmap_obj()
takes one reference on obj for the VMA and drops that reference on error:
drivers/gpu/drm/drm_gem.c:drm_gem_mmap_obj() {
drm_gem_object_get(obj);
vma->vm_private_data = obj;
vma->vm_ops = obj->funcs->vm_ops;
if (obj->funcs->mmap) {
ret = obj->funcs->mmap(obj, vma);
if (ret)
goto err_drm_gem_object_put;
...
err_drm_gem_object_put:
drm_gem_object_put(obj);
return ret;
}
Since drm_gem_vm_close(vma) also calls drm_gem_object_put(vma->vm_private_data),
a failure in dma_mmap_pages() (for example, -ENOMEM in remap_pfn_range() or
-ENXIO on an out-of-range page offset) drops obj's reference twice,
underflowing the reference count held by the GEM handle and prematurely
freeing the buffer object.
Furthermore, when called on an exported DMA-BUF via
dma_buf_mmap_internal() -> drm_gem_dmabuf_mmap() -> drm_gem_prime_mmap(),
vma->vm_private_data is still NULL when obj->funcs->mmap(obj, vma) runs:
drivers/gpu/drm/drm_prime.c:drm_gem_prime_mmap() {
if (obj->funcs && obj->funcs->mmap) {
vma->vm_ops = obj->funcs->vm_ops;
drm_gem_object_get(obj);
ret = obj->funcs->mmap(obj, vma);
if (ret) {
drm_gem_object_put(obj);
return ret;
}
vma->vm_private_data = obj;
return 0;
}
}
In that path, calling drm_gem_vm_close(vma) passes NULL to
drm_gem_object_put(), triggering a NULL pointer dereference.
> +
> +/*
> + * Every page installed now, dirty and writable: they are pinned for the
> + * object's life, and the helper would take two faults on each.
> + */
> +static int thames_gem_shmem_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
> +{
> + struct thames_gem_object *bo = to_thames_bo(obj);
> + unsigned long addr = vma->vm_start;
> + struct scatterlist *sg;
> + pgprot_t prot;
> + unsigned int i;
> + int ret;
> +
> + if (vma_is_cow_mapping(vma))
> + return -EINVAL;
> +
> + vm_flags_set(vma, VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP);
> +
> + prot = vm_get_page_prot(vma->vm_flags);
> + if (vma->vm_flags & VM_WRITE)
> + prot = pte_pgprot(pte_mkdirty(pte_mkwrite_novma(pfn_pte(0, prot))));
> + vma->vm_page_prot = prot;
[Severity: Low]
Could the use of pfn_pte(), pte_mkwrite_novma(), pte_mkdirty(), and
pte_pgprot() in both thames_gem_mmap() and thames_gem_shmem_mmap() break
COMPILE_TEST builds when CONFIG_MMU is disabled or clear protection bits on
architectures without a custom pte_pgprot()?
In drivers/accel/thames/Kconfig, DRM_ACCEL_THAMES depends on
TI_K3_DSP_REMOTEPROC (which allows COMPILE_TEST) without a depends on MMU
dependency. On !CONFIG_MMU targets, pfn_pte(), pte_mkwrite_novma(), and
pte_mkdirty() are not declared. Additionally, on architectures that rely on
the generic fallback in include/linux/pgtable.h:
#ifndef pte_pgprot
#define pte_pgprot(x) ((pgprot_t) {0})
#endif
pte_pgprot() zeroes out all protection bits in prot.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261023-thames-v3-0-3ab40aa17157@tomeuvizos…