From patchwork Fri Jun 17 09:39:14 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Rutland X-Patchwork-Id: 70298 Delivered-To: patch@linaro.org Received: by 10.140.28.4 with SMTP id 4csp188091qgy; Fri, 17 Jun 2016 02:39:47 -0700 (PDT) X-Received: by 10.36.13.76 with SMTP id 73mr31616476itx.79.1466156387025; Fri, 17 Jun 2016 02:39:47 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r13si12818918pag.64.2016.06.17.02.39.46; Fri, 17 Jun 2016 02:39:46 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754850AbcFQJjo (ORCPT + 30 others); Fri, 17 Jun 2016 05:39:44 -0400 Received: from foss.arm.com ([217.140.101.70]:48713 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753128AbcFQJjn (ORCPT ); Fri, 17 Jun 2016 05:39:43 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 43C699B6; Fri, 17 Jun 2016 02:40:25 -0700 (PDT) Received: from leverpostej.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 2E7553F213; Fri, 17 Jun 2016 02:39:41 -0700 (PDT) From: Mark Rutland To: linux-kernel@vger.kernel.org Cc: Mark Rutland , Alexander Potapenko , Andrew Morton , Dmitry Vyukov , James Morse , Kees Cook , Michal Marek Subject: [PATCHv3] kcov: reject open when kernel not instrumented Date: Fri, 17 Jun 2016 10:39:14 +0100 Message-Id: <1466156354-21609-1-git-send-email-mark.rutland@arm.com> X-Mailer: git-send-email 1.9.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If the toolchain does not support -fsanitize-coverage=trace-pc, we blat this option from CFLAGS_KCOV, and build the kernel without instrumentation, even if CONFIG_KCOV was selected. However, we still build the rest of the kcov infrastructure, and expose a kcov file under debugfs. This can be confusing, as the kernel will appear to support kcov, yet will never manage to sample any trace PC values. While we do note this fact at build time, this may be missed, and a user may not have access to build logs. This patch ensures that CC_HAVE_SANCOV_TRACE_PC is defined when the toolchain supports -fsanitize-coverage=trace-pc, and is not defined otherwise. When CC_HAVE_SANCOV_TRACE_PC is not defined, the kernel will return -ENOTSUPP if userspace attempts to open the kcov debugfs file, indicating that kcov functionality is unavailable. As uninstrumented files (e.g. kernel/kcov.c) need to know when this compiler feature is in use, wee pass the define via KBUILD_CFLAGS rather than CFLAGS_KCOV. Signed-off-by: Mark Rutland Cc: Alexander Potapenko Cc: Andrew Morton Cc: Dmitry Vyukov Cc: James Morse Cc: Kees Cook Cc: Michal Marek Cc: linux-kernel@vger.kernel.org --- Makefile | 2 ++ kernel/kcov.c | 9 +++++++++ 2 files changed, 11 insertions(+) Since v1 [1]: * Use CC_HAVE_SANCOV_TRACE_PC rather than CONFIG_KCOV_CC Since v2 [2]: * Use KBUILD_CFLAGS so kernel/kcov.c gets the flag [1] http://lkml.kernel.org/r/1466005756-15626-1-git-send-email-mark.rutland@arm.com [2] http://lkml.kernel.org/r/1466010285-2772-1-git-send-email-mark.rutland@arm.com -- 1.9.1 diff --git a/Makefile b/Makefile index b409076..699d363 100644 --- a/Makefile +++ b/Makefile @@ -687,6 +687,8 @@ ifdef CONFIG_KCOV $(warning Cannot use CONFIG_KCOV: \ -fsanitize-coverage=trace-pc is not supported by compiler) CFLAGS_KCOV = + else + KBUILD_CFLAGS += -DCC_HAVE_SANCOV_TRACE_PC endif endif diff --git a/kernel/kcov.c b/kernel/kcov.c index a02f2dd..0a0b164 100644 --- a/kernel/kcov.c +++ b/kernel/kcov.c @@ -3,6 +3,7 @@ #define DISABLE_BRANCH_PROFILING #include #include +#include #include #include #include @@ -160,6 +161,14 @@ static int kcov_open(struct inode *inode, struct file *filep) { struct kcov *kcov; +#ifndef CC_HAVE_SANCOV_TRACE_PC + /* + * CONFIG_KCOV was selected, but the compiler does not support the + * options KCOV requires. + */ + return -ENOTSUPP; +#endif /* CC_HAVE_SANCOV_TRACE_PC */ + kcov = kzalloc(sizeof(*kcov), GFP_KERNEL); if (!kcov) return -ENOMEM;