Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp3786911rwl; Mon, 10 Apr 2023 00:39:00 -0700 (PDT) X-Google-Smtp-Source: AKy350ZsUOxgOwfGbZ8/oSd8SG2uV42Dj4B18wfvuzrj58GGagFq1A9f4fgWV9xAv0iKrshcbz1r X-Received: by 2002:a62:6455:0:b0:636:afb9:cd5a with SMTP id y82-20020a626455000000b00636afb9cd5amr3586806pfb.13.1681112339963; Mon, 10 Apr 2023 00:38:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681112339; cv=none; d=google.com; s=arc-20160816; b=JhHSY4mmidhG8lqWav3Cx/f+qIUeeGntnKTnXl3MNToas80N+JcM/dd6/JAsOoTlc6 DgzNQY3Npz2Lh7asVbspvTpFxD8V8UmgQ3j8OFzDkhLjhtpAXFW5g1HgmAe/3JtFQUaG Fjc6qUiKsIpBX6lZ7MZr+49f8tuLRW+FfeYl5xWhFK2VMJFy1ooymoT3i6Bgzjhbnjzm nxWEgBX9jXAgN8X0U25pKTzOy3jS2TQBzMwkFE7R31ThlyrSHAY6PLTia1MmYB6b8ftj ePdmsx+IjKzW/wQWJQ5SVX16JGyrxyQGuPq3q3NS/Vah9PJD9b2cczbRaSwUCT7XWS1m n6cw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=7uzc4Gmc54T760tvEhSz8TdAybgvY+nHPJ6ttMqGCTg=; b=gDvA9+rc1tksn5D57s+nxfjyzoggr0jQ5nXsh89KUA3uhdnGaS8pN4AhrhDUV/MkXc Pod09e24gEh7YvRfzrFBUiU4EhiYNFHx1gUoRc00PcDKeVyO9izXibBFI2CCxwC2lWLd KUGvDZnnrCNHYhKPVp+yJV7aA5HrTRvW3XeubhJOCo8ak/cX24nXYjnCpEhAqPhsnqGS xEdf9DZtAhUKZ8VW7vx+CVSGZ31XrEo9HHCqP6c5sFIJ5puJ9bHoEmc12GfkWG9vTY8D xJNIdOo2/FQPYFsjIIb9YB0QzqeWPnmVbmmTnv5ZvZ7b4c+lXmq1DuTRtYagXQPGhzGn khVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@mediatek.com header.s=dk header.b=OebQoBXa; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mediatek.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y84-20020a626457000000b0063957ddc9e3si784402pfb.12.2023.04.10.00.38.48; Mon, 10 Apr 2023 00:38:59 -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=@mediatek.com header.s=dk header.b=OebQoBXa; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mediatek.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229783AbjDJHfr (ORCPT + 99 others); Mon, 10 Apr 2023 03:35:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46404 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229642AbjDJHfp (ORCPT ); Mon, 10 Apr 2023 03:35:45 -0400 Received: from mailgw02.mediatek.com (unknown [210.61.82.184]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E936A2708; Mon, 10 Apr 2023 00:35:40 -0700 (PDT) X-UUID: 47d0307ed77211edb6b9f13eb10bd0fe-20230410 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=7uzc4Gmc54T760tvEhSz8TdAybgvY+nHPJ6ttMqGCTg=; b=OebQoBXaTyU94lHq2McKOGd08J4KFwMjblE9k3waiuqevD1fLF2Hd2mTOE/yxC5WZ8iosXPh19m2PXc5U97ICcFRnRGUExGOE537QMfQ8KCzsd0f9Yyevg+BDZ0sMzKkVLgFXmDSatzxWTciECmZZQ7txKIU0DcklVh/SBEvhTY=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.22,REQID:5ef009e6-f3bb-4b24-a4a5-5bf4026b70c4,IP:0,U RL:0,TC:0,Content:-25,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTIO N:release,TS:-25 X-CID-META: VersionHash:120426c,CLOUDID:a90abab5-beed-4dfc-bd9c-e1b22fa6ccc4,B ulkID:nil,BulkQuantity:0,Recheck:0,SF:102,TC:nil,Content:0,EDM:-3,IP:nil,U RL:11|1,File:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0 X-CID-BVR: 0 X-CID-BAS: 0,_,0,_ X-UUID: 47d0307ed77211edb6b9f13eb10bd0fe-20230410 Received: from mtkmbs11n2.mediatek.inc [(172.21.101.187)] by mailgw02.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 331443076; Mon, 10 Apr 2023 15:35:34 +0800 Received: from mtkmbs13n1.mediatek.inc (172.21.101.193) by mtkmbs10n2.mediatek.inc (172.21.101.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.25; Mon, 10 Apr 2023 15:35:34 +0800 Received: from mtksdccf07.mediatek.inc (172.21.84.99) by mtkmbs13n1.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.25 via Frontend Transport; Mon, 10 Apr 2023 15:35:34 +0800 From: Tze-nan Wu To: , CC: , , , , , , AngeloGioacchino Del Regno , "Paul E. McKenney" , , , , Subject: [PATCH v3] ring-buffer: Prevent inconsistent operation on cpu_buffer->resize_disabled Date: Mon, 10 Apr 2023 15:35:08 +0800 Message-ID: <20230410073512.13362-1-Tze-nan.Wu@mediatek.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20230409024616.31099-1-Tze-nan.Wu@mediatek.com> References: <20230409024616.31099-1-Tze-nan.Wu@mediatek.com> MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS, UNPARSEABLE_RELAY autolearn=unavailable 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 Write to buffer_size_kb can permanently fail, due to cpu_online_mask may changed between two for_each_online_buffer_cpu loops. The number of increasing and decreasing on cpu_buffer->resize_disable may be inconsistent, leading that the resize_disabled in some CPUs becoming none zero after ring_buffer_reset_online_cpus return. This issue can be reproduced by "echo 0 > trace" while hotplugging cpu. After reproducing success, we can find out buffer_size_kb will not be functional anymore. Prevent the two "loops" in this function from iterating through different online cpus by copying cpu_online_mask at the entry of the function. Changes from v1 to v3: Declare the cpumask variable statically rather than dynamically. Changes from v2 to v3: Considering holding cpu_hotplug_lock too long because of the synchronize_rcu(), maybe it's better to prevent the issue by copying cpu_online_mask at the entry of the function as V1 does, instead of using cpus_read_lock(). Link: https://lore.kernel.org/lkml/20230408052226.25268-1-Tze-nan.Wu@mediatek.com/ Link: https://lore.kernel.org/oe-kbuild-all/202304082051.Dp50upfS-lkp@intel.com/ Link: https://lore.kernel.org/oe-kbuild-all/202304081615.eiaqpbV8-lkp@intel.com/ Cc: stable@vger.kernel.org Cc: npiggin@gmail.com Fixes: b23d7a5f4a07 ("ring-buffer: speed up buffer resets by avoiding synchronize_rcu for each CPU") Reported-by: kernel test robot Reviewed-by: Cheng-Jui Wang Signed-off-by: Tze-nan Wu --- kernel/trace/ring_buffer.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 76a2d91eecad..dc758930dacb 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -288,9 +288,6 @@ EXPORT_SYMBOL_GPL(ring_buffer_event_data); #define for_each_buffer_cpu(buffer, cpu) \ for_each_cpu(cpu, buffer->cpumask) -#define for_each_online_buffer_cpu(buffer, cpu) \ - for_each_cpu_and(cpu, buffer->cpumask, cpu_online_mask) - #define TS_SHIFT 27 #define TS_MASK ((1ULL << TS_SHIFT) - 1) #define TS_DELTA_TEST (~TS_MASK) @@ -5353,12 +5350,19 @@ EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu); void ring_buffer_reset_online_cpus(struct trace_buffer *buffer) { struct ring_buffer_per_cpu *cpu_buffer; + cpumask_t reset_online_cpumask; int cpu; + /* + * Record cpu_online_mask here to make sure we iterate through the same + * online CPUs in the following two loops. + */ + cpumask_copy(&reset_online_cpumask, cpu_online_mask); + /* prevent another thread from changing buffer sizes */ mutex_lock(&buffer->mutex); - for_each_online_buffer_cpu(buffer, cpu) { + for_each_cpu_and(cpu, buffer->cpumask, &reset_online_cpumask) { cpu_buffer = buffer->buffers[cpu]; atomic_inc(&cpu_buffer->resize_disabled); @@ -5368,7 +5372,7 @@ void ring_buffer_reset_online_cpus(struct trace_buffer *buffer) /* Make sure all commits have finished */ synchronize_rcu(); - for_each_online_buffer_cpu(buffer, cpu) { + for_each_cpu_and(cpu, buffer->cpumask, &reset_online_cpumask) { cpu_buffer = buffer->buffers[cpu]; reset_disabled_cpu_buffer(cpu_buffer); -- 2.18.0