internal.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. union {
  45. const struct seq_operations *seq_ops;
  46. int (*single_show)(struct seq_file *, void *);
  47. };
  48. proc_write_t write;
  49. void *data;
  50. unsigned int state_size;
  51. unsigned int low_ino;
  52. nlink_t nlink;
  53. kuid_t uid;
  54. kgid_t gid;
  55. loff_t size;
  56. struct proc_dir_entry *parent;
  57. struct rb_root subdir;
  58. struct rb_node subdir_node;
  59. char *name;
  60. umode_t mode;
  61. u8 flags;
  62. u8 namelen;
  63. char inline_name[];
  64. } __randomize_layout;
  65. #define SIZEOF_PDE ( \
  66. sizeof(struct proc_dir_entry) < 128 ? 128 : \
  67. sizeof(struct proc_dir_entry) < 192 ? 192 : \
  68. sizeof(struct proc_dir_entry) < 256 ? 256 : \
  69. sizeof(struct proc_dir_entry) < 512 ? 512 : \
  70. 0)
  71. #define SIZEOF_PDE_INLINE_NAME (SIZEOF_PDE - sizeof(struct proc_dir_entry))
  72. static inline bool pde_is_permanent(const struct proc_dir_entry *pde)
  73. {
  74. return pde->flags & PROC_ENTRY_PERMANENT;
  75. }
  76. static inline void pde_make_permanent(struct proc_dir_entry *pde)
  77. {
  78. pde->flags |= PROC_ENTRY_PERMANENT;
  79. }
  80. static inline bool pde_has_proc_read_iter(const struct proc_dir_entry *pde)
  81. {
  82. return pde->flags & PROC_ENTRY_proc_read_iter;
  83. }
  84. static inline bool pde_has_proc_compat_ioctl(const struct proc_dir_entry *pde)
  85. {
  86. #ifdef CONFIG_COMPAT
  87. return pde->flags & PROC_ENTRY_proc_compat_ioctl;
  88. #else
  89. return false;
  90. #endif
  91. }
  92. static inline bool pde_has_proc_lseek(const struct proc_dir_entry *pde)
  93. {
  94. return pde->flags & PROC_ENTRY_proc_lseek;
  95. }
  96. extern struct kmem_cache *proc_dir_entry_cache;
  97. void pde_free(struct proc_dir_entry *pde);
  98. union proc_op {
  99. int (*proc_get_link)(struct dentry *, struct path *);
  100. int (*proc_show)(struct seq_file *m,
  101. struct pid_namespace *ns, struct pid *pid,
  102. struct task_struct *task);
  103. int lsmid;
  104. };
  105. struct proc_inode {
  106. struct pid *pid;
  107. unsigned int fd;
  108. union proc_op op;
  109. struct proc_dir_entry *pde;
  110. struct ctl_table_header *sysctl;
  111. const struct ctl_table *sysctl_entry;
  112. struct hlist_node sibling_inodes;
  113. const struct proc_ns_operations *ns_ops;
  114. struct inode vfs_inode;
  115. } __randomize_layout;
  116. /*
  117. * General functions
  118. */
  119. static inline struct proc_inode *PROC_I(const struct inode *inode)
  120. {
  121. return container_of(inode, struct proc_inode, vfs_inode);
  122. }
  123. static inline struct proc_dir_entry *PDE(const struct inode *inode)
  124. {
  125. return PROC_I(inode)->pde;
  126. }
  127. static inline struct pid *proc_pid(const struct inode *inode)
  128. {
  129. return PROC_I(inode)->pid;
  130. }
  131. static inline struct task_struct *get_proc_task(const struct inode *inode)
  132. {
  133. return get_pid_task(proc_pid(inode), PIDTYPE_PID);
  134. }
  135. void task_dump_owner(struct task_struct *task, umode_t mode,
  136. kuid_t *ruid, kgid_t *rgid);
  137. unsigned name_to_int(const struct qstr *qstr);
  138. /*
  139. * Offset of the first process in the /proc root directory..
  140. */
  141. #define FIRST_PROCESS_ENTRY 256
  142. /* Worst case buffer size needed for holding an integer. */
  143. #define PROC_NUMBUF 13
  144. #ifdef CONFIG_PAGE_MAPCOUNT
  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. #else /* !CONFIG_PAGE_MAPCOUNT */
  174. static inline int folio_precise_page_mapcount(struct folio *folio,
  175. struct page *page)
  176. {
  177. BUILD_BUG();
  178. }
  179. #endif /* CONFIG_PAGE_MAPCOUNT */
  180. /**
  181. * folio_average_page_mapcount() - Average number of mappings per page in this
  182. * folio
  183. * @folio: The folio.
  184. *
  185. * The average number of user page table entries that reference each page in
  186. * this folio as tracked via the RMAP: either referenced directly (PTE) or
  187. * as part of a larger area that covers this page (e.g., PMD).
  188. *
  189. * The average is calculated by rounding to the nearest integer; however,
  190. * to avoid duplicated code in current callers, the average is at least
  191. * 1 if any page of the folio is mapped.
  192. *
  193. * Returns: The average number of mappings per page in this folio.
  194. */
  195. static inline int folio_average_page_mapcount(struct folio *folio)
  196. {
  197. int mapcount, entire_mapcount, avg;
  198. if (!folio_test_large(folio))
  199. return atomic_read(&folio->_mapcount) + 1;
  200. mapcount = folio_large_mapcount(folio);
  201. if (unlikely(mapcount <= 0))
  202. return 0;
  203. entire_mapcount = folio_entire_mapcount(folio);
  204. if (mapcount <= entire_mapcount)
  205. return entire_mapcount;
  206. mapcount -= entire_mapcount;
  207. /* Round to closest integer ... */
  208. avg = ((unsigned int)mapcount + folio_large_nr_pages(folio) / 2) >> folio_large_order(folio);
  209. /* ... but return at least 1. */
  210. return max_t(int, avg + entire_mapcount, 1);
  211. }
  212. /*
  213. * array.c
  214. */
  215. extern const struct file_operations proc_tid_children_operations;
  216. extern void proc_task_name(struct seq_file *m, struct task_struct *p,
  217. bool escape);
  218. extern int proc_tid_stat(struct seq_file *, struct pid_namespace *,
  219. struct pid *, struct task_struct *);
  220. extern int proc_tgid_stat(struct seq_file *, struct pid_namespace *,
  221. struct pid *, struct task_struct *);
  222. extern int proc_pid_status(struct seq_file *, struct pid_namespace *,
  223. struct pid *, struct task_struct *);
  224. extern int proc_pid_statm(struct seq_file *, struct pid_namespace *,
  225. struct pid *, struct task_struct *);
  226. /*
  227. * base.c
  228. */
  229. extern const struct dentry_operations pid_dentry_operations;
  230. extern int pid_getattr(struct mnt_idmap *, const struct path *,
  231. struct kstat *, u32, unsigned int);
  232. extern int proc_setattr(struct mnt_idmap *, struct dentry *,
  233. struct iattr *);
  234. extern void proc_pid_evict_inode(struct proc_inode *);
  235. extern struct inode *proc_pid_make_inode(struct super_block *, struct task_struct *, umode_t);
  236. extern void pid_update_inode(struct task_struct *, struct inode *);
  237. extern int pid_delete_dentry(const struct dentry *);
  238. extern int proc_pid_readdir(struct file *, struct dir_context *);
  239. struct dentry *proc_pid_lookup(struct dentry *, unsigned int);
  240. extern loff_t mem_lseek(struct file *, loff_t, int);
  241. /* Lookups */
  242. typedef struct dentry *instantiate_t(struct dentry *,
  243. struct task_struct *, const void *);
  244. bool proc_fill_cache(struct file *, struct dir_context *, const char *, unsigned int,
  245. instantiate_t, struct task_struct *, const void *);
  246. /*
  247. * generic.c
  248. */
  249. struct proc_dir_entry *proc_create_reg(const char *name, umode_t mode,
  250. struct proc_dir_entry **parent, void *data);
  251. struct proc_dir_entry *proc_register(struct proc_dir_entry *dir,
  252. struct proc_dir_entry *dp);
  253. extern struct dentry *proc_lookup(struct inode *, struct dentry *, unsigned int);
  254. struct dentry *proc_lookup_de(struct inode *, struct dentry *, struct proc_dir_entry *);
  255. extern int proc_readdir(struct file *, struct dir_context *);
  256. int proc_readdir_de(struct file *, struct dir_context *, struct proc_dir_entry *);
  257. static inline void pde_get(struct proc_dir_entry *pde)
  258. {
  259. refcount_inc(&pde->refcnt);
  260. }
  261. extern void pde_put(struct proc_dir_entry *);
  262. static inline bool is_empty_pde(const struct proc_dir_entry *pde)
  263. {
  264. return S_ISDIR(pde->mode) && !pde->proc_iops;
  265. }
  266. extern ssize_t proc_simple_write(struct file *, const char __user *, size_t, loff_t *);
  267. /*
  268. * inode.c
  269. */
  270. struct pde_opener {
  271. struct list_head lh;
  272. struct file *file;
  273. bool closing;
  274. struct completion *c;
  275. } __randomize_layout;
  276. extern const struct inode_operations proc_link_inode_operations;
  277. extern const struct inode_operations proc_pid_link_inode_operations;
  278. extern const struct super_operations proc_sops;
  279. void proc_init_kmemcache(void);
  280. void proc_invalidate_siblings_dcache(struct hlist_head *inodes, spinlock_t *lock);
  281. void set_proc_pid_nlink(void);
  282. extern struct inode *proc_get_inode(struct super_block *, struct proc_dir_entry *);
  283. extern void proc_entry_rundown(struct proc_dir_entry *);
  284. /*
  285. * proc_namespaces.c
  286. */
  287. extern const struct inode_operations proc_ns_dir_inode_operations;
  288. extern const struct file_operations proc_ns_dir_operations;
  289. /*
  290. * proc_net.c
  291. */
  292. extern const struct file_operations proc_net_operations;
  293. extern const struct inode_operations proc_net_inode_operations;
  294. #ifdef CONFIG_NET
  295. extern int proc_net_init(void);
  296. #else
  297. static inline int proc_net_init(void) { return 0; }
  298. #endif
  299. /*
  300. * proc_self.c
  301. */
  302. extern int proc_setup_self(struct super_block *);
  303. /*
  304. * proc_thread_self.c
  305. */
  306. extern int proc_setup_thread_self(struct super_block *);
  307. extern void proc_thread_self_init(void);
  308. /*
  309. * proc_sysctl.c
  310. */
  311. #ifdef CONFIG_PROC_SYSCTL
  312. extern int proc_sys_init(void);
  313. extern void proc_sys_evict_inode(struct inode *inode,
  314. struct ctl_table_header *head);
  315. #else
  316. static inline void proc_sys_init(void) { }
  317. static inline void proc_sys_evict_inode(struct inode *inode,
  318. struct ctl_table_header *head) { }
  319. #endif
  320. /*
  321. * proc_tty.c
  322. */
  323. #ifdef CONFIG_TTY
  324. extern void proc_tty_init(void);
  325. #else
  326. static inline void proc_tty_init(void) {}
  327. #endif
  328. /*
  329. * root.c
  330. */
  331. extern struct proc_dir_entry proc_root;
  332. extern void proc_self_init(void);
  333. extern unsigned self_inum, thread_self_inum;
  334. /*
  335. * task_[no]mmu.c
  336. */
  337. struct mem_size_stats;
  338. struct proc_maps_locking_ctx {
  339. struct mm_struct *mm;
  340. #ifdef CONFIG_PER_VMA_LOCK
  341. bool mmap_locked;
  342. struct vm_area_struct *locked_vma;
  343. #endif
  344. };
  345. struct proc_maps_private {
  346. struct inode *inode;
  347. struct task_struct *task;
  348. struct vma_iterator iter;
  349. loff_t last_pos;
  350. struct proc_maps_locking_ctx lock_ctx;
  351. #ifdef CONFIG_NUMA
  352. struct mempolicy *task_mempolicy;
  353. #endif
  354. } __randomize_layout;
  355. struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode);
  356. extern const struct file_operations proc_pid_maps_operations;
  357. extern const struct file_operations proc_pid_numa_maps_operations;
  358. extern const struct file_operations proc_pid_smaps_operations;
  359. extern const struct file_operations proc_pid_smaps_rollup_operations;
  360. extern const struct file_operations proc_clear_refs_operations;
  361. extern const struct file_operations proc_pagemap_operations;
  362. extern unsigned long task_vsize(struct mm_struct *);
  363. extern unsigned long task_statm(struct mm_struct *,
  364. unsigned long *, unsigned long *,
  365. unsigned long *, unsigned long *);
  366. extern void task_mem(struct seq_file *, struct mm_struct *);
  367. extern const struct dentry_operations proc_net_dentry_ops;
  368. static inline void pde_force_lookup(struct proc_dir_entry *pde)
  369. {
  370. /* /proc/net/ entries can be changed under us by setns(CLONE_NEWNET) */
  371. pde->flags |= PROC_ENTRY_FORCE_LOOKUP;
  372. }
  373. /*
  374. * Add a new procfs dentry that can't serve as a mountpoint. That should
  375. * encompass anything that is ephemeral and can just disappear while the
  376. * process is still around.
  377. */
  378. static inline struct dentry *proc_splice_unmountable(struct inode *inode,
  379. struct dentry *dentry, const struct dentry_operations *d_ops)
  380. {
  381. dont_mount(dentry);
  382. return d_splice_alias_ops(inode, dentry, d_ops);
  383. }