Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openamp: change rx buffer hold flag to count #524

Merged
merged 1 commit into from
Jan 10, 2024

Conversation

CV-Bowen
Copy link
Contributor

@CV-Bowen CV-Bowen commented Oct 17, 2023

This commit fix one issue, before this commit, if rpmsg_hold_rx_buffer() is called in the endpoint callback to hold current received buffer and call rpmsg_release_rx_buffer() to release this buffer immediately.
This buffer will be returned to the virtqueue in rpmsg_virtio_release_rx_buffer() and returned to virtqueue again in rpmsg_virtio_return_buffer() after the ept->cb().
So same buffer returned to the virtqueue twice, error happened.
Follow shows this process:

rpmsg_virtio_rx_callback()
  - get rp_hdr
  - ept->cb(data = RPMSG_LOCATE_DATA(rp_hdr))
    - rpsmg_hold_rx_buffer(data)
    - rpmsg_release_rx_buffer(data) here has returned buffer to virtqueue
  - rpmsg_virtio_return_buffer() return same buffer to virtqueue again

The root cause of this issue is that the RPMSG_BUF_HELD has been cleared in rpmsg_release_rx_buffer() so rpmsg_virtio_rx_callback() will treat this buffer is not HELD and call rpmsg_virtio_return_buffer() to return this buffer again.

This commit change the HELD flag in rp_hdr to count to fix this issue and also support hold/release rx buffer recursively.

@CV-Bowen CV-Bowen force-pushed the rpmsg-hold-to-count branch 3 times, most recently from d345547 to e98367f Compare October 17, 2023 03:58
@arnopo arnopo requested review from edmooring, arnopo and tnmysh October 17, 2023 17:11
Copy link
Collaborator

@arnopo arnopo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strange to me that you hold the buffer to release it in same function. I would say hold it at the end of the ept->cb if needed...
That say I think it is interesting to use a counter instead of a flag, as we could have several consumers.

What I'm expecting here is that we have same hold mechanism for the RX and the TX buffers. So please update also the TX part

lib/rpmsg/rpmsg_virtio.c Outdated Show resolved Hide resolved
lib/rpmsg/rpmsg_virtio.c Outdated Show resolved Hide resolved
lib/rpmsg/rpmsg_virtio.c Outdated Show resolved Hide resolved
lib/rpmsg/rpmsg_virtio.c Outdated Show resolved Hide resolved
@CV-Bowen
Copy link
Contributor Author

Strange to me that you hold the buffer to release it in same function. I would say hold it at the end of the ept->cb if needed... That say I think it is interesting to use a counter instead of a flag, as we could have several consumers.

What I'm expecting here is that we have same hold mechanism for the RX and the TX buffers. So please update also the TX part

@arnopo Hi, I'm a little confused about the update also the TX part, do you mean add rpmsg_virtio_hold_tx_buffer() in rpmsg_virtio and rpmsg_hold_tx_buffer() in rpmsg, and also add hold_tx_buffer ops to the struct rpmsg_device_ops to make user can call this function?

@CV-Bowen CV-Bowen force-pushed the rpmsg-hold-to-count branch 3 times, most recently from 075dc53 to f7db776 Compare October 20, 2023 03:51
@arnopo arnopo closed this Oct 24, 2023
@arnopo arnopo reopened this Oct 24, 2023
@arnopo
Copy link
Collaborator

arnopo commented Oct 24, 2023

Strange to me that you hold the buffer to release it in same function. I would say hold it at the end of the ept->cb if needed... That say I think it is interesting to use a counter instead of a flag, as we could have several consumers.
What I'm expecting here is that we have same hold mechanism for the RX and the TX buffers. So please update also the TX part

@arnopo Hi, I'm a little confused about the update also the TX part, do you mean add rpmsg_virtio_hold_tx_buffer() in rpmsg_virtio and rpmsg_hold_tx_buffer() in rpmsg, and also add hold_tx_buffer ops to the struct rpmsg_device_ops to make user can call this function?

My request was not cristal clear.
No need to add user interface to hold TX buffer. my point was just to use RPMSG_BUF_HELD_INC() and RPMSG_BUF_HELD_DEC() internally for the TX buffers.

@CV-Bowen CV-Bowen force-pushed the rpmsg-hold-to-count branch 2 times, most recently from c87de36 to 7e2bf84 Compare October 26, 2023 06:35
@CV-Bowen
Copy link
Contributor Author

Strange to me that you hold the buffer to release it in same function. I would say hold it at the end of the ept->cb if needed... That say I think it is interesting to use a counter instead of a flag, as we could have several consumers.
What I'm expecting here is that we have same hold mechanism for the RX and the TX buffers. So please update also the TX part

@arnopo Hi, I'm a little confused about the update also the TX part, do you mean add rpmsg_virtio_hold_tx_buffer() in rpmsg_virtio and rpmsg_hold_tx_buffer() in rpmsg, and also add hold_tx_buffer ops to the struct rpmsg_device_ops to make user can call this function?

