Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B87523515DA for ; Tue, 25 Aug 2026 19:48:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787687296; cv=none; b=CBmz/HrViWIR/fbA9Mvkf63R6nw+BEAPzH1Vu5m3TqL3XAhfILKDnmEgveKLTi9wSPqPL3tO8ZqXype0o1xjGMwlKSms4nwbvgrDCRtYZIVOM8EAvX47AQYIjJqSJjjYv3TcRccyZsPqw3xBvsR/KHs0vudQSldGV0x+NcokkBA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787687296; c=relaxed/simple; bh=XnUB5V8+EVrlmxend2wsqxc2qsxBGEbEmKFm1Za6odQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bcug5g9d0ijH+oaqdx6IZr4AjDZcDq740x1K/jTBov4OL+/KuTeZbSz2fAUKLvVxWL5nyfZDpQ77hOl/MAR5OmDgfVaRVC+ILAEybIxgLKZ8aqDXWcw8IDPVY4lAeQKFc+ByB8Vd1S18XboD92b4cHcgFAvLbKFfxh3Gjn1bAxM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=E4SnWwHA; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="E4SnWwHA" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6C7E81F000E9; Tue, 25 Aug 2026 19:48:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1787687294; bh=D0O0Q5xQaPLFvuV1oad8CevmE+Kt2IiPFv/c4YUGFbE=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=E4SnWwHAOnG1Ozg5mCfhbPma2bie0CX54bw5hMY4x0d45D9Y/OUpykWHMpRSM3AS5 MV1T4Uca/Hox/ha3zRSBcNL1eiBRh3/atrZFQOv/ApOcyl2XCtytUNk5BFJoMC26ZG 9MaykoUvTSpoV7YdWTFUOxWtyJnzP2HVr/mPXGsJwuP/ynRmeKbj89bUmN4U7ghbL4 IUxyBjvDLaUK1pmLrM+/JTlGRUKyma1bxeLHdk7neLLjgjUn7E+tEcvqkxCv9Pd9zR Sg1FqTD9/e2QXaDmCv3bBWaiPLTQZJxlI3VChweumFgg3ph24Uta8kRxnx5YWOQ8/P mWI7LGFjXpTPA== From: Sasha Levin To: stable@vger.kernel.org Cc: Sebastian Andrzej Siewior , Clark Williams , Dmitry Vyukov , Marco Elver , Andrey Konovalov , Steven Rostedt , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH 5.15.y 1/2] kcov: replace local_irq_save() with a local_lock_t Date: Tue, 25 Aug 2026 15:48:10 -0400 Message-ID: <20260825194811.376587-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026082427-contour-certify-9d3c@gregkh> References: <2026082427-contour-certify-9d3c@gregkh> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Sebastian Andrzej Siewior [ Upstream commit d5d2c51f1e5f56ed01d2c773974630c007e5e5f5 ] The kcov code mixes local_irq_save() and spin_lock() in kcov_remote_{start|end}(). This creates a warning on PREEMPT_RT because local_irq_save() disables interrupts and spin_lock_t is turned into a sleeping lock which can not be acquired in a section with disabled interrupts. The kcov_remote_lock is used to synchronize the access to the hash-list kcov_remote_map. The local_irq_save() block protects access to the per-CPU data kcov_percpu_data. There is no compelling reason to change the lock type to raw_spin_lock_t to make it work with local_irq_save(). Changing it would require to move memory allocation (in kcov_remote_add()) and deallocation outside of the locked section. Adding an unlimited amount of entries to the hashlist will increase the IRQ-off time during lookup. It could be argued that this is debug code and the latency does not matter. There is however no need to do so and it would allow to use this facility in an RT enabled build. Using a local_lock_t instead of local_irq_save() has the befit of adding a protection scope within the source which makes it obvious what is protected. On a !PREEMPT_RT && !LOCKDEP build the local_lock_irqsave() maps directly to local_irq_save() so there is overhead at runtime. Replace the local_irq_save() section with a local_lock_t. Link: https://lkml.kernel.org/r/20210923164741.1859522-6-bigeasy@linutronix.de Link: https://lore.kernel.org/r/20210830172627.267989-6-bigeasy@linutronix.de Reported-by: Clark Williams Signed-off-by: Sebastian Andrzej Siewior Acked-by: Dmitry Vyukov Acked-by: Marco Elver Tested-by: Marco Elver Reviewed-by: Andrey Konovalov Cc: Steven Rostedt Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Stable-dep-of: 2eed77fdcb0c ("kcov: fix data corruption and race conditions on PREEMPT_RT") Signed-off-by: Sasha Levin --- kernel/kcov.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/kernel/kcov.c b/kernel/kcov.c index 3260a65a9def6..c6400e9b44796 100644 --- a/kernel/kcov.c +++ b/kernel/kcov.c @@ -88,6 +88,7 @@ static struct list_head kcov_remote_areas = LIST_HEAD_INIT(kcov_remote_areas); struct kcov_percpu_data { void *irq_area; + local_lock_t lock; unsigned int saved_mode; unsigned int saved_size; @@ -96,7 +97,9 @@ struct kcov_percpu_data { int saved_sequence; }; -static DEFINE_PER_CPU(struct kcov_percpu_data, kcov_percpu_data); +static DEFINE_PER_CPU(struct kcov_percpu_data, kcov_percpu_data) = { + .lock = INIT_LOCAL_LOCK(lock), +}; /* Must be called with kcov_remote_lock locked. */ static struct kcov_remote *kcov_remote_find(u64 handle) @@ -834,7 +837,7 @@ void kcov_remote_start(u64 handle) if (!in_task() && !in_softirq_really()) return; - local_irq_save(flags); + local_lock_irqsave(&kcov_percpu_data.lock, flags); /* * Check that kcov_remote_start() is not called twice in background @@ -842,7 +845,7 @@ void kcov_remote_start(u64 handle) */ mode = READ_ONCE(t->kcov_mode); if (WARN_ON(in_task() && kcov_mode_enabled(mode))) { - local_irq_restore(flags); + local_unlock_irqrestore(&kcov_percpu_data.lock, flags); return; } /* @@ -851,14 +854,15 @@ void kcov_remote_start(u64 handle) * happened while collecting coverage from a background thread. */ if (WARN_ON(in_serving_softirq() && t->kcov_softirq)) { - local_irq_restore(flags); + local_unlock_irqrestore(&kcov_percpu_data.lock, flags); return; } spin_lock(&kcov_remote_lock); remote = kcov_remote_find(handle); if (!remote) { - spin_unlock_irqrestore(&kcov_remote_lock, flags); + spin_unlock(&kcov_remote_lock); + local_unlock_irqrestore(&kcov_percpu_data.lock, flags); return; } kcov_debug("handle = %llx, context: %s\n", handle, @@ -879,19 +883,19 @@ void kcov_remote_start(u64 handle) size = CONFIG_KCOV_IRQ_AREA_SIZE; area = this_cpu_ptr(&kcov_percpu_data)->irq_area; } - spin_unlock_irqrestore(&kcov_remote_lock, flags); + spin_unlock(&kcov_remote_lock); /* Can only happen when in_task(). */ if (!area) { + local_unlock_irqrestore(&kcov_percpu_data.lock, flags); area = vmalloc(size * sizeof(unsigned long)); if (!area) { kcov_put(kcov); return; } + local_lock_irqsave(&kcov_percpu_data.lock, flags); } - local_irq_save(flags); - /* Reset coverage size. */ *(u64 *)area = 0; @@ -901,7 +905,7 @@ void kcov_remote_start(u64 handle) } kcov_start(t, kcov, size, area, mode, sequence); - local_irq_restore(flags); + local_unlock_irqrestore(&kcov_percpu_data.lock, flags); } EXPORT_SYMBOL(kcov_remote_start); @@ -975,12 +979,12 @@ void kcov_remote_stop(void) if (!in_task() && !in_softirq_really()) return; - local_irq_save(flags); + local_lock_irqsave(&kcov_percpu_data.lock, flags); mode = READ_ONCE(t->kcov_mode); barrier(); if (!kcov_mode_enabled(mode)) { - local_irq_restore(flags); + local_unlock_irqrestore(&kcov_percpu_data.lock, flags); return; } /* @@ -988,12 +992,12 @@ void kcov_remote_stop(void) * actually found the remote handle and started collecting coverage. */ if (in_serving_softirq() && !t->kcov_softirq) { - local_irq_restore(flags); + local_unlock_irqrestore(&kcov_percpu_data.lock, flags); return; } /* Make sure that kcov_softirq is only set when in softirq. */ if (WARN_ON(!in_serving_softirq() && t->kcov_softirq)) { - local_irq_restore(flags); + local_unlock_irqrestore(&kcov_percpu_data.lock, flags); return; } @@ -1023,7 +1027,7 @@ void kcov_remote_stop(void) spin_unlock(&kcov_remote_lock); } - local_irq_restore(flags); + local_unlock_irqrestore(&kcov_percpu_data.lock, flags); /* Get in kcov_remote_start(). */ kcov_put(kcov); -- 2.53.0