[Devel] [PATCH vz8 v3] proc connector: protect from task cpu migration in send_msg_ve

Alexander Mikhalitsyn alexander.mikhalitsyn at virtuozzo.com
Fri Sep 10 13:36:29 MSK 2021


LGTM

________________________________________
From: Konstantin Khorenko <khorenko at virtuozzo.com>
Sent: Friday, September 10, 2021 13:33
To: Alexander Mikhalitsyn
Cc: Konstantin Khorenko; OpenVZ devel list
Subject: [PATCH vz8 v3] proc connector: protect from task cpu migration in send_msg_ve

this_cpu_ptr() should be used under local_lock() (or task migration
should be disabled instead), so switch the order.

Fixes: 0e60020d2a ("proc connector: take number of listeners and per-cpu
conters from VE")

Signed-off-by: Konstantin Khorenko <khorenko at virtuozzo.com>

--
v3:
 * fix double this_cpu_ptr() for local_unlock() as well

v2:
 * instead of prohibiting migration of the task to another cpu let's
   just put this_cpu_ptr() after local_lock().
 * fix double this_cpu_ptr() (local_lock() contains the second one)
---
 drivers/connector/cn_proc.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/drivers/connector/cn_proc.c b/drivers/connector/cn_proc.c
index 9b63792f0dff..ce7b33345db5 100644
--- a/drivers/connector/cn_proc.c
+++ b/drivers/connector/cn_proc.c
@@ -63,9 +63,15 @@ static inline void send_msg_ve(struct ve_struct *ve, struct cn_msg *msg)
 {
        struct local_event *le_ptr;

-       le_ptr = this_cpu_ptr(ve->cn->local_event);
-       local_lock(&le_ptr->lock);
+       /*
+        * The following hack with local_event->lock address works only
+        * till the "lock" is the first field in the local_event struct,
+        * so be of the safe side.
+        */
+       BUILD_BUG_ON(offsetof(struct local_event, lock) != 0);
+       local_lock(&ve->cn->local_event->lock);

+       le_ptr = this_cpu_ptr(ve->cn->local_event);
        msg->seq = le_ptr->count++;
        ((struct proc_event *)msg->data)->cpu = smp_processor_id();

@@ -77,7 +83,7 @@ static inline void send_msg_ve(struct ve_struct *ve, struct cn_msg *msg)
         */
        cn_netlink_send_ve(ve, msg, 0, CN_IDX_PROC, GFP_NOWAIT);

-       local_unlock(&le_ptr->lock);
+       local_unlock(&ve->cn->local_event->lock);
 }

 static struct cn_msg *cn_msg_fill(__u8 *buffer, struct ve_struct *ve,
--
2.28.0




More information about the Devel mailing list