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

plat/drivers/virtio: Fix virtio read write #970

Closed
wants to merge 1 commit into from

Conversation

zhxj9823
Copy link
Contributor

@zhxj9823 zhxj9823 commented Jul 5, 2023

Add support for variable length of virtio read and write.

Prerequisite checklist

  • Read the contribution guidelines regarding submitting new changes to the project;
  • Tested your changes against relevant architectures and platforms;
  • Ran the checkpatch.uk on your commit series before opening this PR;
  • Updated relevant documentation.

Base target

  • Architecture(s): N/A
  • Platform(s): kvm
  • Application(s): N/A

Description of changes

The vm_get and vm_set in virtio_mmio can only handle reading/writing 1/2/4/8 bytes. However, there are several cases, such as in virtio_net and virtio_9p, that the length of bytes to read is not supported by this function. This PR adds support to this case by using _virtio_cread_bytes and _virtio_cwrite_bytes functions in the default case of vm_set and vm_get functions correspondingly. This approach turns an n-byte read/write operation into n 1-byte read/write operations.

@zhxj9823 zhxj9823 requested a review from a team as a code owner July 5, 2023 04:50
@razvand razvand added kind/bug Something isn't working area/plat Unikraft Patform kind/quick-fix Issue is a quick fix plat/driver/virtio lang/c Issues or PRs to do with C/C++ bug/fix This PR fixes a bug labels Jul 5, 2023
@razvand razvand added this to the v0.14.0 (Prometheus) milestone Jul 5, 2023
@unikraft-bot
Copy link
Member

Checkpatch passed

Beep boop! I ran Unikraft's checkpatch.pl support script on your pull request and it all looks good!

SHA commit checkpatch
fa4a7ea plat/drivers/virtio: Fix virtio read write

@razvand razvand removed the request for review from a team July 5, 2023 11:09
@dinhngtu
Copy link
Member

dinhngtu commented Jul 7, 2023

All looks good to me.

Reviewed-by: Tu Dinh Ngoc dinhngoc.tu@irit.fr

@Krechals
Copy link
Contributor

Krechals commented Jul 8, 2023

Thanks for fixing this, @zhxj9823. I'm fine with the changes. I would only add a comment as to why we are handling virtio-net (mac address) and virtio-9p (tag) in this manner, especially given that the VirtIO 1.0/1.1 specs state that the driver shouldn't do a byte by byte handling:

4.2.2.2 Driver Requirements: MMIO Device Register Layout

The driver MUST only use 32 bit wide and aligned reads and 
writes to access the control registers described in table [4.1].

For the device-specific configuration space, the driver MUST use 
8 bit wide accesses for 8 bit wide fields, 
16 bit wide and aligned accesses for 16 bit wide fields 
and 32 bit wide and aligned accesses for 32 and 64 bit wide fields.

Since we cannot avoid braking the atomicity (the mac address has 6 bytes), I would say this change is fine.
Linux does it the same: https://elixir.bootlin.com/linux/v6.4.2/source/drivers/net/virtio_net.c#L2230

The only issue with removing the assertions is that when we do so, we might silence a future bug. IMO, the risk is quite low, as hopefully these code paths won't be require many changes in the future. Without additional refactoring, which I don't believe is necessary at this time, I don't see a clear method to handle this appropriately.

@zhxj9823
Copy link
Contributor Author

zhxj9823 commented Jul 9, 2023

Thanks for fixing this, @zhxj9823. I'm fine with the changes. I would only add a comment as to why we are handling virtio-net (mac address) and virtio-9p (tag) in this manner, especially given that the VirtIO 1.0/1.1 specs state that the driver shouldn't do a byte by byte handling:

4.2.2.2 Driver Requirements: MMIO Device Register Layout

The driver MUST only use 32 bit wide and aligned reads and 
writes to access the control registers described in table [4.1].

For the device-specific configuration space, the driver MUST use 
8 bit wide accesses for 8 bit wide fields, 
16 bit wide and aligned accesses for 16 bit wide fields 
and 32 bit wide and aligned accesses for 32 and 64 bit wide fields.

Since we cannot avoid braking the atomicity (the mac address has 6 bytes), I would say this change is fine. Linux does it the same: https://elixir.bootlin.com/linux/v6.4.2/source/drivers/net/virtio_net.c#L2230

The only issue with removing the assertions is that when we do so, we might silence a future bug. IMO, the risk is quite low, as hopefully these code paths won't be require many changes in the future. Without additional refactoring, which I don't believe is necessary at this time, I don't see a clear method to handle this appropriately.

Thanks for the comment.

An alternative way of handling this issue is to explicitly use _virtio_cread_bytes and _virtio_cwrite_bytes on every unaligned read and write operation, and Linux does it in a way similar to this. This includes the cases in virtio-net and virtio-9p, and possibly other cases. Since I am not familiar with virtio, I am unable to do such things, so adding a case in vm_get and vm_set is quicker and simpler.

Besides, I think we can add a warning message in this case to give a hint if anything wrong arises.

@michpappas
Copy link
Member

@zhxj9823 please add a comment on the virtio-net and virtio-9p side as requested by @Krechals so that we can get this merged. On the virtio-net there is a comment already that needs to be adapted: https://github.com/unikraft/unikraft/blob/staging/plat/drivers/virtio/virtio_net.c#L932

You can also add a warning if you wish on the virtio-mmio side.

Add support for variable length of virtio read and write. The original
error message is replaced by a warning message.

Two cases in virtio_net and virtio_9p uses this feature and the comments
are added to note the usage.

Signed-off-by: Xingjian Zhang <zhxj9823@qq.com>
@michpappas
Copy link
Member

@Krechals, @eduardvintila please provide your feedback / sign-off so that we can merge this in 0.14, thanks! 🚀

Copy link
Member

@eduardvintila eduardvintila 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, thanks.

Reviewed-by: Eduard Vintilă eduard.vintila47@gmail.com

@Krechals
Copy link
Contributor

LGTM. Thank you, @zhxj9823!

Reviewed-by: Andrei Topala topala.andrei@gmail.com

@michpappas
Copy link
Member

Approved-by: Michalis Pappas <michalis.unikraft.io>

@unikraft-bot unikraft-bot added the ci/merged Merged by CI label Aug 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/plat Unikraft Patform bug/fix This PR fixes a bug ci/merged Merged by CI kind/bug Something isn't working kind/quick-fix Issue is a quick fix lang/c Issues or PRs to do with C/C++ plat/driver/virtio plat/driver
Projects
Status: Done!
Development

Successfully merging this pull request may close these issues.

None yet

7 participants