Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp5775582rwl; Tue, 11 Apr 2023 09:47:11 -0700 (PDT) X-Google-Smtp-Source: AKy350YGn2EVHeXYUWk2IV5AAs8YNB6yFYe2fiGJcQM6tgADPfTa7chePzKSAbZKJKTMTuKaRLGy X-Received: by 2002:a17:906:2a48:b0:92d:145a:6115 with SMTP id k8-20020a1709062a4800b0092d145a6115mr11243937eje.38.1681231631235; Tue, 11 Apr 2023 09:47:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681231631; cv=none; d=google.com; s=arc-20160816; b=afUqVFSxFNuAu4xeYVY8czLu0WpteWaGTv/HUb75IfyBoY6eQB548vWdy7z6pxSiw1 aeQdoqHWBC2O+GrI8+nGqYqJ/LsN1CK84MhKtEsA/FEO13R7pO6SaRIHGfU01ejF3FlL S9DPCGcfbkUXzEI5QcY/ocVw3GEO+Q3U4fUGWQmq0CVDglzyfQevOf+cNV0hYCzzT4ck j8lJ+qGCa1LP6Z3IjNnpWDHRP+8azc1yYiVswaiqHkAfTLIv9+UZqoLk/t1YKElBJJYD Z02fvUwcDei2F0SzlyJxgFb9QEFJDjxfxJbUTIzCWEwHxj8/8TU9Lu0GtlGEvXI8t6Uc Py0g== 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 :references:in-reply-to:message-id:subject:cc:to:from:date; bh=Zrg/TOaIiFsc/suyI10+fn8TAAssxRzE/7Q1Lt+MH/4=; b=Udb101ngLj9B4m7Rr2zyg6adDVhPuCSQkpsqj7uT3YqpoTr2u0r+99nFbPe6iA2O52 y/p18k98UZ6wm1ZaxRX6eei7pmEvLVCA3rEIRQTmv8rq9H24YZ5zWuToCxyuJYCU+PiQ ju+zwEBhaCgjnVRu+Sv2esIol/yTE9qiadIRqYI7aoiLeXickm5fEnqyfqKC+8vfivs6 jhYOFBz0kS3GjLs9JpStMylWMJjmRaPr3rIGfkakGDNsPMG/WhcjFPgse7AC+omTcEim NIBnqSGfr8xH3GVGghrdkJYqEgC96nyl9imCCQTWBZ6xTJ5JP0vR9pZAAqRhycEJ8Ep4 mLyg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id kt1-20020a170906aac100b0094a9116306fsi1494219ejb.471.2023.04.11.09.46.45; Tue, 11 Apr 2023 09:47:11 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230075AbjDKQog (ORCPT + 99 others); Tue, 11 Apr 2023 12:44:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48742 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229995AbjDKQoc (ORCPT ); Tue, 11 Apr 2023 12:44:32 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 24E6649F6; Tue, 11 Apr 2023 09:44:21 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 1A1D662956; Tue, 11 Apr 2023 16:44:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1C903C433D2; Tue, 11 Apr 2023 16:44:05 +0000 (UTC) Date: Tue, 11 Apr 2023 12:44:03 -0400 From: Steven Rostedt To: Tze-nan Wu Cc: , , , , , , AngeloGioacchino Del Regno , "Paul E. McKenney" , , , , Subject: Re: [PATCH v3] ring-buffer: Prevent inconsistent operation on cpu_buffer->resize_disabled Message-ID: <20230411124403.2a31e12d@gandalf.local.home> In-Reply-To: <20230410073512.13362-1-Tze-nan.Wu@mediatek.com> References: <20230409024616.31099-1-Tze-nan.Wu@mediatek.com> <20230410073512.13362-1-Tze-nan.Wu@mediatek.com> X-Mailer: Claws Mail 3.17.8 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.0 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS 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 Please have each new patch be a new thread, and not a Cc to the previous version of the patch. As it makes it hard to find in INBOXs. On Mon, 10 Apr 2023 15:35:08 +0800 Tze-nan Wu wrote: > 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. > The "Changes from" need to go below the '---', otherwise they are added to the git commit (we don't want it there). > 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 > --- This is where the "Changes from" go. And since this patch is not suppose to be a Cc. But since it's still good to have a link to it. You could do: Changes from v2 to v3: https://lore.kernel.org/linux-trace-kernel/20230409024616.31099-1-Tze-nan.Wu@mediatek.com/ 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(). Where the previous version changes has the lore link to the previous patch, in case someone wants to look at it. > 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; It's usually considered bad form to put a cpumask on the stack. As it can be 128 bytes for a machine with 1024 CPUs (and yes they do exist). Also, the mask size is set to NR_CPUS not the actual size, so you do not even need to have it that big. > 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); Anyway, we don't need to modify any of the above, and just do the following instead of atomic_inc(): #define RESET_BIT (1 << 30) atomic_add(&cpu_buffer->resize_disabled, RESET_BIT); > @@ -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]; Then here we can do: /* * If a CPU came online during the synchronize_rcu(), then * ignore it. */ if (!atomic_read(&cpu_buffer->resize_disabled) & RESET_BIT)) continue; atomic_sub(&cpu_buffer->resize_disabled, RESET_BIT); As the resize_disabled only needs to be set to something to make it disabled. -- Steve > > reset_disabled_cpu_buffer(cpu_buffer);