mm-commits.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@linux-foundation.org>
To: tarasmadan@google.com, glider@google.com, elver@google.com,
	dvyukov@google.com, bigeasy@linutronix.de, andreyknvl@gmail.com,
	nogikh@google.com, akpm@linux-foundation.org,
	patches@lists.linux.dev, linux-mm@kvack.org,
	mm-commits@vger.kernel.org, torvalds@linux-foundation.org,
	akpm@linux-foundation.org
Subject: [patch 38/41] kcov: split ioctl handling into locked and unlocked parts
Date: Wed, 23 Mar 2022 16:07:12 -0700	[thread overview]
Message-ID: <20220323230713.3C3B1C340EE@smtp.kernel.org> (raw)
In-Reply-To: <20220323160453.65922ced539cbf445b191555@linux-foundation.org>

From: Aleksandr Nogikh <nogikh@google.com>
Subject: kcov: split ioctl handling into locked and unlocked parts

Patch series "kcov: improve mmap processing", v3.

Subsequent mmaps of the same kcov descriptor currently do not update the
virtual memory of the task and yet return 0 (success).  This is
counter-intuitive and may lead to unexpected memory access errors.

Also, this unnecessarily limits the functionality of kcov to only the
simplest usage scenarios.  Kcov instances are effectively forever attached
to their first address spaces and it becomes impossible to e.g.  reuse the
same kcov handle in forked child processes without mmapping the memory
first.  This is exactly what we tried to do in syzkaller and inadvertently
came upon this behavior.

This patch series addresses the problem described above.


This patch (of 3):

Currently all ioctls are de facto processed under a spinlock in order to
serialise them.  This, however, prohibits the use of vmalloc and other
memory management functions in the implementations of those ioctls,
unnecessary complicating any further changes to the code.

Let all ioctls first be processed inside the kcov_ioctl() function which
should execute the ones that are not compatible with spinlock and then
pass control to kcov_ioctl_locked() for all other ones. 
KCOV_REMOTE_ENABLE is processed both in kcov_ioctl() and
kcov_ioctl_locked() as the steps are easily separable.

Although it is still compatible with a spinlock, move KCOV_INIT_TRACE
handling to kcov_ioctl(), so that the changes from the next commit are
easier to follow.

Link: https://lkml.kernel.org/r/20220117153634.150357-1-nogikh@google.com
Link: https://lkml.kernel.org/r/20220117153634.150357-2-nogikh@google.com
Signed-off-by: Aleksandr Nogikh <nogikh@google.com>
Reviewed-by: Dmitry Vyukov <dvyukov@google.com>
Reviewed-by: Andrey Konovalov <andreyknvl@gmail.com>
Cc: Marco Elver <elver@google.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Taras Madan <tarasmadan@google.com>
Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
---

 kernel/kcov.c |   68 ++++++++++++++++++++++++++----------------------
 1 file changed, 37 insertions(+), 31 deletions(-)

