Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp752698pxb; Wed, 27 Jan 2021 22:01:27 -0800 (PST) X-Google-Smtp-Source: ABdhPJzWHtKpiYFmtvpx5+oZ/UQkVUTPxgaCzCMjdXKSj9toF+tv83m6hSEpSO4X/ksqFDVGRcm+ X-Received: by 2002:a17:906:c793:: with SMTP id cw19mr9313064ejb.246.1611813687528; Wed, 27 Jan 2021 22:01:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611813687; cv=none; d=google.com; s=arc-20160816; b=ZCKD2wdYHfeygn6xcjaptjLI/HlB8fY2Qewpi3T1s5dEzwNqhZ2XOC8i+78S5rGj9h imfkL+/G/nlUe6UVzR89B6WX375kRuvGoO69/wmNPAPSGGKn8+oIY5pnOd3MlseyfXEs XB/ziJTipV+qYlgYIhnKaWKs6ky6LlmxAF221tn3RjAy8K+xb7nrnBuZZZRYphZMhjhq TGT4m4MTQFaRkkPyB2esu+/jFtpLBScAj9ERe1m4Tdb2oyrEr30VYP5Y+peE413H5zyp wiXzY17xRor2lhIY3PnTJGovBIWMcUrNZL8+T4Ztx0EkLl7z8UDfSEPcbd6dtYO3tTpl AegA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=v4y+raquUPwc1i699NxjlXeODdzlW6KUFKCf6sheh5M=; b=gjdKXacgPDzRHLvVX0Y6rPqXjydQ0vrP9hpWJgLW1JVlSApkmiBeEske4dvwRI8whX RYKtj/0+WFiynDxZYK+VZLK4J7RJ8VzdoSE1T4EThJ+vWQJ1EcSd66+vDuWqEQhTMriK yU8L8YFISHzt5qNolVeNZ3SWKRrOGLKfUawyhDusm7Ep3CBh4qQtFIF6giO8irjeTi7f 0HUddAQD1mGsta47djKm0txt+Y5QEwqftJNfsXBgJXUBfhOFd4syLyPekQNpHHoLmM1F r0DLZh435XSaQUQIrGkOkip4raqxVZ0WoTOdRCHIR/mvaHs3v4AW8rLuENbLi0L5+S6f QaGQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id pk16si1939429ejb.114.2021.01.27.22.01.03; Wed, 27 Jan 2021 22:01:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229953AbhA1F7F (ORCPT + 99 others); Thu, 28 Jan 2021 00:59:05 -0500 Received: from out30-54.freemail.mail.aliyun.com ([115.124.30.54]:37263 "EHLO out30-54.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229847AbhA1F7E (ORCPT ); Thu, 28 Jan 2021 00:59:04 -0500 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R191e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e01424;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=7;SR=0;TI=SMTPD_---0UN6om1d_1611813497; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0UN6om1d_1611813497) by smtp.aliyun-inc.com(127.0.0.1); Thu, 28 Jan 2021 13:58:18 +0800 From: Baolin Wang To: axboe@kernel.dk, tj@kernel.org Cc: joseph.qi@linux.alibaba.com, baolin.wang@linux.alibaba.com, linux-block@vger.kernel.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3] blk-cgroup: Use cond_resched() when destroy blkgs Date: Thu, 28 Jan 2021 13:58:15 +0800 Message-Id: <0be1a0a03367f7230497a2e7a5ed47d2a2d5ae1a.1611809091.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 1.8.3.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On !PREEMPT kernel, we can get below softlockup when doing stress testing with creating and destroying block cgroup repeatly. The reason is it may take a long time to acquire the queue's lock in the loop of blkcg_destroy_blkgs(), or the system can accumulate a huge number of blkgs in pathological cases. We can add a need_resched() check on each loop and release locks and do cond_resched() if true to avoid this issue, since the blkcg_destroy_blkgs() is not called from atomic contexts. [ 4757.010308] watchdog: BUG: soft lockup - CPU#11 stuck for 94s! [ 4757.010698] Call trace: [ 4757.010700]  blkcg_destroy_blkgs+0x68/0x150 [ 4757.010701]  cgwb_release_workfn+0x104/0x158 [ 4757.010702]  process_one_work+0x1bc/0x3f0 [ 4757.010704]  worker_thread+0x164/0x468 [ 4757.010705]  kthread+0x108/0x138 Suggested-by: Tejun Heo Signed-off-by: Baolin Wang --- Changes from v2: - Simplify logics suggested by Jens. Changes from v1: - Add might_sleep() in blkcg_destroy_blkgs(). - Add an explicitly need_resched() check before releasing lock. - Add some comments. --- block/blk-cgroup.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c index 02ce205..4b4fcb5 100644 --- a/block/blk-cgroup.c +++ b/block/blk-cgroup.c @@ -1016,6 +1016,8 @@ static void blkcg_css_offline(struct cgroup_subsys_state *css) */ void blkcg_destroy_blkgs(struct blkcg *blkcg) { + might_sleep(); + spin_lock_irq(&blkcg->lock); while (!hlist_empty(&blkcg->blkg_list)) { @@ -1023,14 +1025,20 @@ void blkcg_destroy_blkgs(struct blkcg *blkcg) struct blkcg_gq, blkcg_node); struct request_queue *q = blkg->q; - if (spin_trylock(&q->queue_lock)) { - blkg_destroy(blkg); - spin_unlock(&q->queue_lock); - } else { + if (need_resched() || !spin_trylock(&q->queue_lock)) { + /* + * Given that the system can accumulate a huge number + * of blkgs in pathological cases, check to see if we + * need to rescheduling to avoid softlockup. + */ spin_unlock_irq(&blkcg->lock); - cpu_relax(); + cond_resched(); spin_lock_irq(&blkcg->lock); + continue; } + + blkg_destroy(blkg); + spin_unlock(&q->queue_lock); } spin_unlock_irq(&blkcg->lock); -- 1.8.3.1