Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp808743rwi; Wed, 19 Oct 2022 03:17:11 -0700 (PDT) X-Google-Smtp-Source: AMsMyM79191WY/aWmdRc+Bc7srEKmBGG5keNE4Xf3Q8zMUgG7e4Bla+b9vCdwjUODv7zR/r6kplI X-Received: by 2002:a17:903:22d0:b0:185:5421:a5d3 with SMTP id y16-20020a17090322d000b001855421a5d3mr7582228plg.119.1666174631143; Wed, 19 Oct 2022 03:17:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666174631; cv=none; d=google.com; s=arc-20160816; b=UTNm0Fx1qDbVGJJLjP9jAPFrI8ISoaThm6hSvbGbHsp2npyW61guS8VmJ3QraVg0Sf IJlmwfngUCqthOegd+rYlfA2fC4RS7az/AT43YC4SDn9FeN9IRyG+dZDuWz+aqnbfIgc q+UJUzRvwMmXuKY8np4aXjNRKIWIPw/NouPryut5ZJBJYSU/VgVUg0cGftbXCVSeayP5 bTHbpsz9F+BrP4ay8AE0dthAGJbEE4oYXSnTGkhDrAkBYAqimkEM+yeyc6Zb3Nk5ZzYH xLTSlfyDpoGe/vWwlhKEML+DG1ZbDDds8CCF3/aKTocfSMM8W4V2coDYSrQRbN+CJDwu m2Nw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=p/npWSUVRZlLb9Df+tuvItHwazs/tdMHZLW5QO4s0qE=; b=jTJoRHdFeF/YM7tc3s6K3R45dyLkE9+9FK0LifiGMmEWnP6XUgg26pL+mVTM8MnK3w fgNpkcEJcGrU3Yu6IVKOfrFe4YDn7hKi1SPL2yA425NJ5WuBhoO4iLaDU8f9WwGxWoaR FsyndZAWQIyM2SxkmIU2sH5odWe3xpcUcH0tOK/D40faY7KyCoP+POFeQJTgcKdJfYzI JbKUSJC8AYd4hSHZWDDgGCjeTuLkl5Hm0dIjTEpVEWevl3YkFK7bUFBQkLVFvego6cBG Sh2D5r5x3bWt08Q+5XbfQrVKvabcnW5xMBFnwgilxG5JE8oG6F2gZMHgbDisonROyFoC G2hA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=iqu++Smo; 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=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s3-20020a170902c64300b001865a9586c1si293460pls.59.2022.10.19.03.16.59; Wed, 19 Oct 2022 03:17: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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=iqu++Smo; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232421AbiJSJIp (ORCPT + 99 others); Wed, 19 Oct 2022 05:08:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232447AbiJSJGR (ORCPT ); Wed, 19 Oct 2022 05:06:17 -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 B873C17061; Wed, 19 Oct 2022 01:59:32 -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 C80006183C; Wed, 19 Oct 2022 08:46:50 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D9E4CC433C1; Wed, 19 Oct 2022 08:46:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666169210; bh=esuUoapq9T1x4Pi6HRqvIWsZGok3j+U9rcfDo7+hQDw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iqu++SmoOF93qofrvE8tOHe567ZOX0lSAe5x+f9gCy96MwQqd7jYH5zHlmXCrcNaF IErcH+yhL2WnvU1b35Tdg9kcT+UgNU4TvMCsYuSaQYRgWbpZQ2rfScXRAhuY6rbm/d 5QBCe0nyVJt3zxsqNjrHcJOxi09LGsIkYXLlejtQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng , Steven Rostedt , Waiman Long Subject: [PATCH 6.0 158/862] tracing: Disable interrupt or preemption before acquiring arch_spinlock_t Date: Wed, 19 Oct 2022 10:24:05 +0200 Message-Id: <20221019083256.957306379@linuxfoundation.org> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221019083249.951566199@linuxfoundation.org> References: <20221019083249.951566199@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.4 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 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 From: Waiman Long commit c0a581d7126c0bbc96163276f585fd7b4e4d8d0e upstream. It was found that some tracing functions in kernel/trace/trace.c acquire an arch_spinlock_t with preemption and irqs enabled. An example is the tracing_saved_cmdlines_size_read() function which intermittently causes a "BUG: using smp_processor_id() in preemptible" warning when the LTP read_all_proc test is run. That can be problematic in case preemption happens after acquiring the lock. Add the necessary preemption or interrupt disabling code in the appropriate places before acquiring an arch_spinlock_t. The convention here is to disable preemption for trace_cmdline_lock and interupt for max_lock. Link: https://lkml.kernel.org/r/20220922145622.1744826-1-longman@redhat.com Cc: Peter Zijlstra Cc: Ingo Molnar Cc: Will Deacon Cc: Boqun Feng Cc: stable@vger.kernel.org Fixes: a35873a0993b ("tracing: Add conditional snapshot") Fixes: 939c7a4f04fc ("tracing: Introduce saved_cmdlines_size file") Suggested-by: Steven Rostedt Signed-off-by: Waiman Long Signed-off-by: Steven Rostedt (Google) Signed-off-by: Greg Kroah-Hartman --- kernel/trace/trace.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -1193,12 +1193,14 @@ void *tracing_cond_snapshot_data(struct { void *cond_data = NULL; + local_irq_disable(); arch_spin_lock(&tr->max_lock); if (tr->cond_snapshot) cond_data = tr->cond_snapshot->cond_data; arch_spin_unlock(&tr->max_lock); + local_irq_enable(); return cond_data; } @@ -1334,9 +1336,11 @@ int tracing_snapshot_cond_enable(struct goto fail_unlock; } + local_irq_disable(); arch_spin_lock(&tr->max_lock); tr->cond_snapshot = cond_snapshot; arch_spin_unlock(&tr->max_lock); + local_irq_enable(); mutex_unlock(&trace_types_lock); @@ -1363,6 +1367,7 @@ int tracing_snapshot_cond_disable(struct { int ret = 0; + local_irq_disable(); arch_spin_lock(&tr->max_lock); if (!tr->cond_snapshot) @@ -1373,6 +1378,7 @@ int tracing_snapshot_cond_disable(struct } arch_spin_unlock(&tr->max_lock); + local_irq_enable(); return ret; } @@ -2200,6 +2206,11 @@ static size_t tgid_map_max; #define SAVED_CMDLINES_DEFAULT 128 #define NO_CMDLINE_MAP UINT_MAX +/* + * Preemption must be disabled before acquiring trace_cmdline_lock. + * The various trace_arrays' max_lock must be acquired in a context + * where interrupt is disabled. + */ static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED; struct saved_cmdlines_buffer { unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1]; @@ -2412,7 +2423,11 @@ static int trace_save_cmdline(struct tas * the lock, but we also don't want to spin * nor do we want to disable interrupts, * so if we miss here, then better luck next time. + * + * This is called within the scheduler and wake up, so interrupts + * had better been disabled and run queue lock been held. */ + lockdep_assert_preemption_disabled(); if (!arch_spin_trylock(&trace_cmdline_lock)) return 0; @@ -5890,9 +5905,11 @@ tracing_saved_cmdlines_size_read(struct char buf[64]; int r; + preempt_disable(); arch_spin_lock(&trace_cmdline_lock); r = scnprintf(buf, sizeof(buf), "%u\n", savedcmd->cmdline_num); arch_spin_unlock(&trace_cmdline_lock); + preempt_enable(); return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); } @@ -5917,10 +5934,12 @@ static int tracing_resize_saved_cmdlines return -ENOMEM; } + preempt_disable(); arch_spin_lock(&trace_cmdline_lock); savedcmd_temp = savedcmd; savedcmd = s; arch_spin_unlock(&trace_cmdline_lock); + preempt_enable(); free_saved_cmdlines_buffer(savedcmd_temp); return 0; @@ -6373,10 +6392,12 @@ int tracing_set_tracer(struct trace_arra #ifdef CONFIG_TRACER_SNAPSHOT if (t->use_max_tr) { + local_irq_disable(); arch_spin_lock(&tr->max_lock); if (tr->cond_snapshot) ret = -EBUSY; arch_spin_unlock(&tr->max_lock); + local_irq_enable(); if (ret) goto out; } @@ -7436,10 +7457,12 @@ tracing_snapshot_write(struct file *filp goto out; } + local_irq_disable(); arch_spin_lock(&tr->max_lock); if (tr->cond_snapshot) ret = -EBUSY; arch_spin_unlock(&tr->max_lock); + local_irq_enable(); if (ret) goto out;