relayfs: misc changes
Subsystems (3)
cover relayfs: misc changes
From: Jason Xing <[email protected]>
The series mostly focuss on the error counters which helps every user debug their own kernel module. More patches making the relayfs more robust and functional are around the corner :)
---
Note: this series is made on top of this unmerged commit[1]
[1]: https://lore.kernel.org/all/[email protected]/
Jason Xing (5):
relayfs: support a counter tracking if per-cpu buffers is full
relayfs: introduce dump of relayfs statistics function
blktrace: use rbuf->stats.full as a drop indicator in relayfs
relayfs: support a counter tracking if data is too big to write
relayfs: uniformally use possible cpu iteration
include/linux/relay.h | 22 ++++++++++++++- kernel/relay.c | 62 +++++++++++++++++++++++++++++++++++------ kernel/trace/blktrace.c | 23 ++------------- 3 files changed, 76 insertions(+), 31 deletions(-)
Signature · 1 line
2.43.5
PATCH 1/5 relayfs: support a counter tracking if per-cpu buffers is full
From: Jason Xing <[email protected]>
Add 'full' field in per-cpu buffer structure to detect if the buffer is full, which means: 1) relayfs doesn't intend to accept new data in non-overwrite mode that is also by default, or 2) relayfs is going to start over again and overwrite old unread data when kernel module has its own subbuf_start callback to support overwrite mode. This counter works for both overwrite and non-overwrite modes.
This counter doesn't have any explicit lock to protect from being modified by different threads at the same time for the better performance consideration. In terms of the atomic operation, it's not introduced for incrementing the counter like blktrace because side effect may arise when multiple threads access the counter simultaneously on the machine equipped with many cpus, say, more than 200. As we can see in relay_write() and __relay_write(), the writer at the beginning should consider how to use the lock for the whole write process, thus it's not necessary to add another lock to make sure the counter is accurate.
Using 'pahole --hex -C rchan_buf vmlinux' so you can see this field just fits into one 4-byte hole in the cacheline 2.
--- include/linux/relay.h | 9 +++++++++ kernel/relay.c | 8 +++++++- 2 files changed, 16 insertions(+), 1 deletion(-)
include/linux/relay.h +9 -0
@@ -28,6 +28,14 @@*/ #define RELAYFS_CHANNEL_VERSION 7 +/* +* Relay buffer error statistics dump +*/ +struct rchan_buf_error_stats +{ +unsigned int full;/* counter for buffer full */ +}; +/* * Per-cpu relay channel buffer */
@@ -43,6 +51,7 @@ struct rchan_bufstruct irq_work wakeup_work;/* reader wakeup */ struct dentry *dentry;/* channel file dentry */ struct kref kref;/* channel buffer refcount */ +struct rchan_buf_error_stats stats;/* error stats */ struct page **page_array;/* array of current buffer pages */ unsigned int page_count;/* number of current buffer pages */ unsigned int finalized;/* buffer has been finalized */
kernel/relay.c +7 -1
@@ -252,8 +252,13 @@ EXPORT_SYMBOL_GPL(relay_buf_full);static int relay_subbuf_start (struct rchan_buf *buf,void *subbuf,void *prev_subbuf) { +int buf_full =relay_buf_full (buf); + +if (buf_full) + buf->stats.full++; +if (!buf->chan->cb->subbuf_start) -return !relay_buf_full (buf); +return !buf_full;return buf->chan->cb->subbuf_start (buf, subbuf, prev_subbuf);
@@ -298,6 +303,7 @@ static void __relay_reset(struct rchan_buf *buf, unsigned int init) buf->finalized =0 ; buf->data = buf->start; buf->offset =0 ; + buf->stats.full =0 ;for (i =0 ; i < buf->chan->n_subbufs; i++) buf->padding[i] =0 ;
Signature · 1 line
2.43.5
PATCH 2/5 relayfs: introduce dump of relayfs statistics function
In reply to Jason XingFrom: Jason Xing <[email protected]>
In this version, only support dumping the counter for buffer full and implement the framework of how it works. Users MUST pass a valid @buf with a valid @len that is required to be larger than RELAY_DUMP_BUF_MAX_LEN to acquire which information indicated by @flags to dump.
RELAY_DUMP_BUF_MAX_LEN shows the maximum len of the buffer if users choose to dump all the values.
Users can use this buffer to do whatever they expect in their own kernel module, say, print to console/dmesg or write them into the relay buffer.
--- include/linux/relay.h | 10 ++++++++++ kernel/relay.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+)
include/linux/relay.h +10 -0
@@ -31,6 +31,15 @@/* * Relay buffer error statistics dump */ +enum { +RELAY_DUMP_BUF_FULL = (1 <<0 ), + +RELAY_DUMP_LAST =RELAY_DUMP_BUF_FULL , +RELAY_DUMP_MASK = (RELAY_DUMP_LAST -1 ) |RELAY_DUMP_LAST +}; + +#define RELAY_DUMP_BUF_MAX_LEN 32 +struct rchan_buf_error_stats {unsigned int full;/* counter for buffer full */
@@ -170,6 +179,7 @@ extern int relay_late_setup_files(struct rchan *chan, struct dentry *parent);extern void relay_close (struct rchan *chan);extern void relay_flush (struct rchan *chan); +extern void relay_dump (struct rchan *chan,char *buf,int len,int flags);extern void relay_subbufs_consumed (struct rchan *chan,unsigned int cpu,size_t consumed);
kernel/relay.c +35 -0
@@ -810,6 +810,41 @@ void relay_flush(struct rchan *chan) }EXPORT_SYMBOL_GPL (relay_flush); +/** +* relay_dump - dump statistics of the specified channel buffer +* @chan: the channel +* @buf: buf to store statistics +* @len: len of buf to check +* @flags: select particular information to dump +*/ +void relay_dump (struct rchan *chan,char *buf,int len,int flags) +{ +unsigned int i, full_counter =0 ; +struct rchan_buf *rbuf; +int offset =0 ; + +if (!chan || !buf || flags & ~RELAY_DUMP_MASK ) +return ; + +if (len <RELAY_DUMP_BUF_MAX_LEN ) +return ; + +if (chan->is_global) { + rbuf = *per_cpu_ptr (chan->buf,0 ); + full_counter = rbuf->stats.full; + }else { +for_each_possible_cpu (i) { +if ((rbuf = *per_cpu_ptr (chan->buf, i))) + full_counter += rbuf->stats.full; + } + +if (flags &RELAY_DUMP_BUF_FULL ) + offset +=snprintf (buf,sizeof (unsigned int ),"%u" , full_counter); + +snprintf (buf + offset,1 ,"\n" ); +} +EXPORT_SYMBOL_GPL (relay_dump); +/** * relay_file_open - open file op for relay files * @inode: the inode
Signature · 1 line
2.43.5
PATCH 3/5 blktrace: use rbuf->stats.full as a drop indicator in relayfs
In reply to Jason XingFrom: Jason Xing <[email protected]>
Replace internal subbuf_start with the default policy in relayfs.
Remove dropped field from struct blk_trace. Correspondingly, use per-cpu buffer mechanism to replace atomic operation. Through incrementing full counter to keep track of how many times we encountered a full subbuffer, it aids the user space app in telling how many lost events there were.
Sum up all the fields gathered from all cpus when application is reading.
--- kernel/trace/blktrace.c | 23 ++--------------------- 1 file changed, 2 insertions(+), 21 deletions(-)
kernel/trace/blktrace.c +2 -21
@@ -415,9 +415,9 @@ static ssize_t blk_dropped_read(struct file *filp, char __user *buffer, size_t count, loff_t *ppos) { struct blk_trace *bt = filp->private_data; -char buf[16 ]; +char buf[RELAY_DUMP_BUF_MAX_LEN ]; -snprintf (buf,sizeof (buf),"%u\n" ,atomic_read (&bt->dropped)); +relay_dump (bt->rchan, buf,RELAY_DUMP_BUF_MAX_LEN ,RELAY_DUMP_BUF_FULL );return simple_read_from_buffer (buffer, count, ppos, buf,strlen (buf)); }
@@ -456,23 +456,6 @@ static const struct file_operations blk_msg_fops = { .llseek = noop_llseek, }; -/* -* Keep track of how many times we encountered a full subbuffer, to aid -* the user space app in telling how many lost events there were. -*/ -static int blk_subbuf_start_callback (struct rchan_buf *buf,void *subbuf, -void *prev_subbuf) -{ -struct blk_trace *bt; - -if (!relay_buf_full (buf)) -return 1 ; - - bt = buf->chan->private_data; -atomic_inc (&bt->dropped); -return 0 ; -} -static int blk_remove_buf_file_callback (struct dentry *dentry) {debugfs_remove (dentry);
@@ -491,7 +474,6 @@ static struct dentry *blk_create_buf_file_callback(const char *filename, }static const struct rchan_callbacks blk_relay_callbacks = { - .subbuf_start = blk_subbuf_start_callback, .create_buf_file = blk_create_buf_file_callback, .remove_buf_file = blk_remove_buf_file_callback, };
@@ -580,7 +562,6 @@ static int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev, } bt->dev = dev; -atomic_set (&bt->dropped,0 );INIT_LIST_HEAD (&bt->running_list); ret = -EIO ;
Signature · 1 line
2.43.5
PATCH 4/5 relayfs: support a counter tracking if data is too big to write
In reply to Jason XingFrom: Jason Xing <[email protected]>
It doesn't really matter to let the user/admin know what the last too big value is. Just record how many times this case is triggered.
Also solve the issue where relay_reset() doesn't restore the value.
Store the counter in the per-cpu buffer structure instead of the global buffer structure. It also solves the previous racy condition because in terms of the global structure, it is likely to happen when a few of per-cpu buffers encounter the too big data case.
Remove the printk in relay_close() since kernel module can directly call relay_dump() as they want.
--- include/linux/relay.h | 5 +++-- kernel/relay.c | 19 +++++++++++-------- 2 files changed, 14 insertions(+), 10 deletions(-)
include/linux/relay.h +3 -2
@@ -33,8 +33,9 @@*/ enum {RELAY_DUMP_BUF_FULL = (1 <<0 ), +RELAY_DUMP_WRT_BIG = (1 <<1 ), -RELAY_DUMP_LAST =RELAY_DUMP_BUF_FULL , +RELAY_DUMP_LAST =RELAY_DUMP_WRT_BIG ,RELAY_DUMP_MASK = (RELAY_DUMP_LAST -1 ) |RELAY_DUMP_LAST };
@@ -43,6 +44,7 @@ enum {struct rchan_buf_error_stats {unsigned int full;/* counter for buffer full */ +unsigned int big;/* counter for too big to write */ };/*
@@ -82,7 +84,6 @@ struct rchanconst struct rchan_callbacks *cb;/* client callbacks */ struct kref kref;/* channel refcount */ void *private_data;/* for user-defined data */ -size_t last_toobig;/* tried to log event > subbuf size */ struct rchan_buf *__percpu *buf;/* per-cpu channel buffers */ int is_global;/* One global buffer ? */ struct list_head list;/* for channel list */
kernel/relay.c +11 -8
@@ -304,6 +304,7 @@ static void __relay_reset(struct rchan_buf *buf, unsigned int init) buf->data = buf->start; buf->offset =0 ; buf->stats.full =0 ; + buf->stats.big =0 ;for (i =0 ; i < buf->chan->n_subbufs; i++) buf->padding[i] =0 ;
@@ -712,7 +713,7 @@ size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)return length; toobig: - buf->chan->last_toobig = length; + buf->stats.big++;return 0 ; }EXPORT_SYMBOL_GPL (relay_switch_subbuf);
@@ -772,11 +773,6 @@ void relay_close(struct rchan *chan)if ((buf = *per_cpu_ptr (chan->buf, i)))relay_close_buf (buf); -if (chan->last_toobig) -printk (KERN_WARNING "relay: one or more items not logged " -"[item size (%zd) > sub-buffer size (%zd)]\n" , - chan->last_toobig, chan->subbuf_size); -list_del (&chan->list);kref_put (&chan->kref, relay_destroy_channel);mutex_unlock (&relay_channels_mutex);
@@ -819,7 +815,7 @@ EXPORT_SYMBOL_GPL(relay_flush);*/ void relay_dump (struct rchan *chan,char *buf,int len,int flags) { -unsigned int i, full_counter =0 ; +unsigned int i, full_counter =0 , big_counter =0 ;struct rchan_buf *rbuf;int offset =0 ;
@@ -832,15 +828,22 @@ void relay_dump(struct rchan *chan, char *buf, int len, int flags)if (chan->is_global) { rbuf = *per_cpu_ptr (chan->buf,0 ); full_counter = rbuf->stats.full; + big_counter = rbuf->stats.big; }else {for_each_possible_cpu (i) { -if ((rbuf = *per_cpu_ptr (chan->buf, i))) +if ((rbuf = *per_cpu_ptr (chan->buf, i))) { full_counter += rbuf->stats.full; + big_counter += rbuf->stats.big; + } + } }if (flags &RELAY_DUMP_BUF_FULL ) offset +=snprintf (buf,sizeof (unsigned int ),"%u" , full_counter); +if (flags &RELAY_DUMP_WRT_BIG ) + offset +=snprintf (buf,sizeof (unsigned int ),"%u" , big_counter); +snprintf (buf + offset,1 ,"\n" ); }EXPORT_SYMBOL_GPL (relay_dump);
Signature · 1 line
2.43.5
PATCH 5/5 relayfs: uniformally use possible cpu iteration
In reply to Jason XingFrom: Jason Xing <[email protected]>
Use for_each_possible_cpu to create per-cpu relayfs file to avoid later hotplug cpu which doesn't have its own file.
--- kernel/relay.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/relay.c +2 -2
@@ -519,7 +519,7 @@ struct rchan *relay_open(const char *base_filename,kref_init (&chan->kref);mutex_lock (&relay_channels_mutex); -for_each_online_cpu (i) { +for_each_possible_cpu (i) { buf =relay_open_buf (chan, i);if (!buf) goto free_bufs;
@@ -615,7 +615,7 @@ int relay_late_setup_files(struct rchan *chan,* no files associated. So it's safe to call relay_setup_buf_file() * on all currently online CPUs. */ -for_each_online_cpu (i) { +for_each_possible_cpu (i) { buf = *per_cpu_ptr (chan->buf, i);if (unlikely (!buf)) {WARN_ONCE (1 ,KERN_ERR "CPU has no buffer!\n" );
Signature · 1 line
2.43.5
Andrew Morton
maintainerRe: [PATCH v1 1/5] relayfs: support a counter tracking if per-cpu buffers is full
In reply to Jason XingQuoted 20 lines · On Mon, 12 May 2025 10:49:31 +0800 Jason Xing <[email protected]> wrote:
From: Jason Xing <[email protected]>
Add 'full' field in per-cpu buffer structure to detect if the buffer is full, which means: 1) relayfs doesn't intend to accept new data in non-overwrite mode that is also by default, or 2) relayfs is going to start over again and overwrite old unread data when kernel module has its own
subbuf_startcallback to support overwrite mode. This counter works for both overwrite and non-overwrite modes.This counter doesn't have any explicit lock to protect from being modified by different threads at the same time for the better performance consideration. In terms of the atomic operation, it's not introduced for incrementing the counter like blktrace because side effect may arise when multiple threads access the counter simultaneously on the machine equipped with many cpus, say, more than 200. As we can see in
relay_write()and__relay_write(), the writer at the beginning should consider how to use the lock for the whole write process, thus it's not necessary to add another lock to make sure the counter is accurate.
Quoted 2 lines
Using 'pahole --hex -C
rchan_bufvmlinux' so you can see this field just fits into one 4-byte hole in the cacheline 2.
Does this alter blktrace output? If so is that backward-compatible (and do we care). Is there any blktrace documentation which should be updated?
Also, please check Documentation/filesystems/relay.rst and see if any updates should be made to reflect the changes in this patchset.
I'm not really clear on the use cases of this counter - perhaps you can be more verbose about this in the changelog.
Quoted 5 lines
include/linux/relay.h +0
-0
include/linux/relay.h line 37
Quoted 10 lines
include/linux/relay.h +7
-0@@ -28,6 +28,14 @@*/ #define RELAYFS_CHANNEL_VERSION 7 +/* +* Relay buffer error statistics dump +*/ +struct rchan_buf_error_stats +{ +unsigned int full;/* counter for buffer full */ +};
Why a struct?
include/linux/relay.h struct rchan_buf · line 54
Quoted 8 lines
include/linux/relay.h +1
-0/* * Per-cpu relay channel buffer */ @@ -43,6 +51,7 @@ struct rchan_bufstruct irq_work wakeup_work;/* reader wakeup */ struct dentry *dentry;/* channel file dentry */ struct kref kref;/* channel buffer refcount */ +struct rchan_buf_error_stats stats;/* error stats */
Could simply use
unsigned int full;
here?
Also, the name "full" implies to me "it is full". Perhaps "full_count" would be better.
Quoted 8 lines
include/linux/relay.h +0
-0struct page **page_array;/* array of current buffer pages */ unsigned int page_count;/* number of current buffer pages */ unsigned int finalized;/* buffer has been finalized */ kernel/relay.c +0
-0
kernel/relay.c EXPORT_SYMBOL_GPL(relay_buf_full); · line 258
Quoted 7 lines
kernel/relay.c +4
-0@@ -252,8 +252,13 @@ EXPORT_SYMBOL_GPL(relay_buf_full);static int relay_subbuf_start (struct rchan_buf *buf,void *subbuf,void *prev_subbuf) { +int buf_full =relay_buf_full (buf); + +if (buf_full) + buf->stats.full++;
I don't understand the changelog's description of this, sorry.
Is it saying "this operation is protected by a lock"? If so, please specifically state which lock this is.
Or is it saying "we don't care if this races because the counter will be close enough". If so then maybe so, but things like KCSAN will probably detect and warn and then people will want to address this.
Andrew Morton
maintainerRe: [PATCH v1 2/5] relayfs: introduce dump of relayfs statistics function
In reply to Jason XingQuoted 15 lines · On Mon, 12 May 2025 10:49:32 +0800 Jason Xing <[email protected]> wrote:
From: Jason Xing <[email protected]>
In this version, only support dumping the counter for buffer full and implement the framework of how it works. Users MUST pass a valid @buf with a valid @len that is required to be larger than
RELAY_DUMP_BUF_MAX_LENto acquire which information indicated by @flags to dump.
RELAY_DUMP_BUF_MAX_LENshows the maximum len of the buffer if users choose to dump all the values.Users can use this buffer to do whatever they expect in their own kernel module, say, print to console/dmesg or write them into the relay buffer.
...
kernel/relay.c void relay_flush(struct rchan *chan) · line 820
Quoted 8 lines
kernel/relay.c +8
-0+/** +* relay_dump - dump statistics of the specified channel buffer +* @chan: the channel +* @buf: buf to store statistics +* @len: len of buf to check +* @flags: select particular information to dump +*/ +void relay_dump (struct rchan *chan,char *buf,int len,int flags)
size_t' is probably a more appropriate type for len'.
kernel/relay.c void relay_flush(struct rchan *chan) · line 830
Quoted 10 lines
kernel/relay.c +10
-0+{ +unsigned int i, full_counter =0 ; +struct rchan_buf *rbuf; +int offset =0 ; + +if (!chan || !buf || flags & ~RELAY_DUMP_MASK ) +return ; + +if (len <RELAY_DUMP_BUF_MAX_LEN ) +return ;
So we left the memory at *buf uninitialized but failed to tell the caller this. The caller will then proceed to use uninitialized memory.
It's a programming error, so simply going BUG seems OK.
kernel/relay.c void relay_flush(struct rchan *chan) · line 836
Quoted 5 lines
kernel/relay.c +5
-0+if (chan->is_global) { + rbuf = *per_cpu_ptr (chan->buf,0 ); + full_counter = rbuf->stats.full; + }else { +for_each_possible_cpu (i) {
I'm thinking that at this stage in the patch series, this should be for_each_online_cpu(), then adjust that in patch [5/5].
kernel/relay.c void relay_flush(struct rchan *chan) · line 842
Quoted 6 lines
kernel/relay.c +6
-0+if ((rbuf = *per_cpu_ptr (chan->buf, i))) + full_counter += rbuf->stats.full; + } + +if (flags &RELAY_DUMP_BUF_FULL ) + offset +=snprintf (buf,sizeof (unsigned int ),"%u" , full_counter);
This seems strange. sizeof(unsigned int) has nothing to do with the number of characters which are consumed by expansion of "%u"?
Quoted 8 lines
kernel/relay.c +5
-0+ +snprintf (buf + offset,1 ,"\n" ); +} +EXPORT_SYMBOL_GPL (relay_dump); +/** * relay_file_open - open file op for relay files * @inode: the inode
Andrew Morton
maintainerRe: [PATCH v1 5/5] relayfs: uniformally use possible cpu iteration
In reply to Jason XingQuoted 4 lines · On Mon, 12 May 2025 10:49:35 +0800 Jason Xing <[email protected]> wrote:
From: Jason Xing <[email protected]>
Use
for_each_possible_cputo create per-cpu relayfs file to avoid later hotplug cpu which doesn't have its own file.
I don't understand this. Exactly what problem are we trying to solve?
Quoted 11 lines
Reviewed-by Yushan Zhou <[email protected]>Signed-off-by Jason Xing <[email protected]>--- kernel/relay.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)kernel/relay.c +0
-0
kernel/relay.c struct rchan *relay_open(const char *base_filename, · line 522
Quoted 5 lines
kernel/relay.c +1
-1@@ -519,7 +519,7 @@ struct rchan *relay_open(const char *base_filename,kref_init (&chan->kref);mutex_lock (&relay_channels_mutex); -for_each_online_cpu (i) { +for_each_possible_cpu (i) {
num_possible_cpus() can sometimes greatly exceed num_online_cpus(), so this is an unfortunate change. It would be better to implement the hotplug notifier?
Quoted 14 lines
kernel/relay.c +1
-1buf =relay_open_buf (chan, i);if (!buf) goto free_bufs;@@ -615,7 +615,7 @@ int relay_late_setup_files(struct rchan *chan,* no files associated. So it's safe to call relay_setup_buf_file() * on all currently online CPUs. */ -for_each_online_cpu (i) { +for_each_possible_cpu (i) { buf = *per_cpu_ptr (chan->buf, i);if (unlikely (!buf)) {WARN_ONCE (1 ,KERN_ERR "CPU has no buffer!\n" );Signature · 1 line
2.43.5
Re: [PATCH v1 1/5] relayfs: support a counter tracking if per-cpu buffers is full
In reply to Andrew MortonQuoted 23 lines · Andrew Morton
Quoted 20 lines · On Mon, 12 May 2025 10:49:31 +0800 Jason Xing <[email protected]> wrote:
From: Jason Xing <[email protected]>
Add 'full' field in per-cpu buffer structure to detect if the buffer is full, which means: 1) relayfs doesn't intend to accept new data in non-overwrite mode that is also by default, or 2) relayfs is going to start over again and overwrite old unread data when kernel module has its own
subbuf_startcallback to support overwrite mode. This counter works for both overwrite and non-overwrite modes.This counter doesn't have any explicit lock to protect from being modified by different threads at the same time for the better performance consideration. In terms of the atomic operation, it's not introduced for incrementing the counter like blktrace because side effect may arise when multiple threads access the counter simultaneously on the machine equipped with many cpus, say, more than 200. As we can see in
relay_write()and__relay_write(), the writer at the beginning should consider how to use the lock for the whole write process, thus it's not necessary to add another lock to make sure the counter is accurate.
Quoted 6 lines
Quoted 2 lines
Using 'pahole --hex -C
rchan_bufvmlinux' so you can see this field just fits into one 4-byte hole in the cacheline 2.Does this alter blktrace output? If so is that backward-compatible (and do we care). Is there any blktrace documentation which should be updated?
Thanks for the review.
Surely no, I tested blktrace by running 'blktrace -d /dev/vda1' to verify the dropped field after the entire series applied. So it's compatible.
Quoted 3 lines
Also, please check
Documentation/filesystems/relay.rstand see if any updates should be made to reflect the changes in this patchset.
Right, will do it accordingly.
Quoted 3 lines
I'm not really clear on the use cases of this counter - perhaps you can be more verbose about this in the changelog.
Will add more.
The existing code 'blk_dropped_read' in blktrace might give you a hint. In real production, we sometimes encounter the case where the reader cannot consume the data as fast as possible, which leads to data loss.
Quoted 9 lines
Quoted 9 lines
include/linux/relay.h +0
-0@@ -28,6 +28,14 @@*/ #define RELAYFS_CHANNEL_VERSION 7
Quoted 9 lines
include/linux/relay.h line 37
Quoted 7 lines
include/linux/relay.h +7
-0+/* +* Relay buffer error statistics dump +*/ +struct rchan_buf_error_stats +{ +unsigned int full;/* counter for buffer full */ +};Why a struct?
It is because I'm going to add more counters related to the error information, like patch [4/5]. Does it make any sense?
Quoted 12 lines
include/linux/relay.h struct rchan_buf · line 54
Quoted 9 lines
include/linux/relay.h +1
-0/* * Per-cpu relay channel buffer */ @@ -43,6 +51,7 @@ struct rchan_bufstruct irq_work wakeup_work;/* reader wakeup */ struct dentry *dentry;/* channel file dentry */ struct kref kref;/* channel buffer refcount */ +struct rchan_buf_error_stats stats;/* error stats */ Could simply use
Quoted 6 lines
unsigned int full;here?
Also, the name "full" implies to me "it is full". Perhaps "
full_count" would be better.
Got it. Makes sense to me.
Quoted 19 lines
Quoted 9 lines
include/linux/relay.h +0
-0struct page **page_array;/* array of current buffer pages */ unsigned int page_count;/* number of current buffer pages */ unsigned int finalized;/* buffer has been finalized */ kernel/relay.c +0
-0kernel/relay.c EXPORT_SYMBOL_GPL(relay_buf_full); · line 258
Quoted 7 lines
kernel/relay.c +4
-0@@ -252,8 +252,13 @@ EXPORT_SYMBOL_GPL(relay_buf_full);static int relay_subbuf_start (struct rchan_buf *buf,void *subbuf,void *prev_subbuf) { +int buf_full =relay_buf_full (buf); + +if (buf_full) + buf->stats.full++;I don't understand the changelog's description of this, sorry.
Quoted 7 lines
Is it saying "this operation is protected by a lock"? If so, please specifically state which lock this is.
Or is it saying "we don't care if this races because the counter will be close enough". If so then maybe so, but things like KCSAN will probably detect and warn and then people will want to address this.
Sorry for the confusion. I meant the whole write process should perform with the lock protection which users are supposed to pay more attention to. User calls __relay_write which means he already considers the racy case, adding additional lock before writing. On the assumption that the whole write process is protected, there is no need to add any form of lock internally (even like atomic operations) to ensure the consistency of full_count.
I will revise it in the next re-spin.
Thanks,
Jason
Re: [PATCH v1 2/5] relayfs: introduce dump of relayfs statistics function
In reply to Andrew MortonQuoted 43 lines · Andrew Morton
Quoted 15 lines · On Mon, 12 May 2025 10:49:32 +0800 Jason Xing <[email protected]> wrote:
From: Jason Xing <[email protected]>
In this version, only support dumping the counter for buffer full and implement the framework of how it works. Users MUST pass a valid @buf with a valid @len that is required to be larger than
RELAY_DUMP_BUF_MAX_LENto acquire which information indicated by @flags to dump.
RELAY_DUMP_BUF_MAX_LENshows the maximum len of the buffer if users choose to dump all the values.Users can use this buffer to do whatever they expect in their own kernel module, say, print to console/dmesg or write them into the relay buffer.
...
kernel/relay.c void relay_flush(struct rchan *chan) · line 820
Quoted 8 lines
kernel/relay.c +8
-0+/** +* relay_dump - dump statistics of the specified channel buffer +* @chan: the channel +* @buf: buf to store statistics +* @len: len of buf to check +* @flags: select particular information to dump +*/ +void relay_dump (struct rchan *chan,char *buf,int len,int flags)
size_t' is probably a more appropriate type forlen'.kernel/relay.c void relay_flush(struct rchan *chan) · line 830
Quoted 10 lines
kernel/relay.c +10
-0+{ +unsigned int i, full_counter =0 ; +struct rchan_buf *rbuf; +int offset =0 ; + +if (!chan || !buf || flags & ~RELAY_DUMP_MASK ) +return ; + +if (len <RELAY_DUMP_BUF_MAX_LEN ) +return ;So we left the memory at *buf uninitialized but failed to tell the caller this. The caller will then proceed to use uninitialized memory.
Quoted 1 line
It's a programming error, so simply going BUG seems OK.
Are you suggesting that I should remove the above check because developers should take care of the length of the buffer to write outside of the relay_dump function? or use this instead:WARN_ON_ONCE(len < RELAY_DUMP_BUF_MAX_LEN);
?
Quoted 9 lines
kernel/relay.c void relay_flush(struct rchan *chan) · line 836
Quoted 6 lines
kernel/relay.c +5
-0+if (chan->is_global) { + rbuf = *per_cpu_ptr (chan->buf,0 ); + full_counter = rbuf->stats.full; + }else { +for_each_possible_cpu (i) {I'm thinking that at this stage in the patch series, this should be
for_each_online_cpu(), then adjust that in patch [5/5].
Point taken. Will change it.
Quoted 10 lines
kernel/relay.c void relay_flush(struct rchan *chan) · line 842
Quoted 7 lines
kernel/relay.c +6
-0+if ((rbuf = *per_cpu_ptr (chan->buf, i))) + full_counter += rbuf->stats.full; + } + +if (flags &RELAY_DUMP_BUF_FULL ) + offset +=snprintf (buf,sizeof (unsigned int ),"%u" , full_counter);This seems strange. sizeof(unsigned int) has nothing to do with the number of characters which are consumed by expansion of "%u"?
Sorry, my bad. It should be:
offset += snprintf(buf, len, "%u", full_counter);
Passing 'len' as the second parameter.
Thanks,
Jason
Quoted 10 lines
Quoted 9 lines
kernel/relay.c +5
-0+ +snprintf (buf + offset,1 ,"\n" ); +} +EXPORT_SYMBOL_GPL (relay_dump); +/** * relay_file_open - open file op for relay files * @inode: the inode
Re: [PATCH v1 5/5] relayfs: uniformally use possible cpu iteration
In reply to Andrew MortonQuoted 9 lines · Andrew Morton
Quoted 4 lines · On Mon, 12 May 2025 10:49:35 +0800 Jason Xing <[email protected]> wrote:
From: Jason Xing <[email protected]>
Use
for_each_possible_cputo create per-cpu relayfs file to avoid later hotplug cpu which doesn't have its own file.I don't understand this. Exactly what problem are we trying to solve?
The reason behind this change is can we directly allocate per possible cpu at the initialization phase. After this, even if some cpu goes online, we don't need to care about it.
The idea is directly borrowed from the networking area where people use possible cpu iteration for most cases.
Quoted 14 lines
Quoted 14 lines
Reviewed-by Yushan Zhou <[email protected]>Signed-off-by Jason Xing <[email protected]>--- kernel/relay.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)kernel/relay.c +0
-0@@ -519,7 +519,7 @@ struct rchan *relay_open(const char *base_filename,kref_init (&chan->kref);
Quoted 6 lines
kernel/relay.c struct rchan *relay_open(const char *base_filename, · line 522
Quoted 3 lines
kernel/relay.c +1
-1mutex_lock (&relay_channels_mutex); -for_each_online_cpu (i) { +for_each_possible_cpu (i) {
num_possible_cpus()can sometimes greatly exceednum_online_cpus(), so this is an unfortunate change.
Are you worried about too much extra memory to waste in this case?
A relevant thing I would like to share here:
To keep the high performance of transferring data between kernel space and user space, the per-cpu mechanism is needed like how relay works at the moment. It allocates many unnecessary/big memory chunks especially when the cpu number is very large, say, 256. I'm still working on this to see if we can figure out a good approach to balance the performance and memory.
Quoted 2 lines
It would be better to implement the
hotplug notifier?
Right, but sorry, I hesitate to do so because it involves much more work and corresponding tests.
Thanks,
Jason
Quoted 15 lines
Quoted 15 lines
kernel/relay.c +1
-2buf =relay_open_buf (chan, i);if (!buf) goto free_bufs;@@ -615,7 +615,7 @@ int relay_late_setup_files(struct rchan *chan,* no files associated. So it's safe to call relay_setup_buf_file() * on all currently online CPUs. */ -for_each_online_cpu (i) { +for_each_possible_cpu (i) { buf = *per_cpu_ptr (chan->buf, i);if (unlikely (!buf)) {WARN_ONCE (1 ,KERN_ERR "CPU has no buffer!\n" ); --2.43.5
Andrew Morton
maintainerRe: [PATCH v1 2/5] relayfs: introduce dump of relayfs statistics function
In reply to Jason XingQuoted 14 lines · On Tue, 13 May 2025 09:48:15 +0800 Jason Xing <[email protected]> wrote:
Quoted 14 lines
kernel/relay.c void relay_flush(struct rchan *chan) · line 830
Quoted 10 lines
kernel/relay.c +10
-0+{ +unsigned int i, full_counter =0 ; +struct rchan_buf *rbuf; +int offset =0 ; + +if (!chan || !buf || flags & ~RELAY_DUMP_MASK ) +return ; + +if (len <RELAY_DUMP_BUF_MAX_LEN ) +return ;So we left the memory at *buf uninitialized but failed to tell the caller this. The caller will then proceed to use uninitialized memory.
Quoted 7 lines
Quoted 1 line
It's a programming error, so simply going BUG seems OK.
Are you suggesting that I should remove the above check because developers should take care of the length of the buffer to write outside of the
relay_dumpfunction? or use this instead:WARN_ON_ONCE(len < RELAY_DUMP_BUF_MAX_LEN);
?
It's a poor interface - it returns uninitialized data while not alerting the caller to this. You'll figure something out ;)
Perhaps
BUG_ON(len < RELAY_DUMP_BUF_MAX_LEN); *buf = '\0'; if (!chan || (flags & ~RELAY_DUMP_MASK)) return;
We don't need to check for !buf - the oops message contains the same info.
Maybe we don't need to check !chan either. Can it be NULL here?
Re: [PATCH v1 2/5] relayfs: introduce dump of relayfs statistics function
Quoted 25 lines · Andrew Morton
Quoted 14 lines · On Tue, 13 May 2025 09:48:15 +0800 Jason Xing <[email protected]> wrote:
Quoted 14 lines
kernel/relay.c void relay_flush(struct rchan *chan) · line 830
Quoted 10 lines
kernel/relay.c +10
-0+{ +unsigned int i, full_counter =0 ; +struct rchan_buf *rbuf; +int offset =0 ; + +if (!chan || !buf || flags & ~RELAY_DUMP_MASK ) +return ; + +if (len <RELAY_DUMP_BUF_MAX_LEN ) +return ;So we left the memory at *buf uninitialized but failed to tell the caller this. The caller will then proceed to use uninitialized memory.
Quoted 7 lines
Quoted 1 line
It's a programming error, so simply going BUG seems OK.
Are you suggesting that I should remove the above check because developers should take care of the length of the buffer to write outside of the
relay_dumpfunction? or use this instead:WARN_ON_ONCE(len < RELAY_DUMP_BUF_MAX_LEN);
?
Quoted 6 lines
It's a poor interface - it returns uninitialized data while not alerting the caller to this. You'll figure something out ;)
Perhaps
BUG_ON(len < RELAY_DUMP_BUF_MAX_LEN);
I'm unsure if BUG_ON is appropriate here since technically speaking it's not a bug. For now, only sizeof(u32) is used in the buffer.
Quoted 5 lines
*buf = '\0'; if (!chan || (flags & ~RELAY_DUMP_MASK)) return;We don't need to check for !buf - the oops message contains the same info.
Got it. Thanks.
Quoted 2 lines
Maybe we don't need to check !chan either. Can it be NULL here?
It depends on how users call this. If users call this without initialization of chan, relay_dump() can avoid the crash. It works like kfree() which prevents the NULL object from being freed.
BTW, should I merge this commit [1] into the series in V2 so that you can easily review?
[1]: https://lore.kernel.org/all/[email protected]/
Thanks,
Jason
Andrew Morton
maintainerRe: [PATCH v1 5/5] relayfs: uniformally use possible cpu iteration
In reply to Jason XingQuoted 9 lines · On Tue, 13 May 2025 10:03:01 +0800 Jason Xing <[email protected]> wrote:
Quoted 8 lines · Andrew Morton
Quoted 4 lines · On Mon, 12 May 2025 10:49:35 +0800 Jason Xing <[email protected]> wrote:
From: Jason Xing <[email protected]>
Use
for_each_possible_cputo create per-cpu relayfs file to avoid later hotplug cpu which doesn't have its own file.
Quoted 8 lines
Quoted 1 line
I don't understand this. Exactly what problem are we trying to solve?
The reason behind this change is can we directly allocate per possible cpu at the initialization phase. After this, even if some cpu goes online, we don't need to care about it.
The idea is directly borrowed from the networking area where people use possible cpu iteration for most cases.
I'd rephrase that as "I know this is wrong but I'm lazy so I'll do it this way for now and I'll fix it up later but I never actually do so".
Yes, handling hotplug is a hassle and for_each_possible_cpu() saves a couple of hours at development time, but its cost is forever.
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Reviewed-by Yushan Zhou <[email protected]>Signed-off-by Jason Xing <[email protected]>--- kernel/relay.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)kernel/relay.c +0
-0@@ -519,7 +519,7 @@ struct rchan *relay_open(const char *base_filename,kref_init (&chan->kref);
Quoted 8 lines
Quoted 6 lines
kernel/relay.c struct rchan *relay_open(const char *base_filename, · line 522
Quoted 3 lines
kernel/relay.c +1
-1mutex_lock (&relay_channels_mutex); -for_each_online_cpu (i) { +for_each_possible_cpu (i) {
num_possible_cpus()can sometimes greatly exceednum_online_cpus(), so this is an unfortunate change.Are you worried about too much extra memory to waste in this case?
That, and the cost of iterating over 1024 CPUs instead of 4 CPUs!
Quoted 8 lines
A relevant thing I would like to share here:
To keep the high performance of transferring data between kernel space and user space, the per-cpu mechanism is needed like how relay works at the moment. It allocates many unnecessary/big memory chunks especially when the cpu number is very large, say, 256. I'm still working on this to see if we can figure out a good approach to balance the performance and memory.
Quoted 5 lines
Quoted 2 lines
It would be better to implement the
hotplug notifier?Right, but sorry, I hesitate to do so because it involves much more work and corresponding tests.
I did this conversion a few times, a million years ago. It's surprisingly easy.
Re: [PATCH v1 5/5] relayfs: uniformally use possible cpu iteration
Quoted 60 lines · Andrew Morton
Quoted 9 lines · On Tue, 13 May 2025 10:03:01 +0800 Jason Xing <[email protected]> wrote:
Quoted 8 lines · Andrew Morton
Quoted 4 lines · On Mon, 12 May 2025 10:49:35 +0800 Jason Xing <[email protected]> wrote:
From: Jason Xing <[email protected]>
Use
for_each_possible_cputo create per-cpu relayfs file to avoid later hotplug cpu which doesn't have its own file.Quoted 8 lines
Quoted 1 line
I don't understand this. Exactly what problem are we trying to solve?
The reason behind this change is can we directly allocate per possible cpu at the initialization phase. After this, even if some cpu goes online, we don't need to care about it.
The idea is directly borrowed from the networking area where people use possible cpu iteration for most cases.
I'd rephrase that as "I know this is wrong but I'm lazy so I'll do it this way for now and I'll fix it up later but I never actually do so".
Yes, handling hotplug is a hassle and
for_each_possible_cpu()saves a couple of hours at development time, but its cost is forever.Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Reviewed-by Yushan Zhou <[email protected]>Signed-off-by Jason Xing <[email protected]>--- kernel/relay.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)kernel/relay.c +0
-0@@ -519,7 +519,7 @@ struct rchan *relay_open(const char *base_filename,kref_init (&chan->kref);Quoted 8 lines
Quoted 6 lines
kernel/relay.c struct rchan *relay_open(const char *base_filename, · line 522
Quoted 3 lines
kernel/relay.c +1
-1mutex_lock (&relay_channels_mutex); -for_each_online_cpu (i) { +for_each_possible_cpu (i) {
num_possible_cpus()can sometimes greatly exceednum_online_cpus(), so this is an unfortunate change.Are you worried about too much extra memory to waste in this case?
That, and the cost of iterating over 1024 CPUs instead of 4 CPUs!
Quoted 8 lines
A relevant thing I would like to share here:
To keep the high performance of transferring data between kernel space and user space, the per-cpu mechanism is needed like how relay works at the moment. It allocates many unnecessary/big memory chunks especially when the cpu number is very large, say, 256. I'm still working on this to see if we can figure out a good approach to balance the performance and memory.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
It would be better to implement the
hotplug notifier?Right, but sorry, I hesitate to do so because it involves much more work and corresponding tests.
I did this conversion a few times, a million years ago. It's surprisingly easy.
I see. Let me try this :)
Thanks,
Jason
Andrew Morton
maintainerRe: [PATCH v1 2/5] relayfs: introduce dump of relayfs statistics function
In reply to Jason XingQuoted 6 lines · On Tue, 13 May 2025 10:26:45 +0800 Jason Xing <[email protected]> wrote:
Quoted 2 lines
Maybe we don't need to check !chan either. Can it be NULL here?
It depends on how users call this. If users call this without initialization of chan,
relay_dump()can avoid the crash. It works likekfree()which prevents the NULL object from being freed.
hm, OK. Generally, I don't think that kernel code should be very tolerant of bugs in the caller. If the caller passes us bad stuff then that's the caller's fault and the caller should be fixed. If the client code responds to bad input with a nice solid oops, then great! The caller will get fixed.
Quoted 4 lines
BTW, should I merge this commit [1] into the series in V2 so that you can easily review?
That seems unrelated to this work so it seems inappropriate to combine the two.
I skipped [1] because I'm waiting for overall clarity on what's happening with relay[fs].
Re: [PATCH v1 2/5] relayfs: introduce dump of relayfs statistics function
Quoted 10 lines · Andrew Morton
Quoted 6 lines · On Tue, 13 May 2025 10:26:45 +0800 Jason Xing <[email protected]> wrote:
Quoted 2 lines
Maybe we don't need to check !chan either. Can it be NULL here?
It depends on how users call this. If users call this without initialization of chan,
relay_dump()can avoid the crash. It works likekfree()which prevents the NULL object from being freed.
Quoted 5 lines
hm, OK. Generally, I don't think that kernel code should be very tolerant of bugs in the caller. If the caller passes us bad stuff then that's the caller's fault and the caller should be fixed. If the client code responds to bad input with a nice solid oops, then great! The caller will get fixed.
I learned. Thanks. I will skip the check for that.
Quoted 8 lines
Quoted 5 lines
BTW, should I merge this commit [1] into the series in V2 so that you can easily review?
That seems unrelated to this work so it seems inappropriate to combine the two.
This series is built on top of [1].
Quoted 3 lines
I skipped [1] because I'm waiting for overall clarity on what's happening with relay[fs].
Do you refer to this thread[2]? Well, that conversation/reply made me feel lost. I believe you've already seen that. If so, it seems we're working on the dead code together....
[2]: https://lore.kernel.org/all/[email protected]/
Thanks,
Jason
Re: [PATCH v1 5/5] relayfs: uniformally use possible cpu iteration
In reply to Andrew MortonQuoted 23 lines · Andrew Morton
Quoted 4 lines · On Mon, 12 May 2025 10:49:35 +0800 Jason Xing <[email protected]> wrote:
From: Jason Xing <[email protected]>
Use
for_each_possible_cputo create per-cpu relayfs file to avoid later hotplug cpu which doesn't have its own file.I don't understand this. Exactly what problem are we trying to solve?
Quoted 13 lines
Reviewed-by Yushan Zhou <[email protected]>Signed-off-by Jason Xing <[email protected]>--- kernel/relay.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)kernel/relay.c +0
-0@@ -519,7 +519,7 @@ struct rchan *relay_open(const char *base_filename,kref_init (&chan->kref);
Quoted 7 lines
kernel/relay.c struct rchan *relay_open(const char *base_filename, · line 522
Quoted 3 lines
kernel/relay.c +1
-1mutex_lock (&relay_channels_mutex); -for_each_online_cpu (i) { +for_each_possible_cpu (i) {
num_possible_cpus()can sometimes greatly exceednum_online_cpus(), so this is an unfortunate change. It would be better to implement the hotplug notifier?
For the record, the hotplug notifier has been retired by the commit 530e9b76ae8f("cpu/hotplug: Remove obsolete cpu hotplug register/unregister functions"). And now in relay, a similar feature called hotplug state machine has already been implemented by the commit e6d4989a9ad1 ("relayfs: Convert to hotplug state machine"). So the relay has hotplug support.
Sorry for missing this point. I would drop this patch.
Thanks,
Jason
Quoted 15 lines
Quoted 15 lines
kernel/relay.c +1
-2buf =relay_open_buf (chan, i);if (!buf) goto free_bufs;@@ -615,7 +615,7 @@ int relay_late_setup_files(struct rchan *chan,* no files associated. So it's safe to call relay_setup_buf_file() * on all currently online CPUs. */ -for_each_online_cpu (i) { +for_each_possible_cpu (i) { buf = *per_cpu_ptr (chan->buf, i);if (unlikely (!buf)) {WARN_ONCE (1 ,KERN_ERR "CPU has no buffer!\n" ); --2.43.5
Masami Hiramatsu (Google)
maintainerRe: [PATCH v1 2/5] relayfs: introduce dump of relayfs statistics function
In reply to Jason XingHi Jason,
Quoted 78 lines · On Mon, 12 May 2025 10:49:32 +0800 Jason Xing <[email protected]> wrote:
From: Jason Xing <[email protected]>
In this version, only support dumping the counter for buffer full and implement the framework of how it works. Users MUST pass a valid @buf with a valid @len that is required to be larger than
RELAY_DUMP_BUF_MAX_LENto acquire which information indicated by @flags to dump.
RELAY_DUMP_BUF_MAX_LENshows the maximum len of the buffer if users choose to dump all the values.Users can use this buffer to do whatever they expect in their own kernel module, say, print to console/dmesg or write them into the relay buffer.
Reviewed-by Yushan Zhou <[email protected]>Signed-off-by Jason Xing <[email protected]>--- include/linux/relay.h | 10 ++++++++++ kernel/relay.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+)include/linux/relay.h +10
-0@@ -31,6 +31,15 @@/* * Relay buffer error statistics dump */ +enum { +RELAY_DUMP_BUF_FULL = (1 <<0 ), + +RELAY_DUMP_LAST =RELAY_DUMP_BUF_FULL , +RELAY_DUMP_MASK = (RELAY_DUMP_LAST -1 ) |RELAY_DUMP_LAST +}; + +#define RELAY_DUMP_BUF_MAX_LEN 32 +struct rchan_buf_error_stats {unsigned int full;/* counter for buffer full */ @@ -170,6 +179,7 @@ extern int relay_late_setup_files(struct rchan *chan, struct dentry *parent);extern void relay_close (struct rchan *chan);extern void relay_flush (struct rchan *chan); +extern void relay_dump (struct rchan *chan,char *buf,int len,int flags);extern void relay_subbufs_consumed (struct rchan *chan,unsigned int cpu,size_t consumed);kernel/relay.c +22
-0@@ -810,6 +810,41 @@ void relay_flush(struct rchan *chan) }EXPORT_SYMBOL_GPL (relay_flush); +/** +* relay_dump - dump statistics of the specified channel buffer +* @chan: the channel +* @buf: buf to store statistics +* @len: len of buf to check +* @flags: select particular information to dump +*/ +void relay_dump (struct rchan *chan,char *buf,int len,int flags) +{ +unsigned int i, full_counter =0 ; +struct rchan_buf *rbuf; +int offset =0 ; + +if (!chan || !buf || flags & ~RELAY_DUMP_MASK ) +return ; + +if (len <RELAY_DUMP_BUF_MAX_LEN ) +return ; + +if (chan->is_global) { + rbuf = *per_cpu_ptr (chan->buf,0 ); + full_counter = rbuf->stats.full;
kernel/relay.c void relay_flush(struct rchan *chan) · line 844
Quoted 10 lines
kernel/relay.c +10
-0+ }else { +for_each_possible_cpu (i) { +if ((rbuf = *per_cpu_ptr (chan->buf, i))) + full_counter += rbuf->stats.full; + } + +if (flags &RELAY_DUMP_BUF_FULL ) + offset +=snprintf (buf,sizeof (unsigned int ),"%u" , full_counter); + +snprintf (buf + offset,1 ,"\n" );
Is there any reason to return the value as string? If it returns a digit value and the caller makes it a string, it could be more flexible for other use cases.
Thank you,
Quoted 9 lines
kernel/relay.c +3
-0+} +EXPORT_SYMBOL_GPL (relay_dump); +/** * relay_file_open - open file op for relay files * @inode: the inode Signature · 1 line
2.43.5
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Re: [PATCH v1 2/5] relayfs: introduce dump of relayfs statistics function
Hi Masami,
Quoted 90 lines · Masami Hiramatsu
Hi Jason,
Quoted 84 lines · On Mon, 12 May 2025 10:49:32 +0800 Jason Xing <[email protected]> wrote:
From: Jason Xing <[email protected]>
In this version, only support dumping the counter for buffer full and implement the framework of how it works. Users MUST pass a valid @buf with a valid @len that is required to be larger than
RELAY_DUMP_BUF_MAX_LENto acquire which information indicated by @flags to dump.
RELAY_DUMP_BUF_MAX_LENshows the maximum len of the buffer if users choose to dump all the values.Users can use this buffer to do whatever they expect in their own kernel module, say, print to console/dmesg or write them into the relay buffer.
Reviewed-by Yushan Zhou <[email protected]>Signed-off-by Jason Xing <[email protected]>--- include/linux/relay.h | 10 ++++++++++ kernel/relay.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+)include/linux/relay.h +10
-0@@ -31,6 +31,15 @@/* * Relay buffer error statistics dump */ +enum { +RELAY_DUMP_BUF_FULL = (1 <<0 ), + +RELAY_DUMP_LAST =RELAY_DUMP_BUF_FULL , +RELAY_DUMP_MASK = (RELAY_DUMP_LAST -1 ) |RELAY_DUMP_LAST +}; + +#define RELAY_DUMP_BUF_MAX_LEN 32 +struct rchan_buf_error_stats {unsigned int full;/* counter for buffer full */ @@ -170,6 +179,7 @@ extern int relay_late_setup_files(struct rchan *chan, struct dentry *parent);extern void relay_close (struct rchan *chan);extern void relay_flush (struct rchan *chan); +extern void relay_dump (struct rchan *chan,char *buf,int len,int flags);extern void relay_subbufs_consumed (struct rchan *chan,unsigned int cpu,size_t consumed);kernel/relay.c +28
-0@@ -810,6 +810,41 @@ void relay_flush(struct rchan *chan) }EXPORT_SYMBOL_GPL (relay_flush); +/** +* relay_dump - dump statistics of the specified channel buffer +* @chan: the channel +* @buf: buf to store statistics +* @len: len of buf to check +* @flags: select particular information to dump +*/ +void relay_dump (struct rchan *chan,char *buf,int len,int flags) +{ +unsigned int i, full_counter =0 ; +struct rchan_buf *rbuf; +int offset =0 ; + +if (!chan || !buf || flags & ~RELAY_DUMP_MASK ) +return ; + +if (len <RELAY_DUMP_BUF_MAX_LEN ) +return ; + +if (chan->is_global) { + rbuf = *per_cpu_ptr (chan->buf,0 ); + full_counter = rbuf->stats.full; + }else { +for_each_possible_cpu (i) { +if ((rbuf = *per_cpu_ptr (chan->buf, i))) + full_counter += rbuf->stats.full; + } +
Quoted 8 lines
kernel/relay.c void relay_flush(struct rchan *chan) · line 844
Quoted 4 lines
kernel/relay.c +4
-0+if (flags &RELAY_DUMP_BUF_FULL ) + offset +=snprintf (buf,sizeof (unsigned int ),"%u" , full_counter); + +snprintf (buf + offset,1 ,"\n" );Is there any reason to return the value as string? If it returns a digit value and the caller makes it a string, it could be more flexible for other use cases.
Thanks for the feedback.
I will remove the above one as you pointed out in the next revision. And it seems unnecessary to add '\0' at the end of the buffer like "*buf = '\0';"?
While at it, I'm thinking if I can change the return value of relay_dump() to "how many bytes do we actually write into the buffer"? Does it sound better?
Thanks,
Jason
Quoted 16 lines
Thank you,
Quoted 9 lines
kernel/relay.c +3
-1+} +EXPORT_SYMBOL_GPL (relay_dump); +/** * relay_file_open - open file op for relay files * @inode: the inode -- 2.43.5
--
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerRe: [PATCH v1 2/5] relayfs: introduce dump of relayfs statistics function
Quoted 102 lines · On Tue, 13 May 2025 18:32:29 +0800 Jason Xing <[email protected]> wrote:
Hi Masami,
Quoted 90 lines · Masami Hiramatsu
Hi Jason,
Quoted 84 lines · On Mon, 12 May 2025 10:49:32 +0800 Jason Xing <[email protected]> wrote:
From: Jason Xing <[email protected]>
In this version, only support dumping the counter for buffer full and implement the framework of how it works. Users MUST pass a valid @buf with a valid @len that is required to be larger than
RELAY_DUMP_BUF_MAX_LENto acquire which information indicated by @flags to dump.
RELAY_DUMP_BUF_MAX_LENshows the maximum len of the buffer if users choose to dump all the values.Users can use this buffer to do whatever they expect in their own kernel module, say, print to console/dmesg or write them into the relay buffer.
Reviewed-by Yushan Zhou <[email protected]>Signed-off-by Jason Xing <[email protected]>--- include/linux/relay.h | 10 ++++++++++ kernel/relay.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+)include/linux/relay.h +10
-0@@ -31,6 +31,15 @@/* * Relay buffer error statistics dump */ +enum { +RELAY_DUMP_BUF_FULL = (1 <<0 ), + +RELAY_DUMP_LAST =RELAY_DUMP_BUF_FULL , +RELAY_DUMP_MASK = (RELAY_DUMP_LAST -1 ) |RELAY_DUMP_LAST +}; + +#define RELAY_DUMP_BUF_MAX_LEN 32 +struct rchan_buf_error_stats {unsigned int full;/* counter for buffer full */ @@ -170,6 +179,7 @@ extern int relay_late_setup_files(struct rchan *chan, struct dentry *parent);extern void relay_close (struct rchan *chan);extern void relay_flush (struct rchan *chan); +extern void relay_dump (struct rchan *chan,char *buf,int len,int flags);extern void relay_subbufs_consumed (struct rchan *chan,unsigned int cpu,size_t consumed);kernel/relay.c +28
-0@@ -810,6 +810,41 @@ void relay_flush(struct rchan *chan) }EXPORT_SYMBOL_GPL (relay_flush); +/** +* relay_dump - dump statistics of the specified channel buffer +* @chan: the channel +* @buf: buf to store statistics +* @len: len of buf to check +* @flags: select particular information to dump +*/ +void relay_dump (struct rchan *chan,char *buf,int len,int flags) +{ +unsigned int i, full_counter =0 ; +struct rchan_buf *rbuf; +int offset =0 ; + +if (!chan || !buf || flags & ~RELAY_DUMP_MASK ) +return ; + +if (len <RELAY_DUMP_BUF_MAX_LEN ) +return ; + +if (chan->is_global) { + rbuf = *per_cpu_ptr (chan->buf,0 ); + full_counter = rbuf->stats.full; + }else { +for_each_possible_cpu (i) { +if ((rbuf = *per_cpu_ptr (chan->buf, i))) + full_counter += rbuf->stats.full; + } +Quoted 8 lines
kernel/relay.c void relay_flush(struct rchan *chan) · line 844
Quoted 4 lines
kernel/relay.c +4
-0+if (flags &RELAY_DUMP_BUF_FULL ) + offset +=snprintf (buf,sizeof (unsigned int ),"%u" , full_counter); + +snprintf (buf + offset,1 ,"\n" );Is there any reason to return the value as string? If it returns a digit value and the caller makes it a string, it could be more flexible for other use cases.
Quoted 5 lines
Thanks for the feedback.
I will remove the above one as you pointed out in the next revision. And it seems unnecessary to add '\0' at the end of the buffer like "*buf = '\0';"?
Sorry, I think you missed my point. I meant it should be
/* Return the number of fullfilled buffer in the channel */ size_t relay_full(struct rchan *chan);
And keep the snprintf() in the blk_dropped_read() because that function is responsible for formatting the output.
static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
size_t count, loff_t *ppos)
{
struct blk_trace *bt = filp->private_data;
char buf[16];
snprintf(buf, sizeof(buf), "%zu\n", relay_full(bt->rchan)); return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
}
But the question is that what blk_subbuf_start_callback() counts is really equal to what the relay_full() counts, because it seems relay_full() just returns the current status of the channel, but bt->dropped is the accumlated number of dropping events.
This means that if the client (reader) reads the subbufs the number of full subbufs will be decreased, but the number of dropped event does not change.
Can you recheck it?
Thank you,
Quoted 24 lines
While at it, I'm thinking if I can change the return value of
relay_dump()to "how many bytes do we actually write into the buffer"? Does it sound better?Thanks,
JasonQuoted 16 lines
Thank you,
Quoted 9 lines
kernel/relay.c +3
-1+} +EXPORT_SYMBOL_GPL (relay_dump); +/** * relay_file_open - open file op for relay files * @inode: the inode -- 2.43.5
--
Masami Hiramatsu (Google) <[email protected]>
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Re: [PATCH v1 2/5] relayfs: introduce dump of relayfs statistics function
Quoted 112 lines · Masami Hiramatsu
Quoted 102 lines · On Tue, 13 May 2025 18:32:29 +0800 Jason Xing <[email protected]> wrote:
Hi Masami,
Quoted 90 lines · Masami Hiramatsu
Hi Jason,
Quoted 84 lines · On Mon, 12 May 2025 10:49:32 +0800 Jason Xing <[email protected]> wrote:
From: Jason Xing <[email protected]>
In this version, only support dumping the counter for buffer full and implement the framework of how it works. Users MUST pass a valid @buf with a valid @len that is required to be larger than
RELAY_DUMP_BUF_MAX_LENto acquire which information indicated by @flags to dump.
RELAY_DUMP_BUF_MAX_LENshows the maximum len of the buffer if users choose to dump all the values.Users can use this buffer to do whatever they expect in their own kernel module, say, print to console/dmesg or write them into the relay buffer.
Reviewed-by Yushan Zhou <[email protected]>Signed-off-by Jason Xing <[email protected]>--- include/linux/relay.h | 10 ++++++++++ kernel/relay.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+)include/linux/relay.h +10
-0@@ -31,6 +31,15 @@/* * Relay buffer error statistics dump */ +enum { +RELAY_DUMP_BUF_FULL = (1 <<0 ), + +RELAY_DUMP_LAST =RELAY_DUMP_BUF_FULL , +RELAY_DUMP_MASK = (RELAY_DUMP_LAST -1 ) |RELAY_DUMP_LAST +}; + +#define RELAY_DUMP_BUF_MAX_LEN 32 +struct rchan_buf_error_stats {unsigned int full;/* counter for buffer full */ @@ -170,6 +179,7 @@ extern int relay_late_setup_files(struct rchan *chan, struct dentry *parent);extern void relay_close (struct rchan *chan);extern void relay_flush (struct rchan *chan); +extern void relay_dump (struct rchan *chan,char *buf,int len,int flags);extern void relay_subbufs_consumed (struct rchan *chan,unsigned int cpu,size_t consumed);kernel/relay.c +28
-0@@ -810,6 +810,41 @@ void relay_flush(struct rchan *chan) }EXPORT_SYMBOL_GPL (relay_flush); +/** +* relay_dump - dump statistics of the specified channel buffer +* @chan: the channel +* @buf: buf to store statistics +* @len: len of buf to check +* @flags: select particular information to dump +*/ +void relay_dump (struct rchan *chan,char *buf,int len,int flags) +{ +unsigned int i, full_counter =0 ; +struct rchan_buf *rbuf; +int offset =0 ; + +if (!chan || !buf || flags & ~RELAY_DUMP_MASK ) +return ; + +if (len <RELAY_DUMP_BUF_MAX_LEN ) +return ; + +if (chan->is_global) { + rbuf = *per_cpu_ptr (chan->buf,0 ); + full_counter = rbuf->stats.full; + }else { +for_each_possible_cpu (i) { +if ((rbuf = *per_cpu_ptr (chan->buf, i))) + full_counter += rbuf->stats.full; + } +Quoted 8 lines
kernel/relay.c void relay_flush(struct rchan *chan) · line 844
Quoted 4 lines
kernel/relay.c +4
-0+if (flags &RELAY_DUMP_BUF_FULL ) + offset +=snprintf (buf,sizeof (unsigned int ),"%u" , full_counter); + +snprintf (buf + offset,1 ,"\n" );Is there any reason to return the value as string? If it returns a digit value and the caller makes it a string, it could be more flexible for other use cases.
Quoted 5 lines
Thanks for the feedback.
I will remove the above one as you pointed out in the next revision. And it seems unnecessary to add '\0' at the end of the buffer like "*buf = '\0';"?
Quoted 7 lines
Sorry, I think you missed my point. I meant it should be
/* Return the number of fullfilled buffer in the channel */
size_trelay_full(struct rchan *chan);And keep the
snprintf()in theblk_dropped_read()because that function is responsible for formatting the output.
Oh, sorry, it's not what I want because (please see patch [4/5] [1]) relay_dump() works to print various statistics of the buffer. In this patch, 'full' counter is the first one.
[1]: https://lore.kernel.org/all/[email protected]/
Quoted 12 lines
static
ssize_tblk_dropped_read(struct file *filp, char__user*buffer,
size_tcount,loff_t*ppos){ struct blk_trace *bt = filp->private_data; char buf[16]; snprintf(buf, sizeof(buf), "%zu\n", relay_full(bt->rchan)); return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf)); }
Quoted 8 lines
But the question is that what
blk_subbuf_start_callback()counts is really equal to what therelay_full()counts, because it seemsrelay_full()just returns the current status of the channel, butbt->droppedis the accumlated number of dropping events.This means that if the client (reader) reads the subbufs the number of full subbufs will be decreased, but the number of dropped event does not change.
At least in this series, I didn't give the 'full' counter any chance to decrement, which means it's compatible with blktrace, unless __relay_reset() is triggered :)
While discussing with you on this point, I suddenly recalled that in some network drivers, they implemented 'wake' and 'stop' as a pair to know what the current status of a certain queue is. I think I can add a similar thing to the buffer about 1) how many times are the buffer full, 2) how many times does the buffer get rid of being full.
Thanks,
Jason
Quoted 33 lines
Can you recheck it?
Thank you,
Quoted 24 lines
While at it, I'm thinking if I can change the return value of
relay_dump()to "how many bytes do we actually write into the buffer"? Does it sound better?Thanks,
JasonQuoted 16 lines
Thank you,
Quoted 9 lines
kernel/relay.c +3
-1+} +EXPORT_SYMBOL_GPL (relay_dump); +/** * relay_file_open - open file op for relay files * @inode: the inode -- 2.43.5
--
Masami Hiramatsu (Google) <[email protected]>--
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerRe: [PATCH v1 2/5] relayfs: introduce dump of relayfs statistics function
Quoted 21 lines · On Tue, 13 May 2025 21:46:25 +0800 Jason Xing <[email protected]> wrote:
Quoted 21 lines
Quoted 10 lines
Quoted 9 lines
kernel/relay.c void relay_flush(struct rchan *chan) · line 844
Quoted 5 lines
kernel/relay.c +5
-0+ +if (flags &RELAY_DUMP_BUF_FULL ) + offset +=snprintf (buf,sizeof (unsigned int ),"%u" , full_counter); + +snprintf (buf + offset,1 ,"\n" );Is there any reason to return the value as string? If it returns a digit value and the caller makes it a string, it could be more flexible for other use cases.
Quoted 5 lines
Thanks for the feedback.
I will remove the above one as you pointed out in the next revision. And it seems unnecessary to add '\0' at the end of the buffer like "*buf = '\0';"?
Sorry, I think you missed my point. I meant it should be
/* Return the number of fullfilled buffer in the channel */
size_trelay_full(struct rchan *chan);
Quoted 8 lines
Quoted 2 lines
And keep the
snprintf()in theblk_dropped_read()because that function is responsible for formatting the output.Oh, sorry, it's not what I want because (please see patch [4/5] [1])
relay_dump()works to print various statistics of the buffer. In this patch, 'full' counter is the first one.
Yes, that's why I asked you to make it just returning raw value. The string formatting of those values is blk_dropped_read()s business (because it is a 'read' callback), not for relayfs.
For example, other relayfs user wants to summarize the values or calculate the percentage form that value. Also, we don't need to bother to check the buffer size etc, because blk_dropped_read() knows that.
Quoted 18 lines
Quoted 18 lines
static
ssize_tblk_dropped_read(struct file *filp, char__user*buffer,
size_tcount,loff_t*ppos){ struct blk_trace *bt = filp->private_data; char buf[16]; snprintf(buf, sizeof(buf), "%zu\n", relay_full(bt->rchan)); return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf)); }But the question is that what
blk_subbuf_start_callback()counts is really equal to what therelay_full()counts, because it seemsrelay_full()just returns the current status of the channel, butbt->droppedis the accumlated number of dropping events.
Quoted 7 lines
Quoted 3 lines
This means that if the client (reader) reads the subbufs the number of full subbufs will be decreased, but the number of dropped event does not change.
At least in this series, I didn't give the 'full' counter any chance to decrement, which means it's compatible with blktrace, unless
__relay_reset()is triggered :)
Ah, OK. I missed what [1/5] does. I agree that this does the same thing.
Quoted 6 lines
While discussing with you on this point, I suddenly recalled that in some network drivers, they implemented 'wake' and 'stop' as a pair to know what the current status of a certain queue is. I think I can add a similar thing to the buffer about 1) how many times are the buffer full, 2) how many times does the buffer get rid of being full.
Sounds nice.
Thank you,
Quoted 37 lines
Thanks,
JasonQuoted 33 lines
Can you recheck it?
Thank you,
Quoted 24 lines
While at it, I'm thinking if I can change the return value of
relay_dump()to "how many bytes do we actually write into the buffer"? Does it sound better?Thanks,
JasonQuoted 16 lines
Thank you,
Quoted 9 lines
kernel/relay.c +3
-1+} +EXPORT_SYMBOL_GPL (relay_dump); +/** * relay_file_open - open file op for relay files * @inode: the inode -- 2.43.5
--
Masami Hiramatsu (Google) <[email protected]>--
Masami Hiramatsu (Google) <[email protected]>
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Re: [PATCH v1 2/5] relayfs: introduce dump of relayfs statistics function
Quoted 34 lines · Masami Hiramatsu
Quoted 21 lines · On Tue, 13 May 2025 21:46:25 +0800 Jason Xing <[email protected]> wrote:
Quoted 21 lines
Quoted 10 lines
Quoted 9 lines
kernel/relay.c void relay_flush(struct rchan *chan) · line 844
Quoted 5 lines
kernel/relay.c +5
-0+ +if (flags &RELAY_DUMP_BUF_FULL ) + offset +=snprintf (buf,sizeof (unsigned int ),"%u" , full_counter); + +snprintf (buf + offset,1 ,"\n" );Is there any reason to return the value as string? If it returns a digit value and the caller makes it a string, it could be more flexible for other use cases.
Quoted 5 lines
Thanks for the feedback.
I will remove the above one as you pointed out in the next revision. And it seems unnecessary to add '\0' at the end of the buffer like "*buf = '\0';"?
Sorry, I think you missed my point. I meant it should be
/* Return the number of fullfilled buffer in the channel */
size_trelay_full(struct rchan *chan);Quoted 8 lines
Quoted 2 lines
And keep the
snprintf()in theblk_dropped_read()because that function is responsible for formatting the output.Oh, sorry, it's not what I want because (please see patch [4/5] [1])
relay_dump()works to print various statistics of the buffer. In this patch, 'full' counter is the first one.
Quoted 8 lines
Yes, that's why I asked you to make it just returning raw value. The string formatting of those values is blk_dropped_read()s business (because it is a 'read' callback), not for relayfs.
For example, other relayfs user wants to summarize the values or calculate the percentage form that value. Also, we don't need to bother to check the buffer size etc, because
blk_dropped_read()knows that.
Oh, it makes sense. Thanks.
I will make relay_dump() return raw value which depends on what kind of flag caller passes, like RELAY_DUMP_BUF_FULL or RELAY_DUMP_WRT_BIG or even more other info. On top of that, I can then get rid of the annoying buffer-related code snippets :)
Thanks,
Jason
Quoted 82 lines
Quoted 19 lines
Quoted 19 lines
static
ssize_tblk_dropped_read(struct file *filp, char__user*buffer,
size_tcount,loff_t*ppos){ struct blk_trace *bt = filp->private_data; char buf[16]; snprintf(buf, sizeof(buf), "%zu\n", relay_full(bt->rchan)); return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf)); }But the question is that what
blk_subbuf_start_callback()counts is really equal to what therelay_full()counts, because it seemsrelay_full()just returns the current status of the channel, butbt->droppedis the accumlated number of dropping events.Quoted 7 lines
Quoted 3 lines
This means that if the client (reader) reads the subbufs the number of full subbufs will be decreased, but the number of dropped event does not change.
At least in this series, I didn't give the 'full' counter any chance to decrement, which means it's compatible with blktrace, unless
__relay_reset()is triggered :)Ah, OK. I missed what [1/5] does. I agree that this does the same thing.
Quoted 6 lines
While discussing with you on this point, I suddenly recalled that in some network drivers, they implemented 'wake' and 'stop' as a pair to know what the current status of a certain queue is. I think I can add a similar thing to the buffer about 1) how many times are the buffer full, 2) how many times does the buffer get rid of being full.
Sounds nice.
Thank you,
Quoted 37 lines
Thanks,
JasonQuoted 33 lines
Can you recheck it?
Thank you,
Quoted 24 lines
While at it, I'm thinking if I can change the return value of
relay_dump()to "how many bytes do we actually write into the buffer"? Does it sound better?Thanks,
JasonQuoted 16 lines
Thank you,
Quoted 9 lines
kernel/relay.c +3
-1+} +EXPORT_SYMBOL_GPL (relay_dump); +/** * relay_file_open - open file op for relay files * @inode: the inode -- 2.43.5
--
Masami Hiramatsu (Google) <[email protected]>--
Masami Hiramatsu (Google) <[email protected]>--
Masami Hiramatsu (Google) <[email protected]>