1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */
2*4882a593Smuzhiyun /* Internal procfs definitions
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
5*4882a593Smuzhiyun * Written by David Howells (dhowells@redhat.com)
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <linux/proc_fs.h>
9*4882a593Smuzhiyun #include <linux/proc_ns.h>
10*4882a593Smuzhiyun #include <linux/refcount.h>
11*4882a593Smuzhiyun #include <linux/spinlock.h>
12*4882a593Smuzhiyun #include <linux/atomic.h>
13*4882a593Smuzhiyun #include <linux/binfmts.h>
14*4882a593Smuzhiyun #include <linux/sched/coredump.h>
15*4882a593Smuzhiyun #include <linux/sched/task.h>
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun struct ctl_table_header;
18*4882a593Smuzhiyun struct mempolicy;
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun /*
21*4882a593Smuzhiyun * This is not completely implemented yet. The idea is to
22*4882a593Smuzhiyun * create an in-memory tree (like the actual /proc filesystem
23*4882a593Smuzhiyun * tree) of these proc_dir_entries, so that we can dynamically
24*4882a593Smuzhiyun * add new files to /proc.
25*4882a593Smuzhiyun *
26*4882a593Smuzhiyun * parent/subdir are used for the directory structure (every /proc file has a
27*4882a593Smuzhiyun * parent, but "subdir" is empty for all non-directory entries).
28*4882a593Smuzhiyun * subdir_node is used to build the rb tree "subdir" of the parent.
29*4882a593Smuzhiyun */
30*4882a593Smuzhiyun struct proc_dir_entry {
31*4882a593Smuzhiyun /*
32*4882a593Smuzhiyun * number of callers into module in progress;
33*4882a593Smuzhiyun * negative -> it's going away RSN
34*4882a593Smuzhiyun */
35*4882a593Smuzhiyun atomic_t in_use;
36*4882a593Smuzhiyun refcount_t refcnt;
37*4882a593Smuzhiyun struct list_head pde_openers; /* who did ->open, but not ->release */
38*4882a593Smuzhiyun /* protects ->pde_openers and all struct pde_opener instances */
39*4882a593Smuzhiyun spinlock_t pde_unload_lock;
40*4882a593Smuzhiyun struct completion *pde_unload_completion;
41*4882a593Smuzhiyun const struct inode_operations *proc_iops;
42*4882a593Smuzhiyun union {
43*4882a593Smuzhiyun const struct proc_ops *proc_ops;
44*4882a593Smuzhiyun const struct file_operations *proc_dir_ops;
45*4882a593Smuzhiyun };
46*4882a593Smuzhiyun const struct dentry_operations *proc_dops;
47*4882a593Smuzhiyun union {
48*4882a593Smuzhiyun const struct seq_operations *seq_ops;
49*4882a593Smuzhiyun int (*single_show)(struct seq_file *, void *);
50*4882a593Smuzhiyun };
51*4882a593Smuzhiyun proc_write_t write;
52*4882a593Smuzhiyun void *data;
53*4882a593Smuzhiyun unsigned int state_size;
54*4882a593Smuzhiyun unsigned int low_ino;
55*4882a593Smuzhiyun nlink_t nlink;
56*4882a593Smuzhiyun kuid_t uid;
57*4882a593Smuzhiyun kgid_t gid;
58*4882a593Smuzhiyun loff_t size;
59*4882a593Smuzhiyun struct proc_dir_entry *parent;
60*4882a593Smuzhiyun struct rb_root subdir;
61*4882a593Smuzhiyun struct rb_node subdir_node;
62*4882a593Smuzhiyun char *name;
63*4882a593Smuzhiyun umode_t mode;
64*4882a593Smuzhiyun u8 flags;
65*4882a593Smuzhiyun u8 namelen;
66*4882a593Smuzhiyun char inline_name[];
67*4882a593Smuzhiyun } __randomize_layout;
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun #define SIZEOF_PDE ( \
70*4882a593Smuzhiyun sizeof(struct proc_dir_entry) < 128 ? 128 : \
71*4882a593Smuzhiyun sizeof(struct proc_dir_entry) < 192 ? 192 : \
72*4882a593Smuzhiyun sizeof(struct proc_dir_entry) < 256 ? 256 : \
73*4882a593Smuzhiyun sizeof(struct proc_dir_entry) < 512 ? 512 : \
74*4882a593Smuzhiyun 0)
75*4882a593Smuzhiyun #define SIZEOF_PDE_INLINE_NAME (SIZEOF_PDE - sizeof(struct proc_dir_entry))
76*4882a593Smuzhiyun
pde_is_permanent(const struct proc_dir_entry * pde)77*4882a593Smuzhiyun static inline bool pde_is_permanent(const struct proc_dir_entry *pde)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun return pde->flags & PROC_ENTRY_PERMANENT;
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun extern struct kmem_cache *proc_dir_entry_cache;
83*4882a593Smuzhiyun void pde_free(struct proc_dir_entry *pde);
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun union proc_op {
86*4882a593Smuzhiyun int (*proc_get_link)(struct dentry *, struct path *);
87*4882a593Smuzhiyun int (*proc_show)(struct seq_file *m,
88*4882a593Smuzhiyun struct pid_namespace *ns, struct pid *pid,
89*4882a593Smuzhiyun struct task_struct *task);
90*4882a593Smuzhiyun const char *lsm;
91*4882a593Smuzhiyun };
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun struct proc_inode {
94*4882a593Smuzhiyun struct pid *pid;
95*4882a593Smuzhiyun unsigned int fd;
96*4882a593Smuzhiyun union proc_op op;
97*4882a593Smuzhiyun struct proc_dir_entry *pde;
98*4882a593Smuzhiyun struct ctl_table_header *sysctl;
99*4882a593Smuzhiyun struct ctl_table *sysctl_entry;
100*4882a593Smuzhiyun struct hlist_node sibling_inodes;
101*4882a593Smuzhiyun const struct proc_ns_operations *ns_ops;
102*4882a593Smuzhiyun struct inode vfs_inode;
103*4882a593Smuzhiyun } __randomize_layout;
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun /*
106*4882a593Smuzhiyun * General functions
107*4882a593Smuzhiyun */
PROC_I(const struct inode * inode)108*4882a593Smuzhiyun static inline struct proc_inode *PROC_I(const struct inode *inode)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun return container_of(inode, struct proc_inode, vfs_inode);
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun
PDE(const struct inode * inode)113*4882a593Smuzhiyun static inline struct proc_dir_entry *PDE(const struct inode *inode)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun return PROC_I(inode)->pde;
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun
__PDE_DATA(const struct inode * inode)118*4882a593Smuzhiyun static inline void *__PDE_DATA(const struct inode *inode)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun return PDE(inode)->data;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun
proc_pid(const struct inode * inode)123*4882a593Smuzhiyun static inline struct pid *proc_pid(const struct inode *inode)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun return PROC_I(inode)->pid;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun
get_proc_task(const struct inode * inode)128*4882a593Smuzhiyun static inline struct task_struct *get_proc_task(const struct inode *inode)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun return get_pid_task(proc_pid(inode), PIDTYPE_PID);
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun void task_dump_owner(struct task_struct *task, umode_t mode,
134*4882a593Smuzhiyun kuid_t *ruid, kgid_t *rgid);
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun unsigned name_to_int(const struct qstr *qstr);
137*4882a593Smuzhiyun /*
138*4882a593Smuzhiyun * Offset of the first process in the /proc root directory..
139*4882a593Smuzhiyun */
140*4882a593Smuzhiyun #define FIRST_PROCESS_ENTRY 256
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun /* Worst case buffer size needed for holding an integer. */
143*4882a593Smuzhiyun #define PROC_NUMBUF 13
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun /*
146*4882a593Smuzhiyun * array.c
147*4882a593Smuzhiyun */
148*4882a593Smuzhiyun extern const struct file_operations proc_tid_children_operations;
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun extern void proc_task_name(struct seq_file *m, struct task_struct *p,
151*4882a593Smuzhiyun bool escape);
152*4882a593Smuzhiyun extern int proc_tid_stat(struct seq_file *, struct pid_namespace *,
153*4882a593Smuzhiyun struct pid *, struct task_struct *);
154*4882a593Smuzhiyun extern int proc_tgid_stat(struct seq_file *, struct pid_namespace *,
155*4882a593Smuzhiyun struct pid *, struct task_struct *);
156*4882a593Smuzhiyun extern int proc_pid_status(struct seq_file *, struct pid_namespace *,
157*4882a593Smuzhiyun struct pid *, struct task_struct *);
158*4882a593Smuzhiyun extern int proc_pid_statm(struct seq_file *, struct pid_namespace *,
159*4882a593Smuzhiyun struct pid *, struct task_struct *);
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun /*
162*4882a593Smuzhiyun * base.c
163*4882a593Smuzhiyun */
164*4882a593Smuzhiyun extern const struct dentry_operations pid_dentry_operations;
165*4882a593Smuzhiyun extern int pid_getattr(const struct path *, struct kstat *, u32, unsigned int);
166*4882a593Smuzhiyun extern int proc_setattr(struct dentry *, struct iattr *);
167*4882a593Smuzhiyun extern void proc_pid_evict_inode(struct proc_inode *);
168*4882a593Smuzhiyun extern struct inode *proc_pid_make_inode(struct super_block *, struct task_struct *, umode_t);
169*4882a593Smuzhiyun extern void pid_update_inode(struct task_struct *, struct inode *);
170*4882a593Smuzhiyun extern int pid_delete_dentry(const struct dentry *);
171*4882a593Smuzhiyun extern int proc_pid_readdir(struct file *, struct dir_context *);
172*4882a593Smuzhiyun struct dentry *proc_pid_lookup(struct dentry *, unsigned int);
173*4882a593Smuzhiyun extern loff_t mem_lseek(struct file *, loff_t, int);
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun /* Lookups */
176*4882a593Smuzhiyun typedef struct dentry *instantiate_t(struct dentry *,
177*4882a593Smuzhiyun struct task_struct *, const void *);
178*4882a593Smuzhiyun bool proc_fill_cache(struct file *, struct dir_context *, const char *, unsigned int,
179*4882a593Smuzhiyun instantiate_t, struct task_struct *, const void *);
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun /*
182*4882a593Smuzhiyun * generic.c
183*4882a593Smuzhiyun */
184*4882a593Smuzhiyun struct proc_dir_entry *proc_create_reg(const char *name, umode_t mode,
185*4882a593Smuzhiyun struct proc_dir_entry **parent, void *data);
186*4882a593Smuzhiyun struct proc_dir_entry *proc_register(struct proc_dir_entry *dir,
187*4882a593Smuzhiyun struct proc_dir_entry *dp);
188*4882a593Smuzhiyun extern struct dentry *proc_lookup(struct inode *, struct dentry *, unsigned int);
189*4882a593Smuzhiyun struct dentry *proc_lookup_de(struct inode *, struct dentry *, struct proc_dir_entry *);
190*4882a593Smuzhiyun extern int proc_readdir(struct file *, struct dir_context *);
191*4882a593Smuzhiyun int proc_readdir_de(struct file *, struct dir_context *, struct proc_dir_entry *);
192*4882a593Smuzhiyun
pde_get(struct proc_dir_entry * pde)193*4882a593Smuzhiyun static inline struct proc_dir_entry *pde_get(struct proc_dir_entry *pde)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun refcount_inc(&pde->refcnt);
196*4882a593Smuzhiyun return pde;
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun extern void pde_put(struct proc_dir_entry *);
199*4882a593Smuzhiyun
is_empty_pde(const struct proc_dir_entry * pde)200*4882a593Smuzhiyun static inline bool is_empty_pde(const struct proc_dir_entry *pde)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun return S_ISDIR(pde->mode) && !pde->proc_iops;
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun extern ssize_t proc_simple_write(struct file *, const char __user *, size_t, loff_t *);
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun /*
207*4882a593Smuzhiyun * inode.c
208*4882a593Smuzhiyun */
209*4882a593Smuzhiyun struct pde_opener {
210*4882a593Smuzhiyun struct list_head lh;
211*4882a593Smuzhiyun struct file *file;
212*4882a593Smuzhiyun bool closing;
213*4882a593Smuzhiyun struct completion *c;
214*4882a593Smuzhiyun } __randomize_layout;
215*4882a593Smuzhiyun extern const struct inode_operations proc_link_inode_operations;
216*4882a593Smuzhiyun extern const struct inode_operations proc_pid_link_inode_operations;
217*4882a593Smuzhiyun extern const struct super_operations proc_sops;
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun void proc_init_kmemcache(void);
220*4882a593Smuzhiyun void proc_invalidate_siblings_dcache(struct hlist_head *inodes, spinlock_t *lock);
221*4882a593Smuzhiyun void set_proc_pid_nlink(void);
222*4882a593Smuzhiyun extern struct inode *proc_get_inode(struct super_block *, struct proc_dir_entry *);
223*4882a593Smuzhiyun extern void proc_entry_rundown(struct proc_dir_entry *);
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun /*
226*4882a593Smuzhiyun * proc_namespaces.c
227*4882a593Smuzhiyun */
228*4882a593Smuzhiyun extern const struct inode_operations proc_ns_dir_inode_operations;
229*4882a593Smuzhiyun extern const struct file_operations proc_ns_dir_operations;
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun /*
232*4882a593Smuzhiyun * proc_net.c
233*4882a593Smuzhiyun */
234*4882a593Smuzhiyun extern const struct file_operations proc_net_operations;
235*4882a593Smuzhiyun extern const struct inode_operations proc_net_inode_operations;
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun #ifdef CONFIG_NET
238*4882a593Smuzhiyun extern int proc_net_init(void);
239*4882a593Smuzhiyun #else
proc_net_init(void)240*4882a593Smuzhiyun static inline int proc_net_init(void) { return 0; }
241*4882a593Smuzhiyun #endif
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun /*
244*4882a593Smuzhiyun * proc_self.c
245*4882a593Smuzhiyun */
246*4882a593Smuzhiyun extern int proc_setup_self(struct super_block *);
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun /*
249*4882a593Smuzhiyun * proc_thread_self.c
250*4882a593Smuzhiyun */
251*4882a593Smuzhiyun extern int proc_setup_thread_self(struct super_block *);
252*4882a593Smuzhiyun extern void proc_thread_self_init(void);
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun /*
255*4882a593Smuzhiyun * proc_sysctl.c
256*4882a593Smuzhiyun */
257*4882a593Smuzhiyun #ifdef CONFIG_PROC_SYSCTL
258*4882a593Smuzhiyun extern int proc_sys_init(void);
259*4882a593Smuzhiyun extern void proc_sys_evict_inode(struct inode *inode,
260*4882a593Smuzhiyun struct ctl_table_header *head);
261*4882a593Smuzhiyun #else
proc_sys_init(void)262*4882a593Smuzhiyun static inline void proc_sys_init(void) { }
proc_sys_evict_inode(struct inode * inode,struct ctl_table_header * head)263*4882a593Smuzhiyun static inline void proc_sys_evict_inode(struct inode *inode,
264*4882a593Smuzhiyun struct ctl_table_header *head) { }
265*4882a593Smuzhiyun #endif
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun /*
268*4882a593Smuzhiyun * proc_tty.c
269*4882a593Smuzhiyun */
270*4882a593Smuzhiyun #ifdef CONFIG_TTY
271*4882a593Smuzhiyun extern void proc_tty_init(void);
272*4882a593Smuzhiyun #else
proc_tty_init(void)273*4882a593Smuzhiyun static inline void proc_tty_init(void) {}
274*4882a593Smuzhiyun #endif
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun /*
277*4882a593Smuzhiyun * root.c
278*4882a593Smuzhiyun */
279*4882a593Smuzhiyun extern struct proc_dir_entry proc_root;
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun extern void proc_self_init(void);
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun /*
284*4882a593Smuzhiyun * task_[no]mmu.c
285*4882a593Smuzhiyun */
286*4882a593Smuzhiyun struct mem_size_stats;
287*4882a593Smuzhiyun struct proc_maps_private {
288*4882a593Smuzhiyun struct inode *inode;
289*4882a593Smuzhiyun struct task_struct *task;
290*4882a593Smuzhiyun struct mm_struct *mm;
291*4882a593Smuzhiyun #ifdef CONFIG_MMU
292*4882a593Smuzhiyun struct vm_area_struct *tail_vma;
293*4882a593Smuzhiyun #endif
294*4882a593Smuzhiyun #ifdef CONFIG_NUMA
295*4882a593Smuzhiyun struct mempolicy *task_mempolicy;
296*4882a593Smuzhiyun #endif
297*4882a593Smuzhiyun } __randomize_layout;
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode);
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun extern const struct file_operations proc_pid_maps_operations;
302*4882a593Smuzhiyun extern const struct file_operations proc_pid_numa_maps_operations;
303*4882a593Smuzhiyun extern const struct file_operations proc_pid_smaps_operations;
304*4882a593Smuzhiyun extern const struct file_operations proc_pid_smaps_rollup_operations;
305*4882a593Smuzhiyun extern const struct file_operations proc_clear_refs_operations;
306*4882a593Smuzhiyun extern const struct file_operations proc_pagemap_operations;
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun extern unsigned long task_vsize(struct mm_struct *);
309*4882a593Smuzhiyun extern unsigned long task_statm(struct mm_struct *,
310*4882a593Smuzhiyun unsigned long *, unsigned long *,
311*4882a593Smuzhiyun unsigned long *, unsigned long *);
312*4882a593Smuzhiyun extern void task_mem(struct seq_file *, struct mm_struct *);
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun extern const struct dentry_operations proc_net_dentry_ops;
pde_force_lookup(struct proc_dir_entry * pde)315*4882a593Smuzhiyun static inline void pde_force_lookup(struct proc_dir_entry *pde)
316*4882a593Smuzhiyun {
317*4882a593Smuzhiyun /* /proc/net/ entries can be changed under us by setns(CLONE_NEWNET) */
318*4882a593Smuzhiyun pde->proc_dops = &proc_net_dentry_ops;
319*4882a593Smuzhiyun }
320