aufs5-mmap.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. SPDX-License-Identifier: GPL-2.0
  2. aufs5.1 mmap patch
  3. diff --git a/fs/proc/base.c b/fs/proc/base.c
  4. index 6a803a0b75df..a51f41a1b4dd 100644
  5. --- a/fs/proc/base.c
  6. +++ b/fs/proc/base.c
  7. @@ -2019,7 +2019,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
  8. down_read(&mm->mmap_sem);
  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 3b63be64e436..fb9913bf3d10 100644
  18. --- a/fs/proc/nommu.c
  19. +++ b/fs/proc/nommu.c
  20. @@ -45,7 +45,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 95ca1fe7283c..914d9fa7c596 100644
  33. --- a/fs/proc/task_mmu.c
  34. +++ b/fs/proc/task_mmu.c
  35. @@ -305,7 +305,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. @@ -1760,7 +1763,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 mm_walk walk = {
  54. .hugetlb_entry = gather_hugetlb_stats,
  55. diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
  56. index 36bf0f2e102e..8df6cbc58756 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 6b10c21630f5..5951915df163 100644
  72. --- a/include/linux/mm.h
  73. +++ b/include/linux/mm.h
  74. @@ -1486,6 +1486,28 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
  75. unmap_mapping_range(mapping, holebegin, holelen, 0);
  76. }
  77. +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
  78. +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
  79. + int);
  80. +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
  81. +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
  82. +
  83. +#define vma_file_update_time(vma) vma_do_file_update_time(vma, __func__, \
  84. + __LINE__)
  85. +#define vma_pr_or_file(vma) vma_do_pr_or_file(vma, __func__, \
  86. + __LINE__)
  87. +#define vma_get_file(vma) vma_do_get_file(vma, __func__, __LINE__)
  88. +#define vma_fput(vma) vma_do_fput(vma, __func__, __LINE__)
  89. +
  90. +#ifndef CONFIG_MMU
  91. +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
  92. +extern void vmr_do_fput(struct vm_region *, const char[], int);
  93. +
  94. +#define vmr_pr_or_file(region) vmr_do_pr_or_file(region, __func__, \
  95. + __LINE__)
  96. +#define vmr_fput(region) vmr_do_fput(region, __func__, __LINE__)
  97. +#endif /* !CONFIG_MMU */
  98. +
  99. extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
  100. void *buf, int len, unsigned int gup_flags);
  101. extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
  102. diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
  103. index 4ef4bbe78a1d..8cac6e0bc1a8 100644
  104. --- a/include/linux/mm_types.h
  105. +++ b/include/linux/mm_types.h
  106. @@ -250,6 +250,7 @@ struct vm_region {
  107. unsigned long vm_top; /* region allocated to here */
  108. unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */
  109. struct file *vm_file; /* the backing file or NULL */
  110. + struct file *vm_prfile; /* the virtual backing file or NULL */
  111. int vm_usage; /* region usage count (access under nommu_region_sem) */
  112. bool vm_icache_flushed : 1; /* true if the icache has been flushed for
  113. @@ -324,6 +325,7 @@ struct vm_area_struct {
  114. unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
  115. units */
  116. struct file * vm_file; /* File we map to (can be NULL). */
  117. + struct file *vm_prfile; /* shadow of vm_file */
  118. void * vm_private_data; /* was vm_pte (shared mem) */
  119. atomic_long_t swap_readahead_info;
  120. diff --git a/kernel/fork.c b/kernel/fork.c
  121. index 9dcd18aa210b..cf478ebb6926 100644
  122. --- a/kernel/fork.c
  123. +++ b/kernel/fork.c
  124. @@ -546,7 +546,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
  125. struct inode *inode = file_inode(file);
  126. struct address_space *mapping = file->f_mapping;
  127. - get_file(file);
  128. + vma_get_file(tmp);
  129. if (tmp->vm_flags & VM_DENYWRITE)
  130. atomic_dec(&inode->i_writecount);
  131. i_mmap_lock_write(mapping);
  132. diff --git a/mm/Makefile b/mm/Makefile
  133. index d210cc9d6f80..e77e80ce7298 100644
  134. --- a/mm/Makefile
  135. +++ b/mm/Makefile
  136. @@ -39,7 +39,7 @@ obj-y := filemap.o mempool.o oom_kill.o fadvise.o \
  137. mm_init.o mmu_context.o percpu.o slab_common.o \
  138. compaction.o vmacache.o \
  139. interval_tree.o list_lru.o workingset.o \
  140. - debug.o $(mmu-y)
  141. + prfile.o debug.o $(mmu-y)
  142. obj-y += init-mm.o
  143. obj-y += memblock.o
  144. diff --git a/mm/filemap.c b/mm/filemap.c
  145. index d78f577baef2..7dcd21948a79 100644
  146. --- a/mm/filemap.c
  147. +++ b/mm/filemap.c
  148. @@ -2764,7 +2764,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
  149. vm_fault_t ret = VM_FAULT_LOCKED;
  150. sb_start_pagefault(inode->i_sb);
  151. - file_update_time(vmf->vma->vm_file);
  152. + vma_file_update_time(vmf->vma);
  153. lock_page(page);
  154. if (page->mapping != inode->i_mapping) {
  155. unlock_page(page);
  156. diff --git a/mm/mmap.c b/mm/mmap.c
  157. index bd7b9f293b39..f3fd32a1df36 100644
  158. --- a/mm/mmap.c
  159. +++ b/mm/mmap.c
  160. @@ -181,7 +181,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
  161. if (vma->vm_ops && vma->vm_ops->close)
  162. vma->vm_ops->close(vma);
  163. if (vma->vm_file)
  164. - fput(vma->vm_file);
  165. + vma_fput(vma);
  166. mpol_put(vma_policy(vma));
  167. vm_area_free(vma);
  168. return next;
  169. @@ -930,7 +930,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
  170. if (remove_next) {
  171. if (file) {
  172. uprobe_munmap(next, next->vm_start, next->vm_end);
  173. - fput(file);
  174. + vma_fput(vma);
  175. }
  176. if (next->anon_vma)
  177. anon_vma_merge(vma, next);
  178. @@ -1846,8 +1846,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
  179. return addr;
  180. unmap_and_free_vma:
  181. + vma_fput(vma);
  182. vma->vm_file = NULL;
  183. - fput(file);
  184. /* Undo any partial mapping done by a device driver. */
  185. unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
  186. @@ -2679,7 +2679,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
  187. goto out_free_mpol;
  188. if (new->vm_file)
  189. - get_file(new->vm_file);
  190. + vma_get_file(new);
  191. if (new->vm_ops && new->vm_ops->open)
  192. new->vm_ops->open(new);
  193. @@ -2698,7 +2698,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
  194. if (new->vm_ops && new->vm_ops->close)
  195. new->vm_ops->close(new);
  196. if (new->vm_file)
  197. - fput(new->vm_file);
  198. + vma_fput(new);
  199. unlink_anon_vmas(new);
  200. out_free_mpol:
  201. mpol_put(vma_policy(new));
  202. @@ -2888,7 +2888,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
  203. struct vm_area_struct *vma;
  204. unsigned long populate = 0;
  205. unsigned long ret = -EINVAL;
  206. - struct file *file;
  207. + struct file *file, *prfile;
  208. pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.rst.\n",
  209. current->comm, current->pid);
  210. @@ -2963,10 +2963,27 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
  211. }
  212. }
  213. - file = get_file(vma->vm_file);
  214. + vma_get_file(vma);
  215. + file = vma->vm_file;
  216. + prfile = vma->vm_prfile;
  217. ret = do_mmap_pgoff(vma->vm_file, start, size,
  218. prot, flags, pgoff, &populate, NULL);
  219. + if (!IS_ERR_VALUE(ret) && file && prfile) {
  220. + struct vm_area_struct *new_vma;
  221. +
  222. + new_vma = find_vma(mm, ret);
  223. + if (!new_vma->vm_prfile)
  224. + new_vma->vm_prfile = prfile;
  225. + if (new_vma != vma)
  226. + get_file(prfile);
  227. + }
  228. + /*
  229. + * two fput()s instead of vma_fput(vma),
  230. + * coz vma may not be available anymore.
  231. + */
  232. fput(file);
  233. + if (prfile)
  234. + fput(prfile);
  235. out:
  236. up_write(&mm->mmap_sem);
  237. if (populate)
  238. @@ -3256,7 +3273,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
  239. if (anon_vma_clone(new_vma, vma))
  240. goto out_free_mempol;
  241. if (new_vma->vm_file)
  242. - get_file(new_vma->vm_file);
  243. + vma_get_file(new_vma);
  244. if (new_vma->vm_ops && new_vma->vm_ops->open)
  245. new_vma->vm_ops->open(new_vma);
  246. vma_link(mm, new_vma, prev, rb_link, rb_parent);
  247. diff --git a/mm/nommu.c b/mm/nommu.c
  248. index 749276beb109..c7957f06fbc4 100644
  249. --- a/mm/nommu.c
  250. +++ b/mm/nommu.c
  251. @@ -625,7 +625,7 @@ static void __put_nommu_region(struct vm_region *region)
  252. up_write(&nommu_region_sem);
  253. if (region->vm_file)
  254. - fput(region->vm_file);
  255. + vmr_fput(region);
  256. /* IO memory and memory shared directly out of the pagecache
  257. * from ramfs/tmpfs mustn't be released here */
  258. @@ -763,7 +763,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
  259. if (vma->vm_ops && vma->vm_ops->close)
  260. vma->vm_ops->close(vma);
  261. if (vma->vm_file)
  262. - fput(vma->vm_file);
  263. + vma_fput(vma);
  264. put_nommu_region(vma->vm_region);
  265. vm_area_free(vma);
  266. }
  267. @@ -1286,7 +1286,7 @@ unsigned long do_mmap(struct file *file,
  268. goto error_just_free;
  269. }
  270. }
  271. - fput(region->vm_file);
  272. + vmr_fput(region);
  273. kmem_cache_free(vm_region_jar, region);
  274. region = pregion;
  275. result = start;
  276. @@ -1361,10 +1361,10 @@ unsigned long do_mmap(struct file *file,
  277. up_write(&nommu_region_sem);
  278. error:
  279. if (region->vm_file)
  280. - fput(region->vm_file);
  281. + vmr_fput(region);
  282. kmem_cache_free(vm_region_jar, region);
  283. if (vma->vm_file)
  284. - fput(vma->vm_file);
  285. + vma_fput(vma);
  286. vm_area_free(vma);
  287. return ret;
  288. diff --git a/mm/prfile.c b/mm/prfile.c
  289. new file mode 100644
  290. index 000000000000..024cdcfae1b1
  291. --- /dev/null
  292. +++ b/mm/prfile.c
  293. @@ -0,0 +1,86 @@
  294. +// SPDX-License-Identifier: GPL-2.0
  295. +/*
  296. + * Mainly for aufs which mmap(2) different file and wants to print different
  297. + * path in /proc/PID/maps.
  298. + * Call these functions via macros defined in linux/mm.h.
  299. + *
  300. + * See Documentation/filesystems/aufs/design/06mmap.txt
  301. + *
  302. + * Copyright (c) 2014-2019 Junjro R. Okajima
  303. + * Copyright (c) 2014 Ian Campbell
  304. + */
  305. +
  306. +#include <linux/mm.h>
  307. +#include <linux/file.h>
  308. +#include <linux/fs.h>
  309. +
  310. +/* #define PRFILE_TRACE */
  311. +static inline void prfile_trace(struct file *f, struct file *pr,
  312. + const char func[], int line, const char func2[])
  313. +{
  314. +#ifdef PRFILE_TRACE
  315. + if (pr)
  316. + pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
  317. +#endif
  318. +}
  319. +
  320. +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
  321. + int line)
  322. +{
  323. + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
  324. +
  325. + prfile_trace(f, pr, func, line, __func__);
  326. + file_update_time(f);
  327. + if (f && pr)
  328. + file_update_time(pr);
  329. +}
  330. +
  331. +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
  332. + int line)
  333. +{
  334. + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
  335. +
  336. + prfile_trace(f, pr, func, line, __func__);
  337. + return (f && pr) ? pr : f;
  338. +}
  339. +
  340. +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
  341. +{
  342. + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
  343. +
  344. + prfile_trace(f, pr, func, line, __func__);
  345. + get_file(f);
  346. + if (f && pr)
  347. + get_file(pr);
  348. +}
  349. +
  350. +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
  351. +{
  352. + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
  353. +
  354. + prfile_trace(f, pr, func, line, __func__);
  355. + fput(f);
  356. + if (f && pr)
  357. + fput(pr);
  358. +}
  359. +
  360. +#ifndef CONFIG_MMU
  361. +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
  362. + int line)
  363. +{
  364. + struct file *f = region->vm_file, *pr = region->vm_prfile;
  365. +
  366. + prfile_trace(f, pr, func, line, __func__);
  367. + return (f && pr) ? pr : f;
  368. +}
  369. +
  370. +void vmr_do_fput(struct vm_region *region, const char func[], int line)
  371. +{
  372. + struct file *f = region->vm_file, *pr = region->vm_prfile;
  373. +
  374. + prfile_trace(f, pr, func, line, __func__);
  375. + fput(f);
  376. + if (f && pr)
  377. + fput(pr);
  378. +}
  379. +#endif /* !CONFIG_MMU */