xref: /openbmc/linux/fs/proc/internal.h (revision 85eba5f1759f9eb89273225027254ced57bd18a2)
12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
259d8053fSDavid Howells /* Internal procfs definitions
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
51da177e4SLinus Torvalds  * Written by David Howells (dhowells@redhat.com)
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #include <linux/proc_fs.h>
959d8053fSDavid Howells #include <linux/proc_ns.h>
109cdd83e3SAlexey Dobriyan #include <linux/refcount.h>
1159d8053fSDavid Howells #include <linux/spinlock.h>
1259d8053fSDavid Howells #include <linux/atomic.h>
13e579d2c2SKees Cook #include <linux/binfmts.h>
14f7ccbae4SIngo Molnar #include <linux/sched/coredump.h>
15f719ff9bSIngo Molnar #include <linux/sched/task.h>
1659d8053fSDavid Howells 
171f87f0b5SEric W. Biederman struct ctl_table_header;
189e781440SKAMEZAWA Hiroyuki struct mempolicy;
191da177e4SLinus Torvalds 
2059d8053fSDavid Howells /*
2159d8053fSDavid Howells  * This is not completely implemented yet. The idea is to
2259d8053fSDavid Howells  * create an in-memory tree (like the actual /proc filesystem
2359d8053fSDavid Howells  * tree) of these proc_dir_entries, so that we can dynamically
2459d8053fSDavid Howells  * add new files to /proc.
2559d8053fSDavid Howells  *
26710585d4SNicolas Dichtel  * parent/subdir are used for the directory structure (every /proc file has a
27710585d4SNicolas Dichtel  * parent, but "subdir" is empty for all non-directory entries).
28710585d4SNicolas Dichtel  * subdir_node is used to build the rb tree "subdir" of the parent.
2959d8053fSDavid Howells  */
3059d8053fSDavid Howells struct proc_dir_entry {
31163cf548SAlexey Dobriyan 	/*
32163cf548SAlexey Dobriyan 	 * number of callers into module in progress;
33163cf548SAlexey Dobriyan 	 * negative -> it's going away RSN
34163cf548SAlexey Dobriyan 	 */
35163cf548SAlexey Dobriyan 	atomic_t in_use;
369cdd83e3SAlexey Dobriyan 	refcount_t refcnt;
37163cf548SAlexey Dobriyan 	struct list_head pde_openers;	/* who did ->open, but not ->release */
3853f63345SAlexey Dobriyan 	/* protects ->pde_openers and all struct pde_opener instances */
3953f63345SAlexey Dobriyan 	spinlock_t pde_unload_lock;
40163cf548SAlexey Dobriyan 	struct completion *pde_unload_completion;
41163cf548SAlexey Dobriyan 	const struct inode_operations *proc_iops;
42d56c0d45SAlexey Dobriyan 	union {
43d56c0d45SAlexey Dobriyan 		const struct proc_ops *proc_ops;
44d56c0d45SAlexey Dobriyan 		const struct file_operations *proc_dir_ops;
45d56c0d45SAlexey Dobriyan 	};
461fde6f21SAlexey Dobriyan 	const struct dentry_operations *proc_dops;
473f3942acSChristoph Hellwig 	union {
48fddda2b7SChristoph Hellwig 		const struct seq_operations *seq_ops;
493f3942acSChristoph Hellwig 		int (*single_show)(struct seq_file *, void *);
503f3942acSChristoph Hellwig 	};
51564def71SDavid Howells 	proc_write_t write;
52163cf548SAlexey Dobriyan 	void *data;
5344414d82SChristoph Hellwig 	unsigned int state_size;
5459d8053fSDavid Howells 	unsigned int low_ino;
5559d8053fSDavid Howells 	nlink_t nlink;
5659d8053fSDavid Howells 	kuid_t uid;
5759d8053fSDavid Howells 	kgid_t gid;
5859d8053fSDavid Howells 	loff_t size;
59710585d4SNicolas Dichtel 	struct proc_dir_entry *parent;
604f113437SAlexey Dobriyan 	struct rb_root subdir;
61710585d4SNicolas Dichtel 	struct rb_node subdir_node;
62b4884f23SAlexey Dobriyan 	char *name;
63163cf548SAlexey Dobriyan 	umode_t mode;
64d919b33dSAlexey Dobriyan 	u8 flags;
6559d8053fSDavid Howells 	u8 namelen;
6624074a35SDavid Howells 	char inline_name[];
673859a271SKees Cook } __randomize_layout;
68f2beb798SStephen Wilson 
692d6e4e82SAlexey Dobriyan #define SIZEOF_PDE	(				\
702d6e4e82SAlexey Dobriyan 	sizeof(struct proc_dir_entry) < 128 ? 128 :	\
712d6e4e82SAlexey Dobriyan 	sizeof(struct proc_dir_entry) < 192 ? 192 :	\
722d6e4e82SAlexey Dobriyan 	sizeof(struct proc_dir_entry) < 256 ? 256 :	\
732d6e4e82SAlexey Dobriyan 	sizeof(struct proc_dir_entry) < 512 ? 512 :	\
7424074a35SDavid Howells 	0)
752d6e4e82SAlexey Dobriyan #define SIZEOF_PDE_INLINE_NAME (SIZEOF_PDE - sizeof(struct proc_dir_entry))
7624074a35SDavid Howells 
pde_is_permanent(const struct proc_dir_entry * pde)77d919b33dSAlexey Dobriyan static inline bool pde_is_permanent(const struct proc_dir_entry *pde)
78d919b33dSAlexey Dobriyan {
79d919b33dSAlexey Dobriyan 	return pde->flags & PROC_ENTRY_PERMANENT;
80d919b33dSAlexey Dobriyan }
81d919b33dSAlexey Dobriyan 
pde_make_permanent(struct proc_dir_entry * pde)82ef1d6178SAlexey Dobriyan static inline void pde_make_permanent(struct proc_dir_entry *pde)
83ef1d6178SAlexey Dobriyan {
84ef1d6178SAlexey Dobriyan 	pde->flags |= PROC_ENTRY_PERMANENT;
85ef1d6178SAlexey Dobriyan }
86ef1d6178SAlexey Dobriyan 
87b4884f23SAlexey Dobriyan extern struct kmem_cache *proc_dir_entry_cache;
88b4884f23SAlexey Dobriyan void pde_free(struct proc_dir_entry *pde);
89b4884f23SAlexey Dobriyan 
9059d8053fSDavid Howells union proc_op {
9159d8053fSDavid Howells 	int (*proc_get_link)(struct dentry *, struct path *);
9259d8053fSDavid Howells 	int (*proc_show)(struct seq_file *m,
9359d8053fSDavid Howells 		struct pid_namespace *ns, struct pid *pid,
9459d8053fSDavid Howells 		struct task_struct *task);
956d9c939dSCasey Schaufler 	const char *lsm;
9659d8053fSDavid Howells };
971da177e4SLinus Torvalds 
9859d8053fSDavid Howells struct proc_inode {
9913b41b09SEric W. Biederman 	struct pid *pid;
100771187d6SAlexey Dobriyan 	unsigned int fd;
10159d8053fSDavid Howells 	union proc_op op;
10259d8053fSDavid Howells 	struct proc_dir_entry *pde;
10359d8053fSDavid Howells 	struct ctl_table_header *sysctl;
10459d8053fSDavid Howells 	struct ctl_table *sysctl_entry;
1050afa5ca8SEric W. Biederman 	struct hlist_node sibling_inodes;
1063d3d35b1SAl Viro 	const struct proc_ns_operations *ns_ops;
10759d8053fSDavid Howells 	struct inode vfs_inode;
1083859a271SKees Cook } __randomize_layout;
10999f89551SEric W. Biederman 
110c30480b9SDavid Howells /*
111c30480b9SDavid Howells  * General functions
112c30480b9SDavid Howells  */
PROC_I(const struct inode * inode)113c30480b9SDavid Howells static inline struct proc_inode *PROC_I(const struct inode *inode)
114c30480b9SDavid Howells {
115c30480b9SDavid Howells 	return container_of(inode, struct proc_inode, vfs_inode);
116c30480b9SDavid Howells }
117c30480b9SDavid Howells 
PDE(const struct inode * inode)118c30480b9SDavid Howells static inline struct proc_dir_entry *PDE(const struct inode *inode)
119c30480b9SDavid Howells {
120c30480b9SDavid Howells 	return PROC_I(inode)->pde;
121c30480b9SDavid Howells }
122c30480b9SDavid Howells 
proc_pid(const struct inode * inode)123891ae71dSAlexey Dobriyan static inline struct pid *proc_pid(const struct inode *inode)
1241da177e4SLinus Torvalds {
12599f89551SEric W. Biederman 	return PROC_I(inode)->pid;
12699f89551SEric W. Biederman }
12799f89551SEric W. Biederman 
get_proc_task(const struct inode * inode)128891ae71dSAlexey Dobriyan static inline struct task_struct *get_proc_task(const struct inode *inode)
12999f89551SEric W. Biederman {
13013b41b09SEric W. Biederman 	return get_pid_task(proc_pid(inode), PIDTYPE_PID);
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
133c6eb50d2SAl Viro void task_dump_owner(struct task_struct *task, umode_t mode,
13468eb94f1SEric W. Biederman 		     kuid_t *ruid, kgid_t *rgid);
135faf60af1SCyrill Gorcunov 
1363ee2a199SAlexey Dobriyan unsigned name_to_int(const struct qstr *qstr);
1371dd704b6SDavid Howells /*
13859d8053fSDavid Howells  * Offset of the first process in the /proc root directory..
13959d8053fSDavid Howells  */
14059d8053fSDavid Howells #define FIRST_PROCESS_ENTRY 256
141881adb85SAlexey Dobriyan 
14259d8053fSDavid Howells /* Worst case buffer size needed for holding an integer. */
14359d8053fSDavid Howells #define PROC_NUMBUF 13
1443174c21bSAl Viro 
14559d8053fSDavid Howells /*
14659d8053fSDavid Howells  * array.c
14759d8053fSDavid Howells  */
14859d8053fSDavid Howells extern const struct file_operations proc_tid_children_operations;
14959d8053fSDavid Howells 
15088b72b31STejun Heo extern void proc_task_name(struct seq_file *m, struct task_struct *p,
15188b72b31STejun Heo 			   bool escape);
15259d8053fSDavid Howells extern int proc_tid_stat(struct seq_file *, struct pid_namespace *,
15359d8053fSDavid Howells 			 struct pid *, struct task_struct *);
15459d8053fSDavid Howells extern int proc_tgid_stat(struct seq_file *, struct pid_namespace *,
15559d8053fSDavid Howells 			  struct pid *, struct task_struct *);
15659d8053fSDavid Howells extern int proc_pid_status(struct seq_file *, struct pid_namespace *,
15759d8053fSDavid Howells 			   struct pid *, struct task_struct *);
15859d8053fSDavid Howells extern int proc_pid_statm(struct seq_file *, struct pid_namespace *,
15959d8053fSDavid Howells 			  struct pid *, struct task_struct *);
16059d8053fSDavid Howells 
16159d8053fSDavid Howells /*
1621dd704b6SDavid Howells  * base.c
1631dd704b6SDavid Howells  */
16459d8053fSDavid Howells extern const struct dentry_operations pid_dentry_operations;
165*b74d24f7SChristian Brauner extern int pid_getattr(struct mnt_idmap *, const struct path *,
166549c7297SChristian Brauner 		       struct kstat *, u32, unsigned int);
167c1632a0fSChristian Brauner extern int proc_setattr(struct mnt_idmap *, struct dentry *,
168549c7297SChristian Brauner 			struct iattr *);
1697bc3e6e5SEric W. Biederman extern void proc_pid_evict_inode(struct proc_inode *);
170db978da8SAndreas Gruenbacher extern struct inode *proc_pid_make_inode(struct super_block *, struct task_struct *, umode_t);
1711bbc5513SAl Viro extern void pid_update_inode(struct task_struct *, struct inode *);
1721dd704b6SDavid Howells extern int pid_delete_dentry(const struct dentry *);
173f0c3b509SAl Viro extern int proc_pid_readdir(struct file *, struct dir_context *);
174867aaccfSZhikang Zhang struct dentry *proc_pid_lookup(struct dentry *, unsigned int);
17559d8053fSDavid Howells extern loff_t mem_lseek(struct file *, loff_t, int);
1761dd704b6SDavid Howells 
17759d8053fSDavid Howells /* Lookups */
1780168b9e3SAl Viro typedef struct dentry *instantiate_t(struct dentry *,
17959d8053fSDavid Howells 				     struct task_struct *, const void *);
180a4ef3895SAlexey Dobriyan bool proc_fill_cache(struct file *, struct dir_context *, const char *, unsigned int,
18159d8053fSDavid Howells 			   instantiate_t, struct task_struct *, const void *);
182881adb85SAlexey Dobriyan 
18359d8053fSDavid Howells /*
18459d8053fSDavid Howells  * generic.c
18559d8053fSDavid Howells  */
1867aed53d1SChristoph Hellwig struct proc_dir_entry *proc_create_reg(const char *name, umode_t mode,
1877aed53d1SChristoph Hellwig 		struct proc_dir_entry **parent, void *data);
18861172eaeSChristoph Hellwig struct proc_dir_entry *proc_register(struct proc_dir_entry *dir,
18961172eaeSChristoph Hellwig 		struct proc_dir_entry *dp);
19059d8053fSDavid Howells extern struct dentry *proc_lookup(struct inode *, struct dentry *, unsigned int);
19193ad5bc6SAlexey Dobriyan struct dentry *proc_lookup_de(struct inode *, struct dentry *, struct proc_dir_entry *);
192f0c3b509SAl Viro extern int proc_readdir(struct file *, struct dir_context *);
19393ad5bc6SAlexey Dobriyan int proc_readdir_de(struct file *, struct dir_context *, struct proc_dir_entry *);
1943174c21bSAl Viro 
pde_get(struct proc_dir_entry * pde)195a9389683SHui Su static inline void pde_get(struct proc_dir_entry *pde)
196135d5655SAlexey Dobriyan {
1979cdd83e3SAlexey Dobriyan 	refcount_inc(&pde->refcnt);
198135d5655SAlexey Dobriyan }
19959d8053fSDavid Howells extern void pde_put(struct proc_dir_entry *);
2003174c21bSAl Viro 
is_empty_pde(const struct proc_dir_entry * pde)201eb6d38d5SEric W. Biederman static inline bool is_empty_pde(const struct proc_dir_entry *pde)
202eb6d38d5SEric W. Biederman {
203eb6d38d5SEric W. Biederman 	return S_ISDIR(pde->mode) && !pde->proc_iops;
204eb6d38d5SEric W. Biederman }
205564def71SDavid Howells extern ssize_t proc_simple_write(struct file *, const char __user *, size_t, loff_t *);
206eb6d38d5SEric W. Biederman 
2073174c21bSAl Viro /*
20859d8053fSDavid Howells  * inode.c
2093174c21bSAl Viro  */
210e9720acdSPavel Emelyanov struct pde_opener {
211881adb85SAlexey Dobriyan 	struct list_head lh;
21270a731c0SAlexey Dobriyan 	struct file *file;
213f5887c71SAlexey Dobriyan 	bool closing;
21405c0ae21SAl Viro 	struct completion *c;
215a9fabc3dSAlexey Dobriyan } __randomize_layout;
2167e0e953bSAl Viro extern const struct inode_operations proc_link_inode_operations;
21759d8053fSDavid Howells extern const struct inode_operations proc_pid_link_inode_operations;
21860a3c3a5SDavid Howells extern const struct super_operations proc_sops;
2196b4e306aSEric W. Biederman 
220195b8cf0SAlexey Dobriyan void proc_init_kmemcache(void);
221f90f3cafSEric W. Biederman void proc_invalidate_siblings_dcache(struct hlist_head *inodes, spinlock_t *lock);
2221270dd8dSAlexey Dobriyan void set_proc_pid_nlink(void);
22359d8053fSDavid Howells extern struct inode *proc_get_inode(struct super_block *, struct proc_dir_entry *);
22459d8053fSDavid Howells extern void proc_entry_rundown(struct proc_dir_entry *);
2256b4e306aSEric W. Biederman 
2263174c21bSAl Viro /*
22759d8053fSDavid Howells  * proc_namespaces.c
22859d8053fSDavid Howells  */
2296b4e306aSEric W. Biederman extern const struct inode_operations proc_ns_dir_inode_operations;
2306b4e306aSEric W. Biederman extern const struct file_operations proc_ns_dir_operations;
2316b4e306aSEric W. Biederman 
23259d8053fSDavid Howells /*
23359d8053fSDavid Howells  * proc_net.c
23459d8053fSDavid Howells  */
23559d8053fSDavid Howells extern const struct file_operations proc_net_operations;
23659d8053fSDavid Howells extern const struct inode_operations proc_net_inode_operations;
23759d8053fSDavid Howells 
23859d8053fSDavid Howells #ifdef CONFIG_NET
23959d8053fSDavid Howells extern int proc_net_init(void);
24059d8053fSDavid Howells #else
proc_net_init(void)24159d8053fSDavid Howells static inline int proc_net_init(void) { return 0; }
24259d8053fSDavid Howells #endif
24359d8053fSDavid Howells 
24459d8053fSDavid Howells /*
24559d8053fSDavid Howells  * proc_self.c
24659d8053fSDavid Howells  */
24759d8053fSDavid Howells extern int proc_setup_self(struct super_block *);
24859d8053fSDavid Howells 
24959d8053fSDavid Howells /*
2500097875bSEric W. Biederman  * proc_thread_self.c
2510097875bSEric W. Biederman  */
2520097875bSEric W. Biederman extern int proc_setup_thread_self(struct super_block *);
2530097875bSEric W. Biederman extern void proc_thread_self_init(void);
2540097875bSEric W. Biederman 
2550097875bSEric W. Biederman /*
25659d8053fSDavid Howells  * proc_sysctl.c
25759d8053fSDavid Howells  */
25859d8053fSDavid Howells #ifdef CONFIG_PROC_SYSCTL
25959d8053fSDavid Howells extern int proc_sys_init(void);
260d6cffbbeSKonstantin Khlebnikov extern void proc_sys_evict_inode(struct inode *inode,
261d6cffbbeSKonstantin Khlebnikov 				 struct ctl_table_header *head);
26259d8053fSDavid Howells #else
proc_sys_init(void)26359d8053fSDavid Howells static inline void proc_sys_init(void) { }
proc_sys_evict_inode(struct inode * inode,struct ctl_table_header * head)264d6cffbbeSKonstantin Khlebnikov static inline void proc_sys_evict_inode(struct  inode *inode,
265d6cffbbeSKonstantin Khlebnikov 					struct ctl_table_header *head) { }
26659d8053fSDavid Howells #endif
26734db8aafSDavid Howells 
26834db8aafSDavid Howells /*
26934db8aafSDavid Howells  * proc_tty.c
27034db8aafSDavid Howells  */
27134db8aafSDavid Howells #ifdef CONFIG_TTY
27234db8aafSDavid Howells extern void proc_tty_init(void);
27334db8aafSDavid Howells #else
proc_tty_init(void)27434db8aafSDavid Howells static inline void proc_tty_init(void) {}
27534db8aafSDavid Howells #endif
27659d8053fSDavid Howells 
27759d8053fSDavid Howells /*
27859d8053fSDavid Howells  * root.c
27959d8053fSDavid Howells  */
28059d8053fSDavid Howells extern struct proc_dir_entry proc_root;
28159d8053fSDavid Howells 
28259d8053fSDavid Howells extern void proc_self_init(void);
28359d8053fSDavid Howells 
28459d8053fSDavid Howells /*
28559d8053fSDavid Howells  * task_[no]mmu.c
28659d8053fSDavid Howells  */
287493b0e9dSDaniel Colascione struct mem_size_stats;
28859d8053fSDavid Howells struct proc_maps_private {
2892c03376dSOleg Nesterov 	struct inode *inode;
29059d8053fSDavid Howells 	struct task_struct *task;
29129a40aceSOleg Nesterov 	struct mm_struct *mm;
292c4c84f06SMatthew Wilcox (Oracle) 	struct vma_iterator iter;
29359d8053fSDavid Howells #ifdef CONFIG_NUMA
29459d8053fSDavid Howells 	struct mempolicy *task_mempolicy;
29559d8053fSDavid Howells #endif
2963859a271SKees Cook } __randomize_layout;
29759d8053fSDavid Howells 
2985381e169SOleg Nesterov struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode);
2995381e169SOleg Nesterov 
30059d8053fSDavid Howells extern const struct file_operations proc_pid_maps_operations;
30159d8053fSDavid Howells extern const struct file_operations proc_pid_numa_maps_operations;
30259d8053fSDavid Howells extern const struct file_operations proc_pid_smaps_operations;
303493b0e9dSDaniel Colascione extern const struct file_operations proc_pid_smaps_rollup_operations;
30459d8053fSDavid Howells extern const struct file_operations proc_clear_refs_operations;
30559d8053fSDavid Howells extern const struct file_operations proc_pagemap_operations;
30659d8053fSDavid Howells 
30759d8053fSDavid Howells extern unsigned long task_vsize(struct mm_struct *);
30859d8053fSDavid Howells extern unsigned long task_statm(struct mm_struct *,
30959d8053fSDavid Howells 				unsigned long *, unsigned long *,
31059d8053fSDavid Howells 				unsigned long *, unsigned long *);
31159d8053fSDavid Howells extern void task_mem(struct seq_file *, struct mm_struct *);
312c6c75dedSAlexey Dobriyan 
313c6c75dedSAlexey Dobriyan extern const struct dentry_operations proc_net_dentry_ops;
pde_force_lookup(struct proc_dir_entry * pde)314c6c75dedSAlexey Dobriyan static inline void pde_force_lookup(struct proc_dir_entry *pde)
315c6c75dedSAlexey Dobriyan {
316c6c75dedSAlexey Dobriyan 	/* /proc/net/ entries can be changed under us by setns(CLONE_NEWNET) */
317c6c75dedSAlexey Dobriyan 	pde->proc_dops = &proc_net_dentry_ops;
318c6c75dedSAlexey Dobriyan }
319