Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2209040rwb; Thu, 8 Dec 2022 22:08:44 -0800 (PST) X-Google-Smtp-Source: AA0mqf7ZXNI7gH74ib5MiLC8rcJ/Fysp3pKyoBhf/ENswOhZdiDFf30azHuhZM+tQAeqeK2oUqWO X-Received: by 2002:a17:906:1146:b0:7ae:cda1:76d0 with SMTP id i6-20020a170906114600b007aecda176d0mr3525778eja.15.1670566124429; Thu, 08 Dec 2022 22:08:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670566124; cv=none; d=google.com; s=arc-20160816; b=KVjqyyVQ9q6ok61Zyu33bR3ir0EU7yl3MAcxbAVdXnrC8zwdx2FTHxJhHyuDOgBUcD RsC39j5yt9Cvgj3GfBJ0R4AGt5BZ4kXvXjBJA/DfcyWS/HTIJtGvUTBeN8X+pTOC/4za ABDerxHXM9kZCXqFhsn1WbmEGi/AOoQDtKIx7PUUwJtV9OHk9Rx8O/N+QBhVfp1QkhjG iEZRJLfxgrvTV3dIdydJmNUgcgv9QnJA9KhQtETlS2hblo1Rh0AUgB1SyLu6iZ0l2ZcC NaMoxUTXfiQBEYejM94hiUp0HBfqfw4fs2ZzjPMOXJyHipp4oh2QqvYdfj4rG7L12k3T xkvQ== 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=+omnI8YFazaFuTT/Zo/BmkjQOabYww7+3zysg2utdCM=; b=BNUX4fU0eUeJjMSwXaXgKZ54UozEryjEpSn3qTfrJL+rH7v2wiBSbP/sBSMNBhKBMw 44SOqKH8fmWZGpr9EPDy/kfN+75AjpyL53o2JUwSWhl93vK2HH91Di3O0b8xAYVZLlyH xOdCCwFkNaNKS/Vj66fbowGwVXkaEvN8Se0CDu8aP8l5taguF75EkKmtq/62kkNyBE4Z 7mcjIKXHB1u14EYyCMGbRkiP9sL+/sts0mQf8aA6QadP5mnyxukDkFJnNT6OuwpYawbd SyyehE/5+Obj45imgW5M3YpQ9if5SzPikR+1UBjvtGJ7xpBf4UYXTztQOm3ofr0WV3Qo Fb9Q== 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 he35-20020a1709073da300b007b273d1f664si523704ejc.128.2022.12.08.22.08.26; Thu, 08 Dec 2022 22:08:44 -0800 (PST) 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 S229662AbiLIF3o (ORCPT + 73 others); Fri, 9 Dec 2022 00:29:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51450 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229460AbiLIF3m (ORCPT ); Fri, 9 Dec 2022 00:29:42 -0500 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AB8597125E; Thu, 8 Dec 2022 21:29:40 -0800 (PST) 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 sin.source.kernel.org (Postfix) with ESMTPS id 19EA6CE27D3; Fri, 9 Dec 2022 05:29:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0BA67C433D2; Fri, 9 Dec 2022 05:29:35 +0000 (UTC) Date: Fri, 9 Dec 2022 00:29:32 -0500 From: Steven Rostedt To: Ross Zwisler Cc: LKML , Linux Trace Kernel , Masami Hiramatsu , Andrew Morton , Mathieu Desnoyers , paulmck@kernel.org, Joel Fernandes , Tom Zanussi Subject: Re: [PATCH v2] tracing: Add trace_trigger kernel command line option Message-ID: <20221209002932.2e9e89e2@gandalf.local.home> In-Reply-To: References: <20221020210056.0d8d0a5b@gandalf.local.home> <20221208183945.1de18843@gandalf.local.home> 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=-6.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_PASS 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 Thu, 8 Dec 2022 17:19:21 -0700 Ross Zwisler wrote: > I think that this is because the code to allocate the snapshot buffer uses > workqueues (ring_buffer_resize() calls schedule_work_on() then > wait_for_completion()), but at this point during the init process the > workqueues are up enough that we can enqueue entries, but they are not yet > doing work. Thanks for the report. > > start_kernel() { > ... > > /* > * Allow workqueue creation and work item queueing/cancelling > * early. Work item execution depends on kthreads and starts after > * workqueue_init(). > */ > workqueue_init_early(); > ... > > /* Trace events are available after this */ > trace_init(); // here is where we try and allocate the snapshot > > ... > > arch_call_rest_init(); > rest_init() > kernel_init() > kernel_init_freeable() > workqueue_init() > } > > I'm guessing the best we can do here is just disallow snapshot triggers via > the command line option, so that others don't cut themselves on this sharp > corner? Other ideas? After debugging it, it's because that code path expects to be called with interrupts enabled, but this early in boot up, interrupts haven't been enabled yet and should not be. > [ 0.178253] > [ 0.178253] > [ 0.178254] ? _raw_spin_unlock_irq+0x11/0x40 raw_spin_lock_irq() enables interrupts regardless. > [ 0.178256] ? wait_for_completion+0x7e/0x160 > [ 0.178258] ? ring_buffer_resize+0x320/0x450 > [ 0.178262] ? resize_buffer_duplicate_size+0x38/0xe0 > [ 0.178264] ? tracing_alloc_snapshot_instance+0x23/0x40 > [ 0.178266] ? register_snapshot_trigger+0x16/0x40 > [ 0.178269] ? event_trigger_parse+0x113/0x160 > [ 0.178272] ? trigger_process_regex+0xb8/0x100 > [ 0.178274] ? __trace_early_add_events+0xb8/0x140 > [ 0.178275] ? trace_event_init+0xcc/0x2dd > [ 0.178278] ? start_kernel+0x4a9/0x713 > [ 0.178281] ? secondary_startup_64_no_verify+0xce/0xdb This appears to fix it. -- Steve diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 843818ee4814..ef4da331ff61 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -2062,8 +2062,10 @@ rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer) { struct list_head *pages = &cpu_buffer->new_pages; int retries, success; + unsigned long flags; - raw_spin_lock_irq(&cpu_buffer->reader_lock); + /* Can be called at early boot up, where interrupts have not been enabled */ + raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); /* * We are holding the reader lock, so the reader page won't be swapped * in the ring buffer. Now we are racing with the writer trying to @@ -2120,7 +2122,7 @@ rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer) * tracing */ RB_WARN_ON(cpu_buffer, !success); - raw_spin_unlock_irq(&cpu_buffer->reader_lock); + raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); /* free pages if they weren't inserted */ if (!success) { @@ -2171,6 +2173,7 @@ int ring_buffer_resize(struct trace_buffer *buffer, unsigned long size, { struct ring_buffer_per_cpu *cpu_buffer; unsigned long nr_pages; + unsigned long flags; int cpu, err; /* @@ -2248,8 +2251,19 @@ int ring_buffer_resize(struct trace_buffer *buffer, unsigned long size, rb_update_pages(cpu_buffer); cpu_buffer->nr_pages_to_update = 0; } else { - schedule_work_on(cpu, - &cpu_buffer->update_pages_work); + /* + * Run directly if possible. + * Save flags as this can be called at early boot up. + */ + local_irq_save(flags); + if (cpu != smp_processor_id()) { + local_irq_restore(flags); + schedule_work_on(cpu, + &cpu_buffer->update_pages_work); + } else { + update_pages_handler(&cpu_buffer->update_pages_work); + local_irq_restore(flags); + } } } @@ -2298,9 +2312,20 @@ int ring_buffer_resize(struct trace_buffer *buffer, unsigned long size, if (!cpu_online(cpu_id)) rb_update_pages(cpu_buffer); else { - schedule_work_on(cpu_id, - &cpu_buffer->update_pages_work); - wait_for_completion(&cpu_buffer->update_done); + /* + * Run directly if possible. + * Save flags as this can be called at early boot up. + */ + local_irq_save(flags); + if (cpu_id == smp_processor_id()) { + update_pages_handler(&cpu_buffer->update_pages_work); + local_irq_restore(flags); + } else { + local_irq_restore(flags); + schedule_work_on(cpu_id, + &cpu_buffer->update_pages_work); + wait_for_completion(&cpu_buffer->update_done); + } } cpu_buffer->nr_pages_to_update = 0;