Skip to content

drivers/rpmsg: Coverity cleanups in rpmsg.c and rpmsg_virtio.c - #22058

Open
CV-Bowen wants to merge 10 commits into
apache:masterfrom
CV-Bowen:up/P113-rpmsg-coverity
Open

CV-Bowen wants to merge 10 commits into
apache:masterfrom
CV-Bowen:up/P113-rpmsg-coverity

Conversation

@CV-Bowen

Copy link
Copy Markdown
Contributor

Summary

Static analysis cleanups of the rpmsg core (drivers/rpmsg/rpmsg.c) and
the rpmsg virtio transport (drivers/rpmsg/rpmsg_virtio.c), found by
Coverity (HIS metrics and MISRA C-2012), and three fixes to bugs the
first rework introduced, kept as separate commits in the order they were
made.

  • use a single exit point (3 commits, HIS_metric_violation RETURN):
    functions that returned from several places now keep the result in a
    local variable and return once. The goto again loop in
    rpmsg_register_callback() becomes a while loop that restarts the
    walk of the bind list after each match; the goto err paths in
    rpmsg_create_wqueues() and rpmsg_virtio_probe_cpuname() become
    else branches.
  • restart the bind list walk after each match,
    fix the check in rpmsg_alloc_buf(),
    fix rpmsg_ns_bind() and rpmsg_register_callback(): fixes to the
    first commit (missing breaks, an inverted condition, again set
    only once, the lock release in rpmsg_ns_bind()).
  • define g_rpmsg_dev_ops / g_rpmsg_virtio_ops in its only user
    (Rule 8.9): the two ops tables move into the only function that uses
    them, as static const.
  • use unsigned types for ring counts (Rule 10.4): the vring indices
    and counts in rpmsg_virtio_buffer_nused() and
    rpmsg_virtio_dump_buffer() are uint16_t, printed with %u.
  • initialize variables set through a pointer (Rule 9.1).

One behaviour change: on master rpmsg_ns_bind() returns with
g_rpmsg_lock still read-held when it fails to allocate the bind
record. After this PR the lock is released on that path too.

The intermediate commits between the first one and its fixes are not
bisect-safe for the bind paths; the series as a whole is what was
tested.

Impact

No API, Kconfig or behaviour change, except the lock release above on
an out-of-memory path.

Testing

Host: Ubuntu 22.04.5 LTS x86_64, QEMU version 10.2.1, sim: cc (Ubuntu 13.1.0-8ubuntu1~22.04) 13.1.0, qemu-armv8a: aarch64-none-elf-gcc (Arm GNU Toolchain 12.2.Rel1 (Build arm-12.24)) 12.2.1 20221205

Every commit builds sim:rpserver, sim:rpproxy, qemu-armv8a:rpserver and
qemu-armv8a:rpproxy with no warnings.

sim:rpserver + sim:rpproxy and qemu-armv8a:rpserver + qemu-armv8a:rpproxy
(unchanged configs), same cases on master and on this PR: the fixed
rpmsg cases (dump, ping, rpmsgfs mount and read), plus
rpmsg ping all 200 2048 3 0 on both nodes and rpmsg dump all
(verbose, walks rpmsg_virtio_dump() and rpmsg_virtio_dump_buffer())
on both nodes.

master this PR
sim cases 11/11 11/11
qemu-armv8a cases 11/11 11/11
rpmsg dump all output (endpoints, vq state, buffer counts; addresses and ring indices masked) - identical to master, sim and qemu-armv8a
sim rpmsg ping all 200 2048 3 0 avg, server / proxy 9.998 / 9.999 ms 9.999 / 9.999 ms

The name service paths in rpmsg_ns_bind() were checked with a
test-only syslog() (not in this PR) on the "no service yet, keep a
bind record" branch and on the replay branch of
rpmsg_register_callback(): on sim the same three records are kept on
every boot, on master and on this PR (3 runs each):

P113 defer rpmsg-ping
P113 defer rpmsg-ping-rt
P113 defer rpmsg-test

