From patchwork Wed Jun 15 15:49:16 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Rutland X-Patchwork-Id: 70118 Delivered-To: patch@linaro.org Received: by 10.140.106.246 with SMTP id e109csp2642201qgf; Wed, 15 Jun 2016 08:50:00 -0700 (PDT) X-Received: by 10.36.245.197 with SMTP id k188mr207749ith.46.1466005800493; Wed, 15 Jun 2016 08:50:00 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w129si45799785pfb.139.2016.06.15.08.50.00; Wed, 15 Jun 2016 08:50:00 -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 S933521AbcFOPtp (ORCPT + 30 others); Wed, 15 Jun 2016 11:49:45 -0400 Received: from foss.arm.com ([217.140.101.70]:38566 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932883AbcFOPtn (ORCPT ); Wed, 15 Jun 2016 11:49: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 CC16FF; Wed, 15 Jun 2016 08:50:23 -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 4E86E3F246; Wed, 15 Jun 2016 08:49: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: [PATCH] kcov: reject open when kernel not instrumented Date: Wed, 15 Jun 2016 16:49:16 +0100 Message-Id: <1466005756-15626-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 adds an artificial CONFIG symbol, CONFIG_KCOV_CC, that is only set when the toolchain supports -fsanitize-coverage=trace-pc, and hence the kernel is built with instrumentation. When this is not the case, the kernel will return -ENOTSUPP if userspace attempts to open the kcov debugfs file, indicating that kcov functionality is unavailable. 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 | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) As discussed [1] in reply to the arm64 patch. [1] http://lkml.kernel.org/r/CACT4Y+Z3=juvxBJBXRh5PgE35twFRxg-3iMc-owenONU84x5XQ@mail.gmail.com Mark. -- 1.9.1 diff --git a/Makefile b/Makefile index 0f70de6..e6ef260 100644 --- a/Makefile +++ b/Makefile @@ -369,7 +369,7 @@ LDFLAGS_MODULE = CFLAGS_KERNEL = AFLAGS_KERNEL = CFLAGS_GCOV = -fprofile-arcs -ftest-coverage -fno-tree-loop-im -Wno-maybe-uninitialized -CFLAGS_KCOV = -fsanitize-coverage=trace-pc +CFLAGS_KCOV = -fsanitize-coverage=trace-pc -DCONFIG_KCOV_CC # Use USERINCLUDE when you must reference the UAPI directories only. diff --git a/kernel/kcov.c b/kernel/kcov.c index a02f2dd..df2cafd 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,13 @@ static int kcov_open(struct inode *inode, struct file *filep) { struct kcov *kcov; + /* + * CONFIG_KCOV was selected, but the compiler does not support the + * options KCOV requires. + */ + if (!IS_ENABLED(CONFIG_KCOV_CC)) + return -ENOTSUPP; + kcov = kzalloc(sizeof(*kcov), GFP_KERNEL); if (!kcov) return -ENOMEM;