aufs5-mmap.patch 12.4 KB
Newer Older
J. R. Okajima's avatar
J. R. Okajima committed
1
2
3
4
SPDX-License-Identifier: GPL-2.0
aufs5.x-rcN mmap patch

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