From nobody Sun Dec 14 06:42:51 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1765203705; cv=none; d=zohomail.com; s=zohoarc; b=KcMfT6tIzFHCppUIxaASzstV0zbNAZ/WhwAdg+wQV1Q9i98ZbvQkl5Rfh1pMRCXtIpxBMtq+k+TaNEboBg4eoqgbsi++GDd0ZwzHc4LE/KiJV9KNLjMYVNRH7RM1mLfu+mdEfZSh4XIf/qQ6iqWqA0o3ZXsbYNPiYPgc/uIqPYI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1765203705; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=xYpdNK9V9NdphAkLyhPH0d+dGDBCogdp0MSQ37nPW0M=; b=gyKtIsWC8luvwi3Kzv70hiMWdvVOq8w8cuGA3PawzxCP+YXnQZ4aw/51ErL459k+P20Kqo2gIfOaLPYqVtGV/xt0QHVYBxuUhTHRBbZ9ZsIfWh+5pFS7kgVLUWibqEVZl4ADh8TRyzcjWvTFJCkcS328IpDA6JYJMvw7sGN4exs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1765203705892280.5586060771351; Mon, 8 Dec 2025 06:21:45 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vSc6c-0003bK-QL; Mon, 08 Dec 2025 09:20:50 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vSX2E-0006eS-VJ for qemu-devel@nongnu.org; Mon, 08 Dec 2025 03:55:58 -0500 Received: from mail-lj1-x22f.google.com ([2a00:1450:4864:20::22f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vSX2D-0006SA-22 for qemu-devel@nongnu.org; Mon, 08 Dec 2025 03:55:58 -0500 Received: by mail-lj1-x22f.google.com with SMTP id 38308e7fff4ca-37a3340391cso29916411fa.3 for ; Mon, 08 Dec 2025 00:55:55 -0800 (PST) Received: from fedora.vk.team ([5.181.62.126]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-597d7c1e39asm3989094e87.50.2025.12.08.00.55.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Dec 2025 00:55:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1765184154; x=1765788954; darn=nongnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=xYpdNK9V9NdphAkLyhPH0d+dGDBCogdp0MSQ37nPW0M=; b=Mww5xYf9LMvHjwB3Zfs36TZ2VBzkLIxaEH2eMq4cph98mK2zV0wlrc45sON1YFmqbu 9lry2kGvdhtp0nXXQ8OUBw4fiqg8vZbSnjHBPc80/qC5d1OPnRukS9RtCDvhf/kp+im5 +9XFwCn+lU41uC15ce/l3eolqU4bLWURNK1xZgeRL8R1HQNdG2+3EvrQqgi5LEaIR3tU t2pzw0EweDwPflcdvoW9qMTeX4loPnIOiioAEBgh1Egdl8cGWlDmXGP2nrfViLmioakR Gkea++QZ1oCOMaex76u9KB+/I0cyDg2dszo170RrobY22QYhNZJZQSGdBZKivLOMhEXN 7ELg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765184154; x=1765788954; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=xYpdNK9V9NdphAkLyhPH0d+dGDBCogdp0MSQ37nPW0M=; b=u6YeSSHALwi2XQiiRmryRPgO75aAPpULQXgAAEo/Z7Cf3tOkjiQQpQrvHM2CkjmL8X VSsZQvFpSuqIW/tpylte8ttpPsuBf1t0dDv3A3UVFcgCU0mUYlCNogVouyrl0xpesSWt xqVo/iun8jy6WzZiiyc8wtfyzb/SOX4yTKHqhTt/NzytrVj55TBWA79iAT/pdnKHYj4l qZn9IC3JgxFoZ6s4+jtkL/zqYKnf212fPm8fMwZUxZtw3BtPdEuMtLzGOujeS04lrqSc TWIcYo1NcRIxV9LJN9g1df1zcYks+npWqS3firUoSxMg2c2MleJd8Wgijeka/Kj60UQV T+FA== X-Gm-Message-State: AOJu0YzVuzrUmH7Ez7U5O2vHSLXgrRIbqGYrKnc6Qrs/MKhujZESP/Ly XhuV3RAvB2TD1tssccxChjHsE11YvYKrd10foX1EuhxsfcK0G/Uw0IMS X-Gm-Gg: ASbGncuQspJ6xT8Jg03dBnlhKC1Rx0d6UYvha8FMlnQ1vWdf7x83b2b1iuEfii+v+3A 5ZvUpVTBC6G4UNrLhHRp/TlZmW89UVGc1TdA7oqlpgacm0TXNH33Dt2Ua1bqbl8BCtwSLteV4oh UXh/y90YnMzRIy+3qXQef6qb4YjkdLPE+GbuGwnCOjtMdzWlCjFMEwW9nNQCYWtgv7jP9+9/6uR 0AJ1MhF7poV2JTk+3ojnv6rOc8ubJcGg1rJtJBmbUggYyZpLvGSkLSzSnmBupqWT6mYj8fT3dcZ SJ0p58jqu4mjP6bI/ljGXPNkhxjAyix9qzS5yAI8S289DybQkbkbY5g5ot8uFZFoFotnLuXdpmC ++qpLeqEeF7fu7Dbzb4nHQMLm3xUu4ONcOz/UdHUJPAXCzgdxcDzFqBiH42EIbUrBCK9jcagGBq Um2P5v4mmJxOusUrQ4Ukcw X-Google-Smtp-Source: AGHT+IG4BLaPztMSKK02eYq++M8ot9QiM72qr8rQRlADAb5ebN8Sl5pVxDvRbnlNE07paSDD7V6tUQ== X-Received: by 2002:a05:651c:212a:b0:37b:9b58:dd04 with SMTP id 38308e7fff4ca-37ed1f9e022mr19491991fa.7.1765184153462; Mon, 08 Dec 2025 00:55:53 -0800 (PST) From: Dmitry Guryanov To: qemu-block@nongnu.org Cc: qemu-devel@nongnu.org, Alberto Garcia , Kevin Wolf , Hanna Reitz , Dmitry Guryanov Subject: [PATCH resend] block/throttle-groups: fix deadlock with iolimits and muliple iothreads Date: Mon, 8 Dec 2025 11:55:28 +0300 Message-ID: <20251208085528.890098-1-dmitry.guryanov@gmail.com> X-Mailer: git-send-email 2.52.0 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::22f; envelope-from=dmitry.guryanov@gmail.com; helo=mail-lj1-x22f.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Mon, 08 Dec 2025 09:20:46 -0500 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1765203708929158500 Content-Type: text/plain; charset="utf-8" Details: https://gitlab.com/qemu-project/qemu/-/issues/3144 The function schedule_next_request is called with tg->lock held and it may call throttle_group_co_restart_queue, which takes tgm->throttled_reqs_lock, qemu_co_mutex_lock may leave current coroutine if other iothread has taken the lock. If the next coroutine will call throttle_group_co_io_limits_intercept - it will try to take the mutex tg->lock which will never be released. Here is the backtrace of the iothread: Thread 30 (Thread 0x7f8aad1fd6c0 (LWP 24240) "IO iothread2"): #0 futex_wait (futex_word=3D0x5611adb7d828, expected=3D2, private=3D0) at= ../sysdeps/nptl/futex-internal.h:146 #1 __GI___lll_lock_wait (futex=3Dfutex@entry=3D0x5611adb7d828, private=3D= 0) at lowlevellock.c:49 #2 0x00007f8ab5a97501 in lll_mutex_lock_optimized (mutex=3D0x5611adb7d828= ) at pthread_mutex_lock.c:48 #3 ___pthread_mutex_lock (mutex=3D0x5611adb7d828) at pthread_mutex_lock.c= :93 #4 0x00005611823f5482 in qemu_mutex_lock_impl (mutex=3D0x5611adb7d828, fi= le=3D0x56118289daca "../block/throttle-groups.c", line=3D372) at ../util/qe= mu-thread-posix.c:94 #5 0x00005611822b0b39 in throttle_group_co_io_limits_intercept (tgm=3D0x5= 611af1bb4d8, bytes=3D4096, direction=3DTHROTTLE_READ) at ../block/throttle-= groups.c:372 #6 0x00005611822473b1 in blk_co_do_preadv_part (blk=3D0x5611af1bb490, off= set=3D15972311040, bytes=3D4096, qiov=3D0x7f8aa4000f98, qiov_offset=3D0, fl= ags=3DBDRV_REQ_REGISTERED_BUF) at ../block/block-backend.c:1354 #7 0x0000561182247fa0 in blk_aio_read_entry (opaque=3D0x7f8aa4005910) at = ../block/block-backend.c:1619 #8 0x000056118241952e in coroutine_trampoline (i0=3D-1543497424, i1=3D326= 50) at ../util/coroutine-ucontext.c:175 #9 0x00007f8ab5a56f70 in ?? () at ../sysdeps/unix/sysv/linux/x86_64/__sta= rt_context.S:66 from target:/lib64/libc.so.6 #10 0x00007f8aad1ef190 in ?? () #11 0x0000000000000000 in ?? () The lock is taken in line 386: (gdb) p tg.lock $1 =3D {lock =3D {__data =3D {__lock =3D 2, __count =3D 0, __owner =3D 2424= 0, __nusers =3D 1, __kind =3D 0, __spins =3D 0, __elision =3D 0, __list =3D= {__prev =3D 0x0, __next =3D 0x0}}, __size =3D "\002\000\000\000\000\000\000\000\260^\000\000\001", '\000' = , __align =3D 2}, file =3D 0x56118289daca "../block/throt= tle-groups.c", line =3D 386, initialized =3D true} The solution is to use tg->lock to protect both ThreadGroup fields and ThrottleGroupMember.throttled_reqs. It doesn't seem to be possible to use separate locks because we need to first manipulate ThrottleGroup fields, then schedule next coroutine using throttled_reqs and after than update token field from ThrottleGroup depending on the throttled_reqs state. Signed-off-by: Dmitry Guryanov --- block/throttle-groups.c | 21 ++++++--------------- include/block/throttle-groups.h | 1 - 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/block/throttle-groups.c b/block/throttle-groups.c index 66fdce9a90..bb01e52516 100644 --- a/block/throttle-groups.c +++ b/block/throttle-groups.c @@ -295,19 +295,15 @@ static bool throttle_group_schedule_timer(ThrottleGro= upMember *tgm, /* Start the next pending I/O request for a ThrottleGroupMember. Return wh= ether * any request was actually pending. * + * This assumes that tg->lock is held. + * * @tgm: the current ThrottleGroupMember * @direction: the ThrottleDirection */ static bool coroutine_fn throttle_group_co_restart_queue(ThrottleGroupMemb= er *tgm, ThrottleDirection= direction) { - bool ret; - - qemu_co_mutex_lock(&tgm->throttled_reqs_lock); - ret =3D qemu_co_queue_next(&tgm->throttled_reqs[direction]); - qemu_co_mutex_unlock(&tgm->throttled_reqs_lock); - - return ret; + return qemu_co_queue_next(&tgm->throttled_reqs[direction]);; } =20 /* Look for the next pending I/O request and schedule it. @@ -378,12 +374,8 @@ void coroutine_fn throttle_group_co_io_limits_intercep= t(ThrottleGroupMember *tgm /* Wait if there's a timer set or queued requests of this type */ if (must_wait || tgm->pending_reqs[direction]) { tgm->pending_reqs[direction]++; - qemu_mutex_unlock(&tg->lock); - qemu_co_mutex_lock(&tgm->throttled_reqs_lock); qemu_co_queue_wait(&tgm->throttled_reqs[direction], - &tgm->throttled_reqs_lock); - qemu_co_mutex_unlock(&tgm->throttled_reqs_lock); - qemu_mutex_lock(&tg->lock); + &tg->lock); tgm->pending_reqs[direction]--; } =20 @@ -410,15 +402,15 @@ static void coroutine_fn throttle_group_restart_queue= _entry(void *opaque) ThrottleDirection direction =3D data->direction; bool empty_queue; =20 + qemu_mutex_lock(&tg->lock); empty_queue =3D !throttle_group_co_restart_queue(tgm, direction); =20 /* If the request queue was empty then we have to take care of * scheduling the next one */ if (empty_queue) { - qemu_mutex_lock(&tg->lock); schedule_next_request(tgm, direction); - qemu_mutex_unlock(&tg->lock); } + qemu_mutex_unlock(&tg->lock); =20 g_free(data); =20 @@ -569,7 +561,6 @@ void throttle_group_register_tgm(ThrottleGroupMember *t= gm, read_timer_cb, write_timer_cb, tgm); - qemu_co_mutex_init(&tgm->throttled_reqs_lock); } =20 /* Unregister a ThrottleGroupMember from its group, removing it from the l= ist, diff --git a/include/block/throttle-groups.h b/include/block/throttle-group= s.h index 2355e8d9de..374c7c0215 100644 --- a/include/block/throttle-groups.h +++ b/include/block/throttle-groups.h @@ -36,7 +36,6 @@ typedef struct ThrottleGroupMember { AioContext *aio_context; /* throttled_reqs_lock protects the CoQueues for throttled requests. = */ - CoMutex throttled_reqs_lock; CoQueue throttled_reqs[THROTTLE_MAX]; =20 /* Nonzero if the I/O limits are currently being ignored; generally --=20 2.51.1