All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alexander Potapenko <glider@google.com>
To: glider@google.com
Cc: Alexander Viro <viro@zeniv.linux.org.uk>,
	Andrew Morton <akpm@linux-foundation.org>,
	Andrey Konovalov <andreyknvl@google.com>,
	Andy Lutomirski <luto@kernel.org>, Arnd Bergmann <arnd@arndb.de>,
	Borislav Petkov <bp@alien8.de>, Christoph Hellwig <hch@lst.de>,
	Christoph Lameter <cl@linux.com>,
	David Rientjes <rientjes@google.com>,
	Dmitry Vyukov <dvyukov@google.com>,
	Eric Dumazet <edumazet@google.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Herbert Xu <herbert@gondor.apana.org.au>,
	Ilya Leoshkevich <iii@linux.ibm.com>,
	Ingo Molnar <mingo@redhat.com>, Jens Axboe <axboe@kernel.dk>,
	Joonsoo Kim <iamjoonsoo.kim@lge.com>,
	Kees Cook <keescook@chromium.org>, Marco Elver <elver@google.com>,
	Mark Rutland <mark.rutland@arm.com>,
	Matthew Wilcox <willy@infradead.org>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	Pekka Enberg <penberg@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Petr Mladek <pmladek@suse.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Vasily Gorbik <gor@linux.ibm.com>,
	Vegard Nossum <vegard.nossum@oracle.com>,
	Vlastimil Babka <vbabka@suse.cz>,
	linux-mm@kvack.org, linux-arch@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH v2 47/48] x86: kmsan: handle register passing from uninstrumented code
Date: Tue, 29 Mar 2022 14:40:16 +0200	[thread overview]
Message-ID: <20220329124017.737571-48-glider@google.com> (raw)
In-Reply-To: <20220329124017.737571-1-glider@google.com>

Replace instrumentation_begin() with instrumentation_begin_with_regs()
to let KMSAN handle the non-instrumented code and unpoison pt_regs
passed from the instrumented part. This is done to reduce the number of
false positive reports.

Signed-off-by: Alexander Potapenko <glider@google.com>
---
v2:
 -- this patch was previously called "x86: kmsan: handle register
    passing from uninstrumented code". Instead of adding KMSAN-specific
    code to every instrumentation_begin()/instrumentation_end() section,
    we changed instrumentation_begin() to
    instrumentation_begin_with_regs() where applicable.

Link: https://linux-review.googlesource.com/id/I435ec076cd21752c2f877f5da81f5eced62a2ea4
---
 arch/x86/entry/common.c         |  3 ++-
 arch/x86/include/asm/idtentry.h | 10 +++++-----
 arch/x86/kernel/cpu/mce/core.c  |  2 +-
 arch/x86/kernel/kvm.c           |  2 +-
 arch/x86/kernel/nmi.c           |  2 +-
 arch/x86/kernel/sev.c           |  4 ++--
 arch/x86/kernel/traps.c         | 14 +++++++-------
 arch/x86/mm/fault.c             |  2 +-
 8 files changed, 20 insertions(+), 19 deletions(-)

diff --git a/arch/x86/entry/common.c b/arch/x86/entry/common.c
index 6c2826417b337..047d157987859 100644
--- a/arch/x86/entry/common.c
+++ b/arch/x86/entry/common.c
@@ -14,6 +14,7 @@
 #include <linux/mm.h>
 #include <linux/smp.h>
 #include <linux/errno.h>
+#include <linux/kmsan.h>
 #include <linux/ptrace.h>
 #include <linux/export.h>
 #include <linux/nospec.h>
@@ -75,7 +76,7 @@ __visible noinstr void do_syscall_64(struct pt_regs *regs, int nr)
 	add_random_kstack_offset();
 	nr = syscall_enter_from_user_mode(regs, nr);
 
