fs: create helper file_user_path() for user displayed mapped file path
authorAmir Goldstein <amir73il@gmail.com>
Mon, 9 Oct 2023 15:37:11 +0000 (18:37 +0300)
committerChristian Brauner <brauner@kernel.org>
Thu, 19 Oct 2023 09:03:15 +0000 (11:03 +0200)
Overlayfs uses backing files with "fake" overlayfs f_path and "real"
underlying f_inode, in order to use underlying inode aops for mapped
files and to display the overlayfs path in /proc/<pid>/maps.

In preparation for storing the overlayfs "fake" path instead of the
underlying "real" path in struct backing_file, define a noop helper
file_user_path() that returns f_path for now.

Use the new helper in procfs and kernel logs whenever a path of a
mapped file is displayed to users.

Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Link: https://lore.kernel.org/r/20231009153712.1566422-3-amir73il@gmail.com
Signed-off-by: Christian Brauner <brauner@kernel.org>
arch/arc/kernel/troubleshoot.c
fs/proc/base.c
fs/proc/nommu.c
fs/proc/task_mmu.c
fs/proc/task_nommu.c
include/linux/fs.h
kernel/trace/trace_output.c

index d5b3ed2c58f5030ab8022ac260b7ce8e172c2fa7..c380d8c30704db529a6a9e8ef7d5001ba83a0d7a 100644 (file)
@@ -90,10 +90,12 @@ static void show_faulting_vma(unsigned long address)
         */
        if (vma) {
                char buf[ARC_PATH_MAX];
-               char *nm = "?";
+               char *nm = "anon";
 
                if (vma->vm_file) {
-                       nm = file_path(vma->vm_file, buf, ARC_PATH_MAX-1);
+                       /* XXX: can we use %pD below and get rid of buf? */
+                       nm = d_path(file_user_path(vma->vm_file), buf,
+                                   ARC_PATH_MAX-1);
                        if (IS_ERR(nm))
                                nm = "?";
                }
index ffd54617c35478e92a9f6bef67013e16e6cd3183..20695c928ee69ee95cf9531372fd60d90f4b3df2 100644 (file)
@@ -2218,7 +2218,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
        rc = -ENOENT;
        vma = find_exact_vma(mm, vm_start, vm_end);
        if (vma && vma->vm_file) {
-               *path = vma->vm_file->f_path;
+               *path = *file_user_path(vma->vm_file);
                path_get(path);
                rc = 0;
        }
index 4d3493579458f04e6ede75b24cd605ca2905fabe..c6e7ebc637562d80271ec84181dcbfa812e94518 100644 (file)
@@ -58,7 +58,7 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
 
        if (file) {
                seq_pad(m, ' ');
-               seq_file_path(m, file, "");
+               seq_path(m, file_user_path(file), "");
        }
 
        seq_putc(m, '\n');
index 3dd5be96691b4cc234454353ec1b8a324760e0c2..1593940ca01ee5ca20d84cc2da74e7b13aea512d 100644 (file)
@@ -296,7 +296,7 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
                if (anon_name)
                        seq_printf(m, "[anon_shmem:%s]", anon_name->name);
                else
-                       seq_file_path(m, file, "\n");
+                       seq_path(m, file_user_path(file), "\n");
                goto done;
        }
 
@@ -1967,7 +1967,7 @@ static int show_numa_map(struct seq_file *m, void *v)
 
        if (file) {
                seq_puts(m, " file=");
-               seq_file_path(m, file, "\n\t= ");
+               seq_path(m, file_user_path(file), "\n\t= ");
        } else if (vma_is_initial_heap(vma)) {
                seq_puts(m, " heap");
        } else if (vma_is_initial_stack(vma)) {
index a8ac0dd8041ebca2178ab245c6337a3b9db2cbe6..2d5862c13399b71b58f695a477b69d71e3fd5077 100644 (file)
@@ -157,7 +157,7 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
 
        if (file) {
                seq_pad(m, ' ');
-               seq_file_path(m, file, "");
+               seq_path(m, file_user_path(file), "");
        } else if (mm && vma_is_initial_stack(vma)) {
                seq_pad(m, ' ');
                seq_puts(m, "[stack]");
index ceafc40cc25fbdaa1b66f70823c4ac51518ce354..057a3bbd4d275b8019ba217d3fc35e14751dbaf4 100644 (file)
@@ -2513,6 +2513,20 @@ static inline const struct path *file_real_path(struct file *f)
        return &f->f_path;
 }
 
+/*
+ * file_user_path - get the path to display for memory mapped file
+ *
+ * When mmapping a file on a stackable filesystem (e.g., overlayfs), the file
+ * stored in ->vm_file is a backing file whose f_inode is on the underlying
+ * filesystem.  When the mapped file path is displayed to user (e.g. via
+ * /proc/<pid>/maps), this helper should be used to get the path to display
+ * to the user, which is the path of the fd that user has requested to map.
+ */
+static inline const struct path *file_user_path(struct file *f)
+{
+       return &f->f_path;
+}
+
 static inline struct file *file_clone_open(struct file *file)
 {
        return dentry_open(&file->f_path, file->f_flags, file->f_cred);
index db575094c4982598751690634c83ba160d1016df..d8b302d0108302d9ef2debe735c4b7778a217f90 100644 (file)
@@ -404,7 +404,7 @@ static int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm,
                        vmstart = vma->vm_start;
                }
                if (file) {
-                       ret = trace_seq_path(s, &file->f_path);
+                       ret = trace_seq_path(s, file_user_path(file));
                        if (ret)
                                trace_seq_printf(s, "[+0x%lx]",
                                                 ip - vmstart);