aufs5-mmap.patch 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 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 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 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-$(CONFIG_AUFS_FS:m=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..3f9d6d155171 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. @@ -1873,7 +1873,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
  195. return addr;
  196. unmap_and_free_vma:
  197. - fput(vma->vm_file);
  198. + vma_fput(vma);
  199. vma->vm_file = NULL;
  200. /* Undo any partial mapping done by a device driver. */
  201. @@ -2723,7 +2723,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
  202. goto out_free_mpol;
  203. if (new->vm_file)
  204. - get_file(new->vm_file);
  205. + vma_get_file(new);
  206. if (new->vm_ops && new->vm_ops->open)
  207. new->vm_ops->open(new);
  208. @@ -2742,7 +2742,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
  209. if (new->vm_ops && new->vm_ops->close)
  210. new->vm_ops->close(new);
  211. if (new->vm_file)
  212. - fput(new->vm_file);
  213. + vma_fput(new);
  214. unlink_anon_vmas(new);
  215. out_free_mpol:
  216. mpol_put(vma_policy(new));
  217. @@ -2938,6 +2938,9 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
  218. unsigned long populate = 0;
  219. unsigned long ret = -EINVAL;
  220. struct file *file;
  221. +#if IS_ENABLED(CONFIG_AUFS_FS)
  222. + struct file *prfile;
  223. +#endif
  224. pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.rst.\n",
  225. current->comm, current->pid);
  226. @@ -2993,10 +2996,34 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
  227. if (vma->vm_flags & VM_LOCKED)
  228. flags |= MAP_LOCKED;
  229. +#if IS_ENABLED(CONFIG_AUFS_FS)
  230. + vma_get_file(vma);
  231. + file = vma->vm_file;
  232. + prfile = vma->vm_prfile;
  233. + ret = do_mmap(vma->vm_file, start, size,
  234. + prot, flags, pgoff, &populate, NULL);
  235. + if (!IS_ERR_VALUE(ret) && file && prfile) {
  236. + struct vm_area_struct *new_vma;
  237. +
  238. + new_vma = find_vma(mm, ret);
  239. + if (!new_vma->vm_prfile)
  240. + new_vma->vm_prfile = prfile;
  241. + if (new_vma != vma)
  242. + get_file(prfile);
  243. + }
  244. + /*
  245. + * two fput()s instead of vma_fput(vma),
  246. + * coz vma may not be available anymore.
  247. + */
  248. + fput(file);
  249. + if (prfile)
  250. + fput(prfile);
  251. +#else
  252. file = get_file(vma->vm_file);
  253. ret = do_mmap(vma->vm_file, start, size,
  254. prot, flags, pgoff, &populate, NULL);
  255. fput(file);
  256. +#endif /* CONFIG_AUFS_FS */
  257. out:
  258. mmap_write_unlock(mm);
  259. if (populate)
  260. @@ -3277,7 +3304,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
  261. if (anon_vma_clone(new_vma, vma))
  262. goto out_free_mempol;
  263. if (new_vma->vm_file)
  264. - get_file(new_vma->vm_file);
  265. + vma_get_file(new_vma);
  266. if (new_vma->vm_ops && new_vma->vm_ops->open)
  267. new_vma->vm_ops->open(new_vma);
  268. vma_link(mm, new_vma, prev, rb_link, rb_parent);
  269. diff --git a/mm/nommu.c b/mm/nommu.c
  270. index 02d2427b8f9e..a7419302ab4e 100644
  271. --- a/mm/nommu.c
  272. +++ b/mm/nommu.c
  273. @@ -523,7 +523,7 @@ static void __put_nommu_region(struct vm_region *region)
  274. up_write(&nommu_region_sem);
  275. if (region->vm_file)
  276. - fput(region->vm_file);
  277. + vmr_fput(region);
  278. /* IO memory and memory shared directly out of the pagecache
  279. * from ramfs/tmpfs mustn't be released here */
  280. @@ -655,7 +655,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
  281. if (vma->vm_ops && vma->vm_ops->close)
  282. vma->vm_ops->close(vma);
  283. if (vma->vm_file)
  284. - fput(vma->vm_file);
  285. + vma_fput(vma);
  286. put_nommu_region(vma->vm_region);
  287. vm_area_free(vma);
  288. }
  289. @@ -1175,7 +1175,7 @@ unsigned long do_mmap(struct file *file,
  290. goto error_just_free;
  291. }
  292. }
  293. - fput(region->vm_file);
  294. + vmr_fput(region);
  295. kmem_cache_free(vm_region_jar, region);
  296. region = pregion;
  297. result = start;
  298. @@ -1252,10 +1252,10 @@ unsigned long do_mmap(struct file *file,
  299. up_write(&nommu_region_sem);
  300. error:
  301. if (region->vm_file)
  302. - fput(region->vm_file);
  303. + vmr_fput(region);
  304. kmem_cache_free(vm_region_jar, region);
  305. if (vma->vm_file)
  306. - fput(vma->vm_file);
  307. + vma_fput(vma);
  308. vm_area_free(vma);
  309. return ret;
  310. diff --git a/mm/prfile.c b/mm/prfile.c
  311. new file mode 100644
  312. index 000000000000..511543ab1b41
  313. --- /dev/null
  314. +++ b/mm/prfile.c
  315. @@ -0,0 +1,86 @@
  316. +// SPDX-License-Identifier: GPL-2.0
  317. +/*
  318. + * Mainly for aufs which mmap(2) different file and wants to print different
  319. + * path in /proc/PID/maps.
  320. + * Call these functions via macros defined in linux/mm.h.
  321. + *
  322. + * See Documentation/filesystems/aufs/design/06mmap.txt
  323. + *
  324. + * Copyright (c) 2014-2021 Junjro R. Okajima
  325. + * Copyright (c) 2014 Ian Campbell
  326. + */
  327. +
  328. +#include <linux/mm.h>
  329. +#include <linux/file.h>
  330. +#include <linux/fs.h>
  331. +
  332. +/* #define PRFILE_TRACE */
  333. +static inline void prfile_trace(struct file *f, struct file *pr,
  334. + const char func[], int line, const char func2[])
  335. +{
  336. +#ifdef PRFILE_TRACE
  337. + if (pr)
  338. + pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
  339. +#endif
  340. +}
  341. +
  342. +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
  343. + int line)
  344. +{
  345. + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
  346. +
  347. + prfile_trace(f, pr, func, line, __func__);
  348. + file_update_time(f);
  349. + if (f && pr)
  350. + file_update_time(pr);
  351. +}
  352. +
  353. +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
  354. + int line)
  355. +{
  356. + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
  357. +
  358. + prfile_trace(f, pr, func, line, __func__);
  359. + return (f && pr) ? pr : f;
  360. +}
  361. +
  362. +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
  363. +{
  364. + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
  365. +
  366. + prfile_trace(f, pr, func, line, __func__);
  367. + get_file(f);
  368. + if (f && pr)
  369. + get_file(pr);
  370. +}
  371. +
  372. +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
  373. +{
  374. + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
  375. +
  376. + prfile_trace(f, pr, func, line, __func__);
  377. + fput(f);
  378. + if (f && pr)
  379. + fput(pr);
  380. +}
  381. +
  382. +#ifndef CONFIG_MMU
  383. +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
  384. + int line)
  385. +{
  386. + struct file *f = region->vm_file, *pr = region->vm_prfile;
  387. +
  388. + prfile_trace(f, pr, func, line, __func__);
  389. + return (f && pr) ? pr : f;
  390. +}
  391. +
  392. +void vmr_do_fput(struct vm_region *region, const char func[], int line)
  393. +{
  394. + struct file *f = region->vm_file, *pr = region->vm_prfile;
  395. +
  396. + prfile_trace(f, pr, func, line, __func__);
  397. + fput(f);
  398. + if (f && pr)
  399. + fput(pr);
  400. +}
  401. +#endif /* !CONFIG_MMU */