-	instrumentation_begin();
+	instrumentation_begin_with_regs(regs);
 
 	if (!do_syscall_x64(regs, nr) && !do_syscall_x32(regs, nr) && nr != -1) {
 		/* Invalid system call, but still a system call. */
diff --git a/arch/x86/include/asm/idtentry.h b/arch/x86/include/asm/idtentry.h
index 1345088e99025..f24ff33fc3681 100644
--- a/arch/x86/include/asm/idtentry.h
+++ b/arch/x86/include/asm/idtentry.h
@@ -51,7 +51,7 @@ __visible noinstr void func(struct pt_regs *regs)			\
 {									\
 	irqentry_state_t state = irqentry_enter(regs);			\
 									\
-	instrumentation_begin();					\
+	instrumentation_begin_with_regs(regs);				\
 	__##func (regs);						\
 	instrumentation_end();						\
 	irqentry_exit(regs, state);					\
@@ -98,7 +98,7 @@ __visible noinstr void func(struct pt_regs *regs,			\
 {									\
 	irqentry_state_t state = irqentry_enter(regs);			\
 									\
-	instrumentation_begin();					\
+	instrumentation_begin_with_regs(regs);				\
 	__##func (regs, error_code);					\
 	instrumentation_end();						\
 	irqentry_exit(regs, state);					\
@@ -195,7 +195,7 @@ __visible noinstr void func(struct pt_regs *regs,			\
 	irqentry_state_t state = irqentry_enter(regs);			\
 	u32 vector = (u32)(u8)error_code;				\
 									\
-	instrumentation_begin();					\
+	instrumentation_begin_with_regs(regs);				\
 	kvm_set_cpu_l1tf_flush_l1d();					\
 	run_irq_on_irqstack_cond(__##func, regs, vector);		\
 	instrumentation_end();						\
@@ -235,7 +235,7 @@ __visible noinstr void func(struct pt_regs *regs)			\
 {									\
 	irqentry_state_t state = irqentry_enter(regs);			\
 									\
-	instrumentation_begin();					\
+	instrumentation_begin_with_regs(regs);				\
 	kvm_set_cpu_l1tf_flush_l1d();					\
 	run_sysvec_on_irqstack_cond(__##func, regs);			\
 	instrumentation_end();						\
@@ -262,7 +262,7 @@ __visible noinstr void func(struct pt_regs *regs)			\
 {									\
 	irqentry_state_t state = irqentry_enter(regs);			\
 									\
-	instrumentation_begin();					\
+	instrumentation_begin_with_regs(regs);				\
 	__irq_enter_raw();						\
 	kvm_set_cpu_l1tf_flush_l1d();					\
 	__##func (regs);						\
diff --git a/arch/x86/kernel/cpu/mce/core.c b/arch/x86/kernel/cpu/mce/core.c
index 5818b837fd4d4..7b8c43d8727cc 100644
--- a/arch/x86/kernel/cpu/mce/core.c
+++ b/arch/x86/kernel/cpu/mce/core.c
@@ -1355,7 +1355,7 @@ static void queue_task_work(struct mce *m, char *msg, void (*func)(struct callba
 /* Handle unconfigured int18 (should never happen) */
 static noinstr void unexpected_machine_check(struct pt_regs *regs)
 {
-	instrumentation_begin();
+	instrumentation_begin_with_regs(regs);
 	pr_err("CPU#%d: Unexpected int18 (Machine Check)\n",
 	       smp_processor_id());
 	instrumentation_end();
diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c
index d77481ecb0d5f..eaed9b412908c 100644
--- a/arch/x86/kernel/kvm.c
+++ b/arch/x86/kernel/kvm.c
@@ -249,7 +249,7 @@ noinstr bool __kvm_handle_async_pf(struct pt_regs *regs, u32 token)
 		return false;
 
 	state = irqentry_enter(regs);
-	instrumentation_begin();
+	instrumentation_begin_with_regs(regs);
 
 	/*
 	 * If the host managed to inject an async #PF into an interrupt
diff --git a/arch/x86/kernel/nmi.c b/arch/x86/kernel/nmi.c
index 4bce802d25fb1..3f987a5dc38c7 100644
--- a/arch/x86/kernel/nmi.c
+++ b/arch/x86/kernel/nmi.c
@@ -329,7 +329,7 @@ static noinstr void default_do_nmi(struct pt_regs *regs)
 
 	__this_cpu_write(last_nmi_rip, regs->ip);
 
-	instrumentation_begin();
+	instrumentation_begin_with_regs(regs);
 
 	handled = nmi_handle(NMI_LOCAL, regs);
 	__this_cpu_add(nmi_stats.normal, handled);
diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c
index e6d316a01fdd4..9bfc29fc9c983 100644
--- a/arch/x86/kernel/sev.c
+++ b/arch/x86/kernel/sev.c
@@ -1330,7 +1330,7 @@ DEFINE_IDTENTRY_VC_KERNEL(exc_vmm_communication)
 
 	irq_state = irqentry_nmi_enter(regs);
 
-	instrumentation_begin();
+	instrumentation_begin_with_regs(regs);
 
 	if (!vc_raw_handle_exception(regs, error_code)) {
 		/* Show some debug info */
@@ -1362,7 +1362,7 @@ DEFINE_IDTENTRY_VC_USER(exc_vmm_communication)
 	}
 
 	irqentry_enter_from_user_mode(regs);
-	instrumentation_begin();
+	instrumentation_begin_with_regs(regs);
 
 	if (!vc_raw_handle_exception(regs, error_code)) {
 		/*
diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c
index 8143693a7ea6e..f08741abc0e5b 100644
--- a/arch/x86/kernel/traps.c
+++ b/arch/x86/kernel/traps.c
@@ -229,7 +229,7 @@ static noinstr bool handle_bug(struct pt_regs *regs)
 	/*
 	 * All lies, just get the WARN/BUG out.
 	 */
-	instrumentation_begin();
+	instrumentation_begin_with_regs(regs);
 	/*
 	 * Since we're emulating a CALL with exceptions, restore the interrupt
 	 * state to what it was at the exception site.
@@ -260,7 +260,7 @@ DEFINE_IDTENTRY_RAW(exc_invalid_op)
 		return;
 
 	state = irqentry_enter(regs);
-	instrumentation_begin();
+	instrumentation_begin_with_regs(regs);
 	handle_invalid_op(regs);
 	instrumentation_end();
 	irqentry_exit(regs, state);
@@ -414,7 +414,7 @@ DEFINE_IDTENTRY_DF(exc_double_fault)
 #endif
 
 	irqentry_nmi_enter(regs);
-	instrumentation_begin();
+	instrumentation_begin_with_regs(regs);
 	notify_die(DIE_TRAP, str, regs, error_code, X86_TRAP_DF, SIGSEGV);
 
 	tsk->thread.error_code = error_code;
@@ -690,14 +690,14 @@ DEFINE_IDTENTRY_RAW(exc_int3)
 	 */
 	if (user_mode(regs)) {
 		irqentry_enter_from_user_mode(regs);
-		instrumentation_begin();
+		instrumentation_begin_with_regs(regs);
 		do_int3_user(regs);
 		instrumentation_end();
 		irqentry_exit_to_user_mode(regs);
 	} else {
 		irqentry_state_t irq_state = irqentry_nmi_enter(regs);
 
-		instrumentation_begin();
+		instrumentation_begin_with_regs(regs);
 		if (!do_int3(regs))
 			die("int3", regs, 0);
 		instrumentation_end();
@@ -896,7 +896,7 @@ static __always_inline void exc_debug_kernel(struct pt_regs *regs,
 	 */
 	unsigned long dr7 = local_db_save();
 	irqentry_state_t irq_state = irqentry_nmi_enter(regs);
-	instrumentation_begin();
+	instrumentation_begin_with_regs(regs);
 
 	/*
 	 * If something gets miswired and we end up here for a user mode
@@ -975,7 +975,7 @@ static __always_inline void exc_debug_user(struct pt_regs *regs,
 	 */
 
 	irqentry_enter_from_user_mode(regs);
-	instrumentation_begin();
+	instrumentation_begin_with_regs(regs);
 
 	/*
 	 * Start the virtual/ptrace DR6 value with just the DR_STEP mask
diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c
index f2250a32a10ca..676e394f1af5b 100644
--- a/arch/x86/mm/fault.c
+++ b/arch/x86/mm/fault.c
@@ -1557,7 +1557,7 @@ DEFINE_IDTENTRY_RAW_ERRORCODE(exc_page_fault)
 	 */
 	state = irqentry_enter(regs);
 
-	instrumentation_begin();
+	instrumentation_begin_with_regs(regs);
 	handle_page_fault(regs, error_code, address);
 	instrumentation_end();
 
-- 
2.35.1.1021.g381101b075-goog


  parent reply	other threads:[~2022-03-29 12:55 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-29 12:39 [PATCH v2 00/48] Add KernelMemorySanitizer infrastructure Alexander Potapenko
2022-03-29 12:39 ` [PATCH v2 01/48] x86: add missing include to sparsemem.h Alexander Potapenko
2022-03-29 12:39 ` [PATCH v2 02/48] stackdepot: reserve 5 extra bits in depot_stack_handle_t Alexander Potapenko
2022-03-29 12:39 ` [PATCH v2 03/48] kasan: common: adapt to the new prototype of __stack_depot_save() Alexander Potapenko
2022-03-29 12:39 ` [PATCH v2 04/48] instrumented.h: allow instrumenting both sides of copy_from_user() Alexander Potapenko
2022-03-29 12:39 ` [PATCH v2 05/48] x86: asm: instrument usercopy in get_user() and __put_user_size() Alexander Potapenko
2022-03-29 12:39 ` [PATCH v2 06/48] asm-generic: instrument usercopy in cacheflush.h Alexander Potapenko
2022-03-29 12:39 ` [PATCH v2 07/48] kmsan: add ReST documentation Alexander Potapenko
2022-03-29 12:39 ` [PATCH v2 08/48] kmsan: introduce __no_sanitize_memory and __no_kmsan_checks Alexander Potapenko
2022-03-29 12:39 ` [PATCH v2 09/48] kmsan: mark noinstr as __no_sanitize_memory Alexander Potapenko
2022-03-29 12:39 ` [PATCH v2 10/48] x86: kmsan: pgtable: reduce vmalloc space Alexander Potapenko
2022-03-29 12:39 ` [PATCH v2 11/48] libnvdimm/pfn_dev: increase MAX_STRUCT_PAGE_SIZE Alexander Potapenko
2022-03-29 12:39 ` [PATCH v2 12/48] kcsan: clang: retire CONFIG_KCSAN_KCOV_BROKEN Alexander Potapenko
2022-03-30  6:00   ` Marco Elver
2022-03-29 12:39 ` [PATCH v2 13/48] kmsan: add KMSAN runtime core Alexander Potapenko
2022-03-30  8:58   ` Peter Zijlstra
2022-03-30  9:41     ` Peter Zijlstra
2022-04-04 14:39   ` Alexander Potapenko
2022-03-29 12:39 ` [PATCH v2 14/48] kmsan: implement kmsan_init(), initialize READ_ONCE_NOCHECK() Alexander Potapenko
2022-03-29 12:39 ` [PATCH v2 15/48] kmsan: disable instrumentation of unsupported common kernel code Alexander Potapenko
2022-03-29 12:39 ` [PATCH v2 16/48] MAINTAINERS: add entry for KMSAN Alexander Potapenko
2022-03-29 12:39 ` [PATCH v2 17/48] kmsan: mm: maintain KMSAN metadata for page operations Alexander Potapenko
2022-03-29 12:39 ` [PATCH v2 18/48] kmsan: mm: call KMSAN hooks from SLUB code Alexander Potapenko
2022-03-29 12:39 ` [PATCH v2 19/48] kmsan: handle task creation and exiting Alexander Potapenko
2022-03-29 12:39 ` [PATCH v2 20/48] kmsan: init: call KMSAN initialization routines Alexander Potapenko
2022-03-29 12:39 ` [PATCH v2 21/48] instrumented.h: add KMSAN support Alexander Potapenko
2022-03-29 12:39 ` [PATCH v2 22/48] kmsan: unpoison @tlb in arch_tlb_gather_mmu() Alexander Potapenko
2022-03-29 12:39 ` [PATCH v2 23/48] kmsan: add iomap support Alexander Potapenko
2022-03-29 12:39 ` [PATCH v2 24/48] Input: libps2: mark data received in __ps2_command() as initialized Alexander Potapenko
2022-03-29 12:39 ` [PATCH v2 25/48] kmsan: dma: unpoison DMA mappings Alexander Potapenko
2022-03-29 12:39 ` [PATCH v2 26/48] kmsan: virtio: check/unpoison scatterlist in vring_map_one_sg() Alexander Potapenko
2022-03-29 12:39 ` [PATCH v2 27/48] kmsan: handle memory sent to/from USB Alexander Potapenko
2022-03-29 12:39 ` [PATCH v2 28/48] kmsan: instrumentation.h: add instrumentation_begin_with_regs() Alexander Potapenko
2022-03-29 12:39 ` [PATCH v2 29/48] kmsan: entry: handle register passing from uninstrumented code Alexander Potapenko
2022-03-29 12:39 ` [PATCH v2 30/48] kmsan: add tests for KMSAN Alexander Potapenko
2022-03-29 12:40 ` [PATCH v2 31/48] kernel: kmsan: don't instrument stacktrace.c Alexander Potapenko
2022-04-04 14:56   ` Alexander Potapenko
2022-03-29 12:40 ` [PATCH v2 32/48] kmsan: disable strscpy() optimization under KMSAN Alexander Potapenko
2022-03-29 12:40 ` [PATCH v2 33/48] crypto: kmsan: disable accelerated configs " Alexander Potapenko
2022-03-29 12:40 ` [PATCH v2 34/48] kmsan: disable physical page merging in biovec Alexander Potapenko
2022-03-29 12:40 ` [PATCH v2 35/48] kmsan: block: skip bio block merging logic for KMSAN Alexander Potapenko
2022-03-29 12:40 ` [PATCH v2 36/48] kmsan: kcov: unpoison area->list in kcov_remote_area_put() Alexander Potapenko
2022-03-29 12:40 ` [PATCH v2 37/48] security: kmsan: fix interoperability with auto-initialization Alexander Potapenko
2022-03-29 12:40 ` [PATCH v2 38/48] objtool: kmsan: list KMSAN API functions as uaccess-safe Alexander Potapenko
2022-03-30  8:46   ` Peter Zijlstra
2022-04-14 15:30     ` Alexander Potapenko
2022-04-14 15:38       ` Peter Zijlstra
2022-03-29 12:40 ` [PATCH v2 39/48] x86: kmsan: make READ_ONCE_TASK_STACK() return initialized values Alexander Potapenko
2022-03-29 12:40 ` [PATCH v2 40/48] x86: kmsan: disable instrumentation of unsupported code Alexander Potapenko
2022-03-29 12:40 ` [PATCH v2 41/48] x86: kmsan: skip shadow checks in __switch_to() Alexander Potapenko
2022-03-29 12:40 ` [PATCH v2 42/48] x86: kmsan: handle open-coded assembly in lib/iomem.c Alexander Potapenko
2022-03-29 12:40 ` [PATCH v2 43/48] x86: kmsan: use __msan_ string functions where possible Alexander Potapenko
2022-03-29 12:40 ` [PATCH v2 44/48] x86: kmsan: sync metadata pages on page fault Alexander Potapenko
2022-03-29 12:40 ` [PATCH v2 45/48] x86: kasan: kmsan: support CONFIG_GENERIC_CSUM on x86, enable it for KASAN/KMSAN Alexander Potapenko
2022-03-29 12:40 ` [PATCH v2 46/48] x86: fs: kmsan: disable CONFIG_DCACHE_WORD_ACCESS Alexander Potapenko
2022-03-29 12:40 ` Alexander Potapenko [this message]
2022-03-29 12:40 ` [PATCH v2 48/48] x86: kmsan: enable KMSAN builds for x86 Alexander Potapenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220329124017.737571-48-glider@google.com \
    --to=glider@google.com \
    --cc=akpm@linux-foundation.org \
    --cc=andreyknvl@google.com \
    --cc=arnd@arndb.de \
    --cc=axboe@kernel.dk \
    --cc=bp@alien8.de \
    --cc=cl@linux.com \
    --cc=dvyukov@google.com \
    --cc=edumazet@google.com \
    --cc=elver@google.com \
    --cc=gor@linux.ibm.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=hch@lst.de \
    --cc=herbert@gondor.apana.org.au \
    --cc=iamjoonsoo.kim@lge.com \
    --cc=iii@linux.ibm.com \
    --cc=keescook@chromium.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=luto@kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=mst@redhat.com \
    --cc=penberg@kernel.org \
    --cc=peterz@infradead.org \
    --cc=pmladek@suse.com \
    --cc=rientjes@google.com \
    --cc=rostedt@goodmis.org \
    --cc=tglx@linutronix.de \
    --cc=vbabka@suse.cz \
    --cc=vegard.nossum@oracle.com \
    --cc=viro@zeniv.linux.org.uk \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.