Blame SOURCES/process_queue_pool-Only-acquire-the-VringMutex-lock-.patch

8cf3d9
From 9e55bb375937f8cc93666d9998f09d23a31185f1 Mon Sep 17 00:00:00 2001
8cf3d9
From: Sebastian Hasler <sebastian.hasler@stuvus.uni-stuttgart.de>
8cf3d9
Date: Wed, 2 Feb 2022 17:50:34 +0100
8cf3d9
Subject: [PATCH 1/3] process_queue_pool: Only acquire the VringMutex lock once
8cf3d9
8cf3d9
Previously, the worker task in `process_queue_pool()` called
8cf3d9
up to 3 functions on `worker_vring` where each function is a
8cf3d9
wrapper that first locks the mutex.  This is unneccessary
8cf3d9
congestion.  We fix this by locking the mutex once.
8cf3d9
8cf3d9
Signed-off-by: Sebastian Hasler <sebastian.hasler@stuvus.uni-stuttgart.de>
8cf3d9
(cherry picked from commit c904bd8dbd9557d1a59fa0934e092443c7264d43)
8cf3d9
Signed-off-by: Sergio Lopez <slp@redhat.com>
8cf3d9
---
8cf3d9
 src/main.rs | 72 +++++++++++++++++++----------------------------------
8cf3d9
 1 file changed, 26 insertions(+), 46 deletions(-)
8cf3d9
8cf3d9
diff --git a/src/main.rs b/src/main.rs
8cf3d9
index 2049eda..5a9914f 100644
8cf3d9
--- a/src/main.rs
8cf3d9
+++ b/src/main.rs
8cf3d9
@@ -9,7 +9,7 @@ use passthrough::xattrmap::XattrMap;
8cf3d9
 use std::convert::{self, TryFrom};
8cf3d9
 use std::ffi::CString;
8cf3d9
 use std::os::unix::io::{FromRawFd, RawFd};
8cf3d9
-use std::sync::{Arc, Mutex, MutexGuard, RwLock};
8cf3d9
+use std::sync::{Arc, Mutex, RwLock};
8cf3d9
 use std::{env, error, fmt, io, process};
8cf3d9
 
8cf3d9
 use structopt::StructOpt;
8cf3d9
@@ -128,6 +128,28 @@ impl<F: FileSystem + Send + Sync + 'static> VhostUserFsThread<F> {
8cf3d9
         })
8cf3d9
     }
8cf3d9
 
8cf3d9
+    fn return_descriptor(vring_state: &mut VringState, head_index: u16, event_idx: bool) {
8cf3d9
+        if vring_state.add_used(head_index, 0).is_err() {
8cf3d9
+            warn!("Couldn't return used descriptors to the ring");
8cf3d9
+        }
8cf3d9
+
8cf3d9
+        if event_idx {
8cf3d9
+            match vring_state.needs_notification() {
8cf3d9
+                Err(_) => {
8cf3d9
+                    warn!("Couldn't check if queue needs to be notified");
8cf3d9
+                    vring_state.signal_used_queue().unwrap();
8cf3d9
+                }
8cf3d9
+                Ok(needs_notification) => {
8cf3d9
+                    if needs_notification {
8cf3d9
+                        vring_state.signal_used_queue().unwrap();
8cf3d9
+                    }
8cf3d9
+                }
8cf3d9
+            }
8cf3d9
+        } else {
8cf3d9
+            vring_state.signal_used_queue().unwrap();
8cf3d9
+        }
8cf3d9
+    }
8cf3d9
+
8cf3d9
     fn process_queue_pool(&mut self, vring: VringMutex) -> Result<bool> {
8cf3d9
         let mut used_any = false;
8cf3d9
         let atomic_mem = match &self.mem {
8cf3d9
@@ -168,35 +190,14 @@ impl<F: FileSystem + Send + Sync + 'static> VhostUserFsThread<F> {
8cf3d9
                     .map_err(Error::ProcessQueue)
8cf3d9
                     .unwrap();
8cf3d9
 
8cf3d9
-                if event_idx {
8cf3d9
-                    if worker_vring.add_used(head_index, 0).is_err() {
8cf3d9
-                        warn!("Couldn't return used descriptors to the ring");
8cf3d9
-                    }
8cf3d9
-
8cf3d9
-                    match worker_vring.needs_notification() {
8cf3d9
-                        Err(_) => {
8cf3d9
-                            warn!("Couldn't check if queue needs to be notified");
8cf3d9
-                            worker_vring.signal_used_queue().unwrap();
8cf3d9
-                        }
8cf3d9
-                        Ok(needs_notification) => {
8cf3d9
-                            if needs_notification {
8cf3d9
-                                worker_vring.signal_used_queue().unwrap();
8cf3d9
-                            }
8cf3d9
-                        }
8cf3d9
-                    }
8cf3d9
-                } else {
8cf3d9
-                    if worker_vring.add_used(head_index, 0).is_err() {
8cf3d9
-                        warn!("Couldn't return used descriptors to the ring");
8cf3d9
-                    }
8cf3d9
-                    worker_vring.signal_used_queue().unwrap();
8cf3d9
-                }
8cf3d9
+                Self::return_descriptor(&mut worker_vring.get_mut(), head_index, event_idx);
8cf3d9
             });
8cf3d9
         }
8cf3d9
 
8cf3d9
         Ok(used_any)
8cf3d9
     }
8cf3d9
 
8cf3d9
-    fn process_queue_serial(&mut self, vring_state: &mut MutexGuard<VringState>) -> Result<bool> {
8cf3d9
+    fn process_queue_serial(&mut self, vring_state: &mut VringState) -> Result<bool> {
8cf3d9
         let mut used_any = false;
8cf3d9
         let mem = match &self.mem {
8cf3d9
             Some(m) => m.memory(),
8cf3d9
@@ -226,28 +227,7 @@ impl<F: FileSystem + Send + Sync + 'static> VhostUserFsThread<F> {
8cf3d9
                 .map_err(Error::ProcessQueue)
8cf3d9
                 .unwrap();
8cf3d9
 
8cf3d9
-            if self.event_idx {
8cf3d9
-                if vring_state.add_used(head_index, 0).is_err() {
8cf3d9
-                    warn!("Couldn't return used descriptors to the ring");
8cf3d9
-                }
8cf3d9
-
8cf3d9
-                match vring_state.needs_notification() {
8cf3d9
-                    Err(_) => {
8cf3d9
-                        warn!("Couldn't check if queue needs to be notified");
8cf3d9
-                        vring_state.signal_used_queue().unwrap();
8cf3d9
-                    }
8cf3d9
-                    Ok(needs_notification) => {
8cf3d9
-                        if needs_notification {
8cf3d9
-                            vring_state.signal_used_queue().unwrap();
8cf3d9
-                        }
8cf3d9
-                    }
8cf3d9
-                }
8cf3d9
-            } else {
8cf3d9
-                if vring_state.add_used(head_index, 0).is_err() {
8cf3d9
-                    warn!("Couldn't return used descriptors to the ring");
8cf3d9
-                }
8cf3d9
-                vring_state.signal_used_queue().unwrap();
8cf3d9
-            }
8cf3d9
+            Self::return_descriptor(vring_state, head_index, self.event_idx);
8cf3d9
         }
8cf3d9
 
8cf3d9
         Ok(used_any)
8cf3d9
-- 
8cf3d9
2.35.1
8cf3d9