From 70eb15dbb7cca5672c6e37e0f1853bf5b47c5a78 Mon Sep 17 00:00:00 2001 From: Daniel Verkamp Date: Thu, 3 Jan 2019 09:40:01 -0800 Subject: [PATCH] devices: block: protect disk_size with a mutex This will allow the disk size to be changed from the worker thread during resize operations. BUG=chromium:858815 TEST=build_test Change-Id: I0b2e1a057831856b44f19c2ba30b4dd1ffdeafc3 Signed-off-by: Daniel Verkamp Reviewed-on: https://chromium-review.googlesource.com/1394151 Commit-Ready: ChromeOS CL Exonerator Bot Reviewed-by: Dylan Reid Reviewed-by: Zach Reizner --- devices/src/virtio/block.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/devices/src/virtio/block.rs b/devices/src/virtio/block.rs index 15e0e7301d..ae212c3cf1 100644 --- a/devices/src/virtio/block.rs +++ b/devices/src/virtio/block.rs @@ -13,6 +13,7 @@ use std::thread; use std::time::Duration; use std::u32; +use sync::Mutex; use sys_util::Error as SysError; use sys_util::Result as SysResult; use sys_util::{ @@ -618,7 +619,7 @@ impl Worker { pub struct Block { kill_evt: Option, disk_image: Option, - disk_size: u64, + disk_size: Arc>, avail_features: u64, read_only: bool, } @@ -664,7 +665,7 @@ impl Block { Ok(Block { kill_evt: None, disk_image: Some(disk_image), - disk_size, + disk_size: Arc::new(Mutex::new(disk_size)), avail_features, read_only, }) @@ -704,7 +705,10 @@ impl VirtioDevice for Block { } fn read_config(&self, offset: u64, mut data: &mut [u8]) { - let config_space = build_config_space(self.disk_size); + let config_space = { + let disk_size = self.disk_size.lock(); + build_config_space(*disk_size) + }; let config_len = size_of_val(&config_space) as u64; if offset >= config_len { return;