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 674A3455C0; Mon, 8 Jul 2024 04:18:27 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 389E2402D5; Mon, 8 Jul 2024 04:18:27 +0200 (CEST) Received: from OS0P286CU011.outbound.protection.outlook.com (mail-japanwestazolkn19010013.outbound.protection.outlook.com [52.103.66.13]) by mails.dpdk.org (Postfix) with ESMTP id 3FB82402D3; Mon, 8 Jul 2024 04:18:25 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Eh34G8bmlkcEPdZ+qL2xQBk2svwzM41Yle0A3PS0h28jP9+y6A8kug6CdjIooPJt1MJEnf7ATo4lRkJUco2N8/6GDBMzEIAbok49e5AgOwztrlb5zn4/vgXUnVNW2mXD1roggKk33X+VHjc8k2C6pKxM+6hs3dxX07ayL1uiwMTq+zr35vgTklD3VwSdO6w/SRiXfCg95bSI+JAoh9Xh3D9cXy9FBnmofkVQvvGgu+YMQD8Mp5tOIbyieWx3wNAKInRDRMNsSOZTg/Rl5F9yX0mtP8MD2n03GQw71uCdIQNqaw9dqyFRXulmA1MsSWjIPEMhPk+dHi4lVK/cG4jIng== 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=GTL9XFtRjGv255BB+B7csX8lgy3/s2bZupSBilzZzEU=; b=JBJ4MbIrXfFAlYAsLl/B6FYmw92an+JApbc6a5u7Zmr1Ut8dm2RpX3RM0LWPytfBrStUNDvuRqmgDfJhMDR0+4QJKbraKVPuLSA6ut6VdaucVPpunMXZYgPtnhhUz4TzV9etDHAtkCb8MsQ4rdI70dZO+Y3AFyG/G8EAdoPye1gaO4quV3DM/k5he1WpBs8tqrR21uztTp1cNuceaOPLvO54JE3noUS7f/n5tz2Wjj0oo4jhuLwlmYHdR6O71j63SLPU6QRG2bgcuUn+97qS2OWbGrwCXlUOPqA0JRnga6Rucj9+qqbc0h2q1yopvNmgKfE/NiyjgUhrRp6a3AwJvQ== 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=GTL9XFtRjGv255BB+B7csX8lgy3/s2bZupSBilzZzEU=; b=IGZi8MAYRcPvBAsdz3tTiNzjDkV1myTixQFLURt5pwbV0pPPrJ1mqBvnqPhEq+b27ObTWQyGo55LfNNlXSOHb9pK8Kb5W4iMy/OCJsrLX20waSqwcHS51DJq+wFrKKT0AmgoWAl2o7nBrsP7WwKh0DRzA4YOxKcYTY5FLNDf56qHQ1nvQYUltNzSBePocXBgOXX/GrX/Q+o4bOe8CynbqIV0qUV3+cMB5X8ezp12kYHgbeBWKcPI5yL9Ig7WXdieHadjlfcQcsjo7CH46n0xTeRE0g9pQTb1LK0RI+g7Av5nBx5ydOlC8R/DuGsFDx7vbMHJtl9F9oq0eRaKo+y+Bg== Received: from TYAP286MB0649.JPNP286.PROD.OUTLOOK.COM (2603:1096:402:38::13) by OS3P286MB3337.JPNP286.PROD.OUTLOOK.COM (2603:1096:604:20b::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.34; Mon, 8 Jul 2024 02:18:22 +0000 Received: from TYAP286MB0649.JPNP286.PROD.OUTLOOK.COM ([fe80::a92e:74bb:6df2:7ea2]) by TYAP286MB0649.JPNP286.PROD.OUTLOOK.COM ([fe80::a92e:74bb:6df2:7ea2%5]) with mapi id 15.20.7741.033; Mon, 8 Jul 2024 02:18:22 +0000 From: Gongming Chen To: maxime.coquelin@redhat.com, chenbox@nvidia.com, chengongming1900@outlook.com Cc: dev@dpdk.org, Gongming Chen , stable@dpdk.org Subject: [PATCH v2] vhost: fix crash caused by accessing a freed vsocket Date: Mon, 8 Jul 2024 10:17:45 +0800 Message-ID: X-Mailer: git-send-email 2.32.1 (Apple Git-133) In-Reply-To: References: Content-Transfer-Encoding: 8bit Content-Type: text/plain X-TMN: [U72KI9IXolleTfhM4/Nr2wJ40bwIxeDR] X-ClientProxiedBy: SI1PR02CA0057.apcprd02.prod.outlook.com (2603:1096:4:1f5::10) To TYAP286MB0649.JPNP286.PROD.OUTLOOK.COM (2603:1096:402:38::13) X-Microsoft-Original-Message-ID: <20240708021745.8535-1-chengongming1900@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: TYAP286MB0649:EE_|OS3P286MB3337:EE_ X-MS-Office365-Filtering-Correlation-Id: 00e6d63c-7382-4872-aa3c-08dc9ef43d38 X-Microsoft-Antispam: BCL:0; ARA:14566002|461199028|19110799003|8060799006|440099028|3412199025|3430499032|1710799026; X-Microsoft-Antispam-Message-Info: tKtGnmCTEnr2EcCxoBBoy8nFWW87g4c4SVUT8cO5wZ1bim/NyDph+9kEb4EckQZwnRH+Dzf0Ys62V0zwkdQAnuG26BnXOO6Vw8SZKT+QbJvN5SiaJw27LUfT0bo+lfbSgV8wTclD/42AbikseK/B5JaTRO9muHM70PkURz9X6Da2MatrO7NXTpOUAuSv1ZrZy3WclgrgepKpSI1t8jx9kw3i+5i3C9MFaYrYA8MnPhKH2FkYyxlEr+vo1Xaf90TaCzRHDQYvL0iiGtDqG1cdJZKyNkPZNfojdESKxd/Fv9sUCWcuPYN/qTp3WJiqS5h2pqhgCeEZecJxVULOKZR6vpnoMr4lD4MTjJvU+UPK+01Q7f4CqVLqB0Xc3z+O8tYTcB6P6+WOHkje9KcLJvUZn9fwPrdUxPbvJVhCMCc+i6UPlYPWMXTwry24mv0jJ+avhvMRa3ELC8ztk86U5ZB/CDkKodDrLga+pD++EWkSJzND0lr/YMrEt0A5GWeaYbamwg7laqqjKnjsvP5IwC0rQ8HFZol9+rPn+aCl+dKAa3GXb4iK8vw2Jc6xRrVKpLUAtigQHhS7E8GsA+Te2jo9hXVN0xuFkc+dJs9PrZ8T6mYtEqL4An7DLMF4FiaHpkCcUkDs6oOR4gqdgE/0hWbI1q2E5T8k+CsFW2sqgtnyFgI= X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?BkSt8FVecHZEz2izgPAw4adTnAZfjqFWaNVyd3pecWh+Xj8INGFOzMT208bG?= =?us-ascii?Q?ze6b7uekpNG8LqUq51aamIrLm+0cR8gIQnxz/+gZ06otA+IzFhmT1waj5biI?= =?us-ascii?Q?5XZOmd8MBOmJfqZ57gbNTwWU1MgKQ0waJGWMIW7eewh7rL2dA7zcu0JamgjB?= =?us-ascii?Q?eXIqwim+oRJ6nFGKFsfh/i+HCOMC+8ANcRpTr61SelcCCbfItn5K5vzjtG+Z?= =?us-ascii?Q?6ofYQPH0SwExAl7Ic39WG8zyDswU1hLQnZnqW0p5acYY31mkvC0AFaBU9fKI?= =?us-ascii?Q?xcwTc2HvJIB95SbPLlH0mien4XZ6yRGe/N9Bj/+2FukZmVnKl4T6NljV3FAZ?= =?us-ascii?Q?R7hOSU2ARQxqcudY5labe+ZTf/5O8GYZ88UUUdkznHcm4oIgV3gYba05RVO1?= =?us-ascii?Q?ejWbtasRuediwW+sc8stvfYXV6e/fItIlbEooLH72bA48ngu9lEcmbT7pugX?= =?us-ascii?Q?a8knu5bMoaWg0WcJ3Bprja8Rb2JSyoGVf7RAEC75ygMoOyh9TppEwdHaRoyg?= =?us-ascii?Q?7IVbwYPBZmsvTz14tnbnokTuPSxqHDG7iN8SzjLSiE6QvSqEgWrRal9t0feb?= =?us-ascii?Q?kBK2glYQ5LwtWflwoIWuDRd5QRAD0Ka5i6tH3/VcLUyhXgsQ9pJjZq638PLU?= =?us-ascii?Q?McVjXFtjLnlbZN6T2WyF2P6H0IXuH3CVMORgyordWwoQJ+oYPEQTz6KkZBa7?= =?us-ascii?Q?4BjXHU9mJ6H8H1eDSMNDbpyAZjtT+lOvDHmTX4g/YQKeT341ytbMqbXKcl+R?= =?us-ascii?Q?Wa771MTaVNXQoCmL5eySXadxKjl6S4Fyd990gPmM/951ejhY8GtSGCSgGRc6?= =?us-ascii?Q?2kQS0PTIZQtRkHwUFLc/9cYbSFN3LGDs07IBs/FurLk8ars2U23KB22S2F6m?= =?us-ascii?Q?gC7EPLqlzSGHPphXvnfxuJTrdgg7YvVHckNqPpt/6a+45JNAxJTsguE+Pzvk?= =?us-ascii?Q?WKl8Xa9/kBx5dXpWWNuYnwwnUZnZLu7ZElMVYwqmfD0YbvkIaztLBHeD/zjx?= =?us-ascii?Q?Q5r7+WtzBDtJvcuRPrmrazQKixQq7OKCYgmdYgZrT0NfX/jtldLmhxkR1+Zi?= =?us-ascii?Q?QDvzUij0kZvfKv8zjeegDMpxgJ6OxNHManVcki6Tb2CbsA1NkHbrnukDXd7R?= =?us-ascii?Q?2uoh1giq8UzqaDVBEgM38CFjwBDF9NTt4T+bkRjIfKvNKr3knceAbGxgqJ7q?= =?us-ascii?Q?HpZYtI9pyw9Hk+KnWi+t53WcpoSj13QasOt613VoE7kFqw4TKOd1o+PBDyHX?= =?us-ascii?Q?6/NAeXNFJZmWt8SHd/1O?= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 00e6d63c-7382-4872-aa3c-08dc9ef43d38 X-MS-Exchange-CrossTenant-AuthSource: TYAP286MB0649.JPNP286.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Jul 2024 02:18:22.2021 (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: OS3P286MB3337 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org 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 that has been freed by rte_vhost_driver_unregister 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:323 #2 0x0000000003ed625f in fdset_event_dispatch at lib/vhost/fd_man.c:365 #3 0x0000000004168336 in ctrl_thread_init at lib/eal/common/eal_common_thread.c:282 #4 0x00007ffff7bc6ea5 in start_thread () from /lib64/libpthread.so.0 #5 0x00007ffff6209b0d in clone () from /lib64/libc.so.6 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 | 29 +++++++++++++++++++++++++++++ lib/vhost/vhost_thread.h | 12 ++++++++++++ 5 files changed, 57 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 87a8dc3f3e..32472d5ed7 100644 --- a/lib/vhost/fd_man.c +++ b/lib/vhost/fd_man.c @@ -15,6 +15,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 @@ -342,6 +343,7 @@ fdset_event_dispatch(void *arg) if (numfds < 0) continue; + vhost_thread_read_lock(); for (i = 0; i < numfds; i++) { pthread_mutex_lock(&pfdset->fd_mutex); @@ -379,6 +381,7 @@ fdset_event_dispatch(void *arg) if (remove1 || remove2) fdset_del(pfdset, fd); } + vhost_thread_read_unlock(); if (pfdset->destroy) break; 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 = 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 a75728a2e4..c9c295c2a3 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" @@ -456,6 +457,7 @@ vhost_user_client_reconnect(void *arg __rte_unused) struct vhost_user_reconnect *reconn, *next; while (1) { + vhost_thread_read_lock(); pthread_mutex_lock(&reconn_list.mutex); /* @@ -487,6 +489,7 @@ vhost_user_client_reconnect(void *arg __rte_unused) } pthread_mutex_unlock(&reconn_list.mutex); + vhost_thread_read_unlock(); sleep(1); } @@ -1067,7 +1070,7 @@ rte_vhost_driver_unregister(const char *path) if (path == NULL) return -1; -again: + vhost_thread_write_lock(); pthread_mutex_lock(&vhost_user.mutex); for (i = 0; i < vhost_user.vsocket_cnt; i++) { @@ -1079,14 +1082,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) == -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); } @@ -1098,17 +1097,10 @@ rte_vhost_driver_unregister(const char *path) next = 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) == -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); @@ -1130,9 +1122,11 @@ rte_vhost_driver_unregister(const char *path) vhost_user.vsockets[i] = vhost_user.vsockets[count]; vhost_user.vsockets[count] = 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..df439e68e6 --- /dev/null +++ b/lib/vhost/vhost_thread.c @@ -0,0 +1,29 @@ +#include + +#include "vhost_thread.h" + +static rte_rwlock_t vhost_thread_lock = RTE_RWLOCK_INITIALIZER; + +void +vhost_thread_read_lock(void) +{ + rte_rwlock_read_lock(&vhost_thread_lock); +} + +void +vhost_thread_read_unlock(void) +{ + rte_rwlock_read_unlock(&vhost_thread_lock); +} + +void +vhost_thread_write_lock(void) +{ + rte_rwlock_write_lock(&vhost_thread_lock); +} + +void +vhost_thread_write_unlock(void) +{ + 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..3c44b1c030 --- /dev/null +++ b/lib/vhost/vhost_thread.h @@ -0,0 +1,12 @@ +#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 -- 2.32.1 (Apple Git-133)