From patchwork Tue Jun 6 07:22:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerald Yang X-Patchwork-Id: 1790845 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=canonical.com header.i=@canonical.com header.a=rsa-sha256 header.s=20210705 header.b=s0n0y8NB; dkim-atps=neutral Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Qb24l33bMz20WK for ; Tue, 6 Jun 2023 17:23:03 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1q6R1r-0000T0-0e; Tue, 06 Jun 2023 07:22:55 +0000 Received: from smtp-relay-internal-1.internal ([10.131.114.114] helo=smtp-relay-internal-1.canonical.com) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1q6R1j-0000LP-3K for kernel-team@lists.ubuntu.com; Tue, 06 Jun 2023 07:22:47 +0000 Received: from mail-oi1-f199.google.com (mail-oi1-f199.google.com [209.85.167.199]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id DBB143F0EF for ; Tue, 6 Jun 2023 07:22:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1686036165; bh=vmJtpKhYUdAYezGaQzm5jogkHKsopiQMTe/e2rQ97s8=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=s0n0y8NBuXNxXuMzus++O3WMslusC/QXbEu6hpFtNhyrXASi9xolCPrdKw/tzw4v7 CewibmD5EZqSzbnFB083K98QfuRUz1dGKtEVzcjrcJQER7fpyLuXMqnwylaqHSx1aa MI00GK6fJo8F1ek6RV7uIV3nlShRUGUFkRJqqIlOWoqUIkNrQPIsb8OezOOT/gwAUx Ljp5IwZNuvhN+A3QAmkW21koTBLBH0lVVgrWo524y0V+qVlrYXHSFx+HjnzBVlDcT/ 4btVnSLl81umLHu74SQ1U+Tf4zlqLiZgsHMkh1bDfR2UQXegLZpUQwYJnXUXmSkUTq rhsnVvccQMT6A== Received: by mail-oi1-f199.google.com with SMTP id 5614622812f47-397f9039f68so5471856b6e.0 for ; Tue, 06 Jun 2023 00:22:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686036164; x=1688628164; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vmJtpKhYUdAYezGaQzm5jogkHKsopiQMTe/e2rQ97s8=; b=gR5FRm61WCEVWGowkx5FmEcceu0Kb7Ki4RWjRHjBdu+K5yeKrtXdBCvgO2GIdpHNgu QjSZkfIDfkS+dXHGEiySO6j+mo4R3wNs8pETYwfpfdEuMqNf+g+097+2JCaD4ptAhjff OqYgyHttmNkziKBMCLsPQVT7NPhgHzrG0aXYigt6vAe+2dcp2HwyqNxKdJdkZsrqaIT+ N06xGkF5pDsyLBYAeqsaHmANtX546gZRoxZuIK/7ayfuXMkzO9TG0Lfn2r6SKrzBW6WW HI1+pjDY8WqfmQ0rysAa3BUiC+pUyW2+mwhHtAAFPMWvSOd1VP/iabxID9JXRmJSruBV crwg== X-Gm-Message-State: AC+VfDwmHxWaglYd1axKbdDzNI4w3osQm9PxxcCa5r0260zfQCAmWBUA Im9XNhANWNw2G9Yw3yTd8APSi50ETP73jiSiT6tqXwncTtD+jD3oO33BYSy4Au7FGnamtkSb0Hk 59IvLi33ZvOqs1n8zIGJ1Bt2OWZFkhsVyns3Hc6s3cYjm4jloHw== X-Received: by 2002:a54:4588:0:b0:39b:8f0c:3936 with SMTP id z8-20020a544588000000b0039b8f0c3936mr1382748oib.27.1686036164364; Tue, 06 Jun 2023 00:22:44 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5eWAt30voJBtnc5mkFTnR49Yf17T70s0cCBJjby6+7LVYJaN9anXPDUmF0zHxeq/ALndnj3Q== X-Received: by 2002:a54:4588:0:b0:39b:8f0c:3936 with SMTP id z8-20020a544588000000b0039b8f0c3936mr1382739oib.27.1686036164098; Tue, 06 Jun 2023 00:22:44 -0700 (PDT) Received: from localhost.localdomain (220-135-31-21.hinet-ip.hinet.net. [220.135.31.21]) by smtp.gmail.com with ESMTPSA id j6-20020a170902758600b001a260b5319bsm7776954pll.91.2023.06.06.00.22.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jun 2023 00:22:43 -0700 (PDT) From: Gerald Yang To: kernel-team@lists.ubuntu.com Subject: [PATCH 3/6] sbitmap: Avoid leaving waitqueue in invalid state in __sbq_wake_up() Date: Tue, 6 Jun 2023 15:22:26 +0800 Message-Id: <20230606072229.3988976-4-gerald.yang@canonical.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230606072229.3988976-1-gerald.yang@canonical.com> References: <20230606072229.3988976-1-gerald.yang@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Jan Kara BugLink: https://bugs.launchpad.net/bugs/2022318 When __sbq_wake_up() decrements wait_cnt to 0 but races with someone else waking the waiter on the waitqueue (so the waitqueue becomes empty), it exits without reseting wait_cnt to wake_batch number. Once wait_cnt is 0, nobody will ever reset the wait_cnt or wake the new waiters resulting in possible deadlocks or busyloops. Fix the problem by making sure we reset wait_cnt even if we didn't wake up anybody in the end. Fixes: 040b83fcecfb ("sbitmap: fix possible io hung due to lost wakeup") Reported-by: Keith Busch Signed-off-by: Jan Kara Link: https://lore.kernel.org/r/20220908130937.2795-1-jack@suse.cz Signed-off-by: Jens Axboe (cherry picked from commit 48c033314f372478548203c583529f53080fd078) Signed-off-by: Gerald Yang --- lib/sbitmap.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/sbitmap.c b/lib/sbitmap.c index a39b1a877366..47cd8fb894ba 100644 --- a/lib/sbitmap.c +++ b/lib/sbitmap.c @@ -604,6 +604,7 @@ static bool __sbq_wake_up(struct sbitmap_queue *sbq) struct sbq_wait_state *ws; unsigned int wake_batch; int wait_cnt; + bool ret; ws = sbq_wake_ptr(sbq); if (!ws) @@ -614,12 +615,23 @@ static bool __sbq_wake_up(struct sbitmap_queue *sbq) * For concurrent callers of this, callers should call this function * again to wakeup a new batch on a different 'ws'. */ - if (wait_cnt < 0 || !waitqueue_active(&ws->wait)) + if (wait_cnt < 0) return true; + /* + * If we decremented queue without waiters, retry to avoid lost + * wakeups. + */ if (wait_cnt > 0) - return false; + return !waitqueue_active(&ws->wait); + /* + * When wait_cnt == 0, we have to be particularly careful as we are + * responsible to reset wait_cnt regardless whether we've actually + * woken up anybody. But in case we didn't wakeup anybody, we still + * need to retry. + */ + ret = !waitqueue_active(&ws->wait); wake_batch = READ_ONCE(sbq->wake_batch); /* @@ -648,7 +660,7 @@ static bool __sbq_wake_up(struct sbitmap_queue *sbq) sbq_index_atomic_inc(&sbq->wake_index); atomic_set(&ws->wait_cnt, wake_batch); - return false; + return ret; } void sbitmap_queue_wake_up(struct sbitmap_queue *sbq)