From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (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 EFBD73B38BA for ; Tue, 5 May 2026 09:01:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777971673; cv=none; b=BmMhGr38V6c4wIY4Qr/8V0a7dJfr0NS56ChDCd5TWR2UznTbG5MfbZbwgwrmaTyDKEN2eZSQxm93lg8HMO34faJRqAd+Jp9iBGt8eQWW4zEBoE91si8fv3XH6/Xi0QPzEnMBh5CsYcBdO2CPdTClXYsn8SYzEVZffttXXpubK2Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777971673; c=relaxed/simple; bh=2nn5T0FgJzYs2yggghy6YQC1xXU6/pmr2GxZPp75WRI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:Cc; b=bZC+HwO0oZE1Shi/N71gdC4Pdts6GWrbO1zsSVys2X8xf9LsYLsijj0LTCTHZIVwUvCpKOYZHPa1uWOw6Tm2ZPiaJD0AJgqLv8/DLouF6lq2g9rpsOyj76TyJ1RDus4r675/2ELrtC0m3TCssS6K9J9VZeQoKLXfppz45E5gWhY= 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=Zp9MoZJ2; arc=none smtp.client-ip=209.85.128.41 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="Zp9MoZJ2" Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-4891ca4ce02so68265e9.1 for ; Tue, 05 May 2026 02:01:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1777971670; x=1778576470; 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=FkALiswlmEXekVLqKb1FqBfc7/aRh6KqBGKVubgiei4=; b=Zp9MoZJ220xJH9uo6wcTwXucocZWQRQRJbYf0VLPyuaNskdCN5gX7K9/hU22O17QEn FmmKPHRB5NsaObKXhPRGnbDkp62HHpboc80lp8+T9FuOqme9KyoHwyswjnc3naCO/Yp3 a9S2692M7AuiRC4udH1xRPfEqXVa0oMeGUezzzUPKafcyoESwkO4/YsZ3GdJRE7E2JuM fxP7vvPCnCzZjXlC0Tx/Ogr3TfUC2LKR4+3BQGUyfwTfF9l7D+5/QjURlZVuNkHFT7u3 1q4mQBmLDDbyhsm3BjI2E/3KBBVFsJKRKo8W4QcgG882+84qu68qcg6/P2lAZsAbzgD/ 83vQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777971670; x=1778576470; 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=FkALiswlmEXekVLqKb1FqBfc7/aRh6KqBGKVubgiei4=; b=qeEujgsLpflDClb5YAeh1xMG4XsunV6audAThVo3CTjfjrbTJqo9U40Y36FewCxmv8 CykdnnPAcxnbRSN61TVfVyhJs9r0uhyZaF+VG9MwKGRu7vxpqvZzCNx+8SpjOwiX8nW7 V1f4/QUHwPDLXkHfbTna6hYkesS3z67dnPOJU4NU/KiWEE9pEVbOCBEt5mupwzrbIOBZ XdZHIr8zOPTulYac9EWB7V7BrhQPIv/27fwXfYsxvPaIU7XkrVJq8HqPvLS7Cup3JXAt 48Gytn2Pz/kHPjXbCHBlSBqcUcUDU9Qjr/exJqA37CqXB/NuSPPvW5XSauWDigcj/lB1 4NvQ== X-Forwarded-Encrypted: i=1; AFNElJ9Q7YyUsVRuOtn8tCAKytHxG+PK6jb4luBt1Hf02R0pTtvo2atRehvaVGkJR4J5Cg0WU1GhZfDaK/gUXfA=@vger.kernel.org X-Gm-Message-State: AOJu0YxOBDVlE02Ds6DZD1AiWEAWOJ4qUxcxW6BgCnRL78QlnR5eY6Vz 8qN/wzEozv4ogkFC5w+cDh5PpurJJhuVKPTaeEolGuxRtWm8m7Mf8ZLNvwA57Hn26w== X-Gm-Gg: AeBDietIF4O8iTQCy8s7K4TBZk4wdeMxQ048IjhFlIHIHnxfyRuiGNQBh9LohDLSLBm +1fHP1mMp18FditAVTxxDPObXYlsjoOzJ2AHdXqQZ/hR16eAQ6NQpXDjJTt5ukytJPimJtME2vS 599PWo7uGbtlbleo/zqksU0sYwwWPnwuqqLgkozoysNzDIQJl6ShuZUy20DKQSbyqeRKPzbNpTZ nrUWbZ43Kn+xER0qJLb8murjFcFc5nuM4I3JaNUUvUTxHtqzdckGRJlUVpKDkpO7fPMrFvGZqyf NOeXXXkNSngE81cdqXcbX0Vz4Mtl8n+rhfsEiq9WJCYhrFBUZVr5uRwU0TyiTClT63iZ9S3pSTE 7yau0M+HVK6M4GYIAIMNeLL7BBboyL3fX2IFhBKcImsq8/ECgtMKRFCTbrS3aK2+Kdoa20p7QXp WsFUMJT2W3ujIZy50sBPbyt2tEQTfE6lcRLmCJVcHW29boVPlqjTvTBvwnMPqCycAnmQnfwNhV X-Received: by 2002:a7b:c455:0:b0:489:1bbb:3356 with SMTP id 5b1f17b1804b1-48d146f902cmr669365e9.2.1777971669648; Tue, 05 May 2026 02:01:09 -0700 (PDT) Received: from localhost ([2a00:79e0:288a:8:5b6d:9c6e:4891:35b5]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48d14a12ccesm14497505e9.8.2026.05.05.02.01.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 May 2026 02:01:08 -0700 (PDT) From: Jann Horn Date: Tue, 05 May 2026 11:00:46 +0200 Subject: [PATCH resend] 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: 7bit Message-Id: <20260505-kcov-simultaneous-remote-v1-1-a670ba7cefd2@google.com> X-B4-Tracking: v=1; b=H4sIAL2x+WkC/x2N0QrCMAxFf2Xk2UBbZKC/Ij50XeaCrpGmG5PRf zf6eA7ccw9QKkwK1+6AQhsrSzbwpw7SHPODkEdjCC707hwu+EyyofKyvmrMJKtioUUqYfRD6tP kovMD2PxdaOL9n75ZWimPcDc/s1Ypn99ja+0LJ3cZFYEAAAA= 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=1777971662; l=7450; i=jannh@google.com; s=20240730; h=from:subject:message-id; bh=2nn5T0FgJzYs2yggghy6YQC1xXU6/pmr2GxZPp75WRI=; b=i8a9YBGATVNZwLS6tQ+DXSCIcMLEpu71TwuOIQqAeD0fWWyrGh5SkSrmhruS6iaOv/AL2rfbv frAxEC8dPsXBMvQEGRGxvmxhiRJc6V4xLCBYwTiDVoP1D5AEPWGNkk1 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 --- resending because the previous recipient list was incomplete --- 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; + /* KCOV descriptor for remote coverage collection from other tasks: */ + struct kcov *kcov_remote; + /* KCOV common handle for remote coverage collection: */ u64 kcov_handle; 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 kcov *kcov, WRITE_ONCE(t->kcov_mode, mode); } +/* 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 = NULL; } +/* operates on coverage-generator-owned fields */ static void kcov_task_reset(struct task_struct *t) { kcov_stop(t); t->kcov_sequence = 0; - t->kcov_handle = 0; } void kcov_task_init(struct task_struct *t) { kcov_task_reset(t); + t->kcov_remote = NULL; t->kcov_handle = current->kcov_handle; } @@ -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 = 0; + t->kcov_remote = NULL; kcov_remote_reset(kcov); - else + } else { + kcov_task_reset(t); kcov_reset(kcov); + } } static void kcov_get(struct kcov *kcov) @@ -453,41 +458,47 @@ void kcov_task_exit(struct task_struct *t) unsigned long flags; kcov = t->kcov; - if (kcov == NULL) - return; - - spin_lock_irqsave(&kcov->lock, flags); - kcov_debug("t = %px, kcov->t = %px\n", t, kcov->t); - /* - * For KCOV_ENABLE devices we want to make sure that t->kcov->t == t, - * which comes down to: - * WARN_ON(!kcov->remote && kcov->t != 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 != t->kcov->t, and this gives us the following: - * WARN_ON(kcov->remote && kcov->t != t); - * - * 2. The task that created kcov exiting without calling KCOV_DISABLE, - * and then again we make sure that t->kcov->t == t: - * WARN_ON(kcov->remote && kcov->t != t); - * - * By combining all three checks into one we get: - */ - if (WARN_ON(kcov->t != t)) { + if (kcov) { + spin_lock_irqsave(&kcov->lock, flags); + kcov_debug("t = %px, kcov->t = %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 != 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 = t->kcov_remote; + if (kcov) { + spin_lock_irqsave(&kcov->lock, flags); + kcov_debug("t = %px, kcov->t = %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 != 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); } static int kcov_mmap(struct file *filep, struct vm_area_struct *vma) @@ -629,9 +640,9 @@ static int kcov_ioctl_locked(struct kcov *kcov, unsigned int cmd, case KCOV_DISABLE: /* Disable coverage for the current task. */ unused = arg; - if (unused != 0 || current->kcov != kcov) - return -EINVAL; t = current; + if (unused != 0 || (kcov != t->kcov && kcov != t->kcov_remote)) + return -EINVAL; if (WARN_ON(kcov->t != t)) return -EINVAL; kcov_disable(t, kcov); @@ -641,7 +652,7 @@ static int kcov_ioctl_locked(struct kcov *kcov, unsigned int cmd, if (kcov->mode != KCOV_MODE_INIT || !kcov->area) return -EINVAL; t = current; - if (kcov->t != NULL || t->kcov != NULL) + if (kcov->t != NULL || t->kcov_remote != NULL) return -EBUSY; remote_arg = (struct kcov_remote_arg *)arg; mode = kcov_get_mode(remote_arg->trace_mode); @@ -651,8 +662,7 @@ static int kcov_ioctl_locked(struct kcov *kcov, unsigned int cmd, LONG_MAX / sizeof(unsigned long)) return -EINVAL; kcov->mode = mode; - t->kcov = kcov; - t->kcov_mode = KCOV_MODE_REMOTE; + t->kcov_remote = kcov; kcov->t = t; kcov->remote = true; kcov->remote_size = remote_arg->area_size; --- base-commit: 57b8e2d666a31fa201432d58f5fe3469a0dd83ba change-id: 20260429-kcov-simultaneous-remote-a1bc6cf0a01b -- Jann Horn