Received: by 2002:a05:6358:bb9e:b0:b9:5105:a5b4 with SMTP id df30csp4888210rwb; Tue, 6 Sep 2022 14:35:28 -0700 (PDT) X-Google-Smtp-Source: AA6agR6VSlj0AfrNzdInQVsjJSLJKSXBkj1lYsuUidwHBsASzTt2kCqNVcTVhMui4lTTypFpne4x X-Received: by 2002:a17:907:31ce:b0:742:1206:529e with SMTP id xf14-20020a17090731ce00b007421206529emr282186ejb.643.1662500127973; Tue, 06 Sep 2022 14:35:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662500127; cv=none; d=google.com; s=arc-20160816; b=0wL4VMUzquVqZ2bNsAwINHNQ3k38D12dCE1rw5OsPlD2dT83NvR9WzzkfW4EJOoZcw D5IJ/+sXrCZ75KoR/gaKKVE1xiCn0qIv55zkMJkFAxzukAJ/jAJtiCmBvafZCa8gYeau MkiLgC63lMZ7ErZolWSEvsk0Jg1vdyCIgvCQ/IELiIIWmjYbE9uKeMKWKzJuLbEcuvmC 8wjc+NjDWXIa91m1psWzp67oDO8X07aeoFF+nCp9r6MwawMyW6Es7Jegr06QRqUsR2VT O8i1kWK1CCDe2YhHi47oguZxwhQJiN2POz7foLtqqa8DJ7he1OmdfBgY3CFQpjsKOXTP IrlQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=zf1cV3wKOEaFrMu9TGP3c6Z/IoF1DOxU8iejdzkhUpw=; b=EfpeAVWQovqEE7e2LbXE4y17ZarHDrJOk3sw7GfXI360UPbW2ls7Q4eelZOdWQJK/p KX8Bv6on89UX2PUP88mtoSxvPhttXWU/PLKADfaacfpxuZIU0tdfAP0Ulo3UG4wjhYwU ECpw4ilPrL/nLdWH5IO+quLlxhQERl3gKRn4wlvsvgh3qsuRIHt6NbOzAuIXwktF4swq gtmQmzy2ZGO/lkpedrLIjodddhg+0bR0o87SSgJHiTfZq+Vd4ufKCrwO74QiIBG1n3tm yS3tEU1LcwVTiZG38vHaJx+g/4ESgSg4xSL6uMnpojomTLwN8ALjanzS7O5jImeX1e9Y ppzw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=uqf84Q+g; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hc32-20020a17090716a000b0073bf58c9a09si985620ejc.12.2022.09.06.14.35.00; Tue, 06 Sep 2022 14:35:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=uqf84Q+g; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229956AbiIFV20 (ORCPT + 99 others); Tue, 6 Sep 2022 17:28:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43622 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229939AbiIFV2Q (ORCPT ); Tue, 6 Sep 2022 17:28:16 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 20648BADB0; Tue, 6 Sep 2022 14:28:02 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 759D8B81A65; Tue, 6 Sep 2022 21:27:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8797DC433B5; Tue, 6 Sep 2022 21:27:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1662499675; bh=dCZeZmBCEnwc86WWX7Ly3DyLI7yhO7+PsPdUpFb40oU=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=uqf84Q+gXCZSzgXwZHheeS+21eyn/rsBhdyxH+30/5ZYwAsXM6O4hbRZcA8Ci+0sj dk3ZMM5KZMkJXn58MkyXhtOmQRLuBYKk6cQ0lbQlQbvLSR8Z9gcwvaEInubN2HgkvI kUQgeK6SmmuUIMQGmr9puVKqlS9Swc54V4EkJUqwdja/hoW2DvoYeOVue3j41MssL8 l1LHimEc09fOLUKhrDc/unzbcxWYiRrrGAeberAgNswIPKo1VMpg2khdZNUlALEqpO gOyMf1YTRVrRmKoJ28W4r8hj4u40SM6/wtjAOHrgvF9MpMmqPi4ryQN1Kh97h+M3Yx 0qtrlDES7RGOw== Date: Tue, 6 Sep 2022 15:27:51 -0600 From: Keith Busch To: Yu Kuai Cc: jack@suse.cz, axboe@kernel.dk, osandov@fb.com, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, yukuai3@huawei.com, yi.zhang@huawei.com Subject: Re: [PATCH] sbitmap: fix possible io hung due to lost wakeup Message-ID: References: <20220803121504.212071-1-yukuai1@huaweicloud.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220803121504.212071-1-yukuai1@huaweicloud.com> X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Aug 03, 2022 at 08:15:04PM +0800, Yu Kuai wrote: > wait_cnt = atomic_dec_return(&ws->wait_cnt); > - if (wait_cnt <= 0) { > - int ret; > + /* > + * 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)) > + return true; If wait_cnt is '0', but the waitqueue_active happens to be false due to racing with add_wait_queue(), this returns true so the caller will retry. The next atomic_dec will set the current waitstate wait_cnt < 0, which also forces an early return true. When does the wake up happen, or wait_cnt and wait_index get updated in that case? > - wake_batch = READ_ONCE(sbq->wake_batch); > + if (wait_cnt > 0) > + return false; > > - /* > - * Pairs with the memory barrier in sbitmap_queue_resize() to > - * ensure that we see the batch size update before the wait > - * count is reset. > - */ > - smp_mb__before_atomic(); > + wake_batch = READ_ONCE(sbq->wake_batch); > > - /* > - * For concurrent callers of this, the one that failed the > - * atomic_cmpxhcg() race should call this function again > - * to wakeup a new batch on a different 'ws'. > - */ > - ret = atomic_cmpxchg(&ws->wait_cnt, wait_cnt, wake_batch); > - if (ret == wait_cnt) { > - sbq_index_atomic_inc(&sbq->wake_index); > - wake_up_nr(&ws->wait, wake_batch); > - return false; > - } > + /* > + * Wake up first in case that concurrent callers decrease wait_cnt > + * while waitqueue is empty. > + */ > + wake_up_nr(&ws->wait, wake_batch); > > - return true; > - } > + /* > + * Pairs with the memory barrier in sbitmap_queue_resize() to > + * ensure that we see the batch size update before the wait > + * count is reset. > + * > + * Also pairs with the implicit barrier between decrementing wait_cnt > + * and checking for waitqueue_active() to make sure waitqueue_active() > + * sees result of the wakeup if atomic_dec_return() has seen the result > + * of atomic_set(). > + */ > + smp_mb__before_atomic(); > + > + /* > + * Increase wake_index before updating wait_cnt, otherwise concurrent > + * callers can see valid wait_cnt in old waitqueue, which can cause > + * invalid wakeup on the old waitqueue. > + */ > + sbq_index_atomic_inc(&sbq->wake_index); > + atomic_set(&ws->wait_cnt, wake_batch); > > return false; > } > -- > 2.31.1 >