Skip to content

Commit cb55f27

Browse files
imtangmengmcgrof
authored andcommitted
fs/proc: Introduce list_for_each_table_entry for proc sysctl
Use the list_for_each_table_entry macro to optimize the scenario of traverse ctl_table. This make the code neater and easier to understand. Suggested-by: Davidlohr Bueso<[email protected]> Signed-off-by: Meng Tang <[email protected]> [updated the sysctl_check_table() hunk due to some changes upstream] Signed-off-by: Luis Chamberlain <[email protected]>
1 parent 3c6a4cb commit cb55f27

File tree

1 file changed

+50
-38
lines changed

1 file changed

+50
-38
lines changed

fs/proc/proc_sysctl.c

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
#include <linux/kmemleak.h>
2020
#include "internal.h"
2121

22+
#define list_for_each_table_entry(entry, table) \
23+
for ((entry) = (table); (entry)->procname; (entry)++)
24+
2225
static const struct dentry_operations proc_sys_dentry_operations;
2326
static const struct file_operations proc_sys_file_operations;
2427
static const struct inode_operations proc_sys_inode_operations;
@@ -215,15 +218,19 @@ static void init_header(struct ctl_table_header *head,
215218
INIT_HLIST_HEAD(&head->inodes);
216219
if (node) {
217220
struct ctl_table *entry;
218-
for (entry = table; entry->procname; entry++, node++)
221+
222+
list_for_each_table_entry(entry, table) {
219223
node->header = head;
224+
node++;
225+
}
220226
}
221227
}
222228

223229
static void erase_header(struct ctl_table_header *head)
224230
{
225231
struct ctl_table *entry;
226-
for (entry = head->ctl_table; entry->procname; entry++)
232+
233+
list_for_each_table_entry(entry, head->ctl_table)
227234
erase_entry(head, entry);
228235
}
229236

