Skip to content

Commit 3d3c376

Browse files
covanamrostedt
authored andcommitted
rv: Merge struct rv_reactor_def into struct rv_reactor
Each struct rv_reactor has a unique struct rv_reactor_def associated with it. struct rv_reactor is statically allocated, while struct rv_reactor_def is dynamically allocated. This makes the code more complicated than it should be: - Lookup is required to get the associated rv_reactor_def from rv_reactor - Dynamic memory allocation is required for rv_reactor_def. This is harder to get right compared to static memory. For instance, there is an existing mistake: rv_unregister_reactor() does not free the memory allocated by rv_register_reactor(). This is fortunately not a real memory leak problem as rv_unregister_reactor() is never called. Simplify and merge rv_reactor_def into rv_reactor. Cc: Masami Hiramatsu <[email protected]> Cc: Mathieu Desnoyers <[email protected]> Link: https://lore.kernel.org/71cb91c86cd40df5b8c492b788787f2a73c3eaa3.1753378331.git.namcao@linutronix.de Reviewed-by: Gabriele Monaco <[email protected]> Signed-off-by: Nam Cao <[email protected]> Signed-off-by: Steven Rostedt (Google) <[email protected]>
1 parent 24cbfe1 commit 3d3c376

File tree

3 files changed

+43
-63
lines changed

3 files changed

+43
-63
lines changed

include/linux/rv.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ struct rv_reactor {
9090
const char *name;
9191
const char *description;
9292
__printf(1, 2) void (*react)(const char *msg, ...);
93+
struct list_head list;
94+
/* protected by the monitor interface lock */
95+
int counter;
9396
};
9497
#endif
9598

