From patchwork Thu Dec 14 06:44:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrei Vagin X-Patchwork-Id: 13492440 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="onHZnr5/" Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED6CAB9 for ; Wed, 13 Dec 2023 22:44:45 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-dbcdf587bd6so839098276.0 for ; Wed, 13 Dec 2023 22:44:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702536285; x=1703141085; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=Iko8D140eCOFohGA7VQkAes9lkzKAM1WQZ+wO/F/i90=; b=onHZnr5/C76u4WAUhTwmVougnLzUmAv+9rDMgFylCd+MXirdjLsJk5w4yOzvVXOhvZ /g4AgkKjfdC4HOcRAfYmhcxNmGi3z1q/mYVKugEQoEdI+7cnCh7BmqUhiWcm8wpQTLcF GDT2D35fDRC7k+3WdpAsL9WLmtbgEHqanb6JgPQZ0sZ8+55pWZS6XUdLfH6ROB7cQfyd w0u++OgNvwHPTjjoX1OQjlA4bDe4H0Zmmhzgk29gtNea1O5l1h5dYZ1fvhwCxdBxmqn3 JjXg/r5fGVlmG2oXfsz1jwtMFdIAoJmAPCXVcBS8d18RwzjpOgr8RybW6a+nAXArzaXU kgRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702536285; x=1703141085; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=Iko8D140eCOFohGA7VQkAes9lkzKAM1WQZ+wO/F/i90=; b=VqG6Loojme+OcX5F8GK47X4V8wx+CBnQ5+Sz91F2ONnxVsFciM0hOC5bMaL3kc6hoZ KaaHtJ4UT0Rr6QHqdRrny508xcPFT4ezAtiJIqr2phC/a56cFf6lmkLRn9uE15zfkeZC K+C9mZz0mPg+qufuW6Ke54X6dxdMtpipnqy7q2WR7kOr66w5MT+Cx9tpXu9Be42NX5pF vL5tAaYvznn2bIl/iwzwrmsLMyEt1ejKdyedmlrE/G4MJeng4QiA+AE8Awm0s99aChaJ 3U6fzlxoeskiyqOkr+9+ZVSAg+Y7ybzPelXl6beG5YcXcdATWwFLXkubzpbToe677bp9 0KBw== X-Gm-Message-State: AOJu0YzZJY66hj5NVI+d6rPvZFkuja3UPFP/GNKLqRq5zLepZLmBkfp1 VmYCV4mNLi9h8HENeQ9ZT/mDkREwL/U= X-Google-Smtp-Source: AGHT+IG0bWK9d5BDpYNZYAfiffy36nbfNQQS2AsIHHcETXtjO30MXnPlbJFmNayBbGuGgdo4DBpN1u3Fs3I= X-Received: from avagin.kir.corp.google.com ([2620:0:1008:10:e986:a7c7:2814:c9a8]) (user=avagin job=sendgmr) by 2002:a25:2d2:0:b0:dbc:ca4d:4c1 with SMTP id 201-20020a2502d2000000b00dbcca4d04c1mr32671ybc.11.1702536285111; Wed, 13 Dec 2023 22:44:45 -0800 (PST) Date: Wed, 13 Dec 2023 22:44:38 -0800 Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231214064439.1023011-1-avagin@google.com> Subject: [PATCH 1/2 v2] fs/proc: show correct device and inode numbers in /proc/pid/maps From: Andrei Vagin To: Andrew Morton , Amir Goldstein Cc: linux-kernel@vger.kernel.org, Alexander Mikhalitsyn , Christian Brauner , Alexander Viro , linux-fsdevel@vger.kernel.org, overlayfs , Miklos Szeredi , Andrei Vagin /proc/pid/maps shows device and inode numbers of vma->vm_file-s. Here is an issue. If a mapped file is on a stackable file system (e.g., overlayfs), vma->vm_file is a backing file whose f_inode is on the underlying filesystem. To show correct numbers, we need to get a user file and shows its numbers. The same trick is used to show file paths in /proc/pid/maps. Cc: Alexander Mikhalitsyn Suggested-by: Amir Goldstein Signed-off-by: Andrei Vagin Reviewed-by: Amir Goldstein --- v2: Amir explained that vfs_getattr isn't needed, because file_user_inode(vma->vm_file).i_ino always matches an inode number returned by statx. fs/proc/task_mmu.c | 3 ++- include/linux/fs.h | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 435b61054b5b..1801e409a061 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -273,7 +273,8 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma) const char *name = NULL; if (file) { - struct inode *inode = file_inode(vma->vm_file); + const struct inode *inode = file_user_inode(vma->vm_file); + dev = inode->i_sb->s_dev; ino = inode->i_ino; pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT; diff --git a/include/linux/fs.h b/include/linux/fs.h index 98b7a7a8c42e..838ccfc63323 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2523,20 +2523,28 @@ struct file *backing_file_open(const struct path *user_path, int flags, struct path *backing_file_user_path(struct file *f); /* - * 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//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. + * filesystem. When the mapped file path and inode number are displayed to + * user (e.g. via /proc//maps), these helpers should be used to get the + * path and inode number to display to the user, which is the path of the fd + * that user has requested to map and the inode number that would be returned + * by fstat() on that same fd. */ +/* Get the path to display in /proc//maps */ static inline const struct path *file_user_path(struct file *f) { if (unlikely(f->f_mode & FMODE_BACKING)) return backing_file_user_path(f); return &f->f_path; } +/* Get the inode whose inode number to display in /proc//maps */ +static inline const struct inode *file_user_inode(struct file *f) +{ + if (unlikely(f->f_mode & FMODE_BACKING)) + return d_inode(backing_file_user_path(f)->dentry); + return file_inode(f); +} static inline struct file *file_clone_open(struct file *file) { From patchwork Thu Dec 14 06:44:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrei Vagin X-Patchwork-Id: 13492441 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Fpi9gaNf" Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E452710F for ; Wed, 13 Dec 2023 22:44:47 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-dbcd8f64549so1307301276.3 for ; Wed, 13 Dec 2023 22:44:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702536287; x=1703141087; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Pi0n1hV1ncb/RNwMZFnyh9hgwMGCToBH7DtU0L8OSxs=; b=Fpi9gaNfboAVvku+rZdlb6aNhnJC1QklaxRwMgYroM8zyOeRa4oVbquxJ9+D9Jd75Y 5zkH9BQSc1Pa7nJvouWG15HZZ0bPzIfYvNQLlgppVgDr1wNqpk8JhFjN7NwD3oD7qrjq JTTqXpR1LYvTXl3HzychB1GMrxm7g5FosJtfWM1Y8v68G/u5a6VcMqVQ9tV3CHNm/R0/ /SNgBosZt/tKLbF7T9Jg+ACMiP1gUM0vVttUzkvyY2WjuzBcY5Rj/SgVbgPOVG1nySli Z85whCyXr3Spp+eASR8pzL97psnN2ssvEv7iOnnjOrN8lGTzJzoidKHJOHd2ndAQUGCi 46zQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702536287; x=1703141087; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Pi0n1hV1ncb/RNwMZFnyh9hgwMGCToBH7DtU0L8OSxs=; b=v+DRs6EItL/xr3T2rQRcU0mhRjWOOHSXXkAX7zZiu6BR1Xq9+4aI7ubtQLjZamiwP3 bB7B3QmXVw71yLMXhGy6lWuEioV/rlHYi/M1Rro3FChAJQyM1zH3AntvVHCY/dfFc6r8 Gg+2AhUNDzthR5d0BSwJ4Z7zXeAKr3hCnHJ90X0v4gRJztBD5ySNt4D4hN8gDjDX6bsD t8aYHOb+BFY7X0NAvrXNeFf5+bpVffKZIkNGt+LI5GVQyXrjiIQuMKbbAHBYK8mK2093 k1Mb2WN95xBzO93+aCSUTIfmF/Jy2nD7V80wiiPnD5WhWVOsOg40Tt91Ssv26+U019Xc qdbw== X-Gm-Message-State: AOJu0YygxHaCXsDMhvgARd/0/o0Wjlhz5iduW+mP0yuxFs6OemyH40Fh VnN6sRIZP1V/seaLXhjYujH2fZ/Il2s= X-Google-Smtp-Source: AGHT+IFIcGL80lHU9a3QBXcN8ajJPDrjxhCqjsogxxgpG76jxiYcwGLHlmLh1XM+txh5BQ5FFFg6RJjTlQM= X-Received: from avagin.kir.corp.google.com ([2620:0:1008:10:e986:a7c7:2814:c9a8]) (user=avagin job=sendgmr) by 2002:a05:6902:343:b0:db5:f536:17d4 with SMTP id e3-20020a056902034300b00db5f53617d4mr76906ybs.11.1702536287106; Wed, 13 Dec 2023 22:44:47 -0800 (PST) Date: Wed, 13 Dec 2023 22:44:39 -0800 In-Reply-To: <20231214064439.1023011-1-avagin@google.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231214064439.1023011-1-avagin@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231214064439.1023011-2-avagin@google.com> Subject: [PATCH 2/2] selftests/overlayfs: verify device and inode numbers in /proc/pid/maps From: Andrei Vagin To: Andrew Morton , Amir Goldstein Cc: linux-kernel@vger.kernel.org, Alexander Mikhalitsyn , Christian Brauner , Alexander Viro , linux-fsdevel@vger.kernel.org, overlayfs , Miklos Szeredi , Andrei Vagin When mapping a file on overlayfs, the file stored in ->vm_file is a backing file whose f_inode is on the underlying filesystem. We need to verify that /proc/pid/maps contains numbers of the overlayfs file, but not its backing file. Cc: Amir Goldstein Cc: Alexander Mikhalitsyn Signed-off-by: Andrei Vagin Reviewed-by: Amir Goldstein --- tools/testing/selftests/Makefile | 1 + .../filesystems/overlayfs/.gitignore | 2 + .../selftests/filesystems/overlayfs/Makefile | 7 + .../filesystems/overlayfs/dev_in_maps.c | 182 ++++++++++++++++++ .../selftests/filesystems/overlayfs/log.h | 26 +++ 5 files changed, 218 insertions(+) create mode 100644 tools/testing/selftests/filesystems/overlayfs/.gitignore create mode 100644 tools/testing/selftests/filesystems/overlayfs/Makefile create mode 100644 tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c create mode 100644 tools/testing/selftests/filesystems/overlayfs/log.h diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile index 3b2061d1c1a5..0939a40abb28 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile @@ -26,6 +26,7 @@ TARGETS += filesystems TARGETS += filesystems/binderfs TARGETS += filesystems/epoll TARGETS += filesystems/fat +TARGETS += filesystems/overlayfs TARGETS += firmware TARGETS += fpu TARGETS += ftrace diff --git a/tools/testing/selftests/filesystems/overlayfs/.gitignore b/tools/testing/selftests/filesystems/overlayfs/.gitignore new file mode 100644 index 000000000000..52ae618fdd98 --- /dev/null +++ b/tools/testing/selftests/filesystems/overlayfs/.gitignore @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-only +dev_in_maps diff --git a/tools/testing/selftests/filesystems/overlayfs/Makefile b/tools/testing/selftests/filesystems/overlayfs/Makefile new file mode 100644 index 000000000000..56b2b48a765b --- /dev/null +++ b/tools/testing/selftests/filesystems/overlayfs/Makefile @@ -0,0 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0 + +TEST_GEN_PROGS := dev_in_maps + +CFLAGS := -Wall -Werror + +include ../../lib.mk diff --git a/tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c b/tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c new file mode 100644 index 000000000000..e19ab0e85709 --- /dev/null +++ b/tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c @@ -0,0 +1,182 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../../kselftest.h" +#include "log.h" + +static int sys_fsopen(const char *fsname, unsigned int flags) +{ + return syscall(__NR_fsopen, fsname, flags); +} + +static int sys_fsconfig(int fd, unsigned int cmd, const char *key, const char *value, int aux) +{ + return syscall(__NR_fsconfig, fd, cmd, key, value, aux); +} + +static int sys_fsmount(int fd, unsigned int flags, unsigned int attr_flags) +{ + return syscall(__NR_fsmount, fd, flags, attr_flags); +} + +static int sys_move_mount(int from_dfd, const char *from_pathname, + int to_dfd, const char *to_pathname, + unsigned int flags) +{ + return syscall(__NR_move_mount, from_dfd, from_pathname, to_dfd, to_pathname, flags); +} + +static long get_file_dev_and_inode(void *addr, struct statx *stx) +{ + char buf[4096]; + FILE *mapf; + + mapf = fopen("/proc/self/maps", "r"); + if (mapf == NULL) + return pr_perror("fopen(/proc/self/maps)"); + + while (fgets(buf, sizeof(buf), mapf)) { + unsigned long start, end; + uint32_t maj, min; + __u64 ino; + + if (sscanf(buf, "%lx-%lx %*s %*s %x:%x %llu", + &start, &end, &maj, &min, &ino) != 5) + return pr_perror("unable to parse: %s", buf); + if (start == (unsigned long)addr) { + stx->stx_dev_major = maj; + stx->stx_dev_minor = min; + stx->stx_ino = ino; + return 0; + } + } + + return pr_err("unable to find the mapping"); +} + +static int ovl_mount(void) +{ + int tmpfs, fsfd, ovl; + + fsfd = sys_fsopen("tmpfs", 0); + if (fsfd == -1) + return pr_perror("fsopen(tmpfs)"); + + if (sys_fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0) == -1) + return pr_perror("FSCONFIG_CMD_CREATE"); + + tmpfs = sys_fsmount(fsfd, 0, 0); + if (tmpfs == -1) + return pr_perror("fsmount"); + + close(fsfd); + + /* overlayfs can't be constructed on top of a detached mount. */ + if (sys_move_mount(tmpfs, "", AT_FDCWD, "/tmp", MOVE_MOUNT_F_EMPTY_PATH)) + return pr_perror("move_mount"); + close(tmpfs); + + if (mkdir("/tmp/w", 0755) == -1 || + mkdir("/tmp/u", 0755) == -1 || + mkdir("/tmp/l", 0755) == -1) + return pr_perror("mkdir"); + + fsfd = sys_fsopen("overlay", 0); + if (fsfd == -1) + return pr_perror("fsopen(overlay)"); + if (sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "source", "test", 0) == -1 || + sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "lowerdir", "/tmp/l", 0) == -1 || + sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "upperdir", "/tmp/u", 0) == -1 || + sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "workdir", "/tmp/w", 0) == -1) + return pr_perror("fsconfig"); + if (sys_fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0) == -1) + return pr_perror("fsconfig"); + ovl = sys_fsmount(fsfd, 0, 0); + if (ovl == -1) + return pr_perror("fsmount"); + + return ovl; +} + +/* + * Check that the file device and inode shown in /proc/pid/maps match values + * returned by stat(2). + */ +static int test(void) +{ + struct statx stx, mstx; + int ovl, fd; + void *addr; + + ovl = ovl_mount(); + if (ovl == -1) + return -1; + + fd = openat(ovl, "test", O_RDWR | O_CREAT, 0644); + if (fd == -1) + return pr_perror("openat"); + + addr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0); + if (addr == MAP_FAILED) + return pr_perror("mmap"); + + if (get_file_dev_and_inode(addr, &mstx)) + return -1; + if (statx(fd, "", AT_EMPTY_PATH | AT_STATX_SYNC_AS_STAT, STATX_INO, &stx)) + return pr_perror("statx"); + + if (stx.stx_dev_major != mstx.stx_dev_major || + stx.stx_dev_minor != mstx.stx_dev_minor || + stx.stx_ino != mstx.stx_ino) + return pr_fail("unmatched dev:ino %x:%x:%llx (expected %x:%x:%llx)\n", + mstx.stx_dev_major, mstx.stx_dev_minor, mstx.stx_ino, + stx.stx_dev_major, stx.stx_dev_minor, stx.stx_ino); + + ksft_test_result_pass("devices are matched\n"); + return 0; +} + +int main(int argc, char **argv) +{ + int fsfd; + + fsfd = sys_fsopen("overlay", 0); + if (fsfd == -1) { + ksft_test_result_skip("unable to create overlay mount\n"); + return 1; + } + close(fsfd); + + /* Create a new mount namespace to not care about cleaning test mounts. */ + if (unshare(CLONE_NEWNS) == -1) { + ksft_test_result_skip("unable to create a new mount namespace\n"); + return 1; + } + + if (mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL) == -1) { + pr_perror("mount"); + return 1; + } + + ksft_set_plan(1); + + if (test()) + return 1; + + ksft_exit_pass(); + return 0; +} diff --git a/tools/testing/selftests/filesystems/overlayfs/log.h b/tools/testing/selftests/filesystems/overlayfs/log.h new file mode 100644 index 000000000000..db64df2a8483 --- /dev/null +++ b/tools/testing/selftests/filesystems/overlayfs/log.h @@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __SELFTEST_TIMENS_LOG_H__ +#define __SELFTEST_TIMENS_LOG_H__ + +#define pr_msg(fmt, lvl, ...) \ + ksft_print_msg("[%s] (%s:%d)\t" fmt "\n", \ + lvl, __FILE__, __LINE__, ##__VA_ARGS__) + +#define pr_p(func, fmt, ...) func(fmt ": %m", ##__VA_ARGS__) + +#define pr_err(fmt, ...) \ + ({ \ + ksft_test_result_error(fmt "\n", ##__VA_ARGS__); \ + -1; \ + }) + +#define pr_fail(fmt, ...) \ + ({ \ + ksft_test_result_fail(fmt, ##__VA_ARGS__); \ + -1; \ + }) + +#define pr_perror(fmt, ...) pr_p(pr_err, fmt, ##__VA_ARGS__) + +#endif