Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp3323415ybl; Mon, 19 Aug 2019 16:42:34 -0700 (PDT) X-Google-Smtp-Source: APXvYqz+xDHZ/4EkOMr1ufAM+D9dRITjQ4njFSyBCCnXK0TuQMIqY8UgpvN3uCIaR+FULNtlbptH X-Received: by 2002:a65:4786:: with SMTP id e6mr21674637pgs.448.1566258154639; Mon, 19 Aug 2019 16:42:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566258154; cv=none; d=google.com; s=arc-20160816; b=I0WHpjR1pCcSkoPRDtplYdcT7bx8WKKWsPZrnTxR+h8Ogp03OoXoeuKq20Ieg+1gZj 7chpGAGp68AzBhWQNnmqlIxy4tM7tORu8hJGC3e6KivwMqVbOtWmB1NSwm+5TxQV3JFX 24FLnKhRgKFVclrZ3TaqPHhFKqIsm4YPqfEb6uxa548NLnpAn5XV6ohvSddJNnjwaxfx akiUatQOdeKxlMkEduMM5+YHPiwEwd7uh6FAOpu0cOa/dfZF/MmDuvQe9ADqFmdF2xMS +lITIIN4EbijSN6twAJXJowe33ZmwNhmi3Oa5B0l6AxRTjuWLQImCNrMfkX9xG/hO4XO /bDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=+I63SO+d0YjrD0lNvVimLm7Dyf1rWjapDPKL/f2NuQg=; b=x/1RFPdvXo3VujbXtb+wh97r2KPnAjjqUa7aV0GvVc90kBBESgm9cpVIXJedLetjYA uxzGKJ7LH1T3HtA0T1aWaPtWKhn6yK6tQVv+0DsfdamaQ+9fP4G9bfOEv6k/eKNkNjoW E+Tu/Y+0Tu/jeQ11flFbeEr2qpmwNq0avoCFp0++/PXf2u/NnjYqDr/j9MyXH9AHcTFF l3dusxd5lSDhaSNp28qMX41OGHmf/NyYnUy+Ubcyea3w1Oacdv7RCplt3JhenmpgylIn gYP9hyIN877eBHSm15f/8xMiTL4IbS8ywyn3pbY6mXK3tqNE40GrimLrhZXOo6bBtg7w l6ZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="jz/7cb7n"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u9si9783461pjn.86.2019.08.19.16.42.19; Mon, 19 Aug 2019 16:42:34 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="jz/7cb7n"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728825AbfHSXlZ (ORCPT + 99 others); Mon, 19 Aug 2019 19:41:25 -0400 Received: from mail-pg1-f196.google.com ([209.85.215.196]:46169 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728794AbfHSXlX (ORCPT ); Mon, 19 Aug 2019 19:41:23 -0400 Received: by mail-pg1-f196.google.com with SMTP id m3so2067204pgv.13 for ; Mon, 19 Aug 2019 16:41:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=+I63SO+d0YjrD0lNvVimLm7Dyf1rWjapDPKL/f2NuQg=; b=jz/7cb7nw9ArKTwWLS9o/Ua4EOb7vJlnCtmuzDCXyZU80AbW8Sg+mGEQIyTr1fupSa vT6My6CwVkLuCAWcBldD25Px/wXw3vgSLXn9sJK4IY+IpShRIHoVGPuCnf5c/LbGTtE1 svbBhoRCV8bChtQlnbBNjPaREMh0ZNju+Hkoo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=+I63SO+d0YjrD0lNvVimLm7Dyf1rWjapDPKL/f2NuQg=; b=a9K3c7vXqQWg5sUbAwqxB0veCzfP3hEWWY/ZMvro8jJPTlQjWL37+mmsiFDnfPBC0K ny0vGkXA8OzUAyh7RziECdAyCySG1IOzmsrD8Sw1pK2EZTUi4sjUDSvG7Ub7h/9KEmy0 +VU8bCsB9Mh3DzcNpxD/kTW/obQHPkNW6vyFjeDpAo/QQuSNjrHSvlUO6rIHGNp7nET+ pz30JPtdi7CDWjUa/tWzwPnNUxBAJGf7X9+Vv1pj8CORdKJkd7dVxRpLcWI7YBHhflv9 3xBfQ44WlwxDFQB8iIjX80HMrjXZNUoYu5pAKD/bR/BAvK7skeJtdADaSrQz7VrNPOaK mWWQ== X-Gm-Message-State: APjAAAUPQg5dA1sdcrh1BBpyOXUPnYYdkJeLS9QwV2egi285wGpNe3Mt FgX3ylglQfr4HlsZ+ADZhjUz+A== X-Received: by 2002:a62:1858:: with SMTP id 85mr27275283pfy.120.1566258083255; Mon, 19 Aug 2019 16:41:23 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id l123sm22356875pfl.9.2019.08.19.16.41.19 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 19 Aug 2019 16:41:20 -0700 (PDT) From: Kees Cook To: Andrew Morton Cc: Kees Cook , Christophe Leroy , Drew Davenport , Peter Zijlstra , Arnd Bergmann , "Steven Rostedt (VMware)" , Feng Tang , Petr Mladek , Mauro Carvalho Chehab , Borislav Petkov , YueHaibing , linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 7/7] bug: Move WARN_ON() "cut here" into exception handler Date: Mon, 19 Aug 2019 16:41:11 -0700 Message-Id: <20190819234111.9019-8-keescook@chromium.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190819234111.9019-1-keescook@chromium.org> References: <20190819234111.9019-1-keescook@chromium.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The original clean up of "cut here" missed the WARN_ON() case (that does not have a printk message), which was fixed recently by adding an explicit printk of "cut here". This had the downside of adding a printk() to every WARN_ON() caller, which reduces the utility of using an instruction exception to streamline the resulting code. By making this a new BUGFLAG, all of these can be removed and "cut here" can be handled by the exception handler. This was very pronounced on PowerPC, but the effect can be seen on x86 as well. The resulting text size of a defconfig build shows some small savings from this patch: text data bss dec hex filename 19691167 5134320 1646664 26472151 193eed7 vmlinux.before 19676362 5134260 1663048 26473670 193f4c6 vmlinux.after This change also opens the door for creating something like BUG_MSG(), where a custom printk() before issuing BUG(), without confusing the "cut here" line. Reported-by: Christophe Leroy Fixes: Fixes: 6b15f678fb7d ("include/asm-generic/bug.h: fix "cut here" for WARN_ON for __WARN_TAINT architectures") Signed-off-by: Kees Cook --- include/asm-generic/bug.h | 8 +++----- lib/bug.c | 11 +++++++++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/include/asm-generic/bug.h b/include/asm-generic/bug.h index 588dd59a5b72..da471fcc5487 100644 --- a/include/asm-generic/bug.h +++ b/include/asm-generic/bug.h @@ -10,6 +10,7 @@ #define BUGFLAG_WARNING (1 << 0) #define BUGFLAG_ONCE (1 << 1) #define BUGFLAG_DONE (1 << 2) +#define BUGFLAG_PRINTK (1 << 3) #define BUGFLAG_TAINT(taint) ((taint) << 8) #define BUG_GET_TAINT(bug) ((bug)->flags >> 8) #endif @@ -86,13 +87,10 @@ void warn_slowpath_fmt(const char *file, const int line, unsigned taint, warn_slowpath_fmt(__FILE__, __LINE__, taint, arg) #else extern __printf(1, 2) void __warn_printk(const char *fmt, ...); -#define __WARN() do { \ - printk(KERN_WARNING CUT_HERE); \ - __WARN_FLAGS(BUGFLAG_TAINT(TAINT_WARN)); \ - } while (0) +#define __WARN() __WARN_FLAGS(BUGFLAG_TAINT(TAINT_WARN)) #define __WARN_printf(taint, arg...) do { \ __warn_printk(arg); \ - __WARN_FLAGS(BUGFLAG_TAINT(taint)); \ + __WARN_FLAGS(BUGFLAG_PRINTK | BUGFLAG_TAINT(taint)); \ } while (0) #define WARN_ON_ONCE(condition) ({ \ int __ret_warn_on = !!(condition); \ diff --git a/lib/bug.c b/lib/bug.c index 1077366f496b..6c22e8a6f9de 100644 --- a/lib/bug.c +++ b/lib/bug.c @@ -181,6 +181,15 @@ enum bug_trap_type report_bug(unsigned long bugaddr, struct pt_regs *regs) } } + /* + * BUG() and WARN_ON() families don't print a custom debug message + * before triggering the exception handler, so we must add the + * "cut here" line now. WARN() issues its own "cut here" before the + * extra debugging message it writes before triggering the handler. + */ + if ((bug->flags & BUGFLAG_PRINTK) == 0) + printk(KERN_DEFAULT CUT_HERE); + if (warning) { /* this is a WARN_ON rather than BUG/BUG_ON */ __warn(file, line, (void *)bugaddr, BUG_GET_TAINT(bug), regs, @@ -188,8 +197,6 @@ enum bug_trap_type report_bug(unsigned long bugaddr, struct pt_regs *regs) return BUG_TRAP_TYPE_WARN; } - printk(KERN_DEFAULT CUT_HERE); - if (file) pr_crit("kernel BUG at %s:%u!\n", file, line); else -- 2.17.1