Not tested: the replay branch of rpmsg_register_callback() (no service
in these configs registers after its name was announced), and the
error paths (allocation, register_driver(), rpmsg_init_vdev() and
work queue creation failures).

No regression in rpmsg: sim:rpserver/rpproxy and qemu-armv8a:rpserver/rpproxy boot and pass the fixed rpmsg cases (dump, ping, rpmsgfs; 22/22). qemu-armv8a was built with LIBC_ENVPATH and BINFMT_CHECKEXEC disabled, which current master needs for nxinit to start nsh there; unrelated to this change.

Coverity reports HIS_metric_violation (RETURN) for the functions in
drivers/rpmsg/rpmsg.c that return from several places. Restructure
them to keep the result in a local variable and return once at the
end:

  rpmsg_init_workrx(), rpmsg_create_wqueues(), rpmsg_tickwait(),
  rpmsg_post(), rpmsg_get_local_cpuname(), rpmsg_get_timestamp(),
  rpmsg_alloc_buf(), rpmsg_register_callback(), rpmsg_ns_bind(),
  rpmsg_register(), rpmsg_ioctl_foreach_cb()

The goto in rpmsg_register_callback() becomes a loop over the bind
list that restarts after each match. No functional change
intended.

Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
Coverity reports HIS_metric_violation (RETURN) for the functions in
drivers/rpmsg/rpmsg_virtio.c that return from several places.
Restructure rpmsg_virtio_buffer_nused(), rpmsg_virtio_dump(),
rpmsg_virtio_start_worker() and rpmsg_virtio_probe_cpuname() to return
once at the end, nesting the success path instead of returning or
jumping on each error. No functional change.

Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
rpmsg_register_callback() frees the bind record it matched and sets
again to walk the list from the start, but kept iterating the list it
had just modified. Break out of the walk after the match.

Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
Coverity reports HIS_metric_violation (RETURN) for
rpmsg_get_by_rdev() and rpmsg_get_current_wqueue(), which return from
two places. Keep the result in a local variable and return once. No
functional change.

Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
The single exit rework inverted the condition: rpmsg_alloc_buf() called
ops->alloc_buf() exactly when rpmsg or the op was NULL. Call it only
when both are set.

Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
MISRA C-2012 Rule 8.9: an object should be declared at block scope if
its identifier only appears in a single function. g_rpmsg_dev_ops is
only used by rpmsg_register(), so move it there as a static const.

MISRA C-2012 Rule 10.4: compare the uint16_t nrx with an unsigned
constant in rpmsg_init_workrx().

No functional change.

Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
Fix three problems left by the single exit rework:

- rpmsg_ns_bind() kept walking the callback list after it had dropped
  g_rpmsg_lock and bound the first match. Stop at the first match.
- rpmsg_ns_bind() only released g_rpmsg_lock when the bind record was
  allocated, so an allocation failure returned with the lock held.
  Release it in both cases.
- rpmsg_register_callback() set again only once, so the bind list of
  every device after the first was not walked. Set it for each device.

Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
MISRA C-2012 Rule 10.4: both operands of an arithmetic operator shall
have the same essential type category. The ring indices and counts in
rpmsg_virtio_buffer_nused() and rpmsg_virtio_dump_buffer() are
uint16_t in the vring but were handled as int. Return and store them
as uint16_t, compare with unsigned constants and print them with %u.
No functional change.

Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
MISRA C-2012 Rule 9.1: the value of an object with automatic storage
duration shall not be read before it has been set. Coverity cannot
see that nxsem_get_value() and virtio_read_config_member() always fill
in semcount and shmbuf_pa0/1, so initialize them. No functional
change.

Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
MISRA C-2012 Rule 8.9: an object should be declared at block scope if
its identifier only appears in a single function. g_rpmsg_virtio_ops is
only used by rpmsg_virtio_start_worker(), so move it there as a static
const. No functional change.

Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
@github-actions github-actions Bot added Area: Drivers Drivers issues Size: L The size of the change in this PR is large labels Nov 19, 2026
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Drivers Drivers issues Size: L The size of the change in this PR is large

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants