wok-current view linux/stuff/aufs4-mmap.patch @ rev 25667

Up kernel to 5.10.210 and aufs5
author Stanislas Leduc <shann@slitaz.org>
date Sat Mar 02 17:27:56 2024 +0000 (4 months ago)
parents
children
line source
1 SPDX-License-Identifier: GPL-2.0
2 aufs4.19 mmap patch
4 diff --git a/fs/proc/base.c b/fs/proc/base.c
5 index 7e9f07bf260d2..3ab59011067ee 100644
6 --- a/fs/proc/base.c
7 +++ b/fs/proc/base.c
8 @@ -2016,7 +2016,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
9 down_read(&mm->mmap_sem);
10 vma = find_exact_vma(mm, vm_start, vm_end);
11 if (vma && vma->vm_file) {
12 - *path = vma->vm_file->f_path;
13 + *path = vma_pr_or_file(vma)->f_path;
14 path_get(path);
15 rc = 0;
16 }
17 diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
18 index 3b63be64e4364..fb9913bf3d10d 100644
19 --- a/fs/proc/nommu.c
20 +++ b/fs/proc/nommu.c
21 @@ -45,7 +45,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
22 file = region->vm_file;
24 if (file) {
25 - struct inode *inode = file_inode(region->vm_file);
26 + struct inode *inode;
27 +
28 + file = vmr_pr_or_file(region);
29 + inode = file_inode(file);
30 dev = inode->i_sb->s_dev;
31 ino = inode->i_ino;
32 }
33 diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
34 index 5ea1d64cb0b4c..7865a4707d233 100644
35 --- a/fs/proc/task_mmu.c
36 +++ b/fs/proc/task_mmu.c
37 @@ -305,7 +305,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
38 const char *name = NULL;
40 if (file) {
41 - struct inode *inode = file_inode(vma->vm_file);
42 + struct inode *inode;
43 +
44 + file = vma_pr_or_file(vma);
45 + inode = file_inode(file);
46 dev = inode->i_sb->s_dev;
47 ino = inode->i_ino;
48 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
49 @@ -1727,7 +1730,7 @@ static int show_numa_map(struct seq_file *m, void *v)
50 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
51 struct vm_area_struct *vma = v;
52 struct numa_maps *md = &numa_priv->md;
53 - struct file *file = vma->vm_file;
54 + struct file *file = vma_pr_or_file(vma);
55 struct mm_struct *mm = vma->vm_mm;
56 struct mm_walk walk = {
57 .hugetlb_entry = gather_hugetlb_stats,
58 diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
59 index 0b63d68dedb2a..400d1c594ceb3 100644
60 --- a/fs/proc/task_nommu.c
61 +++ b/fs/proc/task_nommu.c
62 @@ -155,7 +155,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
63 file = vma->vm_file;
65 if (file) {
66 - struct inode *inode = file_inode(vma->vm_file);
67 + struct inode *inode;
68 +
69 + file = vma_pr_or_file(vma);
70 + inode = file_inode(file);
71 dev = inode->i_sb->s_dev;
72 ino = inode->i_ino;
73 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
74 diff --git a/include/linux/mm.h b/include/linux/mm.h
75 index 0416a7204be37..4a298a92681bb 100644
76 --- a/include/linux/mm.h
77 +++ b/include/linux/mm.h
78 @@ -1440,6 +1440,28 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
79 unmap_mapping_range(mapping, holebegin, holelen, 0);
80 }
82 +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
83 +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
84 + int);
85 +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
86 +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
87 +
88 +#define vma_file_update_time(vma) vma_do_file_update_time(vma, __func__, \
89 + __LINE__)
90 +#define vma_pr_or_file(vma) vma_do_pr_or_file(vma, __func__, \
91 + __LINE__)
92 +#define vma_get_file(vma) vma_do_get_file(vma, __func__, __LINE__)
93 +#define vma_fput(vma) vma_do_fput(vma, __func__, __LINE__)
94 +
95 +#ifndef CONFIG_MMU
96 +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
97 +extern void vmr_do_fput(struct vm_region *, const char[], int);
98 +
99 +#define vmr_pr_or_file(region) vmr_do_pr_or_file(region, __func__, \
100 + __LINE__)
101 +#define vmr_fput(region) vmr_do_fput(region, __func__, __LINE__)
102 +#endif /* !CONFIG_MMU */
103 +
104 extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
105 void *buf, int len, unsigned int gup_flags);
106 extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
107 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
108 index 5ed8f6292a533..01229754077f6 100644
109 --- a/include/linux/mm_types.h
110 +++ b/include/linux/mm_types.h
111 @@ -239,6 +239,7 @@ struct vm_region {
112 unsigned long vm_top; /* region allocated to here */
113 unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */
114 struct file *vm_file; /* the backing file or NULL */
115 + struct file *vm_prfile; /* the virtual backing file or NULL */
117 int vm_usage; /* region usage count (access under nommu_region_sem) */
118 bool vm_icache_flushed : 1; /* true if the icache has been flushed for
119 @@ -313,6 +314,7 @@ struct vm_area_struct {
120 unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
121 units */
122 struct file * vm_file; /* File we map to (can be NULL). */
123 + struct file *vm_prfile; /* shadow of vm_file */
124 void * vm_private_data; /* was vm_pte (shared mem) */
126 atomic_long_t swap_readahead_info;
127 diff --git a/kernel/fork.c b/kernel/fork.c
128 index f0b58479534f0..fa562c364d020 100644
129 --- a/kernel/fork.c
130 +++ b/kernel/fork.c
131 @@ -505,7 +505,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
132 struct inode *inode = file_inode(file);
133 struct address_space *mapping = file->f_mapping;
135 - get_file(file);
136 + vma_get_file(tmp);
137 if (tmp->vm_flags & VM_DENYWRITE)
138 atomic_dec(&inode->i_writecount);
139 i_mmap_lock_write(mapping);
140 diff --git a/mm/Makefile b/mm/Makefile
141 index 26ef77a3883b5..b2869af1ef08e 100644
142 --- a/mm/Makefile
143 +++ b/mm/Makefile
144 @@ -39,7 +39,7 @@ obj-y := filemap.o mempool.o oom_kill.o fadvise.o \
145 mm_init.o mmu_context.o percpu.o slab_common.o \
146 compaction.o vmacache.o \
147 interval_tree.o list_lru.o workingset.o \
148 - debug.o $(mmu-y)
149 + prfile.o debug.o $(mmu-y)
151 obj-y += init-mm.o
153 diff --git a/mm/filemap.c b/mm/filemap.c
154 index 52517f28e6f4a..250f675dcfb28 100644
155 --- a/mm/filemap.c
156 +++ b/mm/filemap.c
157 @@ -2700,7 +2700,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
158 vm_fault_t ret = VM_FAULT_LOCKED;
160 sb_start_pagefault(inode->i_sb);
161 - file_update_time(vmf->vma->vm_file);
162 + vma_file_update_time(vmf->vma);
163 lock_page(page);
164 if (page->mapping != inode->i_mapping) {
165 unlock_page(page);
166 diff --git a/mm/mmap.c b/mm/mmap.c
167 index f7cd9cb966c0f..515e88a194bd0 100644
168 --- a/mm/mmap.c
169 +++ b/mm/mmap.c
170 @@ -180,7 +180,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
171 if (vma->vm_ops && vma->vm_ops->close)
172 vma->vm_ops->close(vma);
173 if (vma->vm_file)
174 - fput(vma->vm_file);
175 + vma_fput(vma);
176 mpol_put(vma_policy(vma));
177 vm_area_free(vma);
178 return next;
179 @@ -905,7 +905,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
180 if (remove_next) {
181 if (file) {
182 uprobe_munmap(next, next->vm_start, next->vm_end);
183 - fput(file);
184 + vma_fput(vma);
185 }
186 if (next->anon_vma)
187 anon_vma_merge(vma, next);
188 @@ -1821,8 +1821,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
189 return addr;
191 unmap_and_free_vma:
192 + vma_fput(vma);
193 vma->vm_file = NULL;
194 - fput(file);
196 /* Undo any partial mapping done by a device driver. */
197 unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
198 @@ -2641,7 +2641,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
199 goto out_free_mpol;
201 if (new->vm_file)
202 - get_file(new->vm_file);
203 + vma_get_file(new);
205 if (new->vm_ops && new->vm_ops->open)
206 new->vm_ops->open(new);
207 @@ -2660,7 +2660,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
208 if (new->vm_ops && new->vm_ops->close)
209 new->vm_ops->close(new);
210 if (new->vm_file)
211 - fput(new->vm_file);
212 + vma_fput(new);
213 unlink_anon_vmas(new);
214 out_free_mpol:
215 mpol_put(vma_policy(new));
216 @@ -2822,7 +2822,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
217 struct vm_area_struct *vma;
218 unsigned long populate = 0;
219 unsigned long ret = -EINVAL;
220 - struct file *file;
221 + struct file *file, *prfile;
223 pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.rst.\n",
224 current->comm, current->pid);
225 @@ -2897,10 +2897,27 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
226 }
227 }
229 - file = get_file(vma->vm_file);
230 + vma_get_file(vma);
231 + file = vma->vm_file;
232 + prfile = vma->vm_prfile;
233 ret = do_mmap_pgoff(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 out:
252 up_write(&mm->mmap_sem);
253 if (populate)
254 @@ -3206,7 +3223,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
255 if (anon_vma_clone(new_vma, vma))
256 goto out_free_mempol;
257 if (new_vma->vm_file)
258 - get_file(new_vma->vm_file);
259 + vma_get_file(new_vma);
260 if (new_vma->vm_ops && new_vma->vm_ops->open)
261 new_vma->vm_ops->open(new_vma);
262 vma_link(mm, new_vma, prev, rb_link, rb_parent);
263 diff --git a/mm/nommu.c b/mm/nommu.c
264 index e4aac33216aec..83bfcd1528a00 100644
265 --- a/mm/nommu.c
266 +++ b/mm/nommu.c
267 @@ -625,7 +625,7 @@ static void __put_nommu_region(struct vm_region *region)
268 up_write(&nommu_region_sem);
270 if (region->vm_file)
271 - fput(region->vm_file);
272 + vmr_fput(region);
274 /* IO memory and memory shared directly out of the pagecache
275 * from ramfs/tmpfs mustn't be released here */
276 @@ -763,7 +763,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
277 if (vma->vm_ops && vma->vm_ops->close)
278 vma->vm_ops->close(vma);
279 if (vma->vm_file)
280 - fput(vma->vm_file);
281 + vma_fput(vma);
282 put_nommu_region(vma->vm_region);
283 vm_area_free(vma);
284 }
285 @@ -1286,7 +1286,7 @@ unsigned long do_mmap(struct file *file,
286 goto error_just_free;
287 }
288 }
289 - fput(region->vm_file);
290 + vmr_fput(region);
291 kmem_cache_free(vm_region_jar, region);
292 region = pregion;
293 result = start;
294 @@ -1361,10 +1361,10 @@ unsigned long do_mmap(struct file *file,
295 up_write(&nommu_region_sem);
296 error:
297 if (region->vm_file)
298 - fput(region->vm_file);
299 + vmr_fput(region);
300 kmem_cache_free(vm_region_jar, region);
301 if (vma->vm_file)
302 - fput(vma->vm_file);
303 + vma_fput(vma);
304 vm_area_free(vma);
305 return ret;
307 diff --git a/mm/prfile.c b/mm/prfile.c
308 new file mode 100644
309 index 0000000000000..00d51187c3250
310 --- /dev/null
311 +++ b/mm/prfile.c
312 @@ -0,0 +1,86 @@
313 +// SPDX-License-Identifier: GPL-2.0
314 +/*
315 + * Mainly for aufs which mmap(2) different file and wants to print different
316 + * path in /proc/PID/maps.
317 + * Call these functions via macros defined in linux/mm.h.
318 + *
319 + * See Documentation/filesystems/aufs/design/06mmap.txt
320 + *
321 + * Copyright (c) 2014-2020 Junjro R. Okajima
322 + * Copyright (c) 2014 Ian Campbell
323 + */
324 +
325 +#include <linux/mm.h>
326 +#include <linux/file.h>
327 +#include <linux/fs.h>
328 +
329 +/* #define PRFILE_TRACE */
330 +static inline void prfile_trace(struct file *f, struct file *pr,
331 + const char func[], int line, const char func2[])
332 +{
333 +#ifdef PRFILE_TRACE
334 + if (pr)
335 + pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
336 +#endif
337 +}
338 +
339 +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
340 + int line)
341 +{
342 + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
343 +
344 + prfile_trace(f, pr, func, line, __func__);
345 + file_update_time(f);
346 + if (f && pr)
347 + file_update_time(pr);
348 +}
349 +
350 +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
351 + int line)
352 +{
353 + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
354 +
355 + prfile_trace(f, pr, func, line, __func__);
356 + return (f && pr) ? pr : f;
357 +}
358 +
359 +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
360 +{
361 + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
362 +
363 + prfile_trace(f, pr, func, line, __func__);
364 + get_file(f);
365 + if (f && pr)
366 + get_file(pr);
367 +}
368 +
369 +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
370 +{
371 + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
372 +
373 + prfile_trace(f, pr, func, line, __func__);
374 + fput(f);
375 + if (f && pr)
376 + fput(pr);
377 +}
378 +
379 +#ifndef CONFIG_MMU
380 +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
381 + int line)
382 +{
383 + struct file *f = region->vm_file, *pr = region->vm_prfile;
384 +
385 + prfile_trace(f, pr, func, line, __func__);
386 + return (f && pr) ? pr : f;
387 +}
388 +
389 +void vmr_do_fput(struct vm_region *region, const char func[], int line)
390 +{
391 + struct file *f = region->vm_file, *pr = region->vm_prfile;
392 +
393 + prfile_trace(f, pr, func, line, __func__);
394 + fput(f);
395 + if (f && pr)
396 + fput(pr);
397 +}
398 +#endif /* !CONFIG_MMU */