internal.h 11 KB

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