devices: Introduce remove() for bus

When device's bar is reallocated, the old ranges will be deleted,
this commit add remove() function to delete one old range from
bus->devices.

BUG=b:174705596
TEST=Boot a vm, monitor Bar Reallocation then check function

Change-Id: Id0600ae1d7a3d04c7f05a90abcc8807b7f1020c3
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3184725
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
This commit is contained in:
Xiong Zhang 2021-06-10 14:29:00 +08:00 committed by Commit Bot
parent ccb9014afb
commit d982b8d9a3

View file

@ -150,6 +150,8 @@ pub trait BusDeviceObj {
#[sorted]
#[derive(Error, Debug)]
pub enum Error {
#[error("Bus Range not found")]
Empty,
/// The insertion failed because the new device overlapped with an old device.
#[error("new device overlaps with an old device")]
Overlap,
@ -295,6 +297,28 @@ impl Bus {
Ok(())
}
/// Remove the given device at the given address space.
pub fn remove(&self, base: u64, len: u64) -> Result<()> {
if len == 0 {
return Err(Error::Overlap);
}
let mut devices = self.devices.lock();
if devices
.iter()
.any(|(range, _dev)| range.base == base && range.len == len)
{
let ret = devices.remove(&BusRange { base, len });
if ret.is_some() {
Ok(())
} else {
Err(Error::Empty)
}
} else {
Err(Error::Empty)
}
}
/// Reads data from the device that owns the range containing `addr` and puts it into `data`.
///
/// Returns true on success, otherwise `data` is untouched.