Skip to content

Commit 023542f

Browse files
jnikulatorvalds
authored andcommitted
relay: allow the use of const callback structs
None of the relay users require the use of mutable structs for callbacks, however the relay code does. Instead of assigning the default callback for subbuf_start, add a wrapper to conditionally call the client callback if available, and fall back to default behaviour otherwise. This lets all relay users make their struct rchan_callbacks const data. [[email protected]: cleanups, per Christoph] Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/cc3ff292e4eb4fdc56bee3d690c7b8e39209cd37.1606153547.git.jani.nikula@intel.com Signed-off-by: Jani Nikula <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Cc: Jens Axboe <[email protected]> Cc: Kalle Valo <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 371e038 commit 023542f

File tree

2 files changed

+12
-29
lines changed

2 files changed

+12
-29
lines changed

include/linux/relay.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ struct rchan
6262
size_t subbuf_size; /* sub-buffer size */
6363
size_t n_subbufs; /* number of sub-buffers per buffer */
6464
size_t alloc_size; /* total buffer size allocated */
65-
struct rchan_callbacks *cb; /* client callbacks */
65+
const struct rchan_callbacks *cb; /* client callbacks */
6666
struct kref kref; /* channel refcount */
6767
void *private_data; /* for user-defined data */
6868
size_t last_toobig; /* tried to log event > subbuf size */
@@ -157,7 +157,7 @@ struct rchan *relay_open(const char *base_filename,
157157
struct dentry *parent,
158158
size_t subbuf_size,
159159
size_t n_subbufs,
160-
struct rchan_callbacks *cb,
160+
const struct rchan_callbacks *cb,
161161
void *private_data);
162162
extern int relay_late_setup_files(struct rchan *chan,
163163
const char *base_filename,

kernel/relay.c

+10-27
Original file line numberDiff line numberDiff line change
@@ -252,23 +252,14 @@ EXPORT_SYMBOL_GPL(relay_buf_full);
252252
* High-level relay kernel API and associated functions.
253253
*/
254254

255-
/*
256-
* rchan_callback implementations defining default channel behavior. Used
257-
* in place of corresponding NULL values in client callback struct.
258-
*/
259-
260-
/*
261-
* subbuf_start() default callback. Does nothing.
262-
*/
263-
static int subbuf_start_default_callback (struct rchan_buf *buf,
264-
void *subbuf,
265-
void *prev_subbuf,
266-
size_t prev_padding)
255+
static int relay_subbuf_start(struct rchan_buf *buf, void *subbuf,
256+
void *prev_subbuf, size_t prev_padding)
267257
{
268-
if (relay_buf_full(buf))
269-
return 0;
258+
if (!buf->chan->cb->subbuf_start)
259+
return !relay_buf_full(buf);
270260

271-
return 1;
261+
return buf->chan->cb->subbuf_start(buf, subbuf,
262+
prev_subbuf, prev_padding);
272263
}
273264

274265
/**
@@ -314,7 +305,7 @@ static void __relay_reset(struct rchan_buf *buf, unsigned int init)
314305
for (i = 0; i < buf->chan->n_subbufs; i++)
315306
buf->padding[i] = 0;
316307

317-
buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0);
308+
relay_subbuf_start(buf, buf->data, NULL, 0);
318309
}
319310

320311
/**
@@ -442,14 +433,6 @@ static void relay_close_buf(struct rchan_buf *buf)
442433
kref_put(&buf->kref, relay_remove_buf);
443434
}
444435

445-
static void setup_callbacks(struct rchan *chan,
446-
struct rchan_callbacks *cb)
447-
{
448-
if (!cb->subbuf_start)
449-
cb->subbuf_start = subbuf_start_default_callback;
450-
chan->cb = cb;
451-
}
452-
453436
int relay_prepare_cpu(unsigned int cpu)
454437
{
455438
struct rchan *chan;
@@ -495,7 +478,7 @@ struct rchan *relay_open(const char *base_filename,
495478
struct dentry *parent,
496479
size_t subbuf_size,
497480
size_t n_subbufs,
498-
struct rchan_callbacks *cb,
481+
const struct rchan_callbacks *cb,
499482
void *private_data)
500483
{
501484
unsigned int i;
@@ -529,7 +512,7 @@ struct rchan *relay_open(const char *base_filename,
529512
chan->has_base_filename = 1;
530513
strlcpy(chan->base_filename, base_filename, NAME_MAX);
531514
}
532-
setup_callbacks(chan, cb);
515+
chan->cb = cb;
533516
kref_init(&chan->kref);
534517

535518
mutex_lock(&relay_channels_mutex);
@@ -712,7 +695,7 @@ size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
712695
new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
713696
new = buf->start + new_subbuf * buf->chan->subbuf_size;
714697
buf->offset = 0;
715-
if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) {
698+
if (!relay_subbuf_start(buf, new, old, buf->prev_padding)) {
716699
buf->offset = buf->chan->subbuf_size + 1;
717700
return 0;
718701
}

0 commit comments

Comments
 (0)