@@ -248,7 +255,7 @@ static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
248255
err = insert_links(header);
249256
if (err)
250257
goto fail_links;
251-
for (entry = header->ctl_table; entry->procname; entry++) {
258+
list_for_each_table_entry(entry, header->ctl_table) {
252259
err = insert_entry(header, entry);
253260
if (err)
254261
goto fail;
@@ -1130,35 +1137,36 @@ static int sysctl_check_table_array(const char *path, struct ctl_table *table)
11301137

11311138
static int sysctl_check_table(const char *path, struct ctl_table *table)
11321139
{
1140+
struct ctl_table *entry;
11331141
int err = 0;
1134-
for (; table->procname; table++) {
1135-
if (table->child)
1136-
err |= sysctl_err(path, table, "Not a file");
1137-
1138-
if ((table->proc_handler == proc_dostring) ||
1139-
(table->proc_handler == proc_dointvec) ||
1140-
(table->proc_handler == proc_douintvec) ||
1141-
(table->proc_handler == proc_douintvec_minmax) ||
1142-
(table->proc_handler == proc_dointvec_minmax) ||
1143-
(table->proc_handler == proc_dou8vec_minmax) ||
1144-
(table->proc_handler == proc_dointvec_jiffies) ||
1145-
(table->proc_handler == proc_dointvec_userhz_jiffies) ||
1146-
(table->proc_handler == proc_dointvec_ms_jiffies) ||
1147-
(table->proc_handler == proc_doulongvec_minmax) ||
1148-
(table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
1149-
if (!table->data)
1150-
err |= sysctl_err(path, table, "No data");
1151-
if (!table->maxlen)
1152-
err |= sysctl_err(path, table, "No maxlen");
1142+
list_for_each_table_entry(entry, table) {
1143+
if (entry->child)
1144+
err |= sysctl_err(path, entry, "Not a file");
1145+
1146+
if ((entry->proc_handler == proc_dostring) ||
1147+
(entry->proc_handler == proc_dointvec) ||
1148+
(entry->proc_handler == proc_douintvec) ||
1149+
(entry->proc_handler == proc_douintvec_minmax) ||
1150+
(entry->proc_handler == proc_dointvec_minmax) ||
1151+
(entry->proc_handler == proc_dou8vec_minmax) ||
1152+
(entry->proc_handler == proc_dointvec_jiffies) ||
1153+
(entry->proc_handler == proc_dointvec_userhz_jiffies) ||
1154+
(entry->proc_handler == proc_dointvec_ms_jiffies) ||
1155+
(entry->proc_handler == proc_doulongvec_minmax) ||
1156+
(entry->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
1157+
if (!entry->data)
1158+
err |= sysctl_err(path, entry, "No data");
1159+
if (!entry->maxlen)
1160+
err |= sysctl_err(path, entry, "No maxlen");
11531161
else
1154-
err |= sysctl_check_table_array(path, table);
1162+
err |= sysctl_check_table_array(path, entry);
11551163
}
1156-
if (!table->proc_handler)
1157-
err |= sysctl_err(path, table, "No proc_handler");
1164+
if (!entry->proc_handler)
1165+
err |= sysctl_err(path, entry, "No proc_handler");
11581166

1159-
if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
1160-
err |= sysctl_err(path, table, "bogus .mode 0%o",
1161-
table->mode);
1167+
if ((entry->mode & (S_IRUGO|S_IWUGO)) != entry->mode)
1168+
err |= sysctl_err(path, entry, "bogus .mode 0%o",
1169+
entry->mode);
11621170
}
11631171
return err;
11641172
}
@@ -1174,7 +1182,7 @@ static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table
11741182

11751183
name_bytes = 0;
11761184
nr_entries = 0;
1177-
for (entry = table; entry->procname; entry++) {
1185+
list_for_each_table_entry(entry, table) {
11781186
nr_entries++;
11791187
name_bytes += strlen(entry->procname) + 1;
11801188
}
@@ -1191,14 +1199,16 @@ static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table
11911199
node = (struct ctl_node *)(links + 1);
11921200
link_table = (struct ctl_table *)(node + nr_entries);
11931201
link_name = (char *)&link_table[nr_entries + 1];
1202+
link = link_table;
11941203

1195-
for (link = link_table, entry = table; entry->procname; link++, entry++) {
1204+
list_for_each_table_entry(entry, table) {
11961205
int len = strlen(entry->procname) + 1;
11971206
memcpy(link_name, entry->procname, len);
11981207
link->procname = link_name;
11991208
link->mode = S_IFLNK|S_IRWXUGO;
12001209
link->data = link_root;
12011210
link_name += len;
1211+
link++;
12021212
}
12031213
init_header(links, dir->header.root, dir->header.set, node, link_table);
12041214
links->nreg = nr_entries;
@@ -1213,7 +1223,7 @@ static bool get_links(struct ctl_dir *dir,
12131223
struct ctl_table *entry, *link;
12141224

12151225
/* Are there links available for every entry in table? */
1216-
for (entry = table; entry->procname; entry++) {
1226+
list_for_each_table_entry(entry, table) {
12171227
const char *procname = entry->procname;
12181228
link = find_entry(&head, dir, procname, strlen(procname));
12191229
if (!link)
@@ -1226,7 +1236,7 @@ static bool get_links(struct ctl_dir *dir,
12261236
}
12271237

12281238
/* The checks passed. Increase the registration count on the links */
1229-
for (entry = table; entry->procname; entry++) {
1239+
list_for_each_table_entry(entry, table) {
12301240
const char *procname = entry->procname;
12311241
link = find_entry(&head, dir, procname, strlen(procname));
12321242
head->nreg++;
@@ -1329,7 +1339,7 @@ struct ctl_table_header *__register_sysctl_table(
13291339
struct ctl_node *node;
13301340
int nr_entries = 0;
13311341

1332-
for (entry = table; entry->procname; entry++)
1342+
list_for_each_table_entry(entry, table)
13331343
nr_entries++;
13341344

13351345
header = kzalloc(sizeof(struct ctl_table_header) +
@@ -1456,7 +1466,7 @@ static int count_subheaders(struct ctl_table *table)
14561466
if (!table || !table->procname)
14571467
return 1;
14581468

1459-
for (entry = table; entry->procname; entry++) {
1469+
list_for_each_table_entry(entry, table) {
14601470
if (entry->child)
14611471
nr_subheaders += count_subheaders(entry->child);
14621472
else
@@ -1475,7 +1485,7 @@ static int register_leaf_sysctl_tables(const char *path, char *pos,
14751485
int nr_dirs = 0;
14761486
int err = -ENOMEM;
14771487

1478-
for (entry = table; entry->procname; entry++) {
1488+
list_for_each_table_entry(entry, table) {
14791489
if (entry->child)
14801490
nr_dirs++;
14811491
else
@@ -1492,7 +1502,9 @@ static int register_leaf_sysctl_tables(const char *path, char *pos,
14921502
goto out;
14931503

14941504
ctl_table_arg = files;
1495-
for (new = files, entry = table; entry->procname; entry++) {
1505+
new = files;
1506+
1507+
list_for_each_table_entry(entry, table) {
14961508
if (entry->child)
14971509
continue;
14981510
*new = *entry;
@@ -1516,7 +1528,7 @@ static int register_leaf_sysctl_tables(const char *path, char *pos,
15161528
}
15171529

15181530
/* Recurse into the subdirectories. */
1519-
for (entry = table; entry->procname; entry++) {
1531+
list_for_each_table_entry(entry, table) {
15201532
char *child_pos;
15211533

15221534
if (!entry->child)
@@ -1670,7 +1682,7 @@ static void put_links(struct ctl_table_header *header)
16701682
if (IS_ERR(core_parent))
16711683
return;
16721684

1673-
for (entry = header->ctl_table; entry->procname; entry++) {
1685+
list_for_each_table_entry(entry, header->ctl_table) {
16741686
struct ctl_table_header *link_head;
16751687
struct ctl_table *link;
16761688
const char *name = entry->procname;

0 commit comments

Comments
 (0)