Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] media: em28xx: missing mutex_unlock() in em28xx_v4l2_fini() when v4l2 is NULL
--
commit 5132f3a1057e95055bbcd81d79b2a705ab167335
Author: Drif Abdelmalek Mohamed Said <drifabdelmalekmohamedsaid(a)gmail.com>
media: em28xx: fix v4l2 use-after-free after failed init
This commit fixes a use-after-free when em28xx_v4l2_init() fails after
v4l2_device_register() by clearing dev->v4l2 under dev->lock on the error
path and reading dev->v4l2 under dev->lock in em28xx_v4l2_fini().
> diff --git a/drivers/media/usb/em28xx/em28xx-video.c b/drivers/media/usb/em28xx/em28xx-video.c
> index 84424284d4869..572e6835b851f 100644
> --- a/drivers/media/usb/em28xx/em28xx-video.c
> +++ b/drivers/media/usb/em28xx/em28xx-video.c
> @@ -2414,7 +2414,7 @@ static int em28xx_v4l2_open(struct file *filp)
> */
> static int em28xx_v4l2_fini(struct em28xx *dev)
> {
> - struct em28xx_v4l2 *v4l2 = dev->v4l2;
> + struct em28xx_v4l2 *v4l2;
>
> if (dev->is_audio_only) {
> /* Shouldn't initialize IR for this interface */
> @@ -2426,13 +2426,15 @@ static int em28xx_v4l2_fini(struct em28xx *dev)
> return 0;
> }
>
> - if (!v4l2)
> - return 0;
> -
> dev_info(&dev->intf->dev, "Closing video extension\n");
>
> mutex_lock(&dev->lock);
>
> + /* matches em28xx_v4l2_init error paths */
> + v4l2 = dev->v4l2;
> + if (!v4l2)
> + return 0;
[Severity: High]
Does this early return in em28xx_v4l2_fini() leak dev->lock when dev->v4l2
is NULL?
When em28xx_v4l2_init() fails (including the unregister_dev error path where
this patch sets dev->v4l2 = NULL), dev->v4l2 is NULL when em28xx_v4l2_fini()
runs later during USB disconnect or module unload. Returning 0 here without
calling mutex_unlock(&dev->lock) leaves dev->lock held.
During USB disconnect in drivers/media/usb/em28xx/em28xx-cards.c,
em28xx_usb_disconnect() calls em28xx_release_resources(dev) immediately
after em28xx_close_extension(dev):
em28xx_usb_disconnect()
em28xx_close_extension(dev)
ops->fini(dev) -> em28xx_v4l2_fini(dev)
mutex_lock(&dev->lock);
if (!v4l2)
return 0;
em28xx_release_resources(dev)
mutex_lock(&dev->lock);
Wouldn't this immediately self-deadlock on dev->lock?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261031204723.1415-1-drifabdelmalekmohameds…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] media: i2c: tda1997x: UAF from uncancelled delayed work in tda1997x_probe()
--
commit 161e1a218badfd32b9d01ac7b21a981701e1a288
Author: Guangshuo Li <liguangshuo(a)mail.sdu.edu.cn>
media: i2c: tda1997x: Fix potential use-after-free on probe failure
This commit updates tda1997x_probe() to unregister the V4L2 subdevice via
v4l2_async_unregister_subdev() if audio component registration or IRQ setup
fails after the subdevice has been registered.
> diff --git a/drivers/media/i2c/tda1997x.c b/drivers/media/i2c/tda1997x.c
> index 30dfd8a7b42bc..4ff9acbc005ec 100644
> --- a/drivers/media/i2c/tda1997x.c
> +++ b/drivers/media/i2c/tda1997x.c
> @@ -2776,7 +2776,7 @@ static int tda1997x_probe(struct i2c_client *client)
> &tda1997x_audio_dai, 1);
> if (ret) {
> dev_err(&client->dev, "register audio codec failed\n");
> - goto err_free_media;
> + goto err_unreg_subdev;
> }
> v4l_info(state->client, "registered audio codec\n");
> }
> @@ -2788,11 +2788,13 @@ static int tda1997x_probe(struct i2c_client *client)
> KBUILD_MODNAME, state);
> if (ret) {
> v4l_err(client, "irq%d reg failed: %d\n", client->irq, ret);
> - goto err_free_media;
> + goto err_unreg_subdev;
> }
>
> return 0;
>
> +err_unreg_subdev:
> + v4l2_async_unregister_subdev(sd);
[Severity: Critical]
Should the err_unreg_subdev path also call
cancel_delayed_work_sync(&state->delayed_work_enable_hpd), as tda1997x_remove()
does?
Once v4l2_async_register_subdev(sd) succeeds in tda1997x_probe(), a bound
notifier or userspace can invoke the subdev pad operation tda1997x_set_edid(),
which schedules state->delayed_work_enable_hpd:
tda1997x_set_edid()
-> tda1997x_enable_edid()
schedule_delayed_work(&state->delayed_work_enable_hpd, ...);
If devm_snd_soc_register_component() or devm_request_threaded_irq() then fails,
tda1997x_probe() jumps to err_unreg_subdev, unregisters the subdev, destroys
state->page_lock and state->lock, and frees state via kfree(state) without
cancelling state->delayed_work_enable_hpd.
When tda1997x_delayed_work_enable_hpd() later runs, wouldn't it access freed
state and lock the destroyed state->page_lock via tda1997x_manual_hpd()?
--
Sashiko AI review · https://sashiko.dev/#/patchset/7c26d0753bb5a45f4abdec2ae12f2cba20dbd0c7.179…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] dt-bindings: media: qcom,sm8650-camss: missing minItems on reg and clocks
--
commit e38622494d300e76674ad2714e0222a5d2a3e220
Author: Vladimir Zapolskiy <vladimir.zapolskiy(a)linaro.org>
dt-bindings: media: qcom,sm8650-camss: Add test pattern generators
This commit adds the three test pattern generator (TPG) register spaces and
the non-real time CAMNOC AXI clock to the Qualcomm SM8650 CAMSS device tree
binding schema.
> diff --git a/Documentation/devicetree/bindings/media/qcom,sm8650-camss.yaml b/Documentation/devicetree/bindings/media/qcom,sm8650-camss.yaml
> index 9c8de722601ec..50ae30cdcb82a 100644
> --- a/Documentation/devicetree/bindings/media/qcom,sm8650-camss.yaml
> +++ b/Documentation/devicetree/bindings/media/qcom,sm8650-camss.yaml
> @@ -17,7 +17,7 @@ properties:
> const: qcom,sm8650-camss
>
> reg:
> - maxItems: 17
> + maxItems: 20
>
> reg-names:
> items:
> @@ -38,9 +38,12 @@ properties:
> - const: vfe2
> - const: vfe_lite0
> - const: vfe_lite1
> + - const: tpg0
> + - const: tpg1
> + - const: tpg2
>
> clocks:
> - maxItems: 33
> + maxItems: 34
>
> clock-names:
> items:
> @@ -77,6 +80,7 @@ properties:
> - const: vfe_lite_ahb
> - const: vfe_lite_cphy_rx
> - const: vfe_lite_csid
> + - const: camnoc_nrt_axi
[Severity: Medium]
Should reg and reg-names specify minItems: 17, and should clocks and
clock-names specify minItems: 33 in
Documentation/devicetree/bindings/media/qcom,sm8650-camss.yaml?
In dtschema, increasing maxItems on reg from 17 to 20 and on clocks from
33 to 34 (while appending tpg0, tpg1, tpg2 to reg-names and camnoc_nrt_axi
to clock-names) without specifying minItems implicitly sets minItems equal
to maxItems. This makes the newly added TPG register blocks and clock
mandatory rather than optional.
Existing qcom,sm8650-camss device tree nodes such as in
arch/arm64/boot/dts/qcom/sm8650.dtsi (introduced in commit e8560ac937d92e)
provide 17 reg/reg-names entries and 33 clocks/clock-names entries prior to
the DTS update in this series, or when the binding and driver changes merge
through the media tree independently of the Qualcomm SoC DTS tree. In
addition, commit c865d36a9474 ("media: qcom: camss: Skip TPG if its
registers are not described") in this series explicitly keeps TPG optional
in camss_probe() for backward compatibility with older device trees.
Without minItems: 17 on reg and reg-names and minItems: 33 on clocks and
clock-names, does dtbs_check fail validation on existing qcom,sm8650-camss
device trees that omit the TPG entries?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261031112203.545319-1-vladimir.zapolskiy@l…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] media: qcom: camss: tpg_reset() checks hw_version before initialization
--
commit ed70791a72b90a115dbcf4eead6d852fc6c9f986
Author: Vladimir Zapolskiy <vladimir.zapolskiy(a)linaro.org>
media: qcom: camss: tpg: Support TPG hardware version 2.2.0
This commit lowers the TPG hardware version threshold from 2.4.0 to 2.2.0 in
tpg_stream_on() and tpg_reset() to support Qualcomm SM8650 CAMSS TPG blocks,
which start test pattern generation via TPG_CTRL_CMD rather than a test enable
bit in TPG_CTRL.
> diff --git a/drivers/media/platform/qcom/camss/camss-tpg-gen1.c b/drivers/media/platform/qcom/camss/camss-tpg-gen1.c
> index cabbb2638cf38..a83f9a80c2b6b 100644
> --- a/drivers/media/platform/qcom/camss/camss-tpg-gen1.c
> +++ b/drivers/media/platform/qcom/camss/camss-tpg-gen1.c
[ ... ]
> @@ -183,7 +184,7 @@ static int tpg_stream_on(struct tpg_device *tpg)
>
> static int tpg_reset(struct tpg_device *tpg)
> {
> - if (tpg->hw_version < TPG_HW_VER_2_4_0)
> + if (tpg->hw_version < TPG_HW_VER_2_2_0)
> writel(0, tpg->base + TPG_CTRL);
[Severity: Medium]
When a TPG subdevice is powered on for the first time via tpg_set_power(), is
tpg->hw_version initialized before tpg_reset() checks it?
In drivers/media/platform/qcom/camss/camss-tpg.c, tpg_set_power() invokes the
reset callback before reading and storing the hardware version via the
hw_version callback (tpg_hw_version()):
drivers/media/platform/qcom/camss/camss-tpg.c:tpg_set_power() {
...
if (on) {
...
tpg->res->hw_ops->reset(tpg);
tpg->res->hw_ops->hw_version(tpg);
}
...
}
Because camss->tpg is zero-allocated in camss_probe(), tpg->hw_version is
still 0 on the initial tpg_set_power() -> tpg_reset() call. As a result,
tpg->hw_version < TPG_HW_VER_2_2_0 evaluates to true on the first power-on
even on TPG v2.2.0 and newer hardware, causing tpg_reset() to execute
writel(0, tpg->base + TPG_CTRL).
Should tpg_set_power() call hw_ops->hw_version(tpg) before hw_ops->reset(tpg)?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261031112203.545319-1-vladimir.zapolskiy@l…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] media: qcom: camss: uninitialized hw_version check in tpg_reset()
- [Low] media: qcom: camss: commit message describes omitted Kaanapali changes
--
commit 9872eb254e7eae29edd30791f0f0da3fb4571071
Author: Hangxiang Ma <hangxiang.ma(a)oss.qualcomm.com>
media: qcom: camss: tpg: Add support for v2.4.0 TPG
This commit adds hardware support for the v2.4.0 Test Pattern Generator (TPG)
in the Qualcomm CAMSS driver by handling test-enable and hardware reset via
the TPG_CTRL_CMD register.
> media: qcom: camss: tpg: Add support for v2.4.0 TPG
>
> Add support for the TPG found on Kaanapali. This TPG uses hardware
> version 2.4.0, which drives test-enable and reset through the
> TPG_CTRL_CMD register at offset 0x1f4 instead of the TEST_EN bit in
> TPG_CTRL and the TPG_CLEAR register. Its output is routed into the
> CSID 1080 RX through the CSI2 TPG mux, so the CSID selects the TPG as
> the data source when a test pattern is selected.
>
> Add the TPG resources to the Kaanapali camss resources so that the TPG
> subdevices are created on this SoC.
>
> Signed-off-by: Hangxiang Ma <hangxiang.ma(a)oss.qualcomm.com>
> Reviewed-by: Bryan O'Donoghue <bryan.odonoghue(a)linaro.org>
> [vz: extracted pure TPG hardware support part from the change]
> Signed-off-by: Vladimir Zapolskiy <vladimir.zapolskiy(a)linaro.org>
[Severity: Low]
This isn't a bug, but since only the TPG v2.4.0 hardware support was extracted
into this commit, should the commit message be updated to drop the description
of the CSID 1080 RX mux routing and the Kaanapali camss resources?
> diff --git a/drivers/media/platform/qcom/camss/camss-tpg-gen1.c b/drivers/media/platform/qcom/camss/camss-tpg-gen1.c
> index d29de5f93c18e..cabbb2638cf38 100644
> --- a/drivers/media/platform/qcom/camss/camss-tpg-gen1.c
> +++ b/drivers/media/platform/qcom/camss/camss-tpg-gen1.c
[ ... ]
> @@ -164,18 +167,26 @@ static int tpg_stream_on(struct tpg_device *tpg)
[ ... ]
> static int tpg_reset(struct tpg_device *tpg)
> {
> - writel(0, tpg->base + TPG_CTRL);
> - writel(1, tpg->base + TPG_CLEAR);
> + if (tpg->hw_version < TPG_HW_VER_2_4_0)
> + writel(0, tpg->base + TPG_CTRL);
> +
> + writel(TPG_CTRL_CMD_HW_RESET, tpg->base + TPG_CTRL_CMD);
>
> return 0;
> }
[Severity: Medium]
Does checking tpg->hw_version in tpg_reset() work as intended on the initial
power-on?
In drivers/media/platform/qcom/camss/camss-tpg.c, tpg_set_power() invokes
hw_ops->reset() before hw_ops->hw_version():
drivers/media/platform/qcom/camss/camss-tpg.c:tpg_set_power() {
if (on) {
...
tpg->res->hw_ops->reset(tpg);
tpg->res->hw_ops->hw_version(tpg);
}
}
Because tpg_hw_version() in drivers/media/platform/qcom/camss/camss-tpg-gen1.c
is the only place that reads TPG_HW_VERSION and populates tpg->hw_version,
tpg->hw_version is still 0 when tpg_reset() runs during the first power-on. As
a result, tpg->hw_version < TPG_HW_VER_2_4_0 evaluates to true and writes 0 to
TPG_CTRL on v2.4.0 hardware as well.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261031112203.545319-1-vladimir.zapolskiy@l…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] net: devmem: UAF when netdev_nl_bind_tx_doit() races with phys_dev unregister
--
commit ccf551ffb7f8200a4b1a6abbb90f90e5db975e35
Author: Mina Almasry <almasrymina(a)google.com>
net: devmem: detach TX bindings on NETDEV_UNREGISTER
This commit extends net_devmem_dev_unregister() and
__net_devmem_dmabuf_binding_free() to handle TX devmem bindings when a
physical or virtual network device unregisters. When binding->dev unregisters,
the binding is unmarked live, its dma_buf is unmapped, and both binding->vdev
and binding->dev are cleared; when only binding->vdev unregisters, only
binding->vdev is cleared.
> diff --git a/net/core/devmem.c b/net/core/devmem.c
> index 4610de805ef08..5c7d9640e9d14 100644
> --- a/net/core/devmem.c
> +++ b/net/core/devmem.c
> @@ -149,7 +149,7 @@ void __net_devmem_dmabuf_binding_free(struct work_struct *wq)
> {
> struct net_devmem_dmabuf_binding *binding = container_of(wq, typeof(*binding), unbind_w);
> netdevice_tracker dev_tracker;
> - struct net_device *dev = NULL;
> + struct net_device *dev;
>
> if (binding->freelist)
> WARN(binding->free_count != binding->area.num_niovs,
> @@ -157,8 +157,7 @@ void __net_devmem_dmabuf_binding_free(struct work_struct *wq)
> binding->area.num_niovs, binding->free_count);
>
> kvfree(binding->area.niovs);
> - if (binding->direction == DMA_FROM_DEVICE)
> - dev = net_devmem_lock_dev(binding, &dev_tracker);
> + dev = net_devmem_lock_dev(binding, &dev_tracker);
> net_devmem_dmabuf_binding_unmap(binding);
> xa_erase(&net_devmem_dmabuf_bindings, binding->id);
> net_devmem_unlock_dev(dev, &dev_tracker);
> @@ -252,11 +251,20 @@ void net_devmem_dev_unregister(struct net_device *dev)
>
> xa_lock(&net_devmem_dmabuf_bindings);
> xa_for_each(&net_devmem_dmabuf_bindings, xa_idx, binding) {
> - if (binding->direction != DMA_FROM_DEVICE ||
> - READ_ONCE(binding->dev) != dev ||
> - xa_get_mark(&net_devmem_dmabuf_bindings, xa_idx,
> - NET_DEVMEM_BINDING_LIVE))
> - continue;
> + if (binding->direction == DMA_FROM_DEVICE) {
> + if (READ_ONCE(binding->dev) != dev ||
> + xa_get_mark(&net_devmem_dmabuf_bindings, xa_idx,
> + NET_DEVMEM_BINDING_LIVE))
> + continue;
> + } else {
> + if (READ_ONCE(binding->vdev) == dev)
> + WRITE_ONCE(binding->vdev, NULL);
> + if (READ_ONCE(binding->dev) != dev)
> + continue;
[Severity: High]
Can binding->dev be left pointing to a freed physical net_device if
netdev_nl_bind_tx_doit() on a virtual device races with physical device
unregistration?
When a NETMEM_TX_NO_DMA virtual device (vdev, such as netkit) leases an RX
queue from a physical device (phys_dev), phys_dev is pinned via
rxq_src->lease_tracker until vdev is unregistered.
When phys_dev unregisters in unregister_netdevice_many_notify(), its
reg_state is set to NETREG_UNREGISTERING under netdev_lock(phys_dev) while
__LINK_STATE_PRESENT remains set. During call_netdevice_notifiers() for
NETDEV_UNREGISTER on phys_dev, netdev_genl_netdevice_event() runs first,
calling net_devmem_dev_unregister(phys_dev) under netdev_lock(phys_dev).
Only after that notifier completes does netkit_notifier() invoke
netkit_check_lease_unregister(phys_dev) -> unregister_netdevice_many() to
acquire netdev_lock(vdev) and set vdev->reg_state to NETREG_UNREGISTERING.
Because netdev_nl_family has parallel_ops enabled and NETDEV_CMD_BIND_TX does
not hold rtnl_lock(), a concurrent netdev_nl_bind_tx_doit() on vdev can grab
netdev_lock(vdev) via netdev_get_by_index_lock() while vdev->reg_state is
still NETREG_REGISTERED, look up phys_dev via netdev_find_netmem_tx_dev(),
and then acquire netdev_lock(bind_dev) after net_devmem_dev_unregister() has
already finished on phys_dev:
net/core/netdev-genl.c:netdev_find_netmem_tx_dev() {
...
for (i = 0; i < dev->real_num_rx_queues; i++) {
lease_rxq = READ_ONCE(__netif_get_rx_queue(dev, i)->lease);
if (!lease_rxq)
continue;
phys_dev = lease_rxq->dev;
if (netif_device_present(phys_dev) &&
phys_dev->netmem_tx == NETMEM_TX_DMA)
return phys_dev;
}
}
net/core/netdev-genl.c:netdev_nl_bind_tx_doit() {
...
if (bind_dev != netdev)
netdev_lock(bind_dev);
dma_dev = netdev_queue_get_dma_dev(bind_dev, 0, NETDEV_QUEUE_TYPE_TX);
binding = net_devmem_bind_dmabuf(bind_dev,
bind_dev != netdev ? netdev : NULL,
dma_dev, DMA_TO_DEVICE, dmabuf_fd,
PAGE_SHIFT, priv, NETLINK_CB(skb).sk,
info->extack);
}
Since netdev_nl_bind_tx_doit() does not check that bind_dev->reg_state ==
NETREG_REGISTERED after taking netdev_lock(bind_dev), it creates the binding
with binding->dev = phys_dev and binding->vdev = vdev.
When vdev then runs net_devmem_dev_unregister(vdev), this branch sees
binding->vdev == vdev and binding->dev == phys_dev != vdev, so it only clears
binding->vdev to NULL and continues without unmapping the dma_buf or clearing
binding->dev. Because net_devmem_dev_unregister(phys_dev) already ran,
binding->dev is never cleared. Once netkit_uninit() drops the lease reference
and netdev_run_todo() frees phys_dev, won't netdev_nl_sock_priv_destroy() or
__net_devmem_dmabuf_binding_free() dereference the freed phys_dev in
net_devmem_lock_dev()?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261030083539.777877-1-almasrymina@google.c…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] net: devmem: leased RX bindings never unmapped before dma_dev is freed
--
commit b250f1e4ffd826ff4e6f5c24e078e06c36365baf
Author: Mina Almasry <almasrymina(a)google.com>
net: devmem: unmap dma_buf synchronously on queue uninstall
This patch adds synchronous dma_buf unmapping when RX queues are closed,
uninstalled, or when the bound net_device is unregistered, to avoid a
slab use-after-free in dma_unmap_sg_attrs() if dma_dev is freed while the
devmem binding is still alive.
> diff --git a/net/core/devmem.c b/net/core/devmem.c
> index 32fafbfa1359e..4610de805ef08 100644
> --- a/net/core/devmem.c
> +++ b/net/core/devmem.c
[ ... ]
> @@ -143,12 +228,45 @@ void net_devmem_unbind_dmabuf(struct net_devmem_dmabuf_binding *binding)
>
> rxq_idx = get_netdev_rx_queue_index(rxq);
>
> - netif_mp_close_rxq(binding->dev, rxq_idx, &mp_params);
> + if (netif_mp_close_rxq(binding->dev, rxq_idx, &mp_params))
> + busy = true;
> + }
> +
> + if (binding->direction == DMA_FROM_DEVICE && binding->dev && !busy) {
> + net_devmem_dmabuf_binding_unmap(binding);
> + WRITE_ONCE(binding->dev, NULL);
> }
>
> percpu_ref_kill(&binding->ref);
> }
>
> +void net_devmem_dev_unregister(struct net_device *dev)
> +{
> + struct net_devmem_dmabuf_binding *binding;
> + unsigned long xa_idx;
> +
> + netdev_assert_locked(dev);
> +
> + if (dev->reg_state != NETREG_UNREGISTERING)
> + return;
> +
> + xa_lock(&net_devmem_dmabuf_bindings);
> + xa_for_each(&net_devmem_dmabuf_bindings, xa_idx, binding) {
> + if (binding->direction != DMA_FROM_DEVICE ||
> + READ_ONCE(binding->dev) != dev ||
> + xa_get_mark(&net_devmem_dmabuf_bindings, xa_idx,
> + NET_DEVMEM_BINDING_LIVE))
> + continue;
> +
> + xa_unlock(&net_devmem_dmabuf_bindings);
> + if (!netif_is_queue_leasee(dev))
> + net_devmem_dmabuf_binding_unmap(binding);
> + xa_lock(&net_devmem_dmabuf_bindings);
> + WRITE_ONCE(binding->dev, NULL);
> + }
> + xa_unlock(&net_devmem_dmabuf_bindings);
> +}
[ ... ]
> @@ -551,16 +669,17 @@ static void mp_dmabuf_devmem_uninstall(void *mp_priv,
> unsigned long xa_idx;
>
> xa_for_each(&binding->bound_rxqs, xa_idx, bound_rxq) {
> - if (bound_rxq == rxq) {
> - xa_erase(&binding->bound_rxqs, xa_idx);
> - if (xa_empty(&binding->bound_rxqs)) {
> - mutex_lock(&binding->lock);
> - ASSERT_EXCLUSIVE_WRITER(binding->dev);
> - WRITE_ONCE(binding->dev, NULL);
> - mutex_unlock(&binding->lock);
> - }
> - break;
> + if (bound_rxq != rxq)
> + continue;
> +
> + xa_erase(&binding->bound_rxqs, xa_idx);
> + if (xa_empty(&binding->bound_rxqs)) {
> + if (!rxq->lease)
> + net_devmem_dmabuf_binding_unmap(binding);
> + ASSERT_EXCLUSIVE_WRITER(binding->dev);
> + WRITE_ONCE(binding->dev, NULL);
> }
> + break;
> }
> }
[Severity: High]
As reported by Sashiko on v2 [1], for an RX binding on a leased queue
(for example via netkit), can the binding still be left mapped after the
backing physical device is unregistered and its dma_dev is freed?
When netdev_nl_bind_rx_doit() binds to a virtual queue-leasee device, the
dmabuf is attached to the physical device's dma_dev, but binding->dev is
set to the virtual device and net_devmem_bind_dmabuf_to_queue() records
the virtual rxq in binding->bound_rxqs while netif_mp_open_rxq() installs
mp_params on the physical rxq.
When the virtual device unregisters (either directly or via
netkit_check_lease_unregister() when the physical device unregisters),
ndo_uninit runs:
netkit_uninit()
netkit_queue_unlease()
netdev_rx_queue_unlease(virt_rxq, phys_rxq)
netif_rxq_cleanup_unlease(phys_rxq, virt_rxq)
__netif_mp_close_rxq(phys_rxq->dev, rxq_idx, &p)
__netif_mp_uninstall_rxq(virt_rxq, &p)
mp_dmabuf_devmem_uninstall(binding, virt_rxq)
Because netdev_rx_queue_unlease() only clears rxq_src->lease and
rxq_dst->lease after netif_rxq_cleanup_unlease() returns:
net/core/netdev_rx_queue.c:netdev_rx_queue_unlease() {
...
netif_rxq_cleanup_unlease(rxq_src, rxq_dst);
WRITE_ONCE(rxq_src->lease, NULL);
WRITE_ONCE(rxq_dst->lease, NULL);
...
}
rxq->lease is still non-NULL in mp_dmabuf_devmem_uninstall(), so it skips
net_devmem_dmabuf_binding_unmap(binding) while emptying
binding->bound_rxqs and setting binding->dev to NULL. If the netlink
socket is closed afterwards, net_devmem_unbind_dmabuf() sees
binding->dev == NULL and also skips unmapping.
Similarly, if the netlink socket was closed earlier and
netif_mp_close_rxq() failed (busy == true), net_devmem_dev_unregister()
on the virtual device skips net_devmem_dmabuf_binding_unmap(binding)
because netif_is_queue_leasee(dev) is true, and then clears binding->dev
to NULL.
In both cases, when the physical device finishes unregistering,
dev_memory_provider_uninstall() and net_devmem_dev_unregister() on the
physical device cannot find or unmap the binding because binding->bound_rxqs
is empty and binding->dev was the virtual device (and is now NULL). Once
the physical device frees its dma_dev, won't
__net_devmem_dmabuf_binding_free() later hit the same slab use-after-free
in dma_unmap_sg_attrs()?
[1] https://lore.kernel.org/r/179191526299.434549.7888465945343049764@kernel.org
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261030083539.777877-1-almasrymina@google.c…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] media: camss: camss_video_subdevs_set_stream() stops shared active subdevs
- [Medium] media: camss: camss_video_subdevs_set_stream() aborts early on disable error
--
commit b6aa7c4db5a3a102e15f37d3d030b15cc33ee303
Author: Qianhui Xu <xiaqinghuan(a)mail.ustc.edu.cn>
media: camss: video: factor pipeline stream on/off into a helper
This commit factors the media pipeline subdevice stream start and stop loops
into a shared camss_video_subdevs_set_stream() helper using
media_entity_remote_source_pad_unique() and
v4l2_subdev_enable_streams()/v4l2_subdev_disable_streams(), and unwinds
partially started pipelines on error.
> diff --git a/drivers/media/platform/qcom/camss/camss-video.c b/drivers/media/platform/qcom/camss/camss-video.c
> index 0852eb6f13154..9a633ca06618b 100644
> --- a/drivers/media/platform/qcom/camss/camss-video.c
> +++ b/drivers/media/platform/qcom/camss/camss-video.c
> @@ -249,13 +249,66 @@ static int video_prepare_streaming(struct vb2_queue *q)
> return ret;
> }
>
> +/*
> + * camss_video_subdevs_set_stream() - Enable or disable a video pipeline
> + * @video: The video node whose pipeline is toggled
> + * @enable: true to start the subdevices, false to stop them
> + *
> + * Walk the media pipeline upstream from the video node and toggle the stream on
> + * each subdevice. Each subdevice source pad is located with
> + * media_entity_remote_source_pad_unique() instead of assuming pads[0], and the
> + * stream is toggled with v4l2_subdev_enable_streams()/
> + * v4l2_subdev_disable_streams() rather than the legacy .s_stream() call. The
> + * camss subdevices only implement .s_stream(), so the helpers fall back to it.
> + *
> + * A stream that is already in the requested state reports -EALREADY. On the
> + * teardown path this simply skips subdevices that were never started, so a
> + * partially started pipeline is unwound by disabling it again: the subdevice
> + * that failed to start was not marked enabled and is left untouched. A
> + * subdevice without a streaming operation reports -ENOIOCTLCMD, which is
> + * ignored here to match the previous .s_stream() call sites.
> + *
> + * Return: 0 on success, or the error code of the first subdevice that failed.
> + */
> +static int camss_video_subdevs_set_stream(struct camss_video *video,
> + bool enable)
> +{
> + struct media_entity *entity = &video->vdev.entity;
> +
> + while (1) {
> + struct media_pad *pad;
> + struct v4l2_subdev *subdev;
> + int ret;
> +
> + pad = media_entity_remote_source_pad_unique(entity);
> + if (IS_ERR(pad))
> + break;
> +
> + if (!is_media_entity_v4l2_subdev(pad->entity))
> + break;
> +
> + subdev = media_entity_to_v4l2_subdev(pad->entity);
> +
> + if (enable)
> + ret = v4l2_subdev_enable_streams(subdev, pad->index,
> + BIT_ULL(0));
> + else
> + ret = v4l2_subdev_disable_streams(subdev, pad->index,
> + BIT_ULL(0));
> +
> + if (ret && ret != -EALREADY && ret != -ENOIOCTLCMD)
> + return ret;
[Severity: Medium]
When enable is false, does returning immediately here still abort the
pipeline stop on the first failing subdevice?
The commit message notes that video_stop_streaming() previously returned
early on the first subdevice that failed to stop, leaving the remaining
upstream subdevices running while the pipeline was torn down. However,
camss_video_subdevs_set_stream() still returns immediately on any error
other than -EALREADY or -ENOIOCTLCMD when disabling streams.
If an upstream subdevice implementing .disable_streams (such as a camera
sensor or CSI-2 bridge) returns an error during video_stop_streaming() or
during the error unwind in video_start_streaming(), the loop aborts before
disabling the remaining subdevices further upstream. video_stop_streaming()
then calls video_device_pipeline_stop() and video_unprepare_streaming()
drops the pipeline PM references while those upstream subdevices remain
streaming and stay marked enabled in V4L2 subdevice state (causing later
v4l2_subdev_enable_streams() calls to return -EALREADY without starting
hardware).
> +
> + entity = pad->entity;
> + }
> +
> + return 0;
> +}
> +
> static int video_start_streaming(struct vb2_queue *q, unsigned int count)
> {
> struct camss_video *video = vb2_get_drv_priv(q);
> struct video_device *vdev = &video->vdev;
> - struct media_entity *entity;
> - struct media_pad *pad;
> - struct v4l2_subdev *subdev;
> int ret;
>
> ret = video_device_pipeline_alloc_start(vdev);
> @@ -268,22 +321,11 @@ static int video_start_streaming(struct vb2_queue *q, unsigned int count)
> if (ret < 0)
> goto error;
>
> - entity = &vdev->entity;
> - while (1) {
> - pad = &entity->pads[0];
> - if (!(pad->flags & MEDIA_PAD_FL_SINK))
> - break;
> -
> - pad = media_pad_remote_pad_first(pad);
> - if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
> - break;
> -
> - entity = pad->entity;
> - subdev = media_entity_to_v4l2_subdev(entity);
> -
> - ret = v4l2_subdev_call(subdev, video, s_stream, 1);
> - if (ret < 0 && ret != -ENOIOCTLCMD)
> - goto error;
> + ret = camss_video_subdevs_set_stream(video, true);
> + if (ret < 0) {
> + camss_video_subdevs_set_stream(video, false);
[Severity: High]
Can calling camss_video_subdevs_set_stream(video, false) here and in
video_stop_streaming() disable shared upstream subdevices while another
video node is actively streaming?
On CAMSS platforms where a CSID exposes multiple source pads linked to
separate VFE RDI lines and camss_video nodes while sharing a single
upstream CSID sink pad, CSIPHY source pad, and sensor source pad:
1. Suppose video0 (linked to CSID source pad 1) is actively streaming, so
vfe_rdi0 pad 1, CSID pad 1, CSIPHY pad 1, and sensor pad 0 are marked
enabled.
2. If video1 (linked to CSID source pad 2) calls video_start_streaming()
and fails on its VFE line or CSID pad, unwinding with
camss_video_subdevs_set_stream(video1, false) gets -EALREADY from
v4l2_subdev_disable_streams() on the unstarted VFE and CSID pads.
Because camss_video_subdevs_set_stream() ignores -EALREADY and continues
walking upstream past CSID, it calls v4l2_subdev_disable_streams() on
the shared CSIPHY pad 1 and sensor pad 0, stopping them while video0 is
still streaming.
3. Similarly, if video1 starts successfully (ignoring -EALREADY on the
already-enabled CSIPHY pad 1 and sensor pad 0 because
v4l2_subdev_enable_streams() tracks a boolean bitmask per pad/stream
rather than a reference count) and later stops in
video_stop_streaming(), v4l2_subdev_disable_streams() on CSID pad 2
clears BIT_ULL(2) in sd->enabled_pads while BIT_ULL(1) remains enabled
for video0, and returns 0. Because camss_video_subdevs_set_stream()
keeps walking upstream unconditionally, it disables CSIPHY pad 1 and
sensor pad 0 while video0 is still active.
Additionally, because the CAMSS subdevices only implement .s_stream,
v4l2_subdev_enable_streams() and v4l2_subdev_disable_streams() run with
state == NULL and update sd->enabled_pads without holding a subdevice state
lock, while each camss_video node only holds its own per-node lock. Can
concurrent start/stop calls across two video nodes race on the non-atomic
read-modify-write updates to sd->enabled_pads?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261031041154.308030-1-xiaqinghuan@mail.ust…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] media: ipu3-cio2: IRQ race clearing chan_queue after synchronize_irq()
- [Medium] media: ipu3-cio2: cio2_hw_init() enables channel IRQs before clearing status
- [Medium] media: ipu3-cio2: partial stream teardown on error in cio2_suspend()
- [Medium] media: ipu3-cio2: broken streaming state on error in cio2_resume()
--
commit 30f51c34b5237d965bd4b6806341788ed4c4ee28
Author: Joe Dzado <joe.dzado(a)icloud.com>
media: ipu3-cio2: serialise concurrent stream start and stop
This commit introduces a hardware mutex and per-channel register guards so
that cio2_hw_init() and cio2_hw_exit() can run while other queues are
streaming concurrently.
> diff --git a/drivers/media/pci/intel/ipu3/ipu3-cio2.c b/drivers/media/pci/intel/ipu3/ipu3-cio2.c
> index 06ce58e23397e..071c5ea4af042 100644
> --- a/drivers/media/pci/intel/ipu3/ipu3-cio2.c
> +++ b/drivers/media/pci/intel/ipu3/ipu3-cio2.c
> @@ -371,6 +371,8 @@ static int cio2_hw_init(struct cio2_device *cio2, struct cio2_queue *q)
> u8 lanes, csi2bus = q->csi2.port;
> u8 sensor_vc = SENSOR_VIR_CH_DFLT;
> const unsigned int chan = q->dma_chan;
> + /* another queue already streaming: leave shared blocks alone */
> + const bool first = !cio2->streaming;
> struct cio2_csi2_timing timing = { 0 };
> int i, r;
[Severity: Medium]
How does this check interact with the error paths in cio2_resume()?
In cio2_resume(), cio2->streaming is zeroed before the loop so that the first
resumed queue triggers the shared hardware block setup in cio2_hw_init(), and
then restored at the end:
drivers/media/pci/intel/ipu3/ipu3-cio2.c:cio2_resume() {
...
cio2->streaming = 0;
for (i = 0; i < CIO2_QUEUES; i++) {
q = &cio2->queue[i];
if (!q->streaming)
continue;
r = cio2_hw_init(cio2, q);
cio2->streaming++;
if (r) {
dev_err(dev, "fail to init cio2 hw\n");
break;
}
r = v4l2_subdev_call(q->sensor, video, s_stream, 1);
if (r) {
dev_err(dev, "fail to start sensor streaming\n");
cio2_hw_exit(cio2, q);
break;
}
}
cio2->streaming = streaming;
...
}
If cio2_hw_init() fails early (for example in cio2_csi2_calc_timing() before
writing the shared PBM, HPLL/ISCLK, CGC, and LTR registers) or if sensor
s_stream(1) fails and calls cio2_hw_exit(), the loop breaks without
reinitializing any remaining active queues while leaving q->streaming set to
true, and then unconditionally restores cio2->streaming to the pre-suspend
count.
When a queue is later started via cio2_vb2_start_streaming()->cio2_hw_init(),
won't cio2->streaming still be non-zero, causing first to evaluate to false
and skipping the shared hardware initialization on a device that just came
out of D3?
[ ... ]
> @@ -445,8 +451,8 @@ static int cio2_hw_init(struct cio2_device *cio2, struct cio2_queue *q)
> writel(0, q->csi_rx_base + CIO2_REG_IRQCTRL_LEVEL_NOT_PULSE);
> writel(CIO2_INT_EN_EXT_OE_MASK, base + CIO2_REG_INT_EN_EXT_OE);
>
> - writel(CIO2_REG_INT_EN_IRQ | CIO2_INT_IOC(chan) |
> - CIO2_REG_INT_EN_IOS(chan),
> + writel((first ? 0 : readl(base + CIO2_REG_INT_EN)) |
> + CIO2_REG_INT_EN_IRQ | CIO2_INT_IOC(chan) | CIO2_REG_INT_EN_IOS(chan),
> base + CIO2_REG_INT_EN);
[ ... ]
> @@ -516,9 +529,15 @@ static int cio2_hw_init(struct cio2_device *cio2, struct cio2_queue *q)
>
> /* Clear interrupts */
> writel(CIO2_IRQCTRL_MASK, q->csi_rx_base + CIO2_REG_IRQCTRL_CLEAR);
> - writel(~0, base + CIO2_REG_INT_STS_EXT_OE);
> - writel(~0, base + CIO2_REG_INT_STS_EXT_IE);
> - writel(~0, base + CIO2_REG_INT_STS);
> + if (first) {
> + writel(~0, base + CIO2_REG_INT_STS_EXT_OE);
> + writel(~0, base + CIO2_REG_INT_STS_EXT_IE);
> + writel(~0, base + CIO2_REG_INT_STS);
> + } else { /* don't eat the other streams' pending interrupts */
> + writel(0xffU << (8U * csi2bus), base + CIO2_REG_INT_STS_EXT_IE);
> + writel(CIO2_INT_IOC(chan) | CIO2_INT_IOS_IOLN(chan),
> + base + CIO2_REG_INT_STS);
> + }
[Severity: Medium]
In cio2_hw_init(), CIO2_INT_IOC(chan) and CIO2_REG_INT_EN_IOS(chan) are
unmasked in CIO2_REG_INT_EN before configuring the DMA channel registers
(CDMABA, CDMAC0, CDMAC1) and before clearing CIO2_REG_INT_STS.
When a stream previously stopped in cio2_hw_exit(), it masked CIO2_REG_INT_EN
after polling for DMA halt without clearing any status bits in
CIO2_REG_INT_STS that may have latched during the halt. Since
cio2_vb2_start_streaming() already sets cio2->chan_queue[q->dma_chan] = q
before calling cio2_hw_init(), couldn't unmasking CIO2_REG_INT_EN before
clearing CIO2_REG_INT_STS (or while another active stream triggers cio2_irq()
during cio2_hw_init()) immediately dispatch a spurious cio2_buffer_done() or
cio2_queue_event_sof() for q before the channel is initialized?
>
> /* Enable devices, starting from the last device in the pipe */
> writel(1, q->csi_rx_base + CIO2_REG_MIPIBE_ENABLE);
> @@ -531,8 +550,7 @@ static void cio2_hw_exit(struct cio2_device *cio2, struct cio2_queue *q)
> {
> struct device *dev = &cio2->pci_dev->dev;
> void __iomem *const base = cio2->base;
> - unsigned int i;
> - const unsigned int chan = q->dma_chan;
> + const unsigned int chan = q->dma_chan, port = q->csi2.port;
> u32 value;
> int ret;
>
> @@ -550,12 +568,14 @@ static void cio2_hw_exit(struct cio2_device *cio2, struct cio2_queue *q)
> if (ret)
> dev_err(dev, "DMA %u can not be halted\n", chan);
>
> - for (i = 0; i < CIO2_NUM_PORTS; i++) {
> - writel(readl(base + CIO2_REG_PXM_FRF_CFG(i)) |
> - CIO2_PXM_FRF_CFG_ABORT, base + CIO2_REG_PXM_FRF_CFG(i));
> - writel(readl(base + CIO2_REG_PBM_FOPN_ABORT) |
> - CIO2_PBM_FOPN_ABORT(i), base + CIO2_REG_PBM_FOPN_ABORT);
> - }
> + /* only this queue's port: others may still be streaming */
> + writel(readl(base + CIO2_REG_PXM_FRF_CFG(port)) |
> + CIO2_PXM_FRF_CFG_ABORT, base + CIO2_REG_PXM_FRF_CFG(port));
> + writel(readl(base + CIO2_REG_PBM_FOPN_ABORT) |
> + CIO2_PBM_FOPN_ABORT(port), base + CIO2_REG_PBM_FOPN_ABORT);
> + writel(readl(base + CIO2_REG_INT_EN) &
> + ~(CIO2_INT_IOC(chan) | CIO2_REG_INT_EN_IOS(chan)),
> + base + CIO2_REG_INT_EN);
> }
[Severity: Medium]
When multiple queues are streaming concurrently, what happens if stopping one
of the sensors fails in cio2_suspend()?
drivers/media/pci/intel/ipu3/ipu3-cio2.c:cio2_suspend() {
...
/* Stop streams */
for (i = 0; i < CIO2_QUEUES; i++) {
q = &cio2->queue[i];
if (!q->streaming)
continue;
r = v4l2_subdev_call(q->sensor, video, s_stream, 0);
if (r) {
dev_err(dev, "failed to stop sensor streaming\n");
return r;
}
cio2_hw_exit(cio2, q);
}
...
}
If queue 0 stops its sensor and runs cio2_hw_exit(cio2, q), and then a
subsequent streaming queue fails in s_stream(0), cio2_suspend() returns early
to abort the system suspend without restarting queue 0 or rearranging its
FBPT entries. Doesn't that leave earlier queues halted with their sensors off
while q->streaming remains true?
>
> static void cio2_buffer_done(struct cio2_device *cio2, unsigned int dma_chan)
> @@ -1017,14 +1037,17 @@ static int cio2_vb2_start_streaming(struct vb2_queue *vq, unsigned int count)
> if (r)
> goto fail_pipeline;
>
> + mutex_lock(&cio2->hw_lock);
> cio2->chan_queue[q->dma_chan] = q;
> r = cio2_hw_init(cio2, q);
> if (r) {
> cio2->chan_queue[q->dma_chan] = NULL;
> + mutex_unlock(&cio2->hw_lock);
> goto fail_hw;
> }
> cio2->streaming++;
> q->streaming = true;
> + mutex_unlock(&cio2->hw_lock);
>
> /* Start streaming on sensor */
> r = v4l2_subdev_call(q->sensor, video, s_stream, 1);
> @@ -1034,11 +1057,13 @@ static int cio2_vb2_start_streaming(struct vb2_queue *vq, unsigned int count)
> return 0;
>
> fail_csi2_subdev:
> + mutex_lock(&cio2->hw_lock);
> cio2_hw_exit(cio2, q);
> synchronize_irq(cio2->pci_dev->irq);
> cio2->chan_queue[q->dma_chan] = NULL;
> cio2->streaming--;
> q->streaming = false;
> + mutex_unlock(&cio2->hw_lock);
> fail_hw:
> video_device_pipeline_stop(&q->vdev);
> fail_pipeline:
> @@ -1058,11 +1083,13 @@ static void cio2_vb2_stop_streaming(struct vb2_queue *vq)
> if (v4l2_subdev_call(q->sensor, video, s_stream, 0))
> dev_err(dev, "failed to stop sensor streaming\n");
>
> + mutex_lock(&cio2->hw_lock);
> cio2_hw_exit(cio2, q);
> synchronize_irq(cio2->pci_dev->irq);
> cio2->chan_queue[q->dma_chan] = NULL;
> cio2->streaming--;
> q->streaming = false;
> + mutex_unlock(&cio2->hw_lock);
> cio2_vb2_return_all_buffers(q, VB2_BUF_STATE_ERROR);
> video_device_pipeline_stop(&q->vdev);
> pm_runtime_put(dev);
[Severity: High]
In both cio2_vb2_stop_streaming() and the fail_csi2_subdev path of
cio2_vb2_start_streaming(), synchronize_irq(cio2->pci_dev->irq) is called
before setting cio2->chan_queue[q->dma_chan] = NULL.
In cio2_hw_exit(), CIO2_REG_INT_EN is updated via a posted writel() without a
flushing readl(), and any status bits for chan that latched in
CIO2_REG_INT_STS while polling for CIO2_CDMAC0_DMA_HALTED are left set. Meanwhile,
cio2_irq()->cio2_irq_handle_once() runs without cio2->hw_lock and inspects
CIO2_REG_INT_STS without masking against CIO2_REG_INT_EN:
drivers/media/pci/intel/ipu3/ipu3-cio2.c:cio2_irq_handle_once() {
...
if (int_status & CIO2_INT_IOC_MASK) {
...
for (d = 0; d < CIO2_NUM_DMA_CHAN; d++)
if (int_status & CIO2_INT_IOC(d)) {
clr |= CIO2_INT_IOC(d);
if (cio2->chan_queue[d])
cio2_buffer_done(cio2, d);
}
int_status &= ~clr;
}
if (int_status & CIO2_INT_IOS_IOLN_MASK) {
...
for (d = 0; d < CIO2_NUM_DMA_CHAN; d++)
if (int_status & CIO2_INT_IOS_IOLN(d)) {
clr |= CIO2_INT_IOS_IOLN(d);
if (cio2->chan_queue[d])
cio2_queue_event_sof(cio2,
cio2->chan_queue[d]);
}
int_status &= ~clr;
}
...
}
If an interrupt fires right after synchronize_irq() returns (for instance from
another active streaming queue, a port error interrupt, an un-flushed write
to CIO2_REG_INT_EN, or a shared IRQ line), can cio2_irq_handle_once() race
with clearing cio2->chan_queue[q->dma_chan]?
Specifically:
1. In the CIO2_INT_IOS_IOLN loop, cio2->chan_queue[d] is read for the NULL
check and then reloaded to pass to cio2_queue_event_sof(). If
cio2->chan_queue[q->dma_chan] = NULL happens between those two reads, won't
cio2_queue_event_sof() dereference a NULL queue pointer in hardirq context?
2. If cio2_irq_handle_once() enters cio2_buffer_done() after synchronize_irq()
returns, won't cio2_vb2_return_all_buffers() run concurrently with
cio2_buffer_done() on q->bufs[] without synchronization, risking a NULL
dereference or double completion via vb2_buffer_done()?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261031002332.33609-1-joe.dzado@icloud.com?…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] media: ipu3-cio2: race when clearing chan_queue[] in start/stop streaming
- [Medium] media: ipu3-cio2: inconsistent stream state on suspend/resume error
--
commit fd956418ed7138522d4130df6210d590fc6b0990
Author: Joe Dzado <joe.dzado(a)icloud.com>
media: ipu3-cio2: track the streaming queue by DMA channel
This commit replaces the single cur_queue pointer with a per-DMA-channel
chan_queue[] array and tracks the number of active streaming queues in
cio2->streaming. It also updates the interrupt handler, stream start/stop,
and system suspend/resume paths to iterate over or look up queues by
channel.
> diff --git a/drivers/media/pci/intel/ipu3/ipu3-cio2.c b/drivers/media/pci/intel/ipu3/ipu3-cio2.c
> index 4d2b231291f56..06ce58e23397e 100644
> --- a/drivers/media/pci/intel/ipu3/ipu3-cio2.c
> +++ b/drivers/media/pci/intel/ipu3/ipu3-cio2.c
[ ... ]
> @@ -711,7 +719,8 @@ static void cio2_irq_handle_once(struct cio2_device *cio2, u32 int_status)
> for (d = 0; d < CIO2_NUM_DMA_CHAN; d++)
> if (int_status & CIO2_INT_IOC(d)) {
> clr |= CIO2_INT_IOC(d);
> - cio2_buffer_done(cio2, d);
> + if (cio2->chan_queue[d])
> + cio2_buffer_done(cio2, d);
> }
> int_status &= ~clr;
> }
> @@ -724,9 +733,9 @@ static void cio2_irq_handle_once(struct cio2_device *cio2, u32 int_status)
> for (d = 0; d < CIO2_NUM_DMA_CHAN; d++)
> if (int_status & CIO2_INT_IOS_IOLN(d)) {
> clr |= CIO2_INT_IOS_IOLN(d);
> - if (d == CIO2_DMA_CHAN)
> + if (cio2->chan_queue[d])
> cio2_queue_event_sof(cio2,
> - cio2->cur_queue);
> + cio2->chan_queue[d]);
> }
> int_status &= ~clr;
> }
[ ... ]
> @@ -1009,21 +1017,28 @@ static int cio2_vb2_start_streaming(struct vb2_queue *vq, unsigned int count)
> if (r)
> goto fail_pipeline;
>
> + cio2->chan_queue[q->dma_chan] = q;
> r = cio2_hw_init(cio2, q);
> - if (r)
> + if (r) {
> + cio2->chan_queue[q->dma_chan] = NULL;
> goto fail_hw;
> + }
> + cio2->streaming++;
> + q->streaming = true;
>
> /* Start streaming on sensor */
> r = v4l2_subdev_call(q->sensor, video, s_stream, 1);
> if (r)
> goto fail_csi2_subdev;
>
> - cio2->streaming = true;
> -
> return 0;
>
> fail_csi2_subdev:
> cio2_hw_exit(cio2, q);
> + synchronize_irq(cio2->pci_dev->irq);
> + cio2->chan_queue[q->dma_chan] = NULL;
> + cio2->streaming--;
> + q->streaming = false;
> fail_hw:
> video_device_pipeline_stop(&q->vdev);
> fail_pipeline:
> @@ -1045,10 +1060,12 @@ static void cio2_vb2_stop_streaming(struct vb2_queue *vq)
>
> cio2_hw_exit(cio2, q);
> synchronize_irq(cio2->pci_dev->irq);
> + cio2->chan_queue[q->dma_chan] = NULL;
> + cio2->streaming--;
> + q->streaming = false;
> cio2_vb2_return_all_buffers(q, VB2_BUF_STATE_ERROR);
> video_device_pipeline_stop(&q->vdev);
> pm_runtime_put(dev);
> - cio2->streaming = false;
> }
[Severity: High]
Can clearing cio2->chan_queue[q->dma_chan] after synchronize_irq() race with
cio2_irq_handle_once() and cause a NULL pointer dereference or concurrent
access to q->bufs[]?
In cio2_irq_handle_once(), cio2->chan_queue[d] is checked for non-NULL and
then re-read without a lock or READ_ONCE() when passed to
cio2_queue_event_sof() and inside cio2_buffer_done():
if (cio2->chan_queue[d])
cio2_queue_event_sof(cio2,
cio2->chan_queue[d]);
In cio2_vb2_stop_streaming() and the fail_csi2_subdev path of
cio2_vb2_start_streaming(), cio2->chan_queue[q->dma_chan] = NULL is executed
after synchronize_irq(cio2->pci_dev->irq) rather than before it.
Even with the later commits in this series ("media: ipu3-cio2: serialise
concurrent stream start and stop" and "media: ipu3-cio2: give each CSI-2 port
its own DMA channel"), cio2_irq_handle_once() can still observe
CIO2_INT_IOC(d) or CIO2_INT_IOS_IOLN(d) while chan_queue[q->dma_chan] is
being cleared:
1. cio2_hw_exit() masks CIO2_REG_INT_EN via a posted writel() without a
flushing readl() before synchronize_irq(), and does not clear
CIO2_INT_IOC(chan) or CIO2_INT_IOS_IOLN(chan) in CIO2_REG_INT_STS. When
another active queue or error interrupt fires (or if the DMA halt poll in
cio2_hw_exit() times out), cio2_irq() reads CIO2_REG_INT_STS without masking
against CIO2_REG_INT_EN.
2. When queues share CIO2_DMA_CHAN (in this commit, or when dma_per_port=0
later in the series), starting a second queue in cio2_vb2_start_streaming()
overwrites cio2->chan_queue[0] and, if cio2_hw_init() fails, clears
cio2->chan_queue[0] = NULL without synchronize_irq() while the first queue is
actively streaming.
If cio2->chan_queue[d] becomes NULL between the if (cio2->chan_queue[d])
check and the second load, NULL is passed to cio2_queue_event_sof(), which
dereferences &q->frame_sequence and q->subdev.devnode. Similarly, if
cio2_buffer_done() loads a non-NULL q after synchronize_irq() returns, it can
access q->bufs[] concurrently with cio2_vb2_return_all_buffers().
> @@ -1895,21 +1913,26 @@ static int __maybe_unused cio2_suspend(struct device *dev)
> {
> struct pci_dev *pci_dev = to_pci_dev(dev);
> struct cio2_device *cio2 = pci_get_drvdata(pci_dev);
> - struct cio2_queue *q = cio2->cur_queue;
> + struct cio2_queue *q;
> + unsigned int i;
> int r;
>
> dev_dbg(dev, "cio2 suspend\n");
> if (!cio2->streaming)
> return 0;
>
> - /* Stop stream */
> - r = v4l2_subdev_call(q->sensor, video, s_stream, 0);
> - if (r) {
> - dev_err(dev, "failed to stop sensor streaming\n");
> - return r;
> + /* Stop streams */
> + for (i = 0; i < CIO2_QUEUES; i++) {
> + q = &cio2->queue[i];
> + if (!q->streaming)
> + continue;
> + r = v4l2_subdev_call(q->sensor, video, s_stream, 0);
> + if (r) {
> + dev_err(dev, "failed to stop sensor streaming\n");
> + return r;
> + }
> + cio2_hw_exit(cio2, q);
> }
> -
> - cio2_hw_exit(cio2, q);
> synchronize_irq(pci_dev->irq);
>
> pm_runtime_force_suspend(dev);
> @@ -1918,9 +1941,14 @@ static int __maybe_unused cio2_suspend(struct device *dev)
> * Upon resume, hw starts to process the fbpt entries from beginning,
> * so relocate the queued buffs to the fbpt head before suspend.
> */
> - cio2_fbpt_rearrange(cio2, q);
> - q->bufs_first = 0;
> - q->bufs_next = 0;
> + for (i = 0; i < CIO2_QUEUES; i++) {
> + q = &cio2->queue[i];
> + if (!q->streaming)
> + continue;
> + cio2_fbpt_rearrange(cio2, q);
> + q->bufs_first = 0;
> + q->bufs_next = 0;
> + }
>
> return 0;
> }
> @@ -1928,30 +1956,41 @@ static int __maybe_unused cio2_suspend(struct device *dev)
> static int __maybe_unused cio2_resume(struct device *dev)
> {
> struct cio2_device *cio2 = dev_get_drvdata(dev);
> - struct cio2_queue *q = cio2->cur_queue;
> + unsigned int i, streaming = cio2->streaming;
> + struct cio2_queue *q;
> int r;
>
> dev_dbg(dev, "cio2 resume\n");
> - if (!cio2->streaming)
> + if (!streaming)
> return 0;
> - /* Start stream */
> + /* Start streams */
> r = pm_runtime_force_resume(dev);
> if (r < 0) {
> dev_err(dev, "failed to set power %d\n", r);
> return r;
> }
>
> - r = cio2_hw_init(cio2, q);
> - if (r) {
> - dev_err(dev, "fail to init cio2 hw\n");
> - return r;
> - }
> + /* hw_init does the shared setup only for the first queue it brings up */
> + cio2->streaming = 0;
> + for (i = 0; i < CIO2_QUEUES; i++) {
> + q = &cio2->queue[i];
> + if (!q->streaming)
> + continue;
> + r = cio2_hw_init(cio2, q);
> + cio2->streaming++;
> + if (r) {
> + dev_err(dev, "fail to init cio2 hw\n");
> + break;
> + }
>
> - r = v4l2_subdev_call(q->sensor, video, s_stream, 1);
> - if (r) {
> - dev_err(dev, "fail to start sensor streaming\n");
> - cio2_hw_exit(cio2, q);
> + r = v4l2_subdev_call(q->sensor, video, s_stream, 1);
> + if (r) {
> + dev_err(dev, "fail to start sensor streaming\n");
> + cio2_hw_exit(cio2, q);
> + break;
> + }
> }
> + cio2->streaming = streaming;
>
> return r;
> }
[Severity: Medium]
What happens to the streaming state of the queues if stopping or starting one
of multiple active streams fails during cio2_suspend() or cio2_resume()?
In cio2_suspend(), if an earlier queue in the loop stops its sensor and calls
cio2_hw_exit(cio2, q), and a subsequent queue fails
v4l2_subdev_call(q->sensor, video, s_stream, 0), cio2_suspend() returns early
and aborts suspend. Because cio2_resume() will not run, the earlier queues
are left halted in hardware without synchronize_irq(), FBPT rearrangement, or
stream restart, while their q->streaming flags and cio2->streaming remain set.
Similarly, in cio2_resume(), cio2->streaming is incremented before checking
whether cio2_hw_init(cio2, q) failed, and if cio2_hw_init() or
v4l2_subdev_call(q->sensor, video, s_stream, 1) fails on one queue, the loop
breaks and restores cio2->streaming = streaming, leaving the failed and
remaining queues halted while q->streaming and cio2->chan_queue[q->dma_chan]
remain set.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261031002332.33609-1-joe.dzado@icloud.com?…