Skip to content

Conversation

@likebreath
Copy link
Member

Note: this PR depends on #7057.

The Memory Space Enable (MSE) bit from the COMMAND register in the
PCI configuration space controls whether a PCI device responds to memory
space accesses, e.g. read and write cycles to the device MMIO regions
defined by its BARs. The MSE bit is used by the device drivers to ensure
the correctness of BAR reprogramming. A common workflow is, the driver
first clears the MSE bit, then writes new values to the BAR registers,
and finally set the MSE bit to finish the BAR reprogramming.

This patch changes how we handle BAR reprogramming for all PCI
devices (e.g. virtio-pci, vfio, vfio-user, etc.), so that we follow the
same convention, e.g. moving PCI BARs when its MSE bit is set.

Note that some device drivers (such as edk2) only clear and set MSE once
while reprogramming multiple BARs of a single device. To support such
behavior, this patch adds support for multiple pending BAR reprogramming.

See: #7027 (comment)

Also, the way how we handle PCI configuration space for vfio and vfio-user
devices are different from the rest of PCI devices. Besides accesses to
BAR registers (trapped to access the shadowing PCI config space we
maintained), accesses to other registers (including the COMMAND
register) are handled directly by the underline vfio or vfio-user
device.

This patch adds the proper handling of pending BAR reprogramming for
vfio and vfio-user devices.

Copy link
Member

@rbradford rbradford 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 - did you consider storing the pending program per bar rather than all the BARs together per device? Would that actually end up being more complicated?

@likebreath
Copy link
Member Author

Looks good - did you consider storing the pending program per bar rather than all the BARs together per device? Would that actually end up being more complicated?

I think it should be pretty straight forward, while the code will be a bit more complicated (say using a Hashmap with reg_idx as key, or using a set while adding reg_idx to BarReprogrammingParams with a custom Eq trait etc).

Two potential benefits I can see:

  1. We can log reg_idx or bar_idx information along with the pending bar reprogram parameter;
  2. We can check if the same bar is re-programmed multiple times, but not sure how much we want to restrict such behavior given the device driver definitely can do that.

Also both benefits appear to be minor. Anything I am missing?

@rbradford
Copy link
Member

Looks good - did you consider storing the pending program per bar rather than all the BARs together per device? Would that actually end up being more complicated?

I think it should be pretty straight forward, while the code will be a bit more complicated (say using a Hashmap with reg_idx as key, or using a set while adding reg_idx to BarReprogrammingParams with a custom Eq trait etc).

Two potential benefits I can see:

1. We can log `reg_idx` or `bar_idx` information along with the pending bar reprogram parameter;

2. We can check if the same bar is re-programmed multiple times, but not sure how much we want to restrict such behavior given the device driver definitely can do that.

Also both benefits appear to be minor. Anything I am missing?

  1. Was one of the motivators of my suggestion e.g. if we stored the new pending BAR value as part of the BAR we could just update the pending value - no need to keep the history.

Rather than an external store like a hash table or vector I was thinking that the pending value could be stored on the PciBar and then copied over when the MSE bit is toggled?

The Memory Space Enable (MSE) bit from the COMMAND register in the
PCI configuration space controls whether a PCI device responds to memory
space accesses, e.g. read and write cycles to the device MMIO regions
defined by its BARs. The MSE bit is used by the device drivers to ensure
the correctness of BAR reprogramming. A common workflow is, the driver
first clears the MSE bit, then writes new values to the BAR registers,
and finally set the MSE bit to finish the BAR reprogramming.

This patch changes how we handle BAR reprogramming for all PCI
devices (e.g. virtio-pci, vfio, vfio-user, etc.), so that we follow the
same convention, e.g. moving PCI BARs when its MSE bit is set.

Note that some device drivers (such as edk2) only clear and set MSE once
while reprogramming multiple BARs of a single device. To support such
behavior, this patch adds support for multiple pending BAR reprogramming.

See: cloud-hypervisor#7027 (comment)

Signed-off-by: Bo Chen <[email protected]>
The way how we handle PCI configuration space for vfio and vfio-user
devices are different from the rest of PCI devices. Besides accesses to
BAR registers (trapped to access the shadowing PCI config space we
maintained), accesses to other registers (including the COMMAND
register) are handled directly by the underline vfio or vfio-user
device.

This patch adds the proper handling of pending BAR reprogramming for
vfio and vfio-user devices.

Signed-off-by: Bo Chen <[email protected]>
@likebreath likebreath force-pushed the 0512/fix_move_bar branch from 5621106 to d88a8e0 Compare May 15, 2025 16:00
@likebreath
Copy link
Member Author

We discussed about this PR in the community meeting today. We agreed it is the right way to go, considering more refactoring needed and potential impacts to live-migration with alternative approaches.

@likebreath likebreath enabled auto-merge May 15, 2025 16:55
@likebreath likebreath added this pull request to the merge queue May 15, 2025
Merged via the queue into cloud-hypervisor:main with commit 8da7c13 May 15, 2025
41 of 44 checks passed
@likebreath likebreath added the bug-fix Bug fix to include in release notes label May 22, 2025
@likebreath likebreath moved this from 🆕 New to ✅ Done in Cloud Hypervisor Roadmap May 22, 2025
abarisani added a commit to usbarmory/tamago that referenced this pull request May 27, 2025
@likebreath likebreath deleted the 0512/fix_move_bar branch July 16, 2025 23:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug-fix Bug fix to include in release notes

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants