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

remoteproc: do cache invalidation before reading rsc_table status #500

Merged
merged 2 commits into from
Jul 17, 2023

Conversation

iuliana-prodan
Copy link
Contributor

Do a cache invalidation before reading the resource table's status since this ca be in a cacheable region.

@iuliana-prodan
Copy link
Contributor Author

cc @carlocaione @arnopo

@iuliana-prodan
Copy link
Contributor Author

iuliana-prodan commented Jul 12, 2023

Changes since v1:

  • make cache invalidation optional based on VIRTIO_CACHED_RSC_TABLE;
  • add VIRTIO_CACHED_RSC_TABLE as option.

@@ -40,6 +41,11 @@ static unsigned char rproc_virtio_get_status(struct virtio_device *vdev)
rpvdev = metal_container_of(vdev, struct remoteproc_virtio, vdev);
vdev_rsc = rpvdev->vdev_rsc;
io = rpvdev->vdev_rsc_io;

#ifdef VIRTIO_CACHED_RSC_TABLE
Copy link
Collaborator

@tnmysh tnmysh Jul 12, 2023

Choose a reason for hiding this comment

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

Can we move #ifdef part to relevant header file ?
May be we can introduce function to check if cache is on or not and then based on that we can decide if we need to call metal_cache_invalidate or not.

Copy link
Contributor Author

@iuliana-prodan iuliana-prodan Jul 12, 2023

Choose a reason for hiding this comment

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

So far, in OpenAMP there are 2 ways of doing cache invalidation:

I've chosen the first one.
You're suggesting the second one - just to put the defines in a header, not in .c file, right?

Copy link
Collaborator

Choose a reason for hiding this comment

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

May be we can introduce function to check if cache is on or not and then based on that we can decide if we need to call metal_cache_invalidate or not.

That's way too wasteful IMO, you are going to have a huge hit in performance if for every cache op you are going to check whether the cache is on or not.

To make this nicer we can do what we did here:

#ifdef VIRTIO_CACHED_VRINGS
#define VRING_FLUSH(x) metal_cache_flush(&x, sizeof(x))
#define VRING_INVALIDATE(x) metal_cache_invalidate(&x, sizeof(x))
#else
#define VRING_FLUSH(x) do { } while (0)
#define VRING_INVALIDATE(x) do { } while (0)
#endif /* VIRTIO_CACHED_VRINGS */

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@carlocaione yes, will do like this.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@carlocaione yes, will do like this.

bonus point if you can clean up these two ways of doing the same thing 😜 (in a different PR)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks. Yes second way works.

@iuliana-prodan
Copy link
Contributor Author

iuliana-prodan commented Jul 13, 2023

Changes since v2:

  • use defines, added in header, for cache flush/invalidate functions.

LE: the checkpatch issue is a false-positive.

Copy link
Collaborator

@carlocaione carlocaione left a comment

Choose a reason for hiding this comment

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

LGTM but looking forward to the cleanup PR.

@arnopo
Copy link
Collaborator

arnopo commented Jul 13, 2023

@iuliana-prodan
Copy link
Contributor Author

Changes since v3:

  • fix checkpatch warning;
  • update README in new commit.

Comment on lines 29 to 31
#define RSC_TABLE_FLUSH(x) \
({void *_x = (x); \
metal_cache_flush(&_x, sizeof(_x)); })
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are you sure this is doing what you think it is doing? Looking at it it looks like you are flushing the pointer here, not the actual struct also with a wrong sizeof().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right. I've fixed it.
Also, to pass checkpatch I've added 2 arguments for RSC_TABLE_FLUSH and RSC_TABLE_INVALIDATE

@iuliana-prodan iuliana-prodan force-pushed the fix-rsc-table-cache-invalidate branch 2 times, most recently from cd05c20 to 3f1dd55 Compare July 13, 2023 22:45
@iuliana-prodan
Copy link
Contributor Author

Changes since v4:

  • in order to fix checkpatch warnings I've added 2 arguments for RSC_TABLE_FLUSH and RSC_TABLE_INVALIDATE,

lib/remoteproc/remoteproc_virtio.c Outdated Show resolved Hide resolved
@iuliana-prodan
Copy link
Contributor Author

Changes since v5:

  • call RSC_TABLE_INVALIDATE with sizeof(struct..);
  • do not use address of the pointer for metal_cache_flush/metal_cache_invalidate;

Comment on lines 43 to 45

RSC_TABLE_INVALIDATE(vdev_rsc, sizeof(struct fw_rsc_vdev));

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is fine now, thanks.

Shouldn't we also RSC_TABLE_FLUSH in rproc_virtio_set_status() though?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added RSC_TABLE_FLUSH/RSC_TABLE_INVALIDATE for status, dfeatures and gfeatures.
I don't know if I should do the same for read_config/write_config.
What do you think?
For Zephyr, config is not used: https://github.com/zephyrproject-rtos/zephyr/blob/main/lib/open-amp/resource_table.h#L50

Copy link
Collaborator

Choose a reason for hiding this comment

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

@arnopo I'm not familiar with remoteproc at all. What are the structs that are actually r/w from both sides that needs to be flushed / invalidated?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@iuliana-prodan, @carlocaione

Yes we need cache operation even in read config and write config as well.
One more question, should we perform only flush or invalidate and flush both during writing resource table? Or it is really platform dependent?

Copy link
Collaborator

Choose a reason for hiding this comment

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

"Flush" and "invalidate" should be sufficient if the structures are aligned on a cache line. We can improve this in the future if needed.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Right @TanmayShah-xilinx,
The cache shoule also be managed in rproc_virtio_read_config andrproc_virtio_read_config

@iuliana-prodan
Copy link
Contributor Author

Changes since v6:

  • do flush/invalidate also for write/read config;

Copy link
Collaborator

@carlocaione carlocaione left a comment

Choose a reason for hiding this comment

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

In general this LGTM. I'm a bit concerned about the alignment of these structs. If the structs are not aligned to the cache line, the invalidation will corrupt neighboring structs in memory. It really is a silent killer.

See for example zephyrproject-rtos/zephyr#59456

Comment on lines 80 to 82

RSC_TABLE_INVALIDATE(vdev_rsc, sizeof(struct fw_rsc_vdev));

Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: can you keep the newlines consistent among all the RSC_TABLE_* calls?

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

Do a cache invalidation before reading the resource table's info
since this ca be in a cacheable region.

Make this optional, based on VIRTIO_CACHED_RSC_TABLE.

Signed-off-by: Iuliana Prodan <[email protected]>
Add info for WITH_DCACHE_RSC_TABLE option.

Signed-off-by: Iuliana Prodan <[email protected]>
@iuliana-prodan
Copy link
Contributor Author

Changes since v7:
keep the newlines consistent among all the RSC_TABLE_* calls;

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

Copy link
Collaborator

@tnmysh tnmysh left a comment

Choose a reason for hiding this comment

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

LGTM.

@arnopo arnopo merged commit 5891cb4 into OpenAMP:main Jul 17, 2023
2 checks passed
@arnopo arnopo added this to the Release V2023.10 milestone Sep 18, 2023
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.

4 participants