From nobody Mon May 25 08:11:55 2026 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (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 6B8073016E0 for ; Fri, 15 May 2026 16:22:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778862169; cv=none; b=hHlRNp5xWbyXql0wu9K6Pzv30yk9GQBWWtBHGJDkeQZfdC+HddIHjzWrf0DCGZjDCUMcyJVX9asWolsawIo7f0eDxg2KXVJD/he4An+T79aUbIPToWRojaUdqL882PNSlrh0/LtOXXqd6J8zd2sDeIfqPymCjhVlsFnHfQGAVCs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778862169; c=relaxed/simple; bh=FLpGqs1THR5kJGXBoee1rEx/Yd1bXC+Ja4hnMRG4PHw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:Cc; b=acxbEbUWwcf1BJVZyNH8TxZEgoPXStW/7winTL81ttdYIMAViU63wExvTNLoOFBw3cMcg4BxH+brt6cXPUth4oLiXdBf5Z9NJwbTpfyqu8FDFQVCqp4clwMkC3rg2yl4ofQv8VOwvCCsUwO/sE0NMbUeGmDXgz/qvlKjpEXmJcI= 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=wIYQvgwl; arc=none smtp.client-ip=209.85.128.53 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="wIYQvgwl" Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-48d1c670255so255e9.0 for ; Fri, 15 May 2026 09:22:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1778862166; x=1779466966; 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=qDsUYNhkMlnaZLr5W5A9fXJqYy38c/iFtxlJ3957BR8=; b=wIYQvgwlUAe7SCB/XzTLWMlRQHR2LGWncp4wWzk4fvFJ35I148jRPsbd71Kc9T+cR0 iV4TzJ+l97DYaxoUewHf/O1UnxT8qUZ2IlLoVvogaOCpdpRBOP8IEstz3i9viciU9vEy 7tjN6cujJCFlljtZgrnBSkNFk1jibDT5U2cK765n+0g8FvVxXdsrcsq5dDZgRB/JTOMJ ABEWZLjOGAh3fIeBymixqnpLg9VdyXg2zzabCga79ck7KtUPk4feIMCUhWbizoFdRmY1 U5M4VmB+FSIlj4ugqUCg7S6GbxE7Kuj7sep0iRvbRjej5pFbqDI10hodzEZEiZlUW7dF EPbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778862166; x=1779466966; 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=qDsUYNhkMlnaZLr5W5A9fXJqYy38c/iFtxlJ3957BR8=; b=Ki1BZugJh63oDudKdhSHlkNb9CFXKKeEF8T6S5XV18N68guK5ssMsOY2lfQE9K66qZ m2/T4xUsIpZ4p2+VoE8keqD6+CwhekcvQHzmorkD2f+Noz8YSO3R5/KtZQ5Dfm/ApEak lj3RQUSnGRkLHO6X4dpYNs/QRb4+Lm5eHRKlZnAzYxwwJ3FeaOI/MldtD0QU103VWvZ+ PFiQ8zuQjJoSGQoOGwfRQV3fXEQPnkdZLUfi7djn6nHOV2fPwOmooxfv0WaoYi6eoGbO qM/ITy2ISZdzdQet/V3XLSjPYO2XGDBv0uaaGfXXWlG4TitcfLgOOezPZ/RxAw7ocl7f o2fQ== X-Forwarded-Encrypted: i=1; AFNElJ+rg1eIWM1gHUL+n0hGhdudbHkbW4PXXhL1p/jeia3dqNsGUFnISAxGaoCDQ2i3YHVc8LM5WRLghn2kQr4=@vger.kernel.org X-Gm-Message-State: AOJu0YzA9z2QixdVEfNs3bYAR+34XmmC9puWvDA98OFUQp1IbgjttXZ/ xGDvwnu9wB+i3ScWihkbgKEzH44r6b92+BcU7U2xaVhbbCyIqGkpVgpzzOYLPDqr0g== X-Gm-Gg: Acq92OHD08RbeF49GruJZaNBPXQ30K2K1utItEOu7BqDaM/k5oSj/xy30Xm7HL8xhU4 pPLbgjx0bED1PFcsTgDrOC38FOdDtj5XuUt22pnLyVKnrq+BCedKyq6FP5P4TydCKFBfdtRBWhG QyCzjCEHkYUtlBIOCbAL4RovX3SkVvLH0NrmUHzndzlDhtCgMGI9WCHrtEgxunUpn/4p9fCA68S ClHsF/S/7Dg/lTh16XuM9mufpIzZUxPetIkM+dtFvt5uIJ9941RI7xCBk0+VDNamB113wCC0SkZ tAS2eCRfrLvJMC7lERdxylKOnYTHNz7FQqnHuzbnQSj9iilK4bjZJ8bnyeg82WuA8/CF7VtzCu5 yBJENCLTP/eenZ1cPLTfvLAxuPPzYgn36kr4DACF/3ncJ3WiKY56xlAdKmGWtO/oOEzMqiTjlt/ HwiwfWC+bTp7/vAvSvYLY5nLstoyIAFzO+pe3/WqKWALWMsM2Tr3n37XOP+342Kw== X-Received: by 2002:a05:600c:4d8a:b0:48a:5d55:c194 with SMTP id 5b1f17b1804b1-48fe881b9acmr850525e9.7.1778862165246; Fri, 15 May 2026 09:22:45 -0700 (PDT) Received: from localhost ([2a00:79e0:288a:8:7481:4dac:8e80:6e9b]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48fe5694fbfsm131778665e9.6.2026.05.15.09.22.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 May 2026 09:22:44 -0700 (PDT) From: Jann Horn Date: Fri, 15 May 2026 18:22:30 +0200 Subject: [PATCH v2] 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: <20260515-kcov-simultaneous-remote-v2-1-56fde1cfa509@google.com> X-B4-Tracking: v=1; b=H4sIAEVIB2oC/3WNQQ6CMBBFr0Jm7Zi2EYiuvIdhUcoAE4EhLTQaw t2tuHb5XvLf3yCQZwpwyzbwFDmwTAnMKQPX26kj5CYxGGUKdTFXfDqJGHhch8VOJGtAT6MshFb XrnCtskrXkOazp5ZfR/pRJe45LOLfx1PUX/uL5ir/H40aNdqiVLUtHbWNuXci3UBnJyNU+75/A HFhYEHBAAAA X-Change-ID: 20260429-kcov-simultaneous-remote-a1bc6cf0a01b To: Dmitry Vyukov , Andrey Konovalov , Alexander Potapenko , kasan-dev@googlegroups.com, Andrew Morton Cc: Marco Elver , linux-kernel@vger.kernel.org, Jann Horn X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=ed25519-sha256; t=1778862159; l=8182; i=jannh@google.com; s=20240730; h=from:subject:message-id; bh=FLpGqs1THR5kJGXBoee1rEx/Yd1bXC+Ja4hnMRG4PHw=; b=nDbczvpqNNKXvHXtBTO1ASoxOGjZg9QLdK7oU5Z0RoiLnj6KBZyNPCut3YBtmzs8iupB3QR8/ AF/39UOt7I1D4SE9W16obYrDTzzKoX055wo3FyNUG5plRk28BdwHPqR 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 Reviewed-by: Dmitry Vyukov Signed-off-by: Jann Horn --- (checkpatch complains about %px, but that is preexisting code that I'm just moving around) --- Changes in v2: - remove unused constant KCOV_MODE_REMOTE (dvyukov) - add Reviewed-by from dvyukov - Link to v1: https://lore.kernel.org/r/20260505-kcov-simultaneous-remote-v= 1-1-a670ba7cefd2@google.com --- include/linux/kcov.h | 2 -- include/linux/sched.h | 3 ++ kernel/kcov.c | 94 ++++++++++++++++++++++++++++-------------------= ---- 3 files changed, 55 insertions(+), 44 deletions(-) diff --git a/include/linux/kcov.h b/include/linux/kcov.h index 0143358874b0..58833ad44731 100644 --- a/include/linux/kcov.h +++ b/include/linux/kcov.h @@ -21,8 +21,6 @@ enum kcov_mode { KCOV_MODE_TRACE_PC =3D 2, /* Collecting comparison operands mode. */ KCOV_MODE_TRACE_CMP =3D 3, - /* The process owns a KCOV remote reference. */ - KCOV_MODE_REMOTE =3D 4, }; =20 #define KCOV_IN_CTXSW (1 << 30) 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