My request was not cristal clear. No need to add user interface to hold TX buffer. my point was just to use RPMSG_BUF_HELD_INC() and RPMSG_BUF_HELD_DEC() internally for the TX buffers.

@arnopo Got, and I has deleted the user interface. Could you take a look again.

lib/rpmsg/rpmsg_virtio.c Show resolved Hide resolved

rvdev = metal_container_of(rdev, struct rpmsg_virtio_device, rdev);

metal_mutex_acquire(&rdev->lock);

r_desc->idx = idx;
/* Check the hold status first */
if (RPMSG_BUF_HELD_STATUS(rp_hdr) <= 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be less than 0 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Release one buffer twice? Actually, I want to add a RPMSG_ASSERT here to check the incorrect usage of buffer hold/release API. Do you have some suggestions?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose thta the issue is that user call twice time rpmsg_virtio_buf_held_dec_test, right?
so a test could be added in rpmsg_virtio_buf_held_dec_test with a log error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

lib/rpmsg/rpmsg_virtio.c Outdated Show resolved Hide resolved
lib/rpmsg/rpmsg_virtio.c Show resolved Hide resolved
lib/rpmsg/rpmsg_internal.h Outdated Show resolved Hide resolved
lib/rpmsg/rpmsg_virtio.c Outdated Show resolved Hide resolved
lib/rpmsg/rpmsg_virtio.c Outdated Show resolved Hide resolved
lib/rpmsg/rpmsg_virtio.c Outdated Show resolved Hide resolved
@CV-Bowen CV-Bowen force-pushed the rpmsg-hold-to-count branch from 7e2bf84 to 7f670be Compare October 30, 2023 11:13
@xiaoxiang781216
Copy link
Collaborator

@arnopo do you have more suggestion?

lib/rpmsg/rpmsg_virtio.c Outdated Show resolved Hide resolved
lib/rpmsg/rpmsg_virtio.c Show resolved Hide resolved
lib/rpmsg/rpmsg_virtio.c Outdated Show resolved Hide resolved
lib/rpmsg/rpmsg_virtio.c Show resolved Hide resolved
@CV-Bowen CV-Bowen force-pushed the rpmsg-hold-to-count branch 2 times, most recently from ac266f6 to b7bd536 Compare December 4, 2023 11:51
lib/rpmsg/rpmsg_virtio.c Outdated Show resolved Hide resolved
lib/rpmsg/rpmsg_virtio.c Outdated Show resolved Hide resolved
Copy link
Collaborator

@arnopo arnopo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM adressing @xiaoxiang781216 comment

@CV-Bowen CV-Bowen force-pushed the rpmsg-hold-to-count branch from b7bd536 to 30ff35f Compare January 5, 2024 08:47
Copy link
Contributor

@edmooring edmooring left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

Copy link
Collaborator

@arnopo arnopo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor typos to fix before I merge it.
you can also remove "This commit fix one issue, " in the commit message

lib/rpmsg/rpmsg_virtio.c Outdated Show resolved Hide resolved
lib/rpmsg/rpmsg_virtio.c Outdated Show resolved Hide resolved
lib/rpmsg/rpmsg_virtio.c Outdated Show resolved Hide resolved
lib/rpmsg/rpmsg_virtio.c Outdated Show resolved Hide resolved
lib/rpmsg/rpmsg_virtio.c Outdated Show resolved Hide resolved
Before this commit, if rpmsg_hold_rx_buffer() is called in the endpoint
callback to hold current rx buffer and call rpmsg_release_rx_buffer()
to release this buffer immediately, this rx buffer will be returned to
the virtqueue twice:
1. the first time is in rpmsg_virtio_release_rx_buffer()
2. and the second time is in rpmsg_virtio_return_buffer() after ept->cb()
Follow shows this process:

rpmsg_virtio_rx_callback()
  - get rp_hdr
  - ept->cb(data = RPMSG_LOCATE_DATA(rp_hdr))
    - rpsmg_hold_rx_buffer(data)
    - rpmsg_release_rx_buffer(data) return buffer to virtqueue
  - rpmsg_virtio_return_buffer(data) return same buffer to virtqueue again

So this commit changes the HELD flag in rp_hdr to count to avoid this
use case and also supports hold/release rx buffer recursively.

Signed-off-by: Guiding Li <liguiding1@xiaomi.com>
Signed-off-by: Yin Tao <yintao@xiaomi.com>
Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
@CV-Bowen CV-Bowen force-pushed the rpmsg-hold-to-count branch from 30ff35f to 7e09f36 Compare January 9, 2024 11:54
@CV-Bowen
Copy link
Contributor Author

CV-Bowen commented Jan 9, 2024

@arnopo Thanks, all the comments have been addressed.

@arnopo arnopo merged commit b32187e into OpenAMP:main Jan 10, 2024
3 checks passed
@arnopo arnopo added this to the Release V2024.04 milestone Jan 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants