From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 4EFD343DEE for ; Wed, 3 Apr 2024 17:52:18 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 40847402CE; Wed, 3 Apr 2024 17:52:18 +0200 (CEST) Received: from TY3P286CU002.outbound.protection.outlook.com (mail-japaneastazolkn19011003.outbound.protection.outlook.com [52.103.43.3]) by mails.dpdk.org (Postfix) with ESMTP id 572E840144; Wed, 3 Apr 2024 17:52:15 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=i56U8BD/otBpRUWiHKoj7JTwvKZHC9JbY1Y3N7fjkePwY/29fnWTQyq9XYw5+QpvQgwZFwjnvaPIJ8NB0/szBIbObbdgTv/n04gDX09RmYWAryp1OQjNH5xitghr27FLjXgCTroksbbKCujFmD5jYBRj9ZIkM9pYV1nULInYVCSyXQ1g3t01Ri1Ufi6Hvm0yvzDXMOWKwjQjR1V+GNpABLiQ4OGqbLj4DztzwaMEKGmG1+LhSZy+dUsHyfFL2Z25g0QVXNQryK3KT3qwbx9WJ8VVFHbp3JONwtlUQAJPaHa+5zd2Hh7U3lKCg9TinAna+BHvK7Nx2v8QJiJeieFKnA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=5mGO/PNZAUV2ErLkwR9mYTb0HvBdKQ5k1HRAewqQFro=; b=S0YI4kWwYgVUGt+/nqHcSz3uQifhvi5Vy91LP/hMPo1rt4uHaDvjFjSIMsoavNFz2iEfZQd7kgmFMc6zXYg739Df5TpWvGwEIBsuJxD0zbKCl2NKCuUzWa7074LGgPJjGL8DZ7aDZ27GCpzceFgbuN/UwH8JCGk700RsT0FIOXUmaVR5Czjy4W3LxtIdoXogFwN68mAZ12MGtzD4oRr6P7om/WPGZg7StGT1qqyT+hDa8dJqkRG4QVUVbWS4B7MPHTjvixpIBGy0EVrUkMBy/LJ824hPpxPudP7BA0B8fM4eGtxKfSrVxVTVsuUlDNUFpmt6ygdynVcCH0uDEFm9/g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=5mGO/PNZAUV2ErLkwR9mYTb0HvBdKQ5k1HRAewqQFro=; b=GB2ILcfinAP34S6mkGItXLF1L0wJ4ClCF85+sQpsArXHiVRBxB7ykhFKVVzexsubjlCRBChaRum4SPFZsMPnxNv92xo/7mlVccFRBon9cQLslnxmWoCDAHGiAkuv17AnlgSrtJ+Ysodw5G2+LzkJhXigvKKaB1RshbfIRC1JhC9UemGD4zUJECmQUfAiID0Lujeieey/HrV4H6y51j2FyTkXKS4FesexLWB0bBNJB7UG4Pkco56SoskPsjnOQHW/OgkDIwiYYhF/GDpYUD4N5591qEXc9HtBqbIX3dC7e2GgBZoizUisTpsxuZWdJ8ibh0L2yAJB49/nPlbWL8ATHQ== Received: from TYAP286MB0649.JPNP286.PROD.OUTLOOK.COM (2603:1096:402:38::13) by TY1P286MB3280.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:2e4::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.46; Wed, 3 Apr 2024 15:52:12 +0000 Received: from TYAP286MB0649.JPNP286.PROD.OUTLOOK.COM ([fe80::3b2d:34ba:11e6:b203]) by TYAP286MB0649.JPNP286.PROD.OUTLOOK.COM ([fe80::3b2d:34ba:11e6:b203%4]) with mapi id 15.20.7409.042; Wed, 3 Apr 2024 15:52:12 +0000 Content-Type: text/plain; charset=us-ascii Subject: Re: [PATCH] vhost: fix crash caused by accessing a freed vsocket From: Gongming Chen In-Reply-To: Date: Wed, 3 Apr 2024 23:52:11 +0800 Cc: chenbox@nvidia.com, dev@dpdk.org, Gongming Chen , stable@dpdk.org Content-Transfer-Encoding: quoted-printable Message-ID: References: To: Maxime Coquelin X-Mailer: Apple Mail (2.3693.60.0.1.1) X-TMN: [aOx99804niiKf9paZz8D/by2Tbk5xl4jyBkZzA5tzis=] X-ClientProxiedBy: TYCP286CA0187.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:382::11) To TYAP286MB0649.JPNP286.PROD.OUTLOOK.COM (2603:1096:402:38::13) X-Microsoft-Original-Message-ID: <9B9F4FD2-7618-4B5D-A6DE-546B9E1CB50B@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: TYAP286MB0649:EE_|TY1P286MB3280:EE_ X-MS-Office365-Filtering-Correlation-Id: 881a7890-dbb3-4b0d-ecfa-08dc53f6071a X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ReBWh5po711A6gdGcz4HjMy0zpoLvBRX5VWnTeBORSvSGVhMGVSg3tsvO0HS87X9LFIo+X53zsuDt84PplS+Zh68VfV+xQTWsKv1QhwvA6l+2w3ZeM6sowOYWe8csQinBA2dOQC0oSx6Ggg4N0JBJSpKhsWG2H8pfjvGGTSTxsBlfY8rAEoeKVZ6xP3PnzNVcQ6VMZuTwrGByyyk/Z5sVR5l1ZnrV8UNKmraBMm9dYKObBSUhK5KXmut7X9crAGy01Lp37k7PXHe9MQ9bo1WXVUQbu8SIa6fB7TdNW/ovp9JG+0toTN9MT9BrRlIUBPxNjAx8NyKXmsAqL5/B6m2NmTEg8Ft7k2bsrkuL0kwsWXM7wtrxcoWFPoIxwb3KVH6GDYIvJ6HlFQptZU1qeWIRapVRr+fJ9Kr01XG+PuCXrBV0F+xThcuroOXwJhoknzYA9DNXiukD35ySdPspyA2TVyQ6PRF2Q9SHqdLuMG91d6+yE2gFtFmZtmDxqqimTNLYiveiOfYU3He0cH3YBmuUQB0ASroeDrxsFn/Dub+rGo5lESY3Pc57Pl3wLBirwvM X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?1SZ5yH0ODRgqUlz2NO/wEwP6TmlnJlKIq+sBi+0ScSvudk5LrePvs8pLCSbN?= =?us-ascii?Q?CXNobh/c/3Fb4L3Kofy3EoNEMFywmQ2gP1cb4Y1zWYr9y5mauP8HbbtSfqrm?= =?us-ascii?Q?qpa3yHfOQfilxl1py46vo0VhFJ3taYDFn5qJ0YmFbcQGZKsW8gAArueLJpWi?= =?us-ascii?Q?NLwQH78Ft5/RyTdtsM5eu1slnyop/7i4N5mPGw02UcUjixpQW+2O8W/mUGgH?= =?us-ascii?Q?7FOsMk41/wCOz6pkVe88z2RHmSdjAvpFcIAvILNlef+6ws9XM+P9JGcAKen/?= =?us-ascii?Q?CU6nC8zmTxEXdP3kk43gbJ9tkpnVyKJ2l4vIWuuUx96oKkENoNhHlIBFHkkA?= =?us-ascii?Q?xpS6mfzzLfQvXDvPjEkY67NXkJiGnpAh96zNX+/NDtQeFTxZ8OF8z5Wsndwt?= =?us-ascii?Q?DQQmHYJfNr85SwG1KHu+MIngW4Uj8zGCkxEvnFPh3uPriRp5EUGkppS7/Dxm?= =?us-ascii?Q?F3C6broxt0W7Hk7Eqv2912CQ3KcKkIDevuQKFXEo80NsFli2E9Nm3JUxkeUp?= =?us-ascii?Q?6ruwwPiimwnmMlc1t1fDa6d/RVRnqtkaOq6VmHKppkxH7iznIK5REE2RLe5u?= =?us-ascii?Q?JngczHdvkSZM5LXnSph6wwEXHXDd9bya6tBvmUHpBFuA90KtxEtbQlMX4nPu?= =?us-ascii?Q?666GrACUJGKjUeSzWqxdlXfoYIO8eq4OKhIxElPt/tIJHBVV0tFS5bzSF2/m?= =?us-ascii?Q?+lfWLEgFwEGhlOwU8X9oPYuDH40KFP2OEcS8QFnwM/7UaI43jmBXrPexztyD?= =?us-ascii?Q?StFDDKXJhi8nHnzE15D+NJRKHOKts5RrApeM84lZ3y9XAG+XsSgBJklH0WFZ?= =?us-ascii?Q?vFHNsgZ/emfNsfpMolzwjDVQRwoNakwgZOyFnBeucMCYdGapUyKLpkqfqJ4J?= =?us-ascii?Q?iRqerJsCvOgAD4z1AIVGKLIk17jbtaBIw0GXogm1xSfFA2PznrQttJhiHN32?= =?us-ascii?Q?5e04xE7VmEeety9hlGwPxdrNmPJ5BQgQ2DuCrtVfts8lhO5p6+N9T/A3eZS9?= =?us-ascii?Q?SGin8KpOSkjvxZ89LyMjgZouHMDZbbwIIVPjBRSmw5k4AIiCWMQQvkphRUtV?= =?us-ascii?Q?GB7TMZfVfb6DzoEm6/lQCf6XEiYm4x9HlAPHfI7yQNuTJZjYhXNrybNye9k4?= =?us-ascii?Q?18TOHAk3aRaIYaqHQ0J0GkDx819UpLj634rrWXCuXBjqvmCJ5VCk4Jl7jYso?= =?us-ascii?Q?o8xoqej1l/h92moGfy1DwEksXI3WUHOXoJwMv1PgqrHK21hfzEWCdSjFyFZy?= =?us-ascii?Q?9M6nbuF+M+j8Ix8dnoY4?= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 881a7890-dbb3-4b0d-ecfa-08dc53f6071a X-MS-Exchange-CrossTenant-AuthSource: TYAP286MB0649.JPNP286.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Apr 2024 15:52:12.7399 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: TY1P286MB3280 X-BeenThere: stable@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: patches for DPDK stable branches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: stable-bounces@dpdk.org Hi Maxime, Thanks for review. > On Apr 3, 2024, at 5:39 PM, Maxime Coquelin = wrote: >=20 > Hi Gongming, >=20 > It's the 9th time the patch has been sent. > I'm not sure whether there are changes between them or these are just > re-sends, but that's something to avoid. >=20 Sorry, there's something wrong with my mailbox. I will send a v1 version as the latest patch, but they are actually the sam= e. > If there are differences, you should use versionning to highlight it. > If unsure, please check the contributions guidelines first. >=20 > Regarding the patch itself, I don't know if this is avoidable, but I > would prefer we do not introduce yet another lock in there. >=20 > Thanks, > Maxime >=20 I totally agree with your. Therefore, initially I hoped to solve this problem without introducing new lock. However, the result was not expected. 1. The vsocket is shared between the event and reconnect threads by transmitting the vsocket pointer. Therefore, there is no way to protect vsocket through a simple vsocket lock. 2. The event and reconnect threads can transmit vsocket pointers to each other, so there is no way to ensure that vsocket will not be accessed by locking the two threads separately. 3. Therefore, on the vsocket resource, event and reconnect are in the same critical section. Only by locking two threads at the same time can the vsocket be ensured that it will not be accessed and can be freed safely. Currently, app config, event, and reconnect threads respectively have locks corresponding to their own maintenance resources, vhost_user.mutex, pfdset->fd_mutex, and reconn_list.mutex. I think there is a thread-level lock missing here to protect the critical section between threads, just like the rcu scene protection. After app config acquires the write lock, it ensures that the event and reconnect threads are outside the critical section. This is to completely clean up the resources associated with vsocket and safely free vsocket. Therefore, considering future expansion, if there may be more resources like vsocket, this thread lock can also be used to ensure that resources are safely released after complete cleanup. In this way, the threads will be clearer, and the complicated try lock method is no longer needed. Thanks, Gongming > On 4/3/24 08:31, Gongming Chen wrote: >> From: Gongming Chen >> When a vhost user message handling error in the event dispatch thread, >> vsocket reconn is added to the reconnection list of the reconnection >> thread. >> Since the reconnection, event dispatching and app configuration thread >> do not have common thread protection restrictions, the app config >> thread freed vsocket in the rte_vhost_driver_unregister process, >> but vsocket reconn can still exist in the reconn_list through this >> mechanism. >> Then in the reconnection thread, the vsocket is connected again and >> conn is added to the dispatch thread. >> Finally, the vsocket is accessed again in the event dispatch thread, >> resulting in a use-after-free error. >> This patch adds a vhost threads read-write lock to restrict >> reconnection, event dispatching and app configuration threads. >> When the vhost driver unregisters, it exclusively holds the lock to >> safely free the vsocket. >> #0 0x0000000000000025 in ?? () >> #1 0x0000000003ed7ca0 in vhost_user_read_cb at lib/vhost/socket.c:330 >> #2 0x0000000003ed625f in fdset_event_dispatch at lib/vhost/fd_man.c:283 >> Fixes: e623e0c6d8a5 ("vhost: add vhost-user client mode") >> Cc: stable@dpdk.org >> Signed-off-by: Gongming Chen >> --- >> lib/vhost/fd_man.c | 3 +++ >> lib/vhost/meson.build | 1 + >> lib/vhost/socket.c | 30 ++++++++++++------------------ >> lib/vhost/vhost_thread.c | 37 +++++++++++++++++++++++++++++++++++++ >> lib/vhost/vhost_thread.h | 16 ++++++++++++++++ >> 5 files changed, 69 insertions(+), 18 deletions(-) >> create mode 100644 lib/vhost/vhost_thread.c >> create mode 100644 lib/vhost/vhost_thread.h >> diff --git a/lib/vhost/fd_man.c b/lib/vhost/fd_man.c >> index 481e6b900a..b0e0aa2640 100644 >> --- a/lib/vhost/fd_man.c >> +++ b/lib/vhost/fd_man.c >> @@ -9,6 +9,7 @@ >> #include >> #include "fd_man.h" >> +#include "vhost_thread.h" >> RTE_LOG_REGISTER_SUFFIX(vhost_fdset_logtype, fdset, INFO); >> #define RTE_LOGTYPE_VHOST_FDMAN vhost_fdset_logtype >> @@ -250,6 +251,7 @@ fdset_event_dispatch(void *arg) >> if (val < 0) >> continue; >> + vhost_thread_read_lock(); >> need_shrink =3D 0; >> for (i =3D 0; i < numfds; i++) { >> pthread_mutex_lock(&pfdset->fd_mutex); >> @@ -303,6 +305,7 @@ fdset_event_dispatch(void *arg) >> if (need_shrink) >> fdset_shrink(pfdset); >> + vhost_thread_read_unlock(); >> } >> return 0; >> diff --git a/lib/vhost/meson.build b/lib/vhost/meson.build >> index 41b622a9be..7bc1840ed0 100644 >> --- a/lib/vhost/meson.build >> +++ b/lib/vhost/meson.build >> @@ -25,6 +25,7 @@ sources =3D files( >> 'vdpa.c', >> 'vhost.c', >> 'vhost_crypto.c', >> + 'vhost_thread.c', >> 'vhost_user.c', >> 'virtio_net.c', >> 'virtio_net_ctrl.c', >> diff --git a/lib/vhost/socket.c b/lib/vhost/socket.c >> index 96b3ab5595..e05d36f549 100644 >> --- a/lib/vhost/socket.c >> +++ b/lib/vhost/socket.c >> @@ -20,6 +20,7 @@ >> #include "fd_man.h" >> #include "vduse.h" >> #include "vhost.h" >> +#include "vhost_thread.h" >> #include "vhost_user.h" >> @@ -463,6 +464,7 @@ vhost_user_client_reconnect(void *arg __rte_unuse= d) >> struct vhost_user_reconnect *reconn, *next; >> while (1) { >> + vhost_thread_read_lock(); >> pthread_mutex_lock(&reconn_list.mutex); >> /* >> @@ -494,6 +496,7 @@ vhost_user_client_reconnect(void *arg __rte_unused) >> } >> pthread_mutex_unlock(&reconn_list.mutex); >> + vhost_thread_read_unlock(); >> sleep(1); >> } >> @@ -1071,7 +1074,7 @@ rte_vhost_driver_unregister(const char *path) >> if (path =3D=3D NULL) >> return -1; >> -again: >> + vhost_thread_write_lock(); >> pthread_mutex_lock(&vhost_user.mutex); >> for (i =3D 0; i < vhost_user.vsocket_cnt; i++) { >> @@ -1083,14 +1086,10 @@ rte_vhost_driver_unregister(const char *path) >> vduse_device_destroy(path); >> } else if (vsocket->is_server) { >> /* >> - * If r/wcb is executing, release vhost_user's >> - * mutex lock, and try again since the r/wcb >> - * may use the mutex lock. >> + * The vhost thread write lock has been acquired, >> + * and fd must be deleted from fdset. >> */ >> - if (fdset_try_del(&vhost_user.fdset, vsocket->socket_fd) =3D=3D -1) = { >> - pthread_mutex_unlock(&vhost_user.mutex); >> - goto again; >> - } >> + fdset_del(&vhost_user.fdset, vsocket->socket_fd); >> } else if (vsocket->reconnect) { >> vhost_user_remove_reconnect(vsocket); >> } >> @@ -1102,17 +1101,10 @@ rte_vhost_driver_unregister(const char *path) >> next =3D TAILQ_NEXT(conn, next); >> /* >> - * If r/wcb is executing, release vsocket's >> - * conn_mutex and vhost_user's mutex locks, and >> - * try again since the r/wcb may use the >> - * conn_mutex and mutex locks. >> + * The vhost thread write lock has been acquired, >> + * and fd must be deleted from fdset. >> */ >> - if (fdset_try_del(&vhost_user.fdset, >> - conn->connfd) =3D=3D -1) { >> - pthread_mutex_unlock(&vsocket->conn_mutex); >> - pthread_mutex_unlock(&vhost_user.mutex); >> - goto again; >> - } >> + fdset_del(&vhost_user.fdset, conn->connfd); >> VHOST_CONFIG_LOG(path, INFO, "free connfd %d", conn->connfd); >> close(conn->connfd); >> @@ -1134,9 +1126,11 @@ rte_vhost_driver_unregister(const char *path) >> vhost_user.vsockets[i] =3D vhost_user.vsockets[count]; >> vhost_user.vsockets[count] =3D NULL; >> pthread_mutex_unlock(&vhost_user.mutex); >> + vhost_thread_write_unlock(); >> return 0; >> } >> pthread_mutex_unlock(&vhost_user.mutex); >> + vhost_thread_write_unlock(); >> return -1; >> } >> diff --git a/lib/vhost/vhost_thread.c b/lib/vhost/vhost_thread.c >> new file mode 100644 >> index 0000000000..6b5dc22042 >> --- /dev/null >> +++ b/lib/vhost/vhost_thread.c >> @@ -0,0 +1,37 @@ >> +/* SPDX-License-Identifier: BSD-3-Clause >> + * Copyright (c) 2024 China Telecom Cloud Technology Co., Ltd >> + */ >> + >> +#include >> + >> +#include "vhost_thread.h" >> + >> +static rte_rwlock_t vhost_thread_lock =3D RTE_RWLOCK_INITIALIZER; >> + >> +void >> +vhost_thread_read_lock(void) >> + __rte_no_thread_safety_analysis >> +{ >> + rte_rwlock_read_lock(&vhost_thread_lock); >> +} >> + >> +void >> +vhost_thread_read_unlock(void) >> + __rte_no_thread_safety_analysis >> +{ >> + rte_rwlock_read_unlock(&vhost_thread_lock); >> +} >> + >> +void >> +vhost_thread_write_lock(void) >> + __rte_no_thread_safety_analysis >> +{ >> + rte_rwlock_write_lock(&vhost_thread_lock); >> +} >> + >> +void >> +vhost_thread_write_unlock(void) >> + __rte_no_thread_safety_analysis >> +{ >> + rte_rwlock_write_unlock(&vhost_thread_lock); >> +} >> diff --git a/lib/vhost/vhost_thread.h b/lib/vhost/vhost_thread.h >> new file mode 100644 >> index 0000000000..61679172af >> --- /dev/null >> +++ b/lib/vhost/vhost_thread.h >> @@ -0,0 +1,16 @@ >> +/* SPDX-License-Identifier: BSD-3-Clause >> + * Copyright (c) 2024 China Telecom Cloud Technology Co., Ltd >> + */ >> + >> +#ifndef _VHOST_THREAD_H_ >> +#define _VHOST_THREAD_H_ >> + >> +void vhost_thread_read_lock(void); >> + >> +void vhost_thread_read_unlock(void); >> + >> +void vhost_thread_write_lock(void); >> + >> +void vhost_thread_write_unlock(void); >> + >> +#endif >=20