From patchwork Sat Aug 24 23:06:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wentao Zhang X-Patchwork-Id: 822274 Received: from mail-io1-f65.google.com (mail-io1-f65.google.com [209.85.166.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 65A38142652 for ; Sat, 24 Aug 2024 23:07:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.65 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724540825; cv=none; b=f4UYSsAoN93CAj5DoCBIhin9uMDg3GUWKgLAcxqkaG99xsdK04cUJJ7aPbxt5PllsOukaQDiMq0Q7gYRQiUjVkkpQbTcTR02gdd/XxLiMebCOg1U7ksY8t/nLc9RnWPx4lo7IcG3kmYGImULiW9LTMp0MVas4UzQo8yfzrewTvE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724540825; c=relaxed/simple; bh=HFFgMUe0sXLt8uy3R2lYoPH0xrrMmgwABqkiDy8B3Qo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kDaokf5jmL0LqHzfgmxSDiW6Fb3dnvwbwgGgCZRab79vqzFNqmOkBrCsIxZAf+R9h8SAL5OMk4dxGeJ49WbhQeDRrGbLgOi1bT1SidWzvzSAyBEXtHGqd/ze5vtnj+i6GxH7i+RBxkE/sIP3kfUp+4goJwrZKh+m9bFVxInb4GA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=illinois.edu; spf=pass smtp.mailfrom=illinois.edu; dkim=pass (2048-bit key) header.d=illinois-edu.20230601.gappssmtp.com header.i=@illinois-edu.20230601.gappssmtp.com header.b=gK56Mtz0; arc=none smtp.client-ip=209.85.166.65 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=illinois.edu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=illinois.edu Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=illinois-edu.20230601.gappssmtp.com header.i=@illinois-edu.20230601.gappssmtp.com header.b="gK56Mtz0" Received: by mail-io1-f65.google.com with SMTP id ca18e2360f4ac-81fd1e1d38bso192192439f.1 for ; Sat, 24 Aug 2024 16:07:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=illinois-edu.20230601.gappssmtp.com; s=20230601; t=1724540822; x=1725145622; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=6KIwALI9lcPb0lT76UFCEG4r4cwMraGgAV9q6NNiJF4=; b=gK56Mtz0TjsY+Rp/mE17J30X/dypHWfN5918T/Q8RxwkDOL+OH78rpLMhjofuwOABh Wri0Kp7WQu6vPvLYRFaayGyuQgYcnUm92Cl2hy9kx20In2BLbj1aO7KoYifZw/GU+jKp qK/SrjtRea7aqwzd45sIEf3soIUPPjbxgsxJBuR4UjCQGYRRi+zhbK8+QtanYt4TV8p4 r4daB55RbkEAC4c7ax4KwKjaRgZ26+6G00OZ8n79yNaOx+w5bqQPlWcaU+m1nX0chI9w GMzzV5yQ1mWMPl9pQIbLSblPhDjqG9wvJW10qegj40vUDNLUA8hANz+a+Weokkat/6yp lEvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724540822; x=1725145622; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6KIwALI9lcPb0lT76UFCEG4r4cwMraGgAV9q6NNiJF4=; b=epr01mwpSBOIuZODScQvXLy0ZuYH9MqoD5wN8AfyEXihE+GgVWyKfxgXPq4Ksu4VzH 3UY1edDU+OQWHZgIJVPgiErmNAhRsGklmIKIm/emFtI23tccelMLM/IzqF1tx+Kb6AtE JAusvg8G/k4ItJGU+YRsxXCPJOxhAd7Z0GTWEMrug/prWRea0qqWYjnk2TFEiCxgFI2M z2RhwrWPNg8w8xTHZm4nruJScDuCK0VFyHP/sxhn+o4gtNuh/JwLmjcWl3Vg2u31FySV 5XAD7cKccJaD6pRIZsNMgwePHdgfmhrvsPoBnc6sCDPBEbW+eKpIlFe552T8hU0z5HYj /yww== X-Forwarded-Encrypted: i=1; AJvYcCVPeYz1CWETtCHhdGobgakgGhZHX4eFasuEEJCnzORjte4B9iMGs+V1ayJfbCKBmIOGrHzPZSqSynM=@vger.kernel.org X-Gm-Message-State: AOJu0YwJ29Ig5lLTqUMQoJ9fLeqcyeC5SPi5dEEFZWEmKhr3KS2GNslV Z8kqVBJ3WDttkRl2ZnG2rpdcrPORckoq3SHXPl/q9t0yL7NBpSyyyhPKxFWZVg== X-Google-Smtp-Source: AGHT+IF5JX0Jk4k4VxnekA9ZAMpPy+eSge9GLI2pHIyDjL7LjOoFyPID7Vz4y3rMhqO0ju3PL8Nfvg== X-Received: by 2002:a05:6602:2c94:b0:824:e42e:a1bd with SMTP id ca18e2360f4ac-827881eb403mr770732339f.17.1724540822378; Sat, 24 Aug 2024 16:07:02 -0700 (PDT) Received: from shizuku.. (mobile-130-126-255-62.near.illinois.edu. [130.126.255.62]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-4ce70f85671sm1563938173.80.2024.08.24.16.07.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 24 Aug 2024 16:07:01 -0700 (PDT) From: Wentao Zhang To: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-efi@vger.kernel.org, linux-um@lists.infradead.org, linux-arch@vger.kernel.org, linux-trace-kernel@vger.kernel.org, llvm@lists.linux.dev, x86@kernel.org Cc: wentaoz5@illinois.edu, marinov@illinois.edu, tyxu@illinois.edu, jinghao7@illinois.edu, tingxur@illinois.edu, steven.h.vanderleest@boeing.com, chuck.wolber@boeing.com, matthew.l.weber3@boeing.com, Matt.Kelly2@boeing.com, andrew.j.oppelt@boeing.com, samuel.sarkisian@boeing.com, morbo@google.com, samitolvanen@google.com, masahiroy@kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, luto@kernel.org, ardb@kernel.org, richard@nod.at, anton.ivanov@cambridgegreys.com, johannes@sipsolutions.net, arnd@arndb.de, rostedt@goodmis.org, mhiramat@kernel.org, oberpar@linux.ibm.com, akpm@linux-foundation.org, paulmck@kernel.org, bhelgaas@google.com, kees@kernel.org, jpoimboe@kernel.org, peterz@infradead.org, kent.overstreet@linux.dev, nathan@kernel.org, hpa@zytor.com, mathieu.desnoyers@efficios.com, ndesaulniers@google.com, justinstitt@google.com, maskray@google.com, dvyukov@google.com Subject: [RFC PATCH 1/3] llvm-cov: add Clang's Source-based Code Coverage support Date: Sat, 24 Aug 2024 18:06:39 -0500 Message-Id: <20240824230641.385839-2-wentaoz5@illinois.edu> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240824230641.385839-1-wentaoz5@illinois.edu> References: <20240824230641.385839-1-wentaoz5@illinois.edu> Precedence: bulk X-Mailing-List: linux-efi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Implement debugfs entries for serializing profiles and resetting counters/bitmaps. Also adds Source-based Code Coverage flags and kconfig options. This work reuses a portion of code from a previous effort by Sami Tolvanen et al. [1], specifically its debugfs interface and the underlying profile processing, but discards all its PGO-specific parts, notably value profiling. To our end (code coverage, like MC/DC, required for high assurance), we do instrumentation at the compiler frontend, instead of IR; we care about counters and bitmaps, but not value profiling. [1] https://lore.kernel.org/lkml/20210407211704.367039-1-morbo@google.com/ Signed-off-by: Wentao Zhang Signed-off-by: Chuck Wolber --- Makefile | 3 + arch/Kconfig | 1 + arch/x86/Kconfig | 1 + arch/x86/kernel/vmlinux.lds.S | 2 + include/asm-generic/vmlinux.lds.h | 38 +++++ kernel/Makefile | 1 + kernel/llvm-cov/Kconfig | 29 ++++ kernel/llvm-cov/Makefile | 5 + kernel/llvm-cov/fs.c | 253 ++++++++++++++++++++++++++++++ kernel/llvm-cov/llvm-cov.h | 156 ++++++++++++++++++ scripts/Makefile.lib | 10 ++ scripts/mod/modpost.c | 2 + 12 files changed, 501 insertions(+) create mode 100644 kernel/llvm-cov/Kconfig create mode 100644 kernel/llvm-cov/Makefile create mode 100644 kernel/llvm-cov/fs.c create mode 100644 kernel/llvm-cov/llvm-cov.h diff --git a/Makefile b/Makefile index 68ebd6d6b444..1750a2b7dfe8 100644 --- a/Makefile +++ b/Makefile @@ -737,6 +737,9 @@ endif # KBUILD_EXTMOD # Defaults to vmlinux, but the arch makefile usually adds further targets all: vmlinux +CFLAGS_LLVM_COV := -fprofile-instr-generate -fcoverage-mapping +export CFLAGS_LLVM_COV + CFLAGS_GCOV := -fprofile-arcs -ftest-coverage ifdef CONFIG_CC_IS_GCC CFLAGS_GCOV += -fno-tree-loop-im diff --git a/arch/Kconfig b/arch/Kconfig index 975dd22a2dbd..0727265f677f 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -1601,6 +1601,7 @@ config ARCH_HAS_KERNEL_FPU_SUPPORT the kernel, as described in Documentation/core-api/floating-point.rst. source "kernel/gcov/Kconfig" +source "kernel/llvm-cov/Kconfig" source "scripts/gcc-plugins/Kconfig" diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 007bab9f2a0e..87a793b82bab 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -85,6 +85,7 @@ config X86 select ARCH_HAS_FORTIFY_SOURCE select ARCH_HAS_GCOV_PROFILE_ALL select ARCH_HAS_KCOV if X86_64 + select ARCH_HAS_LLVM_COV if X86_64 select ARCH_HAS_KERNEL_FPU_SUPPORT select ARCH_HAS_MEM_ENCRYPT select ARCH_HAS_MEMBARRIER_SYNC_CORE diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S index 6e73403e874f..90433772240a 100644 --- a/arch/x86/kernel/vmlinux.lds.S +++ b/arch/x86/kernel/vmlinux.lds.S @@ -191,6 +191,8 @@ SECTIONS BUG_TABLE + LLVM_COV_DATA + ORC_UNWIND_TABLE . = ALIGN(PAGE_SIZE); diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h index 1ae44793132a..770ff8469dcd 100644 --- a/include/asm-generic/vmlinux.lds.h +++ b/include/asm-generic/vmlinux.lds.h @@ -334,6 +334,44 @@ #define THERMAL_TABLE(name) #endif +#ifdef CONFIG_LLVM_COV_KERNEL +#define LLVM_COV_DATA \ + __llvm_prf_data : AT(ADDR(__llvm_prf_data) - LOAD_OFFSET) { \ + __llvm_prf_start = .; \ + __llvm_prf_data_start = .; \ + *(__llvm_prf_data) \ + __llvm_prf_data_end = .; \ + } \ + __llvm_prf_cnts : AT(ADDR(__llvm_prf_cnts) - LOAD_OFFSET) { \ + __llvm_prf_cnts_start = .; \ + *(__llvm_prf_cnts) \ + __llvm_prf_cnts_end = .; \ + } \ + __llvm_prf_names : AT(ADDR(__llvm_prf_names) - LOAD_OFFSET) { \ + __llvm_prf_names_start = .; \ + *(__llvm_prf_names) \ + __llvm_prf_names_end = .; \ + } \ + __llvm_prf_bits : AT(ADDR(__llvm_prf_bits) - LOAD_OFFSET) { \ + __llvm_prf_bits_start = .; \ + *(__llvm_prf_bits) \ + __llvm_prf_bits_end = .; \ + } \ + __llvm_covfun : AT(ADDR(__llvm_covfun) - LOAD_OFFSET) { \ + __llvm_covfun_start = .; \ + *(__llvm_covfun) \ + __llvm_covfun_end = .; \ + } \ + __llvm_covmap : AT(ADDR(__llvm_covmap) - LOAD_OFFSET) { \ + __llvm_covmap_start = .; \ + *(__llvm_covmap) \ + __llvm_covmap_end = .; \ + __llvm_prf_end = .; \ + } +#else +#define LLVM_COV_DATA +#endif + #define KERNEL_DTB() \ STRUCT_ALIGN(); \ __dtb_start = .; \ diff --git a/kernel/Makefile b/kernel/Makefile index 3c13240dfc9f..773e6a9ee00a 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -117,6 +117,7 @@ obj-$(CONFIG_HAVE_STATIC_CALL) += static_call.o obj-$(CONFIG_HAVE_STATIC_CALL_INLINE) += static_call_inline.o obj-$(CONFIG_CFI_CLANG) += cfi.o obj-$(CONFIG_NUMA) += numa.o +obj-$(CONFIG_LLVM_COV_KERNEL) += llvm-cov/ obj-$(CONFIG_PERF_EVENTS) += events/ diff --git a/kernel/llvm-cov/Kconfig b/kernel/llvm-cov/Kconfig new file mode 100644 index 000000000000..505eba5bd23c --- /dev/null +++ b/kernel/llvm-cov/Kconfig @@ -0,0 +1,29 @@ +# SPDX-License-Identifier: GPL-2.0-only +menu "Clang's source-based kernel coverage measurement (EXPERIMENTAL)" + +config ARCH_HAS_LLVM_COV + bool + +config LLVM_COV_KERNEL + bool "Enable Clang's source-based kernel coverage measurement" + depends on DEBUG_FS + depends on ARCH_HAS_LLVM_COV + depends on CC_IS_CLANG && CLANG_VERSION >= 180000 + default n + help + This option enables Clang's Source-based Code Coverage. + + If unsure, say N. + + On a kernel compiled with this option, run your test suites, and + download the raw profile from /sys/kernel/debug/llvm-cov/profraw. + This file can then be converted into the indexed format with + llvm-profdata and used to generate coverage reports with llvm-cov. + + Note that a kernel compiled with coverage flags will be significantly + larger and run slower. + + Note that the debugfs filesystem has to be mounted to access the raw + profile. + +endmenu diff --git a/kernel/llvm-cov/Makefile b/kernel/llvm-cov/Makefile new file mode 100644 index 000000000000..a0f45e05f8fb --- /dev/null +++ b/kernel/llvm-cov/Makefile @@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 +GCOV_PROFILE := n +LLVM_COV_PROFILE := n + +obj-y += fs.o \ No newline at end of file diff --git a/kernel/llvm-cov/fs.c b/kernel/llvm-cov/fs.c new file mode 100644 index 000000000000..c56f660a174e --- /dev/null +++ b/kernel/llvm-cov/fs.c @@ -0,0 +1,253 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2019 Sami Tolvanen , Google, Inc. + * Copyright (C) 2024 Jinghao Jia , UIUC + * Copyright (C) 2024 Wentao Zhang , UIUC + * + * This software is licensed under the terms of the GNU General Public + * License version 2, as published by the Free Software Foundation, and + * may be copied, distributed, and modified under those terms. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#define pr_fmt(fmt) "llvm-cov: " fmt + +#include +#include +#include +#include +#include +#include +#include +#include "llvm-cov.h" + +/* + * This lock guards both counter/bitmap reset and serialization of the + * raw profile data. Keeping both of these activities separate via locking + * ensures that we don't try to serialize data that's being reset. + */ +DEFINE_SPINLOCK(llvm_cov_lock); + +static struct dentry *directory; + +struct llvm_cov_private_data { + char *buffer; + unsigned long size; +}; + +/* + * Raw profile data format: + * https://llvm.org/docs/InstrProfileFormat.html#raw-profile-format. We will + * only populate information that's relevant to basic Source-based Code Coverage + * before serialization. Other features like binary IDs, continuous mode, + * single-byte mode, value profiling, type profiling etc are not implemented. + */ + +static void llvm_cov_fill_raw_profile_header(void **buffer) +{ + struct __llvm_profile_header *header = *(struct __llvm_profile_header **)buffer; + + header->magic = INSTR_PROF_RAW_MAGIC_64; + header->version = INSTR_PROF_RAW_VERSION; + header->binary_ids_size = 0; + header->num_data = __llvm_prf_data_count(); + header->padding_bytes_before_counters = 0; + header->num_counters = __llvm_prf_cnts_count(); + header->padding_bytes_after_counters = + __llvm_prf_get_padding(__llvm_prf_cnts_size()); + header->num_bitmap_bytes = __llvm_prf_bits_size(); + header->padding_bytes_after_bitmap_bytes = + __llvm_prf_get_padding(__llvm_prf_bits_size()); + header->names_size = __llvm_prf_names_size(); + header->counters_delta = (u64)__llvm_prf_cnts_start - + (u64)__llvm_prf_data_start; + header->bitmap_delta = (u64)__llvm_prf_bits_start - + (u64)__llvm_prf_data_start; + header->names_delta = (u64)__llvm_prf_names_start; +#if defined(CONFIG_CC_IS_CLANG) && CONFIG_CLANG_VERSION >= 190000 + header->num_v_tables = 0; + header->v_names_size = 0; +#endif + header->value_kind_last = IPVK_LAST; + + *buffer += sizeof(*header); +} + +/* + * Copy the source into the buffer, incrementing the pointer into buffer in the + * process. + */ +static void llvm_cov_copy_section_to_buffer(void **buffer, void *src, + unsigned long size) +{ + memcpy(*buffer, src, size); + *buffer += size; +} + +static unsigned long llvm_cov_get_raw_profile_size(void) +{ + return sizeof(struct __llvm_profile_header) + + __llvm_prf_data_size() + + __llvm_prf_cnts_size() + + __llvm_prf_get_padding(__llvm_prf_cnts_size()) + + __llvm_prf_bits_size() + + __llvm_prf_get_padding(__llvm_prf_bits_size()) + + __llvm_prf_names_size() + + __llvm_prf_get_padding(__llvm_prf_names_size()); +} + +/* + * Serialize in-memory data into a format LLVM tools can understand + * (https://llvm.org/docs/InstrProfileFormat.html#raw-profile-format) + */ +static int llvm_cov_serialize_raw_profile(struct llvm_cov_private_data *p) +{ + int err = 0; + void *buffer; + + p->size = llvm_cov_get_raw_profile_size(); + p->buffer = vzalloc(p->size); + + if (!p->buffer) { + err = -ENOMEM; + goto out; + } + + buffer = p->buffer; + + llvm_cov_fill_raw_profile_header(&buffer); + llvm_cov_copy_section_to_buffer(&buffer, __llvm_prf_data_start, + __llvm_prf_data_size()); + llvm_cov_copy_section_to_buffer(&buffer, __llvm_prf_cnts_start, + __llvm_prf_cnts_size()); + buffer += __llvm_prf_get_padding(__llvm_prf_cnts_size()); + llvm_cov_copy_section_to_buffer(&buffer, __llvm_prf_bits_start, + __llvm_prf_bits_size()); + buffer += __llvm_prf_get_padding(__llvm_prf_bits_size()); + llvm_cov_copy_section_to_buffer(&buffer, __llvm_prf_names_start, + __llvm_prf_names_size()); + buffer += __llvm_prf_get_padding(__llvm_prf_names_size()); + +out: + return err; +} + +/* open() implementation for llvm-cov data file. */ +static int llvm_cov_open(struct inode *inode, struct file *file) +{ + struct llvm_cov_private_data *data; + unsigned long flags; + int err; + + data = kzalloc(sizeof(*data), GFP_KERNEL); + if (!data) { + err = -ENOMEM; + goto out; + } + + flags = llvm_cov_claim_lock(); + + err = llvm_cov_serialize_raw_profile(data); + if (unlikely(err)) { + kfree(data); + goto out_unlock; + } + + file->private_data = data; + +out_unlock: + llvm_cov_release_lock(flags); +out: + return err; +} + +/* read() implementation for llvm-cov data file. */ +static ssize_t llvm_cov_read(struct file *file, char __user *buf, size_t count, + loff_t *ppos) +{ + struct llvm_cov_private_data *data = file->private_data; + + if (!data) + return -EBADF; + + return simple_read_from_buffer(buf, count, ppos, data->buffer, + data->size); +} + +/* release() implementation for llvm-cov data file. */ +static int llvm_cov_release(struct inode *inode, struct file *file) +{ + struct llvm_cov_private_data *data = file->private_data; + + if (data) { + vfree(data->buffer); + kfree(data); + } + + return 0; +} + +static const struct file_operations llvm_cov_data_fops = { + .owner = THIS_MODULE, + .open = llvm_cov_open, + .read = llvm_cov_read, + .llseek = default_llseek, + .release = llvm_cov_release +}; + +/* write() implementation for llvm-cov reset file */ +static ssize_t reset_write(struct file *file, const char __user *addr, + size_t len, loff_t *pos) +{ + unsigned long flags; + + flags = llvm_cov_claim_lock(); + memset(__llvm_prf_cnts_start, 0, __llvm_prf_cnts_size()); + memset(__llvm_prf_bits_start, 0, __llvm_prf_bits_size()); + llvm_cov_release_lock(flags); + + return len; +} + +static const struct file_operations llvm_cov_reset_fops = { + .owner = THIS_MODULE, + .write = reset_write, + .llseek = noop_llseek, +}; + +/* Create debugfs entries. */ +static int __init llvm_cov_init(void) +{ + directory = debugfs_create_dir("llvm-cov", NULL); + if (!directory) + goto err_remove; + + if (!debugfs_create_file("profraw", 0400, directory, NULL, + &llvm_cov_data_fops)) + goto err_remove; + + if (!debugfs_create_file("reset", 0200, directory, NULL, + &llvm_cov_reset_fops)) + goto err_remove; + + return 0; + +err_remove: + debugfs_remove_recursive(directory); + pr_err("initialization failed\n"); + return -EIO; +} + +/* Remove debugfs entries. */ +static void __exit llvm_cov_exit(void) +{ + debugfs_remove_recursive(directory); +} + +module_init(llvm_cov_init); +module_exit(llvm_cov_exit); diff --git a/kernel/llvm-cov/llvm-cov.h b/kernel/llvm-cov/llvm-cov.h new file mode 100644 index 000000000000..d9551a685756 --- /dev/null +++ b/kernel/llvm-cov/llvm-cov.h @@ -0,0 +1,156 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (C) 2019 Sami Tolvanen , Google, Inc. + * Copyright (C) 2024 Jinghao Jia , UIUC + * Copyright (C) 2024 Wentao Zhang , UIUC + * + * This software is licensed under the terms of the GNU General Public + * License version 2, as published by the Free Software Foundation, and + * may be copied, distributed, and modified under those terms. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#ifndef _LLVM_COV_H +#define _LLVM_COV_H + +extern spinlock_t llvm_cov_lock; + +static __always_inline unsigned long llvm_cov_claim_lock(void) +{ + unsigned long flags; + + spin_lock_irqsave(&llvm_cov_lock, flags); + + return flags; +} + +static __always_inline void llvm_cov_release_lock(unsigned long flags) +{ + spin_unlock_irqrestore(&llvm_cov_lock, flags); +} + +/* + * Note: These internal LLVM definitions must match the compiler version. + * See llvm/include/llvm/ProfileData/InstrProfData.inc in LLVM's source code. + */ + +#define INSTR_PROF_RAW_MAGIC_64 \ + ((u64)255 << 56 | \ + (u64)'l' << 48 | \ + (u64)'p' << 40 | \ + (u64)'r' << 32 | \ + (u64)'o' << 24 | \ + (u64)'f' << 16 | \ + (u64)'r' << 8 | \ + (u64)129) + +#if defined(CONFIG_CC_IS_CLANG) && CONFIG_CLANG_VERSION >= 190000 +#define INSTR_PROF_RAW_VERSION 10 +#define INSTR_PROF_DATA_ALIGNMENT 8 +#define IPVK_LAST 2 +#elif defined(CONFIG_CC_IS_CLANG) && CONFIG_CLANG_VERSION >= 180000 +#define INSTR_PROF_RAW_VERSION 9 +#define INSTR_PROF_DATA_ALIGNMENT 8 +#define IPVK_LAST 1 +#endif + +/** + * struct __llvm_profile_header - represents the raw profile header data + * structure. Description of each member can be found here: + * https://llvm.org/docs/InstrProfileFormat.html#header. + */ +struct __llvm_profile_header { + u64 magic; + u64 version; + u64 binary_ids_size; + u64 num_data; + u64 padding_bytes_before_counters; + u64 num_counters; + u64 padding_bytes_after_counters; + u64 num_bitmap_bytes; + u64 padding_bytes_after_bitmap_bytes; + u64 names_size; + u64 counters_delta; + u64 bitmap_delta; + u64 names_delta; +#if defined(CONFIG_CC_IS_CLANG) && CONFIG_CLANG_VERSION >= 190000 + u64 num_v_tables; + u64 v_names_size; +#endif + u64 value_kind_last; +}; + +/** + * struct __llvm_profile_data - represents the per-function control structure. + * Description of each member can be found here: + * https://llvm.org/docs/InstrProfileFormat.html#profile-metadata. To measure + * Source-based Code Coverage, the internals of this struct don't matter at run + * time. The only purpose of the definition below is to run sizeof() against it + * so that we can calculate the "num_data" field in header. + */ +struct __llvm_profile_data { + const u64 name_ref; + const u64 func_hash; + const void *counter_ptr; + const void *bitmap_ptr; + const void *function_pointer; + void *values; + const u32 num_counters; + const u16 num_value_sites[IPVK_LAST + 1]; + const u32 num_bitmap_bytes; +} __aligned(INSTR_PROF_DATA_ALIGNMENT); + +/* Payload sections */ + +extern struct __llvm_profile_data __llvm_prf_data_start[]; +extern struct __llvm_profile_data __llvm_prf_data_end[]; + +extern u64 __llvm_prf_cnts_start[]; +extern u64 __llvm_prf_cnts_end[]; + +extern char __llvm_prf_names_start[]; +extern char __llvm_prf_names_end[]; + +extern char __llvm_prf_bits_start[]; +extern char __llvm_prf_bits_end[]; + +#define __DEFINE_SECTION_SIZE(s) \ + static inline unsigned long __llvm_prf_ ## s ## _size(void) \ + { \ + unsigned long start = \ + (unsigned long)__llvm_prf_ ## s ## _start; \ + unsigned long end = \ + (unsigned long)__llvm_prf_ ## s ## _end; \ + return end - start; \ + } +#define __DEFINE_SECTION_COUNT(s) \ + static inline unsigned long __llvm_prf_ ## s ## _count(void) \ + { \ + return __llvm_prf_ ## s ## _size() / \ + sizeof(__llvm_prf_ ## s ## _start[0]); \ + } + +__DEFINE_SECTION_SIZE(data) +__DEFINE_SECTION_SIZE(cnts) +__DEFINE_SECTION_SIZE(names) +__DEFINE_SECTION_SIZE(bits) + +__DEFINE_SECTION_COUNT(data) +__DEFINE_SECTION_COUNT(cnts) +__DEFINE_SECTION_COUNT(names) +__DEFINE_SECTION_COUNT(bits) + +#undef __DEFINE_SECTION_SIZE +#undef __DEFINE_SECTION_COUNT + +static inline unsigned long __llvm_prf_get_padding(unsigned long size) +{ + return 7 & (sizeof(u64) - size % sizeof(u64)); +} + +#endif /* _LLVM_COV_H */ diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib index fe3668dc4954..b9ceaee34b28 100644 --- a/scripts/Makefile.lib +++ b/scripts/Makefile.lib @@ -158,6 +158,16 @@ _c_flags += $(if $(patsubst n%,, \ $(CFLAGS_GCOV)) endif +# +# Enable Clang's Source-based Code Coverage flags for a file or directory +# depending on variables LLVM_COV_PROFILE_obj.o and LLVM_COV_PROFILE. +# +ifeq ($(CONFIG_LLVM_COV_KERNEL),y) +_c_flags += $(if $(patsubst n%,, \ + $(LLVM_COV_PROFILE_$(basetarget).o)$(LLVM_COV_PROFILE)y), \ + $(CFLAGS_LLVM_COV)) +endif + # # Enable address sanitizer flags for kernel except some files or directories # we don't want to check (depends on variables KASAN_SANITIZE_obj.o, KASAN_SANITIZE) diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index d16d0ace2775..836c2289b8d8 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -743,6 +743,8 @@ static const char *const section_white_list[] = ".gnu.lto*", ".discard.*", ".llvm.call-graph-profile", /* call graph */ + "__llvm_covfun", + "__llvm_covmap", NULL }; From patchwork Sat Aug 24 23:06:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wentao Zhang X-Patchwork-Id: 822782 Received: from mail-io1-f68.google.com (mail-io1-f68.google.com [209.85.166.68]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 823B81448EF for ; Sat, 24 Aug 2024 23:07:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.68 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724540843; cv=none; b=pgEHHA/1Jo1Y6bhzRly++RTmHtfkfbYqu8+XVXPluwC0qSlNUsuLhthnSF5sZqaOol/Yq1s8Z+dmExDn5J76pML7LaG4mElfvUeXTIrT4e1ZBm0zzcHbiKyO9TsMQEcyUcRSBnO1tWjnTiF6ZbKYRAydKjJQ+zXfB2+5f7NdbwU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724540843; c=relaxed/simple; bh=U0S4KY4P4mMk6kXYBXhOMBGppwcV8ZYcGXaIpEav1FE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=dn15+taxofXl32+fW0sTUf3BkkC/2kgtenNsmkr3xXj5TE9E8wV3S2E77MFAb/Qoj2cZrGJvmKZyVsigmglQtpMwEkyJvK7qqDaoBgAFjY52wGYgHHOdiltjmZbaHG6MDOLSKohXI+kLR1osDhpjbxt3JtH2MIOtXKzXP348heM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=illinois.edu; spf=pass smtp.mailfrom=illinois.edu; dkim=pass (2048-bit key) header.d=illinois-edu.20230601.gappssmtp.com header.i=@illinois-edu.20230601.gappssmtp.com header.b=ahNNMuu3; arc=none smtp.client-ip=209.85.166.68 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=illinois.edu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=illinois.edu Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=illinois-edu.20230601.gappssmtp.com header.i=@illinois-edu.20230601.gappssmtp.com header.b="ahNNMuu3" Received: by mail-io1-f68.google.com with SMTP id ca18e2360f4ac-81f96eaa02aso185976939f.2 for ; Sat, 24 Aug 2024 16:07:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=illinois-edu.20230601.gappssmtp.com; s=20230601; t=1724540840; x=1725145640; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=eplaYsiiWgCTy5PRkFB4ff2nqvq9q3OfHUIofrtUZ0k=; b=ahNNMuu3ulrpbVkyPWNKCl2Bk8ogfR3zSqzuxlMpajB5Xtb9UDKhaiBbWY9VhtVWyX VHjl3+vq/kjFVPvUtSp18vNB1H7R+eGUgSRvId+PFwgyZYU63GYLrYYfP0psPHEMWjPV rt4crUFF3Z6Iazfn9GKhSaX0mYqXz6FvftLolcXQjvCMVc9W58g25m8U3ByR5tv2cFih 4WUhggnonBInufetnfhOS605gjMqlNN+lwePT8ho/Dvy5wQaUEHp/xp1B9/Wt7pd8CEs +yF2py+HsyoGKSNqD/MUSqxcALn4fQ8ispcza7Kj0mIzK1CGb/xyV2pqFWjK7seF8Qjb IWkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724540840; x=1725145640; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eplaYsiiWgCTy5PRkFB4ff2nqvq9q3OfHUIofrtUZ0k=; b=sb2nEPdviX0B4uF/9LjgvqAEaOnEnQwhyo4w60x8Pcn8fh2gDOX9x6ZoE0yRN4Zy+i HPc05t/EWytrn/YiIBxDq6uqG7+B3Iuv2gpmV95wq3UHgIB/jRhQKRaU+NzxQElV7RPZ GniQbdBlDuqW2tN2RJzIdb77NPsfOzFYo15M7E74Quoc3jwHwunLZndsWGd4mdXkqe4j B5rg++ss6Yf+DL+CWe4A23CT/LnBfhBnC5themehs4yU+YZi5drzHJz4pxggm5zo5UXU GhWGSOKcV9G7nb0l5Giw0s8rGb0/QuCPemyZjDgkGSxVI2kcJ+Dy5/17yh/uwCJcZhR8 Q+uw== X-Forwarded-Encrypted: i=1; AJvYcCU9mUDWJVftUk15bQWWXRokrd3rkON0NA5byxlOw0G8b2znjyy4IsbpSW9LPdHe5BOxDiuK31JK/D0=@vger.kernel.org X-Gm-Message-State: AOJu0Ywz53Oy0xk6P8Do1atlnbzgrmvEkd5i8ZcfWuf3bnage3+058V6 VQK8DAJWga3hnk+MJVg74EN/V31Yc19dbjD7kv4H06l2F4T2beWwCa9F7P8UDQ== X-Google-Smtp-Source: AGHT+IGMHgrjxuTK1IXh9WwnvBXGAgm3x0MPaDSYXa/O8CNdJcXDMS3NKmaFRHTenHG7ThmwiB9kVg== X-Received: by 2002:a05:6602:6b87:b0:804:2b28:75db with SMTP id ca18e2360f4ac-82787329bbemr590220339f.10.1724540840544; Sat, 24 Aug 2024 16:07:20 -0700 (PDT) Received: from shizuku.. (mobile-130-126-255-62.near.illinois.edu. [130.126.255.62]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-4ce70f85671sm1563938173.80.2024.08.24.16.07.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 24 Aug 2024 16:07:20 -0700 (PDT) From: Wentao Zhang To: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-efi@vger.kernel.org, linux-um@lists.infradead.org, linux-arch@vger.kernel.org, linux-trace-kernel@vger.kernel.org, llvm@lists.linux.dev, x86@kernel.org Cc: wentaoz5@illinois.edu, marinov@illinois.edu, tyxu@illinois.edu, jinghao7@illinois.edu, tingxur@illinois.edu, steven.h.vanderleest@boeing.com, chuck.wolber@boeing.com, matthew.l.weber3@boeing.com, Matt.Kelly2@boeing.com, andrew.j.oppelt@boeing.com, samuel.sarkisian@boeing.com, morbo@google.com, samitolvanen@google.com, masahiroy@kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, luto@kernel.org, ardb@kernel.org, richard@nod.at, anton.ivanov@cambridgegreys.com, johannes@sipsolutions.net, arnd@arndb.de, rostedt@goodmis.org, mhiramat@kernel.org, oberpar@linux.ibm.com, akpm@linux-foundation.org, paulmck@kernel.org, bhelgaas@google.com, kees@kernel.org, jpoimboe@kernel.org, peterz@infradead.org, kent.overstreet@linux.dev, nathan@kernel.org, hpa@zytor.com, mathieu.desnoyers@efficios.com, ndesaulniers@google.com, justinstitt@google.com, maskray@google.com, dvyukov@google.com Subject: [RFC PATCH 2/3] kbuild, llvm-cov: disable instrumentation in odd or sensitive code Date: Sat, 24 Aug 2024 18:06:40 -0500 Message-Id: <20240824230641.385839-3-wentaoz5@illinois.edu> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240824230641.385839-1-wentaoz5@illinois.edu> References: <20240824230641.385839-1-wentaoz5@illinois.edu> Precedence: bulk X-Mailing-List: linux-efi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Disable instrumentation in the same areas that were disabled for kernel/gcov/ Signed-off-by: Wentao Zhang Signed-off-by: Chuck Wolber --- arch/x86/boot/Makefile | 1 + arch/x86/boot/compressed/Makefile | 1 + arch/x86/entry/vdso/Makefile | 1 + arch/x86/platform/efi/Makefile | 1 + arch/x86/purgatory/Makefile | 1 + arch/x86/realmode/rm/Makefile | 1 + arch/x86/um/vdso/Makefile | 1 + drivers/firmware/efi/libstub/Makefile | 2 ++ kernel/trace/Makefile | 1 + scripts/Makefile.modfinal | 1 + 10 files changed, 11 insertions(+) diff --git a/arch/x86/boot/Makefile b/arch/x86/boot/Makefile index 9cc0ff6e9067..2cc2c55af305 100644 --- a/arch/x86/boot/Makefile +++ b/arch/x86/boot/Makefile @@ -57,6 +57,7 @@ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ KBUILD_CFLAGS += $(call cc-option,-fmacro-prefix-map=$(srctree)/=) KBUILD_CFLAGS += -fno-asynchronous-unwind-tables KBUILD_CFLAGS += $(CONFIG_CC_IMPLICIT_FALLTHROUGH) +LLVM_COV_PROFILE := n $(obj)/bzImage: asflags-y := $(SVGA_MODE) diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/Makefile index f2051644de94..86c79c36db2e 100644 --- a/arch/x86/boot/compressed/Makefile +++ b/arch/x86/boot/compressed/Makefile @@ -43,6 +43,7 @@ KBUILD_CFLAGS += -D__DISABLE_EXPORTS # Disable relocation relaxation in case the link is not PIE. KBUILD_CFLAGS += $(call cc-option,-Wa$(comma)-mrelax-relocations=no) KBUILD_CFLAGS += -include $(srctree)/include/linux/hidden.h +LLVM_COV_PROFILE := n # sev.c indirectly includes inat-table.h which is generated during # compilation and stored in $(objtree). Add the directory to the includes so diff --git a/arch/x86/entry/vdso/Makefile b/arch/x86/entry/vdso/Makefile index c9216ac4fb1e..d9587b3b6bf2 100644 --- a/arch/x86/entry/vdso/Makefile +++ b/arch/x86/entry/vdso/Makefile @@ -156,6 +156,7 @@ quiet_cmd_vdso = VDSO $@ VDSO_LDFLAGS = -shared --hash-style=both --build-id=sha1 \ $(call ld-option, --eh-frame-hdr) -Bsymbolic -z noexecstack +LLVM_COV_PROFILE := n quiet_cmd_vdso_and_check = VDSO $@ cmd_vdso_and_check = $(cmd_vdso); $(cmd_vdso_check) diff --git a/arch/x86/platform/efi/Makefile b/arch/x86/platform/efi/Makefile index 500cab4a7f7c..a07852e8f3ae 100644 --- a/arch/x86/platform/efi/Makefile +++ b/arch/x86/platform/efi/Makefile @@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 KASAN_SANITIZE := n GCOV_PROFILE := n +LLVM_COV_PROFILE := n obj-$(CONFIG_EFI) += memmap.o quirks.o efi.o efi_$(BITS).o \ efi_stub_$(BITS).o diff --git a/arch/x86/purgatory/Makefile b/arch/x86/purgatory/Makefile index ebdfd7b84feb..b4e619114898 100644 --- a/arch/x86/purgatory/Makefile +++ b/arch/x86/purgatory/Makefile @@ -28,6 +28,7 @@ PURGATORY_LDFLAGS := -e purgatory_start -z nodefaultlib LDFLAGS_purgatory.ro := -r $(PURGATORY_LDFLAGS) LDFLAGS_purgatory.chk := $(PURGATORY_LDFLAGS) targets += purgatory.ro purgatory.chk +LLVM_COV_PROFILE := n # These are adjustments to the compiler flags used for objects that # make up the standalone purgatory.ro diff --git a/arch/x86/realmode/rm/Makefile b/arch/x86/realmode/rm/Makefile index a0fb39abc5c8..d36338bfa2ce 100644 --- a/arch/x86/realmode/rm/Makefile +++ b/arch/x86/realmode/rm/Makefile @@ -67,3 +67,4 @@ KBUILD_CFLAGS := $(REALMODE_CFLAGS) -D_SETUP -D_WAKEUP \ -I$(srctree)/arch/x86/boot KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ KBUILD_CFLAGS += -fno-asynchronous-unwind-tables +LLVM_COV_PROFILE := n diff --git a/arch/x86/um/vdso/Makefile b/arch/x86/um/vdso/Makefile index 6a77ea6434ff..3ea2f5d123fe 100644 --- a/arch/x86/um/vdso/Makefile +++ b/arch/x86/um/vdso/Makefile @@ -60,3 +60,4 @@ quiet_cmd_vdso = VDSO $@ sh $(src)/checkundef.sh '$(NM)' '$@' VDSO_LDFLAGS = -fPIC -shared -Wl,--hash-style=sysv -z noexecstack +LLVM_COV_PROFILE := n diff --git a/drivers/firmware/efi/libstub/Makefile b/drivers/firmware/efi/libstub/Makefile index ed4e8ddbe76a..b8224ff291d9 100644 --- a/drivers/firmware/efi/libstub/Makefile +++ b/drivers/firmware/efi/libstub/Makefile @@ -62,6 +62,8 @@ KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_LTO), $(KBUILD_CFLAGS)) # `-fdata-sections` flag from KBUILD_CFLAGS_KERNEL KBUILD_CFLAGS_KERNEL := $(filter-out -fdata-sections, $(KBUILD_CFLAGS_KERNEL)) +LLVM_COV_PROFILE := n + lib-y := efi-stub-helper.o gop.o secureboot.o tpm.o \ file.o mem.o random.o randomalloc.o pci.o \ skip_spaces.o lib-cmdline.o lib-ctype.o \ diff --git a/kernel/trace/Makefile b/kernel/trace/Makefile index 057cd975d014..0293acc50afa 100644 --- a/kernel/trace/Makefile +++ b/kernel/trace/Makefile @@ -30,6 +30,7 @@ endif ifdef CONFIG_GCOV_PROFILE_FTRACE GCOV_PROFILE := y endif +LLVM_COV_PROFILE := n # Functions in this file could be invoked from early interrupt # code and produce random code coverage. diff --git a/scripts/Makefile.modfinal b/scripts/Makefile.modfinal index 1fa98b5e952b..4fc791fff26c 100644 --- a/scripts/Makefile.modfinal +++ b/scripts/Makefile.modfinal @@ -22,6 +22,7 @@ __modfinal: $(modules:%.o=%.ko) modname = $(notdir $(@:.mod.o=)) part-of-module = y GCOV_PROFILE := n +LLVM_COV_PROFILE := n KCSAN_SANITIZE := n quiet_cmd_cc_o_c = CC [M] $@ From patchwork Sat Aug 24 23:06:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wentao Zhang X-Patchwork-Id: 822273 Received: from mail-io1-f67.google.com (mail-io1-f67.google.com [209.85.166.67]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 14545146010 for ; Sat, 24 Aug 2024 23:07:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.67 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724540854; cv=none; b=RxrEw+sFP65uMRlj9GGVC48Ab+VR2J6eqJQ5IznBkWt03/VGDBXno/nF86gdBpUbVZ/kVkGT03WVDjpMrMLYBx7apBWEz8vaWt61nfKPq9gW4GOL8a9a7CJpxTav3Tq7OB9wHplgystN/x8ca2LIchdQl3b6iMLyVP96kLJz3R4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724540854; c=relaxed/simple; bh=y7rZxTKpyl0JwzsBx4Lq7x9Sp+C4O7QrJbTBCbHGnoU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=uxQhW3M7XP717yzVD2AQjiyiUvzHlX6neFIvwRpRGZOxW2wYqVm/tUovuJ5s9Xoi4cF5hl+uDImqo6fI/iOrlyq+KvFv2P/MHpM/KlylE18zUUp6KYZPO1hfL3fRW2dnklamu+1xiWSLIGiyFMNG/TL2JnDtoMkHPgUn/Y2e8mo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=illinois.edu; spf=pass smtp.mailfrom=illinois.edu; dkim=pass (2048-bit key) header.d=illinois-edu.20230601.gappssmtp.com header.i=@illinois-edu.20230601.gappssmtp.com header.b=NnKogAAV; arc=none smtp.client-ip=209.85.166.67 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=illinois.edu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=illinois.edu Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=illinois-edu.20230601.gappssmtp.com header.i=@illinois-edu.20230601.gappssmtp.com header.b="NnKogAAV" Received: by mail-io1-f67.google.com with SMTP id ca18e2360f4ac-81f921c40f2so116373039f.0 for ; Sat, 24 Aug 2024 16:07:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=illinois-edu.20230601.gappssmtp.com; s=20230601; t=1724540852; x=1725145652; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=pI1ahIsPt9dhXQGKmSRkAPO4yB+V9yLk9R0g7YeqYis=; b=NnKogAAVgsa4Frhgb3ibrJ4dLtjjzNajsG5zsyO0jpTii+SrxAAUR3BhuB/3VwSCsW em5Kg6MG7JfMk0zHCDNGCcR/+LGYMhWN3e8BpHrP7UlHMWIAX42BOn2L5+2HJMKOZxcn 8z1lPsawUQ8O2U8zPzJ3T50H64Dn/svAKY9IotUi4t71SfN7Ya+jzHpxG+n8cakluBEJ ZIHoN3sUJFqrFuPabV/tnocDYLpTDlTs0tFtpy4EjvVuvR3AN+svR/GMqHGJWGBC/SG/ YeuZaJfwP05r+O7T+t/cg2zi00ihGrbEYaKc8aIVBTQXxy6jBDFsPFxriXKw6w9H9LTR i4XA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724540852; x=1725145652; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=pI1ahIsPt9dhXQGKmSRkAPO4yB+V9yLk9R0g7YeqYis=; b=boel9omubMsrfz36NMo/gsYZJGywO3XkoXGTkVWbdRP/PBm/YYxoB9WTtNZ3HlBIaq PDHeOHaC6vs1I7F+aZmg9E0hP9WaaRyEqZRtLZE+Ia4ZTBZXvbmRlB2zOg/eqLZsO/JJ IzuJ2+MAfv8plAIScNhOdiBIznXszoQbVQdOEUOztKz3lxNDEJuwinxzlXM5EMKxXiO6 d4e7u7pqcCjPGWqpB597ET3IxicDFR8eoPf3OpUvc2hUxxM7/XtB7eKn7eDW4slTK6XQ d1wsM+5WJwXWWZp2A0MzdFlJP1RQSMB2M2chLULTZKbaKYvXZ1BH0rmoin9WRgpXo4PB aHgA== X-Forwarded-Encrypted: i=1; AJvYcCUb38FO7RRsn9HFp3mQz+AyYGrI1btNcF7pcyTJyVghP+WZAHkvwN43+8X/cP/gqNRWaMkbDVeapGk=@vger.kernel.org X-Gm-Message-State: AOJu0YzMUQkY4WUp6Zk5Em0CYQeay3TmuEDKNxYbvzhwyi+VSfTSxm9e V4SEKPzSuMMxuvkct6fl/eZRqbqfuwBn/a+WVchMPYQr9D8SPQ9CeVdjKgCk8g== X-Google-Smtp-Source: AGHT+IFl2rO8s4tG2IK/76OuZILBkQM0bjWjC93REDF+iiyuiQbUEz5yxBnC5GLBkdA4N6JUhLkQxw== X-Received: by 2002:a05:6602:6103:b0:825:2f0:9f74 with SMTP id ca18e2360f4ac-8278739c0famr756294039f.16.1724540852172; Sat, 24 Aug 2024 16:07:32 -0700 (PDT) Received: from shizuku.. (mobile-130-126-255-62.near.illinois.edu. [130.126.255.62]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-4ce70f85671sm1563938173.80.2024.08.24.16.07.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 24 Aug 2024 16:07:31 -0700 (PDT) From: Wentao Zhang To: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-efi@vger.kernel.org, linux-um@lists.infradead.org, linux-arch@vger.kernel.org, linux-trace-kernel@vger.kernel.org, llvm@lists.linux.dev, x86@kernel.org Cc: wentaoz5@illinois.edu, marinov@illinois.edu, tyxu@illinois.edu, jinghao7@illinois.edu, tingxur@illinois.edu, steven.h.vanderleest@boeing.com, chuck.wolber@boeing.com, matthew.l.weber3@boeing.com, Matt.Kelly2@boeing.com, andrew.j.oppelt@boeing.com, samuel.sarkisian@boeing.com, morbo@google.com, samitolvanen@google.com, masahiroy@kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, luto@kernel.org, ardb@kernel.org, richard@nod.at, anton.ivanov@cambridgegreys.com, johannes@sipsolutions.net, arnd@arndb.de, rostedt@goodmis.org, mhiramat@kernel.org, oberpar@linux.ibm.com, akpm@linux-foundation.org, paulmck@kernel.org, bhelgaas@google.com, kees@kernel.org, jpoimboe@kernel.org, peterz@infradead.org, kent.overstreet@linux.dev, nathan@kernel.org, hpa@zytor.com, mathieu.desnoyers@efficios.com, ndesaulniers@google.com, justinstitt@google.com, maskray@google.com, dvyukov@google.com Subject: [RFC PATCH 3/3] llvm-cov: add Clang's MC/DC support Date: Sat, 24 Aug 2024 18:06:41 -0500 Message-Id: <20240824230641.385839-4-wentaoz5@illinois.edu> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240824230641.385839-1-wentaoz5@illinois.edu> References: <20240824230641.385839-1-wentaoz5@illinois.edu> Precedence: bulk X-Mailing-List: linux-efi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add Clang flags and kconfig options for measuring the kernel's modified condition/decision coverage (MC/DC). As of Clang 19, users can determine the max number of conditions in a decision to measure via option LLVM_COV_KERNEL_MCDC_MAX_CONDITIONS, which controls -fmcdc-max-conditions flag of Clang cc1 [1]. Since MC/DC implementation utilizes bitmaps to track the execution of test vectors, more memory is consumed if larger decisions are getting counted. The maximum value supported by Clang is 32767. According to local experiments, the working maximum for Linux kernel is 44, with the largest decisions in kernel codebase (with 45 conditions) excluded, otherwise the kernel image size limit will be exceeded. The largest decisions in kernel are contributed for example by macros checking CPUID. Code exceeding LLVM_COV_KERNEL_MCDC_MAX_CONDITIONS will produce compiler warnings. As of LLVM 19, certain expressions are still not covered, and will produce build warnings when they are encountered: "[...] if a boolean expression is embedded in the nest of another boolean expression but separated by a non-logical operator, this is also not supported. For example, in x = (a && b && c && func(d && f)), the d && f case starts a new boolean expression that is separated from the other conditions by the operator func(). When this is encountered, a warning will be generated and the boolean expression will not be instrumented." [2] [1] https://discourse.llvm.org/t/rfc-coverage-new-algorithm-and-file-format-for-mc-dc/76798 [2] https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#mc-dc-instrumentation Signed-off-by: Wentao Zhang Signed-off-by: Chuck Wolber --- Makefile | 6 ++++++ kernel/llvm-cov/Kconfig | 36 ++++++++++++++++++++++++++++++++++++ scripts/Makefile.lib | 11 +++++++++++ 3 files changed, 53 insertions(+) diff --git a/Makefile b/Makefile index 1750a2b7dfe8..4aa263e5f67f 100644 --- a/Makefile +++ b/Makefile @@ -740,6 +740,12 @@ all: vmlinux CFLAGS_LLVM_COV := -fprofile-instr-generate -fcoverage-mapping export CFLAGS_LLVM_COV +CFLAGS_LLVM_COV_MCDC := -fcoverage-mcdc +ifdef CONFIG_LLVM_COV_KERNEL_MCDC_MAX_CONDITIONS +CFLAGS_LLVM_COV_MCDC += -Xclang -fmcdc-max-conditions=$(CONFIG_LLVM_COV_KERNEL_MCDC_MAX_CONDITIONS) +endif +export CFLAGS_LLVM_COV_MCDC + CFLAGS_GCOV := -fprofile-arcs -ftest-coverage ifdef CONFIG_CC_IS_GCC CFLAGS_GCOV += -fno-tree-loop-im diff --git a/kernel/llvm-cov/Kconfig b/kernel/llvm-cov/Kconfig index 505eba5bd23c..40b6a4fd590e 100644 --- a/kernel/llvm-cov/Kconfig +++ b/kernel/llvm-cov/Kconfig @@ -26,4 +26,40 @@ config LLVM_COV_KERNEL Note that the debugfs filesystem has to be mounted to access the raw profile. +config LLVM_COV_KERNEL_MCDC + bool "Enable measuring modified condition/decision coverage (MC/DC)" + depends on LLVM_COV_KERNEL + depends on CLANG_VERSION >= 180000 + help + This option enables modified condition/decision coverage (MC/DC) + code coverage instrumentation. + + If unsure, say N. + + This will add Clang's Source-based Code Coverage MC/DC + instrumentation to your kernel. As of LLVM 19, certain expressions + are still not covered, and will produce build warnings when they are + encountered. + + "[...] if a boolean expression is embedded in the nest of another + boolean expression but separated by a non-logical operator, this is + also not supported. For example, in + x = (a && b && c && func(d && f)), the d && f case starts a new + boolean expression that is separated from the other conditions by the + operator func(). When this is encountered, a warning will be + generated and the boolean expression will not be instrumented." + + https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#mc-dc-instrumentation + +config LLVM_COV_KERNEL_MCDC_MAX_CONDITIONS + int "Maximum number of conditions in a decision to instrument" + range 6 32767 + depends on LLVM_COV_KERNEL_MCDC + depends on CLANG_VERSION >= 190000 + default "6" + help + This value is passed to "-fmcdc-max-conditions" flag of Clang cc1. + Expressions whose number of conditions is greater than this value will + produce warnings and will not be instrumented. + endmenu diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib index b9ceaee34b28..b8dfad01cb52 100644 --- a/scripts/Makefile.lib +++ b/scripts/Makefile.lib @@ -168,6 +168,17 @@ _c_flags += $(if $(patsubst n%,, \ $(CFLAGS_LLVM_COV)) endif +# +# Flag that turns on modified condition/decision coverage (MC/DC) measurement +# with Clang's Source-based Code Coverage. Enable the flag for a file or +# directory depending on variables LLVM_COV_PROFILE_obj.o and LLVM_COV_PROFILE. +# +ifeq ($(CONFIG_LLVM_COV_KERNEL_MCDC),y) +_c_flags += $(if $(patsubst n%,, \ + $(LLVM_COV_PROFILE_$(basetarget).o)$(LLVM_COV_PROFILE)y), \ + $(CFLAGS_LLVM_COV_MCDC)) +endif + # # Enable address sanitizer flags for kernel except some files or directories # we don't want to check (depends on variables KASAN_SANITIZE_obj.o, KASAN_SANITIZE) From patchwork Thu Sep 5 04:32:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wentao Zhang X-Patchwork-Id: 825691 Received: from mail-qk1-f170.google.com (mail-qk1-f170.google.com [209.85.222.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1B6A114F125 for ; Thu, 5 Sep 2024 04:35:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725510904; cv=none; b=kzqFxQofKf/6mV+muOJYCsxiE4G5VOVBStEASw/DDpA+znAjaOcNsXEOuOQQ7Hv7YBNGzsxX5va8E3b45QxFWZdBKpJDPyw+ZRwyNcUJLqJRSekVy0Af4xLhK7WBCuchmqQsaonoNxpZaeeczJR0Subc3Z4pF6JuEWMXNfd6wcE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725510904; c=relaxed/simple; bh=ZpcEvvudV3zwcYeNP62knioScPXoayjlokROh8sA/AQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=N3ks7P1I9x6CyYI+yTa1dAtdy08Ynz9uK7/iGyfLMQF2JSqDCIy9Y4w5k+RJD93l6SHOS+4xQhymF/wvDVq3wjCY8TuvVFXcW7fvdt+V4wLYCB0Cnfwvq78J6pdpclcaX/0TBqy2Wf/XBLwD9CtY6kQhlYdR5aY+A+F9GGAcj6U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=illinois.edu; spf=pass smtp.mailfrom=illinois.edu; dkim=pass (2048-bit key) header.d=illinois-edu.20230601.gappssmtp.com header.i=@illinois-edu.20230601.gappssmtp.com header.b=ggJtWKRe; arc=none smtp.client-ip=209.85.222.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=illinois.edu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=illinois.edu Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=illinois-edu.20230601.gappssmtp.com header.i=@illinois-edu.20230601.gappssmtp.com header.b="ggJtWKRe" Received: by mail-qk1-f170.google.com with SMTP id af79cd13be357-7a80511d124so21021385a.1 for ; Wed, 04 Sep 2024 21:35:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=illinois-edu.20230601.gappssmtp.com; s=20230601; t=1725510901; x=1726115701; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=mA8QNkfTPcJhmIVKfWV2EgXocZ+TYN3IbgT82p4dkrQ=; b=ggJtWKReyeYUJwzk9EutrB3PRzJYAwLSUUoX6hxshD15mKMMFRkOO85+t+FaW6pBF3 fbV6dxEMfnZqja3GCEnuU6vlY+7gPcXH4vluOUmW7thQGUmAhmSoSe6KVcDIdCtuwv+e 3suRz2eT/QodGjuv37Jt2TH4lbR1ZAuds66a5vARgTktZSojCuogk+W+AD04wt+Co3Ak um+XCcrbn5ZRoW6JaXd+DPjj++Nbsthm2QBYSgQ/Sc+wcqIQ+/ZvDXFSDXW977DuQJZa Lnhd7V93metJ3zlamcGHwubiLvf9GtMOUA0bTO81gMz5d+Wl8Pcian8AjBCxagpKbTsM WJhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725510901; x=1726115701; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mA8QNkfTPcJhmIVKfWV2EgXocZ+TYN3IbgT82p4dkrQ=; b=fbGj/ZtguPWl4MtR5wJZ9fPJgMoQGN3HejToPTYFVheHOTFWe26hshAtn/aNIUP/IJ D7NTqqx8UwbGNYAyQh/fVrHZKMcauE57Jg046kG/DwuqTnANM35olToadwVTX/K12rbe oYWrqazqwi6zCLkfdXNpb615VgmjFBbvKhawXxxwAdVKiUcygNVkqY0vu143By+UYj5M sWrBdZcfqvCKKUgUWpWpgfElDqf3HgEMxzq9+JF7pxQz+xz7e5GkVdKrLN+V5TDkqhQo hqshECn7YiuZq7TUwiLghSCeJR/Sl5nYsLZQsCtp6nP7MoACPEvrGxk/qe5wEHf1ES55 WI/Q== X-Forwarded-Encrypted: i=1; AJvYcCVmpmAIztirOcPqDs1DtoE+tkEGdTuvhEbnUaqq3eo2dJijaOYQeQ14/IVgVKYb2KmxnMJtl453Dl4=@vger.kernel.org X-Gm-Message-State: AOJu0YyGS/B2JqaXhUYjdEUd7LidEO6rugHolIip3W4SSlciM/mmRE1T yybly19wOldw5gHoIq9RjmY9ivWB3qTD/XhvBo/wJybohbEFZtfX15ECM7MWLg== X-Google-Smtp-Source: AGHT+IFRf1D5vt1QAYst2+47RlHDBe/oTQ0pdJOm5kim3EC+oveF5BzPVYhrOWbWoMHiQ4QFDP6QZA== X-Received: by 2002:a05:620a:2a08:b0:79d:55ab:3867 with SMTP id af79cd13be357-7a902f29ac7mr2154420785a.46.1725510900949; Wed, 04 Sep 2024 21:35:00 -0700 (PDT) Received: from node0.kernel3.linux-mcdc-pg0.utah.cloudlab.us ([128.110.218.246]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-45801b4cf4csm4182341cf.48.2024.09.04.21.34.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 21:35:00 -0700 (PDT) From: Wentao Zhang To: wentaoz5@illinois.edu Cc: Matt.Kelly2@boeing.com, akpm@linux-foundation.org, andrew.j.oppelt@boeing.com, anton.ivanov@cambridgegreys.com, ardb@kernel.org, arnd@arndb.de, bhelgaas@google.com, bp@alien8.de, chuck.wolber@boeing.com, dave.hansen@linux.intel.com, dvyukov@google.com, hpa@zytor.com, jinghao7@illinois.edu, johannes@sipsolutions.net, jpoimboe@kernel.org, justinstitt@google.com, kees@kernel.org, kent.overstreet@linux.dev, linux-arch@vger.kernel.org, linux-efi@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-um@lists.infradead.org, llvm@lists.linux.dev, luto@kernel.org, marinov@illinois.edu, masahiroy@kernel.org, maskray@google.com, mathieu.desnoyers@efficios.com, matthew.l.weber3@boeing.com, mhiramat@kernel.org, mingo@redhat.com, morbo@google.com, nathan@kernel.org, ndesaulniers@google.com, oberpar@linux.ibm.com, paulmck@kernel.org, peterz@infradead.org, richard@nod.at, rostedt@goodmis.org, samitolvanen@google.com, samuel.sarkisian@boeing.com, steven.h.vanderleest@boeing.com, tglx@linutronix.de, tingxur@illinois.edu, tyxu@illinois.edu, x86@kernel.org Subject: [PATCH v2 4/4] x86: enable llvm-cov support Date: Wed, 4 Sep 2024 23:32:45 -0500 Message-ID: <20240905043245.1389509-5-wentaoz5@illinois.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240905043245.1389509-1-wentaoz5@illinois.edu> References: <20240824230641.385839-1-wentaoz5@illinois.edu> <20240905043245.1389509-1-wentaoz5@illinois.edu> Precedence: bulk X-Mailing-List: linux-efi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Set ARCH_HAS_* options to "y" in kconfig and include section description in linker script. Signed-off-by: Wentao Zhang Reviewed-by: Chuck Wolber Tested-by: Chuck Wolber Reviewed-by: Nathan Chancellor --- arch/x86/Kconfig | 2 ++ arch/x86/kernel/vmlinux.lds.S | 2 ++ 2 files changed, 4 insertions(+) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 007bab9f2..e0a8f7b42 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -85,6 +85,8 @@ config X86 select ARCH_HAS_FORTIFY_SOURCE select ARCH_HAS_GCOV_PROFILE_ALL select ARCH_HAS_KCOV if X86_64 + select ARCH_HAS_LLVM_COV if X86_64 + select ARCH_HAS_LLVM_COV_PROFILE_ALL if X86_64 select ARCH_HAS_KERNEL_FPU_SUPPORT select ARCH_HAS_MEM_ENCRYPT select ARCH_HAS_MEMBARRIER_SYNC_CORE diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S index 6e73403e8..904337722 100644 --- a/arch/x86/kernel/vmlinux.lds.S +++ b/arch/x86/kernel/vmlinux.lds.S @@ -191,6 +191,8 @@ SECTIONS BUG_TABLE + LLVM_COV_DATA + ORC_UNWIND_TABLE . = ALIGN(PAGE_SIZE);