Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

relayfs: support a counter tracking if per-cpu buffers is full

When using relay mechanism, we often encounter the case where new data are
lost or old unconsumed data are overwritten because of slow reader.

Add 'full' field in per-cpu buffer structure to detect if the above case
is happening. Relay has two modes: 1) non-overwrite mode, 2) overwrite
mode. So buffer being full here respectively means: 1) relayfs doesn't
intend to accept new data and then simply drop them, or 2) relayfs is
going to start over again and overwrite old unread data with new data.

Note: this counter doesn't need any explicit lock to protect from being
modified by different threads for the better performance consideration.
Writers calling __relay_write/relay_write should consider how to use the
lock and ensure it performs under the lock protection, thus it's not
necessary to add a new small lock here.

Link: https://lkml.kernel.org/r/20250612061201.34272-3-kerneljasonxing@gmail.com
Signed-off-by: Jason Xing <kernelxing@tencent.com>
Reviewed-by: Yushan Zhou <katrinzhou@tencent.com>
Reviewed-by: Jens Axboe <axboe@kernel.dk>
Reviewed-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Jason Xing and committed by
Andrew Morton
ca01a90a 2489e958

+16 -1
+9
include/linux/relay.h
··· 29 29 #define RELAYFS_CHANNEL_VERSION 7 30 30 31 31 /* 32 + * Relay buffer statistics 33 + */ 34 + struct rchan_buf_stats 35 + { 36 + unsigned int full_count; /* counter for buffer full */ 37 + }; 38 + 39 + /* 32 40 * Per-cpu relay channel buffer 33 41 */ 34 42 struct rchan_buf ··· 51 43 struct irq_work wakeup_work; /* reader wakeup */ 52 44 struct dentry *dentry; /* channel file dentry */ 53 45 struct kref kref; /* channel buffer refcount */ 46 + struct rchan_buf_stats stats; /* buffer stats */ 54 47 struct page **page_array; /* array of current buffer pages */ 55 48 unsigned int page_count; /* number of current buffer pages */ 56 49 unsigned int finalized; /* buffer has been finalized */
+7 -1
kernel/relay.c
··· 251 251 static int relay_subbuf_start(struct rchan_buf *buf, void *subbuf, 252 252 void *prev_subbuf) 253 253 { 254 + int full = relay_buf_full(buf); 255 + 256 + if (full) 257 + buf->stats.full_count++; 258 + 254 259 if (!buf->chan->cb->subbuf_start) 255 - return !relay_buf_full(buf); 260 + return !full; 256 261 257 262 return buf->chan->cb->subbuf_start(buf, subbuf, 258 263 prev_subbuf); ··· 302 297 buf->finalized = 0; 303 298 buf->data = buf->start; 304 299 buf->offset = 0; 300 + buf->stats.full_count = 0; 305 301 306 302 for (i = 0; i < buf->chan->n_subbufs; i++) 307 303 buf->padding[i] = 0;