Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp3200618pxb; Mon, 9 Nov 2020 05:27:27 -0800 (PST) X-Google-Smtp-Source: ABdhPJy2hAbPYzsH1uuaFbU3RTfl0SeyQw/pi224Kwb0BQ4QVLhIcA4zLFD6DdVuHO784dC8RXr1 X-Received: by 2002:a50:9f2b:: with SMTP id b40mr15220560edf.20.1604928447710; Mon, 09 Nov 2020 05:27:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604928447; cv=none; d=google.com; s=arc-20160816; b=ZT9wD1oBjCJOQJnRpWRYxuAjJIX9/G1Ag5LPr+2ghIfTAfZL0A0D409IPF8mRdi5Wi 1BUu8un4NdByJWGX8GK2FlZHmY2M7I87BAv2AMZsEHQOHvRVdd8K9kUd+ag9FivdIuIy Fw12hJdQzynlThzLnVyxF+qqBNH9ZNE1UCtid4NFHnt+vpxj7NoizZ7htY3C1NLBM7a/ P4isTu0u+QCrhqpKMJF+K02k1IxYlTssgNdM2g9Mjn7pAp/1UsOzY6A9fPJ8fTYYZi/l b/qnd2//Ueo8bxDV2x8Y+arIUxd2+qE0YZveyA/9oxOX1CfHhOjKKxHFX6f8ZkchEyo0 xuXQ== 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=mFf3K41dhg6YsiMOSCSoPVEVyiLqf3SRWEKkCRs0Vkw=; b=BK0vYDcyXN6+malbVZ0kC8c93+/qaGLyrdXaHNT5mSNiP0nSKvphXzci5bcNT2LdyN ClxNE2CUbfJYbHCVvkgd3KmNEY6cAq/wyYFQRggg3jC5jR5pe/k8/ku3zZ4kiCBdIIkN ypYjkl7IfN3uzD/5ClMM0Xz49lmkWAH5ZFr/tp2e14FPojsmR/9B6gu1ppCBgcUt8Eel ZngYld14uEhRgA7mWcqPvPkjOhRpt0ca6n7KRr4SaBo88tTM0aAih+qy8NC4a8rvoSzC epIrCjGX3bNh00i+bf8XYcTSU91hBNJOpxeRwPaIBjoXBLAeP2Q9ZJtUQMtbcPKxrsz6 9eaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=RFxPe38j; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id gn35si1001288ejc.628.2020.11.09.05.27.04; Mon, 09 Nov 2020 05:27: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; dkim=pass header.i=@kernel.org header.s=default header.b=RFxPe38j; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387917AbgKINUO (ORCPT + 99 others); Mon, 9 Nov 2020 08:20:14 -0500 Received: from mail.kernel.org ([198.145.29.99]:47704 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387894AbgKINUL (ORCPT ); Mon, 9 Nov 2020 08:20:11 -0500 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 77303206D8; Mon, 9 Nov 2020 13:20:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604928010; bh=yakpp09G5l4f/rE3il3+MK6ReUoTT/4nqVv32upIcAQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RFxPe38j7wcshKumIJtx/dJ03sNGacVlbB1Gir+wwOHP9bjCu52Spbfokj95D6Pcq n8KMBWC5CBZuLTv4KoUMvcurJGH1990PO7LHuA4TAFQfZ9Lx8Ci3oSQZjR0mB+aEZu Jo1C/CqAU14k8GABqZjtiwAqW37IA3V1X+1vbA8Q= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Steven Rostedt (VMware)" Subject: [PATCH 5.9 066/133] ftrace: Handle tracing when switching between context Date: Mon, 9 Nov 2020 13:55:28 +0100 Message-Id: <20201109125033.909766819@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201109125030.706496283@linuxfoundation.org> References: <20201109125030.706496283@linuxfoundation.org> User-Agent: quilt/0.66 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 From: Steven Rostedt (VMware) commit 726b3d3f141fba6f841d715fc4d8a4a84f02c02a upstream. When an interrupt or NMI comes in and switches the context, there's a delay from when the preempt_count() shows the update. As the preempt_count() is used to detect recursion having each context have its own bit get set when tracing starts, and if that bit is already set, it is considered a recursion and the function exits. But if this happens in that section where context has changed but preempt_count() has not been updated, this will be incorrectly flagged as a recursion. To handle this case, create another bit call TRANSITION and test it if the current context bit is already set. Flag the call as a recursion if the TRANSITION bit is already set, and if not, set it and continue. The TRANSITION bit will be cleared normally on the return of the function that set it, or if the current context bit is clear, set it and clear the TRANSITION bit to allow for another transition between the current context and an even higher one. Cc: stable@vger.kernel.org Fixes: edc15cafcbfa3 ("tracing: Avoid unnecessary multiple recursion checks") Signed-off-by: Steven Rostedt (VMware) Signed-off-by: Greg Kroah-Hartman --- kernel/trace/trace.h | 23 +++++++++++++++++++++-- kernel/trace/trace_selftest.c | 9 +++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -636,6 +636,12 @@ enum { * function is called to clear it. */ TRACE_GRAPH_NOTRACE_BIT, + + /* + * When transitioning between context, the preempt_count() may + * not be correct. Allow for a single recursion to cover this case. + */ + TRACE_TRANSITION_BIT, }; #define trace_recursion_set(bit) do { (current)->trace_recursion |= (1<<(bit)); } while (0) @@ -690,8 +696,21 @@ static __always_inline int trace_test_an return 0; bit = trace_get_context_bit() + start; - if (unlikely(val & (1 << bit))) - return -1; + if (unlikely(val & (1 << bit))) { + /* + * It could be that preempt_count has not been updated during + * a switch between contexts. Allow for a single recursion. + */ + bit = TRACE_TRANSITION_BIT; + if (trace_recursion_test(bit)) + return -1; + trace_recursion_set(bit); + barrier(); + return bit + 1; + } + + /* Normal check passed, clear the transition to allow it again */ + trace_recursion_clear(TRACE_TRANSITION_BIT); val |= 1 << bit; current->trace_recursion = val; --- a/kernel/trace/trace_selftest.c +++ b/kernel/trace/trace_selftest.c @@ -492,8 +492,13 @@ trace_selftest_function_recursion(void) unregister_ftrace_function(&test_rec_probe); ret = -1; - if (trace_selftest_recursion_cnt != 1) { - pr_cont("*callback not called once (%d)* ", + /* + * Recursion allows for transitions between context, + * and may call the callback twice. + */ + if (trace_selftest_recursion_cnt != 1 && + trace_selftest_recursion_cnt != 2) { + pr_cont("*callback not called once (or twice) (%d)* ", trace_selftest_recursion_cnt); goto out; }