aufs5-mmap.patch 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. SPDX-License-Identifier: GPL-2.0
  2. aufs5.15.41 mmap patch
  3. diff --git a/fs/proc/base.c b/fs/proc/base.c
  4. index 1f394095eb88..93f2479ef319 100644
  5. --- a/fs/proc/base.c
  6. +++ b/fs/proc/base.c
  7. @@ -2189,7 +2189,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
  8. rc = -ENOENT;
  9. vma = find_exact_vma(mm, vm_start, vm_end);
  10. if (vma && vma->vm_file) {
  11. - *path = vma->vm_file->f_path;
  12. + *path = vma_pr_or_file(vma)->f_path;
  13. path_get(path);
  14. rc = 0;
  15. }
  16. diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
  17. index 13452b32e2bd..38acccfef9d4 100644
  18. --- a/fs/proc/nommu.c
  19. +++ b/fs/proc/nommu.c
  20. @@ -40,7 +40,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
  21. file = region->vm_file;
  22. if (file) {
  23. - struct inode *inode = file_inode(region->vm_file);
  24. + struct inode *inode;
  25. +
  26. + file = vmr_pr_or_file(region);
  27. + inode = file_inode(file);
  28. dev = inode->i_sb->s_dev;
  29. ino = inode->i_ino;
  30. }
  31. diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
  32. index 79ca4d69dfd6..3d3067c8d868 100644
  33. --- a/fs/proc/task_mmu.c
  34. +++ b/fs/proc/task_mmu.c
  35. @@ -280,7 +280,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
  36. const char *name = NULL;
  37. if (file) {
  38. - struct inode *inode = file_inode(vma->vm_file);
  39. + struct inode *inode;
  40. +
  41. + file = vma_pr_or_file(vma);
  42. + inode = file_inode(file);
  43. dev = inode->i_sb->s_dev;
  44. ino = inode->i_ino;
  45. pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
  46. @@ -1888,7 +1891,7 @@ static int show_numa_map(struct seq_file *m, void *v)
  47. struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
  48. struct vm_area_struct *vma = v;
  49. struct numa_maps *md = &numa_priv->md;
  50. - struct file *file = vma->vm_file;
  51. + struct file *file = vma_pr_or_file(vma);
  52. struct mm_struct *mm = vma->vm_mm;
  53. struct mempolicy *pol;
  54. char buffer[64];
  55. diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
  56. index a6d21fc0033c..02c2de31196e 100644
  57. --- a/fs/proc/task_nommu.c
  58. +++ b/fs/proc/task_nommu.c
  59. @@ -155,7 +155,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
  60. file = vma->vm_file;
  61. if (file) {
  62. - struct inode *inode = file_inode(vma->vm_file);
  63. + struct inode *inode;
  64. +
  65. + file = vma_pr_or_file(vma);
  66. + inode = file_inode(file);
  67. dev = inode->i_sb->s_dev;
  68. ino = inode->i_ino;
  69. pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
  70. diff --git a/include/linux/mm.h b/include/linux/mm.h
  71. index 04345ff97f8c..4ab9bb75c894 100644
  72. --- a/include/linux/mm.h
  73. +++ b/include/linux/mm.h
  74. @@ -1813,6 +1813,43 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
  75. unmap_mapping_range(mapping, holebegin, holelen, 0);
  76. }
  77. +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
  78. +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
  79. +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
  80. + int);
  81. +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
  82. +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
  83. +
  84. +#define vma_file_update_time(vma) vma_do_file_update_time(vma, __func__, \
  85. + __LINE__)
  86. +#define vma_pr_or_file(vma) vma_do_pr_or_file(vma, __func__, \
  87. + __LINE__)
  88. +#define vma_get_file(vma) vma_do_get_file(vma, __func__, __LINE__)
  89. +#define vma_fput(vma) vma_do_fput(vma, __func__, __LINE__)
  90. +
  91. +#ifndef CONFIG_MMU
  92. +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
  93. +extern void vmr_do_fput(struct vm_region *, const char[], int);
  94. +
  95. +#define vmr_pr_or_file(region) vmr_do_pr_or_file(region, __func__, \
  96. + __LINE__)
  97. +#define vmr_fput(region) vmr_do_fput(region, __func__, __LINE__)
  98. +#endif /* !CONFIG_MMU */
  99. +
  100. +#else
  101. +
  102. +#define vma_file_update_time(vma) file_update_time((vma)->vm_file)
  103. +#define vma_pr_or_file(vma) (vma)->vm_file
  104. +#define vma_get_file(vma) get_file((vma)->vm_file)
  105. +#define vma_fput(vma) fput((vma)->vm_file)
  106. +
  107. +#ifndef CONFIG_MMU
  108. +#define vmr_pr_or_file(region) (region)->vm_file
  109. +#define vmr_fput(region) fput((region)->vm_file)
  110. +#endif /* !CONFIG_MMU */
  111. +
  112. +#endif /* CONFIG_AUFS_FS */
  113. +
  114. extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
  115. void *buf, int len, unsigned int gup_flags);
  116. extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
  117. diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
  118. index 7f8ee09c711f..8fea872e08f7 100644
  119. --- a/include/linux/mm_types.h
  120. +++ b/include/linux/mm_types.h
  121. @@ -294,6 +294,9 @@ struct vm_region {
  122. unsigned long vm_top; /* region allocated to here */
  123. unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */
  124. struct file *vm_file; /* the backing file or NULL */
  125. +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
  126. + struct file *vm_prfile; /* the virtual backing file or NULL */
  127. +#endif
  128. int vm_usage; /* region usage count (access under nommu_region_sem) */
  129. bool vm_icache_flushed : 1; /* true if the icache has been flushed for
  130. @@ -373,6 +376,9 @@ struct vm_area_struct {
  131. unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
  132. units */
  133. struct file * vm_file; /* File we map to (can be NULL). */
  134. +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
  135. + struct file *vm_prfile; /* shadow of vm_file */
  136. +#endif
  137. void * vm_private_data; /* was vm_pte (shared mem) */
  138. #ifdef CONFIG_SWAP
  139. diff --git a/kernel/fork.c b/kernel/fork.c
  140. index 89475c994ca9..82f46f8f4090 100644
  141. --- a/kernel/fork.c
  142. +++ b/kernel/fork.c
  143. @@ -573,7 +573,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
  144. if (file) {
  145. struct address_space *mapping = file->f_mapping;
  146. - get_file(file);
  147. + vma_get_file(tmp);
  148. i_mmap_lock_write(mapping);
  149. if (tmp->vm_flags & VM_SHARED)
  150. mapping_allow_writable(mapping);
  151. diff --git a/mm/Makefile b/mm/Makefile
  152. index fc60a40ce954..d45773672730 100644
  153. --- a/mm/Makefile
  154. +++ b/mm/Makefile
  155. @@ -130,3 +130,4 @@ obj-$(CONFIG_PAGE_REPORTING) += page_reporting.o
  156. obj-$(CONFIG_IO_MAPPING) += io-mapping.o
  157. obj-$(CONFIG_HAVE_BOOTMEM_INFO_NODE) += bootmem_info.o
  158. obj-$(CONFIG_GENERIC_IOREMAP) += ioremap.o
  159. +obj-y += prfile.o
  160. diff --git a/mm/filemap.c b/mm/filemap.c
  161. index 00e391e75880..d8b52d814319 100644
  162. --- a/mm/filemap.c
  163. +++ b/mm/filemap.c
  164. @@ -3353,7 +3353,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
  165. vm_fault_t ret = VM_FAULT_LOCKED;
  166. sb_start_pagefault(mapping->host->i_sb);
  167. - file_update_time(vmf->vma->vm_file);
  168. + vma_file_update_time(vmf->vma);
  169. lock_page(page);
  170. if (page->mapping != mapping) {
  171. unlock_page(page);
  172. diff --git a/mm/mmap.c b/mm/mmap.c
  173. index 6bb553ed5c55..44a7823a0de3 100644
  174. --- a/mm/mmap.c
  175. +++ b/mm/mmap.c
  176. @@ -183,7 +183,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
  177. if (vma->vm_ops && vma->vm_ops->close)
  178. vma->vm_ops->close(vma);
  179. if (vma->vm_file)
  180. - fput(vma->vm_file);
  181. + vma_fput(vma);
  182. mpol_put(vma_policy(vma));
  183. vm_area_free(vma);
  184. return next;
  185. @@ -952,7 +952,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
  186. if (remove_next) {
  187. if (file) {
  188. uprobe_munmap(next, next->vm_start, next->vm_end);
  189. - fput(file);
  190. + vma_fput(vma);
  191. }
  192. if (next->anon_vma)
  193. anon_vma_merge(vma, next);
  194. @@ -1810,7 +1810,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
  195. * fput the vma->vm_file here or we would add an extra fput for file
  196. * and cause general protection fault ultimately.
  197. */
  198. - fput(vma->vm_file);
  199. + vma_fput(vma);
  200. vm_area_free(vma);
  201. vma = merge;
  202. /* Update vm_flags to pick up the change. */
  203. @@ -1873,7 +1873,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
  204. return addr;
  205. unmap_and_free_vma:
  206. - fput(vma->vm_file);
  207. + vma_fput(vma);
  208. vma->vm_file = NULL;
  209. /* Undo any partial mapping done by a device driver. */
  210. @@ -2723,7 +2723,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
  211. goto out_free_mpol;
  212. if (new->vm_file)
  213. - get_file(new->vm_file);
  214. + vma_get_file(new);
  215. if (new->vm_ops && new->vm_ops->open)
  216. new->vm_ops->open(new);
  217. @@ -2742,7 +2742,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
  218. if (new->vm_ops && new->vm_ops->close)
  219. new->vm_ops->close(new);
  220. if (new->vm_file)
  221. - fput(new->vm_file);
  222. + vma_fput(new);
  223. unlink_anon_vmas(new);
  224. out_free_mpol:
  225. mpol_put(vma_policy(new));
  226. @@ -2938,6 +2938,9 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
  227. unsigned long populate = 0;
  228. unsigned long ret = -EINVAL;
  229. struct file *file;
  230. +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
  231. + struct file *prfile;
  232. +#endif
  233. pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.rst.\n",
  234. current->comm, current->pid);
  235. @@ -2993,10 +2996,34 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
  236. if (vma->vm_flags & VM_LOCKED)
  237. flags |= MAP_LOCKED;
  238. +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
  239. + vma_get_file(vma);
  240. + file = vma->vm_file;
  241. + prfile = vma->vm_prfile;
  242. + ret = do_mmap(vma->vm_file, start, size,
  243. + prot, flags, pgoff, &populate, NULL);
  244. + if (!IS_ERR_VALUE(ret) && file && prfile) {
  245. + struct vm_area_struct *new_vma;
  246. +
  247. + new_vma = find_vma(mm, ret);
  248. + if (!new_vma->vm_prfile)
  249. + new_vma->vm_prfile = prfile;
  250. + if (prfile)
  251. + get_file(prfile);
  252. + }
  253. + /*
  254. + * two fput()s instead of vma_fput(vma),
  255. + * coz vma may not be available anymore.
  256. + */
  257. + fput(file);
  258. + if (prfile)
  259. + fput(prfile);
  260. +#else
  261. file = get_file(vma->vm_file);
  262. ret = do_mmap(vma->vm_file, start, size,
  263. prot, flags, pgoff, &populate, NULL);
  264. fput(file);
  265. +#endif /* CONFIG_AUFS_FS */
  266. out:
  267. mmap_write_unlock(mm);
  268. if (populate)
  269. @@ -3277,7 +3304,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
  270. if (anon_vma_clone(new_vma, vma))
  271. goto out_free_mempol;
  272. if (new_vma->vm_file)
  273. - get_file(new_vma->vm_file);
  274. + vma_get_file(new_vma);
  275. if (new_vma->vm_ops && new_vma->vm_ops->open)
  276. new_vma->vm_ops->open(new_vma);
  277. vma_link(mm, new_vma, prev, rb_link, rb_parent);
  278. diff --git a/mm/nommu.c b/mm/nommu.c
  279. index 02d2427b8f9e..a7419302ab4e 100644
  280. --- a/mm/nommu.c
  281. +++ b/mm/nommu.c
  282. @@ -523,7 +523,7 @@ static void __put_nommu_region(struct vm_region *region)
  283. up_write(&nommu_region_sem);
  284. if (region->vm_file)
  285. - fput(region->vm_file);
  286. + vmr_fput(region);
  287. /* IO memory and memory shared directly out of the pagecache
  288. * from ramfs/tmpfs mustn't be released here */
  289. @@ -655,7 +655,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
  290. if (vma->vm_ops && vma->vm_ops->close)
  291. vma->vm_ops->close(vma);
  292. if (vma->vm_file)
  293. - fput(vma->vm_file);
  294. + vma_fput(vma);
  295. put_nommu_region(vma->vm_region);
  296. vm_area_free(vma);
  297. }
  298. @@ -1175,7 +1175,7 @@ unsigned long do_mmap(struct file *file,
  299. goto error_just_free;
  300. }
  301. }
  302. - fput(region->vm_file);
  303. + vmr_fput(region);
  304. kmem_cache_free(vm_region_jar, region);
  305. region = pregion;
  306. result = start;
  307. @@ -1252,10 +1252,10 @@ unsigned long do_mmap(struct file *file,
  308. up_write(&nommu_region_sem);
  309. error:
  310. if (region->vm_file)
  311. - fput(region->vm_file);
  312. + vmr_fput(region);
  313. kmem_cache_free(vm_region_jar, region);
  314. if (vma->vm_file)
  315. - fput(vma->vm_file);
  316. + vma_fput(vma);
  317. vm_area_free(vma);
  318. return ret;
  319. diff --git a/mm/prfile.c b/mm/prfile.c
  320. new file mode 100644
  321. index 000000000000..511543ab1b41
  322. --- /dev/null
  323. +++ b/mm/prfile.c
  324. @@ -0,0 +1,86 @@
  325. +// SPDX-License-Identifier: GPL-2.0
  326. +/*
  327. + * Mainly for aufs which mmap(2) different file and wants to print different
  328. + * path in /proc/PID/maps.
  329. + * Call these functions via macros defined in linux/mm.h.
  330. + *
  331. + * See Documentation/filesystems/aufs/design/06mmap.txt
  332. + *
  333. + * Copyright (c) 2014-2021 Junjro R. Okajima
  334. + * Copyright (c) 2014 Ian Campbell
  335. + */
  336. +
  337. +#include <linux/mm.h>
  338. +#include <linux/file.h>
  339. +#include <linux/fs.h>
  340. +
  341. +/* #define PRFILE_TRACE */
  342. +static inline void prfile_trace(struct file *f, struct file *pr,
  343. + const char func[], int line, const char func2[])
  344. +{
  345. +#ifdef PRFILE_TRACE
  346. + if (pr)
  347. + pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
  348. +#endif
  349. +}
  350. +
  351. +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
  352. + int line)
  353. +{
  354. + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
  355. +
  356. + prfile_trace(f, pr, func, line, __func__);
  357. + file_update_time(f);
  358. + if (f && pr)
  359. + file_update_time(pr);
  360. +}
  361. +
  362. +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
  363. + int line)
  364. +{
  365. + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
  366. +
  367. + prfile_trace(f, pr, func, line, __func__);
  368. + return (f && pr) ? pr : f;
  369. +}
  370. +
  371. +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
  372. +{
  373. + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
  374. +
  375. + prfile_trace(f, pr, func, line, __func__);
  376. + get_file(f);
  377. + if (f && pr)
  378. + get_file(pr);
  379. +}
  380. +
  381. +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
  382. +{
  383. + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
  384. +
  385. + prfile_trace(f, pr, func, line, __func__);
  386. + fput(f);
  387. + if (f && pr)
  388. + fput(pr);
  389. +}
  390. +
  391. +#ifndef CONFIG_MMU
  392. +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
  393. + int line)
  394. +{
  395. + struct file *f = region->vm_file, *pr = region->vm_prfile;
  396. +
  397. + prfile_trace(f, pr, func, line, __func__);
  398. + return (f && pr) ? pr : f;
  399. +}
  400. +
  401. +void vmr_do_fput(struct vm_region *region, const char func[], int line)
  402. +{
  403. + struct file *f = region->vm_file, *pr = region->vm_prfile;
  404. +
  405. + prfile_trace(f, pr, func, line, __func__);
  406. + fput(f);
  407. + if (f && pr)
  408. + fput(pr);
  409. +}
  410. +#endif /* !CONFIG_MMU */