summaryrefslogtreecommitdiff
path: root/include/linux/ring_buffer.h
diff options
context:
space:
mode:
authorMark Brown <broonie@kernel.org>2026-09-28 19:49:41 +0100
committerMark Brown <broonie@kernel.org>2026-09-28 19:49:41 +0100
commit012c37f09490db4d9b51bbcbf8def4436fc60f06 (patch)
tree29f3f8e49081cececd15361129b86b8c5bb80fbc /include/linux/ring_buffer.h
parent8e51dd6db0bc7fa888326c913edcb8fe8c1fdc16 (diff)
parent9999d5a07ee037d4653c7231884b0a1e45be801c (diff)
regmap: convert map->lock/unlock users to a scoped guard
Peng Fan (OSS) <peng.fan@oss.nxp.com> says: The regmap lock is taken by calling the lock and unlock callbacks directly at every call site: map->lock(map->lock_arg); ... map->unlock(map->lock_arg); The open-coded pattern forces every error path to unlock by hand, which spreads goto out_unlock chains and duplicated unlock statements throughout regmap.c and regcache.c and makes it easy to leak the lock on a newly added return path. Define a scoped guard for the regmap lock (DEFINE_GUARD) in internal.h and convert the users to it. The lock and unlock callbacks are chosen at init time (mutex, spinlock, raw spinlock, hwspinlock or none) and return void, so an unconditional guard is sufficient. Function-scope critical sections use guard(regmap)(); sites that must run work after the lock is dropped - for example regmap_register_patch() calling regmap_async_complete() and the debugfs cache_only handler calling regcache_sync() (which takes the lock itself) - use scoped_guard(regmap, ...) so the trailing work stays outside the guarded region. regcache_sync() and regcache_sync_region() are deliberately left unconverted: they already use a single goto out unlock path, so a guard would save nothing while forcing either a goto inside a scoped_guard scope or a control-flow rewrite, neither of which is an improvement. This patchset removes many manual unlock statements together with the associated goto out_unlock labels. The series is split to keep the regcache: and regmap: changes on their own commits and to preserve independent revertibility: 1. define the guard and convert regmap.c 2. convert regcache.c (except the sync helpers, see above) 3. convert the regcache rbtree debugfs dump 4. convert the regmap debugfs write handlers No functional change. Tested with the regmap KUnit suite (drivers/base/regmap/regmap-kunit.c) under ARCH=um: 551/551 tests pass, and 551/551 again with lockdep (PROVE_LOCKING, DEBUG_LOCK_ALLOC, DEBUG_ATOMIC_SLEEP) enabled with no splats. Built clean with sparse (C=1) showing no lock-context imbalance warnings. Link: https://patch.msgid.link/20260924-regmap-lock-guard-v3-0-8a6127223c16@nxp.com
Diffstat (limited to 'include/linux/ring_buffer.h')
-rw-r--r--include/linux/ring_buffer.h24
1 files changed, 20 insertions, 4 deletions
diff --git a/include/linux/ring_buffer.h b/include/linux/ring_buffer.h
index 0670742b2d60..eac3e9080c3c 100644
--- a/include/linux/ring_buffer.h
+++ b/include/linux/ring_buffer.h
@@ -3,8 +3,9 @@
#define _LINUX_RING_BUFFER_H
#include <linux/mm.h>
-#include <linux/seq_file.h>
#include <linux/poll.h>
+#include <linux/ring_buffer_types.h>
+#include <linux/seq_file.h>
#include <uapi/linux/trace_mmap.h>
@@ -218,14 +219,15 @@ bool ring_buffer_time_stamp_abs(struct trace_buffer *buffer);
size_t ring_buffer_nr_dirty_pages(struct trace_buffer *buffer, int cpu);
struct buffer_data_read_page;
-struct buffer_data_read_page *
-ring_buffer_alloc_read_page(struct trace_buffer *buffer, int cpu);
+int ring_buffer_alloc_read_page(struct trace_buffer *buffer, int cpu,
+ struct buffer_data_read_page **rpage);
void ring_buffer_free_read_page(struct trace_buffer *buffer, int cpu,
struct buffer_data_read_page *page);
int ring_buffer_read_page(struct trace_buffer *buffer,
struct buffer_data_read_page *data_page,
size_t len, int cpu, int full);
void *ring_buffer_read_page_data(struct buffer_data_read_page *page);
+unsigned int ring_buffer_read_page_size(struct buffer_data_read_page *rpage);
struct trace_seq;
@@ -278,11 +280,25 @@ static inline struct ring_buffer_desc *__first_ring_buffer_desc(struct trace_buf
return (struct ring_buffer_desc *)(&desc->__data[0]);
}
+/*
+ * Returns the number of pages for a ring_buffer_desc. The caller must ensure it
+ * does not overflow ring_buffer_desc::nr_page_va.
+ */
+static inline unsigned long __calc_nr_pages_ring_buffer_desc(size_t size)
+{
+ /* Takes into account the reader page */
+ return max(DIV_ROUND_UP(size, PAGE_SIZE - BUF_PAGE_HDR_SIZE), 2UL) + 1;
+}
+
static inline size_t trace_buffer_desc_size(size_t buffer_size, unsigned int nr_cpus)
{
- unsigned int nr_pages = max(DIV_ROUND_UP(buffer_size, PAGE_SIZE), 2UL) + 1;
+ unsigned long nr_pages = __calc_nr_pages_ring_buffer_desc(buffer_size);
struct ring_buffer_desc *rbdesc;
+ /* Capped by ring_buffer_desc::nr_page_va */
+ if (nr_pages > UINT_MAX)
+ return SIZE_MAX;
+
return size_add(offsetof(struct trace_buffer_desc, __data),
size_mul(nr_cpus, struct_size(rbdesc, page_va, nr_pages)));
}