device_cgroup: Refactor devcgroup_seq_show to use seq_put* helpers

Replace set_access(), set_majmin(), and type_to_char() with new helpers
seq_putaccess(), seq_puttype(), and seq_putversion() that write directly
to 'seq_file'.

Simplify devcgroup_seq_show() by hard-coding "a *:* rwm", and use the
new seq_put* helper functions to list the exceptions otherwise.

This allows us to remove the intermediate string buffers while
maintaining the same functionality, including wildcard handling.

Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
Acked-by: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Paul Moore <paul@paul-moore.com>
This commit is contained in:
Thorsten Blum 2025-10-31 22:39:14 +01:00 committed by Paul Moore
parent dfa024bc3f
commit 0e6ebf8778
1 changed files with 25 additions and 31 deletions

View File

@ -244,45 +244,40 @@ static void devcgroup_css_free(struct cgroup_subsys_state *css)
#define DEVCG_DENY 2 #define DEVCG_DENY 2
#define DEVCG_LIST 3 #define DEVCG_LIST 3
#define MAJMINLEN 13 static void seq_putaccess(struct seq_file *m, short access)
#define ACCLEN 4
static void set_access(char *acc, short access)
{ {
int idx = 0;
memset(acc, 0, ACCLEN);
if (access & DEVCG_ACC_READ) if (access & DEVCG_ACC_READ)
acc[idx++] = 'r'; seq_putc(m, 'r');
if (access & DEVCG_ACC_WRITE) if (access & DEVCG_ACC_WRITE)
acc[idx++] = 'w'; seq_putc(m, 'w');
if (access & DEVCG_ACC_MKNOD) if (access & DEVCG_ACC_MKNOD)
acc[idx++] = 'm'; seq_putc(m, 'm');
} }
static char type_to_char(short type) static void seq_puttype(struct seq_file *m, short type)
{ {
if (type == DEVCG_DEV_ALL) if (type == DEVCG_DEV_ALL)
return 'a'; seq_putc(m, 'a');
if (type == DEVCG_DEV_CHAR) else if (type == DEVCG_DEV_CHAR)
return 'c'; seq_putc(m, 'c');
if (type == DEVCG_DEV_BLOCK) else if (type == DEVCG_DEV_BLOCK)
return 'b'; seq_putc(m, 'b');
return 'X'; else
seq_putc(m, 'X');
} }
static void set_majmin(char *str, unsigned m) static void seq_putversion(struct seq_file *m, unsigned int version)
{ {
if (m == ~0) if (version == ~0)
strcpy(str, "*"); seq_putc(m, '*');
else else
sprintf(str, "%u", m); seq_printf(m, "%u", version);
} }
static int devcgroup_seq_show(struct seq_file *m, void *v) static int devcgroup_seq_show(struct seq_file *m, void *v)
{ {
struct dev_cgroup *devcgroup = css_to_devcgroup(seq_css(m)); struct dev_cgroup *devcgroup = css_to_devcgroup(seq_css(m));
struct dev_exception_item *ex; struct dev_exception_item *ex;
char maj[MAJMINLEN], min[MAJMINLEN], acc[ACCLEN];
rcu_read_lock(); rcu_read_lock();
/* /*
@ -292,18 +287,17 @@ static int devcgroup_seq_show(struct seq_file *m, void *v)
* This way, the file remains as a "whitelist of devices" * This way, the file remains as a "whitelist of devices"
*/ */
if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) { if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) {
set_access(acc, DEVCG_ACC_MASK); seq_puts(m, "a *:* rwm\n");
set_majmin(maj, ~0);
set_majmin(min, ~0);
seq_printf(m, "%c %s:%s %s\n", type_to_char(DEVCG_DEV_ALL),
maj, min, acc);
} else { } else {
list_for_each_entry_rcu(ex, &devcgroup->exceptions, list) { list_for_each_entry_rcu(ex, &devcgroup->exceptions, list) {
set_access(acc, ex->access); seq_puttype(m, ex->type);
set_majmin(maj, ex->major); seq_putc(m, ' ');
set_majmin(min, ex->minor); seq_putversion(m, ex->major);
seq_printf(m, "%c %s:%s %s\n", type_to_char(ex->type), seq_putc(m, ':');
maj, min, acc); seq_putversion(m, ex->minor);
seq_putc(m, ' ');
seq_putaccess(m, ex->access);
seq_putc(m, '\n');
} }
} }
rcu_read_unlock(); rcu_read_unlock();