From nobody Tue Jun 16 15:55:56 2026 Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (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 9D9ED363C55 for ; Thu, 30 Apr 2026 13:50:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777557041; cv=none; b=AUEGeTT5SMg6hTplEYoldHkS7yf03WfEo03HzkCBAtJ4yZXEwx0mlaH7Yb/fsPE7N1m+912xEBPwkEkt760gBuD9vA4fkuH5Ts6vRE3nByLHnRosjuaAYB6SRCLtF5jP4OH/O+/R+aZvmt19rctM46tRqfATGAUxbTjWCYAJ+f8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777557041; c=relaxed/simple; bh=m74wSiVJ5avYbxsHTcFWlhGwydwW7/4X43hAi4kGelw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:Cc; b=Ae112AtUcVrHHDzWkQAgihxjEM6/VFSzCAsEu7M11vdqTcw39+h7L9l4v1/4Tkeo0TdC7h7kSPsxw7aGEypahPRsTs+xFGSH0HZg5F9EqIdOqvsm/RmHrldsmoiYB+pFZQ4/7eAhYkS/lM826RuoR5v1XhbmKkyv1SULkapmeR8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=QQSbSVeW; arc=none smtp.client-ip=209.85.128.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="QQSbSVeW" Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-488940ccfa6so90405e9.1 for ; Thu, 30 Apr 2026 06:50:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1777557038; x=1778161838; darn=vger.kernel.org; h=cc:to:message-id:content-transfer-encoding:mime-version:subject :date:from:from:to:cc:subject:date:message-id:reply-to; bh=4pwiou0ab4KZ5YToJBFfnG2UUIimAwTaNM1IgrvgjaM=; b=QQSbSVeWQFlDVo17fSGf7Orw9SPJir/JZRd6D/u49mk7T59j2j50vu0s/KjFBayY+b ZtNhm0rM9YiubxrVJ0Gyt628TGqkTi0bAptvHl7MTR0aW8RLhzncY6cvG0yquPl9iyIs QRE4VNUi0S5GQTrGnKlrMMpSs4A6Thl0YIiy/kWEKD5D+C5YBc/cToIBDLbDhCrD/+lN ASpU9C7b1SzqSaiJ8/6YmBlvzZxPzlAbKpldC8GzdTcwOszr9TvE6q87IhoQhiDSJt7A uzLn+JfFBO/EvXpZtZUV0Q6nwq5EqkyrtcJ1IUFQbFPMOXt5l6cxiVjM0G3LxY5e7MCi 7DHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777557038; x=1778161838; h=cc:to:message-id:content-transfer-encoding:mime-version:subject :date:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=4pwiou0ab4KZ5YToJBFfnG2UUIimAwTaNM1IgrvgjaM=; b=l26vtM85nJxcjeIQV9jnONY0QBqPxWFHkerNvvl9csEi2kl5vt3bv9RZXTimx6h+aC CUmh/2oVQsEfOEJ3JXxmN6xmgUy/epu/EVPI675erEBXGGueufSeGeTQqAbmMSXzYMZ2 UH4a7Ab/aAs4Vd9jeUFH6664BPWIlgOKAMDg+ntVLxoW+smaSSuiow7d4SzVcBK9OF0l hya5RFlJum5fmA4nu2VHEs1Pj0E2XJ9e9BeiM3Wf89c122dENc+51W0aq8FhR67zNUO0 xsnk1mX1tKuCS9t5lnt28CIZNkK+5vFh/ohMfF+QVQ6Ff6Zfum6enZQE2SWamP/coE2a 2WLQ== X-Gm-Message-State: AOJu0Ywh2E2INO1dJyQzlkwC7YDQ6FB+hLmnWiZ/1acLBKmX1P9dCEhT HsRXU/gQvs6QVaejPthk9sFW+/4Sz2UPHgj8LYsdzmlnZvRFwdlJ0hohKr+2Mb8kqg== X-Gm-Gg: AeBDietS5GIPvGZNgvbWfi7F3oAuxWbURZ5dqQUBP/3bHmhCHK11LIDSXl/95+EoHbr z/ClRMT4RE9DDFaUTSFyHhRNuK8kTYYr2EBsD2NNwDvtn50lys8nQlcXWOpFWaWKTi2amn4z384 meqO2eWqYcEuSz0/c9ZhgJ4TAJdN81GhCtYgGO8Dx3DGACGsxzmnN2zeps+r4PnXRTSwawS/I/D VZx6dDrfuIRoJGMZ+JtQQrthBcGs+VpX4REZYSpNEnhyfKlSbfYAq0MHS0vMUP26KjMSMUBfnBP rumhibE2t9hKF3m9pHQR2NbWYWXuVOvp3ewihsq2M7EyvXaXA+eAbVE+LqNZ7NrQcSs/g+LDIBP hk+PqN36ivBed8b5coriADFJP3VjW7S4TfJeBgJnO3g8vJAXF7PcX7eQbsCMEfaYp13KGcxBSLz nzjF9ArBkE/KIHtZ0x/3qhwsZ5ei9CO8Uu36OI2sXHb7g3epT9MG93fUOXy40qj/oDr0Ubn1Y= X-Received: by 2002:a05:600c:3491:b0:485:1a54:9407 with SMTP id 5b1f17b1804b1-48a82ff5b3amr1370045e9.0.1777557037259; Thu, 30 Apr 2026 06:50:37 -0700 (PDT) Received: from localhost ([2a00:79e0:288a:8:353:619f:ca87:2581]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48a822be902sm75064015e9.6.2026.04.30.06.50.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Apr 2026 06:50:36 -0700 (PDT) From: Jann Horn Date: Thu, 30 Apr 2026 15:50:27 +0200 Subject: [PATCH] kcov: allow simultaneous KCOV_ENABLE/KCOV_REMOTE_ENABLE Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260430-kcov-simultaneous-remote-v1-1-133d5bc9032b@google.com> X-B4-Tracking: v=1; b=H4sIACNe82kC/x3MQQqAIBBA0avErBtQiaCuEi3MphoqDScjiO6et HyL/x8QikwCbfFApIuFg8/QZQFusX4m5DEbjDK1qkyDqwsXCu9pO62nkAQj7eEktHpwtZuUVXq AnB+RJr7/dde/7wfcDlQLagAAAA== X-Change-ID: 20260429-kcov-simultaneous-remote-a1bc6cf0a01b To: Dmitry Vyukov , Andrey Konovalov , kasan-dev@googlegroups.com Cc: linux-kernel@vger.kernel.org, Jann Horn X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=ed25519-sha256; t=1777557033; l=7383; i=jannh@google.com; s=20240730; h=from:subject:message-id; bh=m74wSiVJ5avYbxsHTcFWlhGwydwW7/4X43hAi4kGelw=; b=WO8yGcCYFAyo3dVurOch4mK/jl9sAfZJiSF0cU91nmhZd0lyLnh/7VIRQzqg0ReGZ8jZAhaKE qJ6uVPJh9InD3iN1pqYYZcyD2HGQqOT9PkM8YxPPc0l/Hzy3NvvRxe/ X-Developer-Key: i=jannh@google.com; a=ed25519; pk=AljNtGOzXeF6khBXDJVVvwSEkVDGnnZZYqfWhP1V+C8= Allow the same userspace thread to simultaneously collect normal coverage in syscall context (KCOV_ENABLE) and remote coverage of asynchronous work created by the thread (KCOV_REMOTE_ENABLE). With this, remote KCOV coverage becomes useful for generic fuzzing and not just fuzzing of specific data injection interfaces. This requires that the task_struct::kcov_* fields are separated into ones that are used by the task that generates coverage, and ones that are used by the task that requested remote coverage. To split this up: - Split task_struct::kcov into kcov and kcov_remote. kcov_task_exit() now has to clean up both separately. - Only use task_struct::kcov_mode on the task that generates coverage. - Only reset task_struct::kcov_handle on the task that requested remote coverage. After this change, fields used by the task that generates coverage are: - kcov_mode - kcov_size - kcov_area - kcov - kcov_sequence - kcov_softirq Fields used by the task that requested remote coverage are: - kcov_remote - kcov_handle Signed-off-by: Jann Horn --- include/linux/sched.h | 3 ++ kernel/kcov.c | 94 ++++++++++++++++++++++++++++-------------------= ---- 2 files changed, 55 insertions(+), 42 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index 368c7b4d7cb5..c3bc248a0d27 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1514,6 +1514,9 @@ struct task_struct { /* KCOV descriptor wired with this task or NULL: */ struct kcov *kcov; =20 + /* KCOV descriptor for remote coverage collection from other tasks: */ + struct kcov *kcov_remote; + /* KCOV common handle for remote coverage collection: */ u64 kcov_handle; =20 diff --git a/kernel/kcov.c b/kernel/kcov.c index 0b369e88c7c9..bcbde917d4df 100644 --- a/kernel/kcov.c +++ b/kernel/kcov.c @@ -368,6 +368,7 @@ static void kcov_start(struct task_struct *t, struct kc= ov *kcov, WRITE_ONCE(t->kcov_mode, mode); } =20 +/* operates on coverage-generator-owned fields */ static void kcov_stop(struct task_struct *t) { WRITE_ONCE(t->kcov_mode, KCOV_MODE_DISABLED); @@ -377,16 +378,17 @@ static void kcov_stop(struct task_struct *t) t->kcov_area =3D NULL; } =20 +/* operates on coverage-generator-owned fields */ static void kcov_task_reset(struct task_struct *t) { kcov_stop(t); t->kcov_sequence =3D 0; - t->kcov_handle =3D 0; } =20 void kcov_task_init(struct task_struct *t) { kcov_task_reset(t); + t->kcov_remote =3D NULL; t->kcov_handle =3D current->kcov_handle; } =20 @@ -423,11 +425,14 @@ static void kcov_remote_reset(struct kcov *kcov) static void kcov_disable(struct task_struct *t, struct kcov *kcov) __must_hold(&kcov->lock) { - kcov_task_reset(t); - if (kcov->remote) + if (kcov->remote) { + t->kcov_handle =3D 0; + t->kcov_remote =3D NULL; kcov_remote_reset(kcov); - else + } else { + kcov_task_reset(t); kcov_reset(kcov); + } } =20 static void kcov_get(struct kcov *kcov) @@ -453,41 +458,47 @@ void kcov_task_exit(struct task_struct *t) unsigned long flags; =20 kcov =3D t->kcov; - if (kcov =3D=3D NULL) - return; - - spin_lock_irqsave(&kcov->lock, flags); - kcov_debug("t =3D %px, kcov->t =3D %px\n", t, kcov->t); - /* - * For KCOV_ENABLE devices we want to make sure that t->kcov->t =3D=3D t, - * which comes down to: - * WARN_ON(!kcov->remote && kcov->t !=3D t); - * - * For KCOV_REMOTE_ENABLE devices, the exiting task is either: - * - * 1. A remote task between kcov_remote_start() and kcov_remote_stop(). - * In this case we should print a warning right away, since a task - * shouldn't be exiting when it's in a kcov coverage collection - * section. Here t points to the task that is collecting remote - * coverage, and t->kcov->t points to the thread that created the - * kcov device. Which means that to detect this case we need to - * check that t !=3D t->kcov->t, and this gives us the following: - * WARN_ON(kcov->remote && kcov->t !=3D t); - * - * 2. The task that created kcov exiting without calling KCOV_DISABLE, - * and then again we make sure that t->kcov->t =3D=3D t: - * WARN_ON(kcov->remote && kcov->t !=3D t); - * - * By combining all three checks into one we get: - */ - if (WARN_ON(kcov->t !=3D t)) { + if (kcov) { + spin_lock_irqsave(&kcov->lock, flags); + kcov_debug("t =3D %px, kcov->t =3D %px\n", t, kcov->t); + /* + * This could be a remote task between kcov_remote_start() and + * kcov_remote_stop(). + * In this case we should print a warning right away, since a + * task shouldn't be exiting when it's in a kcov coverage + * collection section. + * + * Otherwise, this should be a task that created a local + * kcov instance and hasn't called KCOV_DISABLE. + * Make sure that t->kcov->t is consistent. + */ + if (WARN_ON(kcov->remote) || WARN_ON(kcov->t !=3D t)) { + spin_unlock_irqrestore(&kcov->lock, flags); + return; + } + /* Just to not leave dangling references behind. */ + kcov_disable(t, kcov); spin_unlock_irqrestore(&kcov->lock, flags); - return; + kcov_put(kcov); + } + kcov =3D t->kcov_remote; + if (kcov) { + spin_lock_irqsave(&kcov->lock, flags); + kcov_debug("t =3D %px, kcov->t =3D %px\n", t, kcov->t); + /* + * This is a KCOV_REMOTE_ENABLE device, and the task is the + * user task which has requested remote coverage collection. + * Make sure that t->kcov->t is consistent. + */ + if (WARN_ON(!kcov->remote) || WARN_ON(kcov->t !=3D t)) { + spin_unlock_irqrestore(&kcov->lock, flags); + return; + } + /* Just to not leave dangling references behind. */ + kcov_disable(t, kcov); + spin_unlock_irqrestore(&kcov->lock, flags); + kcov_put(kcov); } - /* Just to not leave dangling references behind. */ - kcov_disable(t, kcov); - spin_unlock_irqrestore(&kcov->lock, flags); - kcov_put(kcov); } =20 static int kcov_mmap(struct file *filep, struct vm_area_struct *vma) @@ -629,9 +640,9 @@ static int kcov_ioctl_locked(struct kcov *kcov, unsigne= d int cmd, case KCOV_DISABLE: /* Disable coverage for the current task. */ unused =3D arg; - if (unused !=3D 0 || current->kcov !=3D kcov) - return -EINVAL; t =3D current; + if (unused !=3D 0 || (kcov !=3D t->kcov && kcov !=3D t->kcov_remote)) + return -EINVAL; if (WARN_ON(kcov->t !=3D t)) return -EINVAL; kcov_disable(t, kcov); @@ -641,7 +652,7 @@ static int kcov_ioctl_locked(struct kcov *kcov, unsigne= d int cmd, if (kcov->mode !=3D KCOV_MODE_INIT || !kcov->area) return -EINVAL; t =3D current; - if (kcov->t !=3D NULL || t->kcov !=3D NULL) + if (kcov->t !=3D NULL || t->kcov_remote !=3D NULL) return -EBUSY; remote_arg =3D (struct kcov_remote_arg *)arg; mode =3D kcov_get_mode(remote_arg->trace_mode); @@ -651,8 +662,7 @@ static int kcov_ioctl_locked(struct kcov *kcov, unsigne= d int cmd, LONG_MAX / sizeof(unsigned long)) return -EINVAL; kcov->mode =3D mode; - t->kcov =3D kcov; - t->kcov_mode =3D KCOV_MODE_REMOTE; + t->kcov_remote =3D kcov; kcov->t =3D t; kcov->remote =3D true; kcov->remote_size =3D remote_arg->area_size; --- base-commit: 57b8e2d666a31fa201432d58f5fe3469a0dd83ba change-id: 20260429-kcov-simultaneous-remote-a1bc6cf0a01b -- =20 Jann Horn