Skip to content

Commit 09570f9

Browse files
dhowellstorvalds
authored andcommitted
proc: make struct proc_dir_entry::name a terminal array rather than a pointer
Since __proc_create() appends the name it is given to the end of the PDE structure that it allocates, there isn't a need to store a name pointer. Instead we can just replace the name pointer with a terminal char array of _unspecified_ length. The compiler will simply append the string to statically defined variables of PDE type overlapping any hole at the end of the structure and, unlike specifying an explicitly _zero_ length array, won't give a warning if you try to statically initialise it with a string of more than zero length. Also, whilst we're at it: (1) Move namelen to end just prior to name and reduce it to a single byte (name shouldn't be longer than NAME_MAX). (2) Move pde_unload_lock two places further on so that if it's four bytes in size on a 64-bit machine, it won't cause an unused hole in the PDE struct. Signed-off-by: David Howells <dhowells@redhat.com> Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 5fd00b0 commit 09570f9

4 files changed

Lines changed: 7 additions & 8 deletions

File tree

‎fs/proc/generic.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -620,8 +620,7 @@ static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
620620
if (!ent) goto out;
621621

622622
memset(ent, 0, sizeof(struct proc_dir_entry));
623-
memcpy(((char *) ent) + sizeof(struct proc_dir_entry), fn, len + 1);
624-
ent->name = ((char *) ent) + sizeof(*ent);
623+
memcpy(ent->name, fn, len + 1);
625624
ent->namelen = len;
626625
ent->mode = mode;
627626
ent->nlink = nlink;

‎fs/proc/proc_net.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,15 +197,15 @@ static __net_init int proc_net_ns_init(struct net *net)
197197
int err;
198198

199199
err = -ENOMEM;
200-
netd = kzalloc(sizeof(*netd), GFP_KERNEL);
200+
netd = kzalloc(sizeof(*netd) + 4, GFP_KERNEL);
201201
if (!netd)
202202
goto out;
203203

204204
netd->data = net;
205205
netd->nlink = 2;
206-
netd->name = "net";
207206
netd->namelen = 3;
208207
netd->parent = &proc_root;
208+
memcpy(netd->name, "net", 4);
209209

210210
err = -EEXIST;
211211
net_statd = proc_net_mkdir(net, "stat", netd);

‎fs/proc/root.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,13 @@ static const struct inode_operations proc_root_inode_operations = {
186186
struct proc_dir_entry proc_root = {
187187
.low_ino = PROC_ROOT_INO,
188188
.namelen = 5,
189-
.name = "/proc",
190189
.mode = S_IFDIR | S_IRUGO | S_IXUGO,
191190
.nlink = 2,
192191
.count = ATOMIC_INIT(1),
193192
.proc_iops = &proc_root_inode_operations,
194193
.proc_fops = &proc_root_operations,
195194
.parent = &proc_root,
195+
.name = "/proc",
196196
};
197197

198198
int pid_ns_prepare_proc(struct pid_namespace *ns)

‎include/linux/proc_fs.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ typedef int (write_proc_t)(struct file *file, const char __user *buffer,
5050

5151
struct proc_dir_entry {
5252
unsigned int low_ino;
53-
unsigned int namelen;
54-
const char *name;
5553
mode_t mode;
5654
nlink_t nlink;
5755
uid_t uid;
@@ -73,9 +71,11 @@ struct proc_dir_entry {
7371
write_proc_t *write_proc;
7472
atomic_t count; /* use count */
7573
int pde_users; /* number of callers into module in progress */
76-
spinlock_t pde_unload_lock; /* proc_fops checks and pde_users bumps */
7774
struct completion *pde_unload_completion;
7875
struct list_head pde_openers; /* who did ->open, but not ->release */
76+
spinlock_t pde_unload_lock; /* proc_fops checks and pde_users bumps */
77+
u8 namelen;
78+
char name[];
7979
};
8080

8181
enum kcore_type {

0 commit comments

Comments
 (0)