x86_64: Fix File descriptor sharing in MSR handler

Now the File descriptors aren't stored in MsrHandlers level, for every
passthrough handler, the fd will be refresh in MsrHandlers level.

This can't avoid much File descriptors creation. Fix this error.

BUG=b:225375705
TEST=Set `--userspace-msr=0x1a2,type=r,action=pass,from=cpu0`

Change-Id: I2c67a5c257f545c6469fc71d8b53606256af22c0
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3606974
Reviewed-by: Junichi Uekawa <uekawa@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Junichi Uekawa <uekawa@chromium.org>
This commit is contained in:
Mi Dapeng 2022-04-25 21:12:28 +08:00 committed by Chromeos LUCI
parent e40b5b8f4b
commit 158e668dea

View file

@ -269,11 +269,16 @@ impl MsrExitHandler for MsrHandlers {
return Err(MsrExitHandlerError::InvalidParam);
}
let msr_file = Rc::new(RefCell::new(BTreeMap::new()));
let new_msr_file = Rc::new(RefCell::new(BTreeMap::new()));
let msr_file = match &self.msr_file {
Some(old_msr_file) => old_msr_file,
None => &new_msr_file,
};
match msr_config.action.as_ref().unwrap() {
MsrAction::MsrPassthrough => {
let msr_handler: Rc<RefCell<Box<dyn MsrHandling>>> =
match MsrPassthroughHandler::new(index, &msr_config, &msr_file) {
match MsrPassthroughHandler::new(index, &msr_config, msr_file) {
Ok(r) => Rc::new(RefCell::new(Box::new(r))),
Err(e) => {
error!(
@ -288,7 +293,7 @@ impl MsrExitHandler for MsrHandlers {
}
MsrAction::MsrEmulate => {
let msr_handler: Rc<RefCell<Box<dyn MsrHandling>>> =
match MsrEmulateHandler::new(index, &msr_config, &msr_file) {
match MsrEmulateHandler::new(index, &msr_config, msr_file) {
Ok(r) => Rc::new(RefCell::new(Box::new(r))),
Err(e) => {
error!(
@ -303,8 +308,8 @@ impl MsrExitHandler for MsrHandlers {
}
};
// Empty only when no 'passthrough' handler exists.
if !msr_file.borrow().is_empty() {
self.msr_file = Some(msr_file);
if self.msr_file.is_none() && !msr_file.borrow().is_empty() {
self.msr_file = Some(new_msr_file);
}
Ok(())
}