Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp854275pxu; Mon, 23 Nov 2020 05:59:23 -0800 (PST) X-Google-Smtp-Source: ABdhPJxBhNk53h2C+aLZA949Dje5MjZMzCYxKEgdgm+5Lw72C/U336saZblmn+sUF/uQlS5SsfXA X-Received: by 2002:a05:6402:1813:: with SMTP id g19mr49138742edy.105.1606139963166; Mon, 23 Nov 2020 05:59:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606139963; cv=none; d=google.com; s=arc-20160816; b=AUbDjAbqxiW5cewGuuIga64M8DS2NHY/ZCeFfCVe2LJ9ZkLuPOgugr2+jPuz2iwASH IabZ/eTNXr/hG21JKi8Cl7c6h5AiF767XvFmiv7pbsywcVPkaYqy6nljLas6XUWjNdnl hwQq/K5UjDjXX1wYULVHBVJwhAZPmBoHjckS6NmDQFgrf0fYFCZEVfZw2Trtcw6MPh2X NciHW0/xjTJpwd/hALGlDnmZ7Tc2AwQuyT6DS6n3JXXWb2yqYlZpPPQjP7rUi47LcUq9 Lnv6LUpclP6kf0Xr7uJa+Xb4DPlUZlquPTwjLT1b1rHRSL4fO+qkP5yc7oLDVN8fkg1F Y3ew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=uUPaDYAemmJYHuwFkYYuuftPgDPHYdEdgPTzaW0nVXY=; b=nHDAAR5JwWPa0Z6L2GNgM7LUUKbL26QOeJJCrmv4oMDiOyZ7iZA/QbyWoHMixZ7dC/ tMaS7FipSEKaAkiAZ5ORuGDRKV/DDasS/vsJf+Byyk6J4h5XFa0g2mnzj3QqrKTBSE7+ O1H7/hjHk3vlA2400HcUogwpbaFQzaH1Rnr43nDQDLReFb0lwDb31xaIyTY5cEYWU9+8 7mPV41tgGd5zWQH+iLlJo1DLVZC1TM6eEwDKrfduMW3FBqTa+1j0dH20duItHxz7c8Cx oqWOCwsSL+JY2IDk42sF//50VK1/+3wJhLRWrawav7VLySrvKk2fazU/et6dZNSai+jv mjig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=Uqui2mMb; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ap16si1487068ejc.413.2020.11.23.05.58.59; Mon, 23 Nov 2020 05:59:23 -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=@infradead.org header.s=casper.20170209 header.b=Uqui2mMb; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388870AbgKWNzT (ORCPT + 99 others); Mon, 23 Nov 2020 08:55:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40762 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729792AbgKWNzS (ORCPT ); Mon, 23 Nov 2020 08:55:18 -0500 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A859FC0613CF for ; Mon, 23 Nov 2020 05:55:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=uUPaDYAemmJYHuwFkYYuuftPgDPHYdEdgPTzaW0nVXY=; b=Uqui2mMbeRyawJOPg5AAfYJY6W WltTLAk6IRy2Og9Slxu+Cr4Vk1RASFdKjY3/ME8jJHcZZTU6HVCBE3IEvOejVJpbhukorM2v+LyPq ukts4lnW+9laDJmhDRum9/tDkxbZ1EVfG7CeQfFvkMkCCDpEVYbWRbKLH/YolaMY64FxnifCGB0VI 9HRi0aw3IG6YEiE8GlEtKiW3TtBQYxWulLtRoXsLzAmNRmuPkzHQ+U22HcYSF63STEyajcW1GzJvi 33vYzdpAw9plvIYTsrlMZkNQJwXv57OTZmpR66DeWB2n1pqKieG+uVrU5h/Rh2wKTZoc6pKNVsrWu U/zMrRiQ==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1khCJE-0006V8-Ss; Mon, 23 Nov 2020 13:55:13 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 7BF013069B1; Mon, 23 Nov 2020 14:55:12 +0100 (CET) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 5D477201D16D7; Mon, 23 Nov 2020 14:55:12 +0100 (CET) Date: Mon, 23 Nov 2020 14:55:12 +0100 From: Peter Zijlstra To: Marco Elver Cc: paulmck@kernel.org, will@kernel.org, tglx@linutronix.de, mingo@kernel.org, mark.rutland@arm.com, boqun.feng@gmail.com, dvyukov@google.com, kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] kcsan: Avoid scheduler recursion by using non-instrumented preempt_{disable,enable}() Message-ID: <20201123135512.GM3021@hirez.programming.kicks-ass.net> References: <20201123132300.1759342-1-elver@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201123132300.1759342-1-elver@google.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Nov 23, 2020 at 02:23:00PM +0100, Marco Elver wrote: > When enabling KCSAN for kernel/sched (remove KCSAN_SANITIZE := n from > kernel/sched/Makefile), with CONFIG_DEBUG_PREEMPT=y, we can observe > recursion due to: > > check_access() [via instrumentation] > kcsan_setup_watchpoint() > reset_kcsan_skip() > kcsan_prandom_u32_max() > get_cpu_var() > preempt_disable() > preempt_count_add() [in kernel/sched/core.c] > check_access() [via instrumentation] > > Avoid this by rewriting kcsan_prandom_u32_max() to only use safe > versions of preempt_disable() and preempt_enable() that do not call into > scheduler code. > > Note, while this currently does not affect an unmodified kernel, it'd be > good to keep a KCSAN kernel working when KCSAN_SANITIZE := n is removed > from kernel/sched/Makefile to permit testing scheduler code with KCSAN > if desired. > > Fixes: cd290ec24633 ("kcsan: Use tracing-safe version of prandom") > Signed-off-by: Marco Elver > --- > v2: > * Update comment to also point out preempt_enable(). > --- > kernel/kcsan/core.c | 15 ++++++++++++--- > 1 file changed, 12 insertions(+), 3 deletions(-) > > diff --git a/kernel/kcsan/core.c b/kernel/kcsan/core.c > index 3994a217bde7..10513f3e2349 100644 > --- a/kernel/kcsan/core.c > +++ b/kernel/kcsan/core.c > @@ -284,10 +284,19 @@ should_watch(const volatile void *ptr, size_t size, int type, struct kcsan_ctx * > */ > static u32 kcsan_prandom_u32_max(u32 ep_ro) > { > - struct rnd_state *state = &get_cpu_var(kcsan_rand_state); > - const u32 res = prandom_u32_state(state); > + struct rnd_state *state; > + u32 res; > + > + /* > + * Avoid recursion with scheduler by using non-tracing versions of > + * preempt_disable() and preempt_enable() that do not call into > + * scheduler code. > + */ > + preempt_disable_notrace(); > + state = raw_cpu_ptr(&kcsan_rand_state); > + res = prandom_u32_state(state); > + preempt_enable_no_resched_notrace(); This is a preemption bug. Does preempt_enable_notrace() not work? > > - put_cpu_var(kcsan_rand_state); > return (u32)(((u64) res * ep_ro) >> 32); > } > > -- > 2.29.2.454.gaff20da3a2-goog >