--- a/kernel/kcov.c~kcov-split-ioctl-handling-into-locked-and-unlocked-parts
+++ a/kernel/kcov.c
@@ -564,31 +564,12 @@ static int kcov_ioctl_locked(struct kcov
 			     unsigned long arg)
 {
 	struct task_struct *t;
-	unsigned long size, unused;
+	unsigned long flags, unused;
 	int mode, i;
 	struct kcov_remote_arg *remote_arg;
 	struct kcov_remote *remote;
-	unsigned long flags;
 
 	switch (cmd) {
-	case KCOV_INIT_TRACE:
-		/*
-		 * Enable kcov in trace mode and setup buffer size.
-		 * Must happen before anything else.
-		 */
-		if (kcov->mode != KCOV_MODE_DISABLED)
-			return -EBUSY;
-		/*
-		 * Size must be at least 2 to hold current position and one PC.
-		 * Later we allocate size * sizeof(unsigned long) memory,
-		 * that must not overflow.
-		 */
-		size = arg;
-		if (size < 2 || size > INT_MAX / sizeof(unsigned long))
-			return -EINVAL;
-		kcov->size = size;
-		kcov->mode = KCOV_MODE_INIT;
-		return 0;
 	case KCOV_ENABLE:
 		/*
 		 * Enable coverage for the current task.
@@ -692,9 +673,32 @@ static long kcov_ioctl(struct file *file
 	struct kcov_remote_arg *remote_arg = NULL;
 	unsigned int remote_num_handles;
 	unsigned long remote_arg_size;
-	unsigned long flags;
+	unsigned long size, flags;
 
-	if (cmd == KCOV_REMOTE_ENABLE) {
+	kcov = filep->private_data;
+	switch (cmd) {
+	case KCOV_INIT_TRACE:
+		/*
+		 * Enable kcov in trace mode and setup buffer size.
+		 * Must happen before anything else.
+		 *
+		 * First check the size argument - it must be at least 2
+		 * to hold the current position and one PC. Later we allocate
+		 * size * sizeof(unsigned long) memory, that must not overflow.
+		 */
+		size = arg;
+		if (size < 2 || size > INT_MAX / sizeof(unsigned long))
+			return -EINVAL;
+		spin_lock_irqsave(&kcov->lock, flags);
+		if (kcov->mode != KCOV_MODE_DISABLED) {
+			spin_unlock_irqrestore(&kcov->lock, flags);
+			return -EBUSY;
+		}
+		kcov->size = size;
+		kcov->mode = KCOV_MODE_INIT;
+		spin_unlock_irqrestore(&kcov->lock, flags);
+		return 0;
+	case KCOV_REMOTE_ENABLE:
 		if (get_user(remote_num_handles, (unsigned __user *)(arg +
 				offsetof(struct kcov_remote_arg, num_handles))))
 			return -EFAULT;
@@ -710,16 +714,18 @@ static long kcov_ioctl(struct file *file
 			return -EINVAL;
 		}
 		arg = (unsigned long)remote_arg;
+		fallthrough;
+	default:
+		/*
+		 * All other commands can be normally executed under a spin lock, so we
+		 * obtain and release it here in order to simplify kcov_ioctl_locked().
+		 */
+		spin_lock_irqsave(&kcov->lock, flags);
+		res = kcov_ioctl_locked(kcov, cmd, arg);
+		spin_unlock_irqrestore(&kcov->lock, flags);
+		kfree(remote_arg);
+		return res;
 	}
-
-	kcov = filep->private_data;
-	spin_lock_irqsave(&kcov->lock, flags);
-	res = kcov_ioctl_locked(kcov, cmd, arg);
-	spin_unlock_irqrestore(&kcov->lock, flags);
-
-	kfree(remote_arg);
-
-	return res;
 }
 
 static const struct file_operations kcov_fops = {
_

  parent reply	other threads:[~2022-03-23 23:07 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-23 23:04 incoming Andrew Morton
2022-03-23 23:05 ` [patch 01/41] proc: alloc PATH_MAX bytes for /proc/${pid}/fd/ symlinks Andrew Morton
2022-03-23 23:05 ` [patch 02/41] proc/vmcore: fix possible deadlock on concurrent mmap and read Andrew Morton
2022-03-23 23:05 ` [patch 03/41] proc/vmcore: fix vmcore_alloc_buf() kernel-doc comment Andrew Morton
2022-03-23 23:05 ` [patch 04/41] linux/types.h: remove unnecessary __bitwise__ Andrew Morton
2022-03-23 23:05 ` [patch 05/41] Documentation/sparse: add hints about __CHECKER__ Andrew Morton
2022-03-23 23:05 ` [patch 06/41] kernel/ksysfs.c: use helper macro __ATTR_RW Andrew Morton
2022-03-23 23:05 ` [patch 07/41] Kconfig.debug: make DEBUG_INFO selectable from a choice Andrew Morton
2022-03-23 23:05 ` [patch 08/41] include: drop pointless __compiler_offsetof indirection Andrew Morton
2022-03-23 23:05 ` [patch 09/41] ilog2: force inlining of __ilog2_u32() and __ilog2_u64() Andrew Morton
2022-03-23 23:05 ` [patch 10/41] bitfield: add explicit inclusions to the example Andrew Morton
2022-03-23 23:05 ` [patch 11/41] lib/Kconfig.debug: add ARCH dependency for FUNCTION_ALIGN option Andrew Morton
2022-03-23 23:05 ` [patch 12/41] lib: bitmap: fix many kernel-doc warnings Andrew Morton
2022-03-23 23:05 ` [patch 13/41] checkpatch: prefer MODULE_LICENSE("GPL") over MODULE_LICENSE("GPL v2") Andrew Morton
2022-03-23 23:05 ` [patch 14/41] checkpatch: add --fix option for some TRAILING_STATEMENTS Andrew Morton
2022-03-23 23:06 ` [patch 15/41] checkpatch: add early_param exception to blank line after struct/function test Andrew Morton
2022-03-23 23:06 ` [patch 16/41] checkpatch: use python3 to find codespell dictionary Andrew Morton
2022-03-23 23:06 ` [patch 17/41] init: use ktime_us_delta() to make initcall_debug log more precise Andrew Morton
2022-03-23 23:06 ` [patch 18/41] init.h: improve __setup and early_param documentation Andrew Morton
2022-03-23 23:06 ` [patch 19/41] init/main.c: return 1 from handled __setup() functions Andrew Morton
2022-03-23 23:06 ` [patch 20/41] fs/pipe: use kvcalloc to allocate a pipe_buffer array Andrew Morton
2022-03-23 23:06 ` [patch 21/41] fs/pipe.c: local vars have to match types of proper pipe_inode_info fields Andrew Morton
2022-03-23 23:06 ` [patch 22/41] minix: fix bug when opening a file with O_DIRECT Andrew Morton
2022-03-23 23:06 ` [patch 23/41] fat: use pointer to simple type in put_user() Andrew Morton
2022-03-23 23:06 ` [patch 24/41] cgroup: use irqsave in cgroup_rstat_flush_locked() Andrew Morton
2022-03-23 23:06 ` [patch 25/41] kexec: make crashk_res, crashk_low_res and crash_notes symbols always visible Andrew Morton
2022-03-23 23:06 ` [patch 26/41] riscv: mm: init: use IS_ENABLED(CONFIG_KEXEC_CORE) instead of #ifdef Andrew Morton
2022-03-23 23:06 ` [patch 27/41] x86/setup: " Andrew Morton
2022-03-23 23:06 ` [patch 28/41] arm64: mm: " Andrew Morton
2022-03-23 23:06 ` [patch 29/41] docs: kdump: update description about sysfs file system support Andrew Morton
2022-03-23 23:06 ` [patch 30/41] docs: kdump: add scp example to write out the dump file Andrew Morton
2022-03-23 23:06 ` [patch 31/41] panic: unset panic_on_warn inside panic() Andrew Morton
2022-03-23 23:06 ` [patch 32/41] ubsan: no need to unset panic_on_warn in ubsan_epilogue() Andrew Morton
2022-03-23 23:06 ` [patch 33/41] kasan: no need to unset panic_on_warn in end_report() Andrew Morton
2022-03-23 23:07 ` [patch 34/41] taskstats: remove unneeded dead assignment Andrew Morton
2022-03-23 23:07 ` [patch 35/41] docs: sysctl/kernel: add missing bit to panic_print Andrew Morton
2022-03-23 23:07 ` [patch 36/41] panic: add option to dump all CPUs backtraces in panic_print Andrew Morton
2022-03-23 23:07 ` [patch 37/41] panic: move panic_print before kmsg dumpers Andrew Morton
2022-03-23 23:07 ` Andrew Morton [this message]
2022-03-23 23:07 ` [patch 39/41] kcov: properly handle subsequent mmap calls Andrew Morton
2022-03-23 23:07 ` [patch 40/41] kernel/resource: fix kfree() of bootmem memory again Andrew Morton
2022-03-23 23:07 ` [patch 41/41] Revert "ubsan, kcsan: Don't combine sanitizer with kcov on clang" Andrew Morton

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=20220323230713.3C3B1C340EE@smtp.kernel.org \
    --to=akpm@linux-foundation.org \
    --cc=andreyknvl@gmail.com \
    --cc=bigeasy@linutronix.de \
    --cc=dvyukov@google.com \
    --cc=elver@google.com \
    --cc=glider@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mm-commits@vger.kernel.org \
    --cc=nogikh@google.com \
    --cc=patches@lists.linux.dev \
    --cc=tarasmadan@google.com \
    --cc=torvalds@linux-foundation.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).