@@ -101,7 +104,7 @@ struct rv_monitor {
101104
void (*disable)(void);
102105
void (*reset)(void);
103106
#ifdef CONFIG_RV_REACTORS
104-
struct rv_reactor_def *rdef;
107+
struct rv_reactor *reactor;
105108
__printf(1, 2) void (*react)(const char *msg, ...);
106109
bool reacting;
107110
#endif

kernel/trace/rv/rv.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@ struct rv_interface {
2323
extern struct mutex rv_interface_lock;
2424
extern struct list_head rv_monitors_list;
2525

26-
#ifdef CONFIG_RV_REACTORS
27-
struct rv_reactor_def {
28-
struct list_head list;
29-
struct rv_reactor *reactor;
30-
/* protected by the monitor interface lock */
31-
int counter;
32-
};
33-
#endif
34-
3526
struct dentry *get_monitors_root(void);
3627
int rv_disable_monitor(struct rv_monitor *mon);
3728
int rv_enable_monitor(struct rv_monitor *mon);

kernel/trace/rv/rv_reactors.c

Lines changed: 39 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@
7070
*/
7171
static LIST_HEAD(rv_reactors_list);
7272

73-
static struct rv_reactor_def *get_reactor_rdef_by_name(char *name)
73+
static struct rv_reactor *get_reactor_rdef_by_name(char *name)
7474
{
75-
struct rv_reactor_def *r;
75+
struct rv_reactor *r;
7676

7777
list_for_each_entry(r, &rv_reactors_list, list) {
78-
if (strcmp(name, r->reactor->name) == 0)
78+
if (strcmp(name, r->name) == 0)
7979
return r;
8080
}
8181
return NULL;
@@ -86,9 +86,9 @@ static struct rv_reactor_def *get_reactor_rdef_by_name(char *name)
8686
*/
8787
static int reactors_show(struct seq_file *m, void *p)
8888
{
89-
struct rv_reactor_def *rea_def = p;
89+
struct rv_reactor *reactor = p;
9090

91-
seq_printf(m, "%s\n", rea_def->reactor->name);
91+
seq_printf(m, "%s\n", reactor->name);
9292
return 0;
9393
}
9494

@@ -139,12 +139,12 @@ static const struct file_operations available_reactors_ops = {
139139
static int monitor_reactor_show(struct seq_file *m, void *p)
140140
{
141141
struct rv_monitor *mon = m->private;
142-
struct rv_reactor_def *rdef = p;
142+
struct rv_reactor *reactor = p;
143143

144-
if (mon->rdef == rdef)
145-
seq_printf(m, "[%s]\n", rdef->reactor->name);
144+
if (mon->reactor == reactor)
145+
seq_printf(m, "[%s]\n", reactor->name);
146146
else
147-
seq_printf(m, "%s\n", rdef->reactor->name);
147+
seq_printf(m, "%s\n", reactor->name);
148148
return 0;
149149
}
150150

@@ -159,50 +159,50 @@ static const struct seq_operations monitor_reactors_seq_ops = {
159159
};
160160

161161
static void monitor_swap_reactors_single(struct rv_monitor *mon,
162-
struct rv_reactor_def *rdef,
162+
struct rv_reactor *reactor,
163163
bool reacting, bool nested)
164164
{
165165
bool monitor_enabled;
166166

167167
/* nothing to do */
168-
if (mon->rdef == rdef)
168+
if (mon->reactor == reactor)
169169
return;
170170

171171
monitor_enabled = mon->enabled;
172172
if (monitor_enabled)
173173
rv_disable_monitor(mon);
174174

175175
/* swap reactor's usage */
176-
mon->rdef->counter--;
177-
rdef->counter++;
176+
mon->reactor->counter--;
177+
reactor->counter++;
178178

179-
mon->rdef = rdef;
179+
mon->reactor = reactor;
180180
mon->reacting = reacting;
181-
mon->react = rdef->reactor->react;
181+
mon->react = reactor->react;
182182

183183
/* enable only once if iterating through a container */
184184
if (monitor_enabled && !nested)
185185
rv_enable_monitor(mon);
186186
}
187187

188188
static void monitor_swap_reactors(struct rv_monitor *mon,
189-
struct rv_reactor_def *rdef, bool reacting)
189+
struct rv_reactor *reactor, bool reacting)
190190
{
191191
struct rv_monitor *p = mon;
192192

193193
if (rv_is_container_monitor(mon))
194194
list_for_each_entry_continue(p, &rv_monitors_list, list) {
195195
if (p->parent != mon)
196196
break;
197-
monitor_swap_reactors_single(p, rdef, reacting, true);
197+
monitor_swap_reactors_single(p, reactor, reacting, true);
198198
}
199199
/*
200200
* This call enables and disables the monitor if they were active.
201201
* In case of a container, we already disabled all and will enable all.
202202
* All nested monitors are enabled also if they were off, we may refine
203203
* this logic in the future.
204204
*/
205-
monitor_swap_reactors_single(mon, rdef, reacting, false);
205+
monitor_swap_reactors_single(mon, reactor, reacting, false);
206206
}
207207

208208
static ssize_t
@@ -211,7 +211,7 @@ monitor_reactors_write(struct file *file, const char __user *user_buf,
211211
{
212212
char buff[MAX_RV_REACTOR_NAME_SIZE + 2];
213213
struct rv_monitor *mon;
214-
struct rv_reactor_def *rdef;
214+
struct rv_reactor *reactor;
215215
struct seq_file *seq_f;
216216
int retval = -EINVAL;
217217
bool enable;
@@ -243,16 +243,16 @@ monitor_reactors_write(struct file *file, const char __user *user_buf,
243243

244244
retval = -EINVAL;
245245

246-
list_for_each_entry(rdef, &rv_reactors_list, list) {
247-
if (strcmp(ptr, rdef->reactor->name) != 0)
246+
list_for_each_entry(reactor, &rv_reactors_list, list) {
247+
if (strcmp(ptr, reactor->name) != 0)
248248
continue;
249249

250-
if (rdef == get_reactor_rdef_by_name("nop"))
250+
if (strcmp(reactor->name, "nop"))
251251
enable = false;
252252
else
253253
enable = true;
254254

255-
monitor_swap_reactors(mon, rdef, enable);
255+
monitor_swap_reactors(mon, reactor, enable);
256256

257257
retval = count;
258258
break;
@@ -299,23 +299,16 @@ static const struct file_operations monitor_reactors_ops = {
299299

300300
static int __rv_register_reactor(struct rv_reactor *reactor)
301301
{
302-
struct rv_reactor_def *r;
302+
struct rv_reactor *r;
303303

304304
list_for_each_entry(r, &rv_reactors_list, list) {
305-
if (strcmp(reactor->name, r->reactor->name) == 0) {
305+
if (strcmp(reactor->name, r->name) == 0) {
306306
pr_info("Reactor %s is already registered\n", reactor->name);
307307
return -EINVAL;
308308
}
309309
}
310310

311-
r = kzalloc(sizeof(struct rv_reactor_def), GFP_KERNEL);
312-
if (!r)
313-
return -ENOMEM;
314-
315-
r->reactor = reactor;
316-
r->counter = 0;
317-
318-
list_add_tail(&r->list, &rv_reactors_list);
311+
list_add_tail(&reactor->list, &rv_reactors_list);
319312

320313
return 0;
321314
}
@@ -350,26 +343,19 @@ int rv_register_reactor(struct rv_reactor *reactor)
350343
*/
351344
int rv_unregister_reactor(struct rv_reactor *reactor)
352345
{
353-
struct rv_reactor_def *ptr, *next;
354346
int ret = 0;
355347

356348
mutex_lock(&rv_interface_lock);
357349

358-
list_for_each_entry_safe(ptr, next, &rv_reactors_list, list) {
359-
if (strcmp(reactor->name, ptr->reactor->name) == 0) {
360-
361-
if (!ptr->counter) {
362-
list_del(&ptr->list);
363-
} else {
364-
printk(KERN_WARNING
365-
"rv: the rv_reactor %s is in use by %d monitor(s)\n",
366-
ptr->reactor->name, ptr->counter);
367-
printk(KERN_WARNING "rv: the rv_reactor %s cannot be removed\n",
368-
ptr->reactor->name);
369-
ret = -EBUSY;
370-
break;
371-
}
372-
}
350+
if (!reactor->counter) {
351+
list_del(&reactor->list);
352+
} else {
353+
printk(KERN_WARNING
354+
"rv: the rv_reactor %s is in use by %d monitor(s)\n",
355+
reactor->name, reactor->counter);
356+
printk(KERN_WARNING "rv: the rv_reactor %s cannot be removed\n",
357+
reactor->name);
358+
ret = -EBUSY;
373359
}
374360

375361
mutex_unlock(&rv_interface_lock);
@@ -469,8 +455,8 @@ int reactor_populate_monitor(struct rv_monitor *mon)
469455
/*
470456
* Configure as the rv_nop reactor.
471457
*/
472-
mon->rdef = get_reactor_rdef_by_name("nop");
473-
mon->rdef->counter++;
458+
mon->reactor = get_reactor_rdef_by_name("nop");
459+
mon->reactor->counter++;
474460
mon->reacting = false;
475461

476462
return 0;
@@ -483,8 +469,8 @@ int reactor_populate_monitor(struct rv_monitor *mon)
483469
void reactor_cleanup_monitor(struct rv_monitor *mon)
484470
{
485471
lockdep_assert_held(&rv_interface_lock);
486-
mon->rdef->counter--;
487-
WARN_ON_ONCE(mon->rdef->counter < 0);
472+
mon->reactor->counter--;
473+
WARN_ON_ONCE(mon->reactor->counter < 0);
488474
}
489475

490476
/*

0 commit comments

Comments
 (0)