History

erofs: ensure valid f_path for page cache sharing

Previously, backing files for page cache sharing were set up with f_path left as NULL (only f_inode was valid). It worked, but a recent mincore fix relies on f_path.mnt and crashes (found by "erofs/028" on 7.2-rc4): BUG: kernel NULL pointer dereference, address: 0000000000000018 #PF: supervisor read access in kernel mode #PF: error_code(0x0000) - not-present page PGD 0 P4D 0 Oops: Oops: 0000 [#1] SMP PTI CPU: 3 UID: 0 PID: 675528 Comm: fincore Not tainted 7.2.0-rc4-00002-g[]-dirty #1 PREEMPT(lazy) Hardware name: Red Hat KVM, BIOS 1.16.0-4.al8 04/01/2014 RIP: 0010:__do_sys_mincore+0xc0/0x2c0 ... Specify valid paths using valid disconnected dentries together with erofs_ishare_mnt instead of leaving f_path empty, so they are more like real backing files in a pseudo filesystem and standard backing_file_open() can be used directly.

Gao Xiang committed 3 months ago

Commit 96b2dbb1 parent 97ab5aa3 files · +31 −32Browse files

Unified · Split

3 files
  1. fs/erofs/Kconfig +1 −0
  2. fs/erofs/internal.h +2 −2
  3. fs/erofs/ishare.c +28 −30
fs/erofs/Kconfig+1−0
@@ -189,6 +189,7 @@ config EROFS_FS_PCPU_KTHREAD_HIPRI · 188 unmodified lines
189config EROFS_FS_PAGE_CACHE_SHARE189config EROFS_FS_PAGE_CACHE_SHARE
190 bool "EROFS page cache share support (experimental)"190 bool "EROFS page cache share support (experimental)"
191 depends on EROFS_FS && EROFS_FS_XATTR191 depends on EROFS_FS && EROFS_FS_XATTR
192 select FS_STACK
192 help193 help
193 This enables page cache sharing among inodes with identical194 This enables page cache sharing among inodes with identical
194 content fingerprints on the same machine.195 content fingerprints on the same machine.
fs/erofs/internal.h+2−2
@@ -288,8 +288,8 @@ struct erofs_inode { · 287 unmodified lines
288 struct erofs_inode_fingerprint fingerprint;288 struct erofs_inode_fingerprint fingerprint;
289 spinlock_t ishare_lock;289 spinlock_t ishare_lock;
290 };290 };
291 /* for each real inode */291 /* for each real filesystem inode */
292 struct inode *sharedinode;292 struct dentry *sharedentry;
293 };293 };
294#endif294#endif
295 /* the corresponding vfs inode */295 /* the corresponding vfs inode */
fs/erofs/ishare.c+28−30
@@ -2,14 +2,13 @@ · 1 unmodified line
2/*2/*
3 * Copyright (C) 2024, Alibaba Cloud3 * Copyright (C) 2024, Alibaba Cloud
4 */4 */
5#include <linux/backing-file.h>
5#include <linux/xxhash.h>6#include <linux/xxhash.h>
6#include <linux/mount.h>7#include <linux/mount.h>
7#include <linux/security.h>8#include <linux/security.h>
8#include "internal.h"9#include "internal.h"
9#include "xattr.h"10#include "xattr.h"
10 11
11#include "../internal.h"
12
13static struct vfsmount *erofs_ishare_mnt;12static struct vfsmount *erofs_ishare_mnt;
14 13
15static int erofs_ishare_iget5_eq(struct inode *inode, void *data)14static int erofs_ishare_iget5_eq(struct inode *inode, void *data)
@@ -33,10 +32,12 @@ static int erofs_ishare_iget5_set(struct inode *inode, void *data) · 17 unmodified lines
33 32
34bool erofs_ishare_fill_inode(struct inode *inode)33bool erofs_ishare_fill_inode(struct inode *inode)
35{34{
35 static const struct file_operations empty_fops = {};
36 struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);36 struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);
37 const struct address_space_operations *aops;37 const struct address_space_operations *aops;
38 struct erofs_inode *vi = EROFS_I(inode);38 struct erofs_inode *vi = EROFS_I(inode);
39 struct erofs_inode_fingerprint fp;39 struct erofs_inode_fingerprint fp;
40 struct dentry *sd;
40 struct inode *si;41 struct inode *si;
41 42
42 aops = erofs_get_aops(inode);43 aops = erofs_get_aops(inode);
@@ -49,7 +50,9 @@ bool erofs_ishare_fill_inode(struct inode *inode) · 6 unmodified lines
49 xxh32(fp.opaque, fp.size, 0),50 xxh32(fp.opaque, fp.size, 0),
50 erofs_ishare_iget5_eq, erofs_ishare_iget5_set, &fp);51 erofs_ishare_iget5_eq, erofs_ishare_iget5_set, &fp);
51 if (si && (inode_state_read_once(si) & I_NEW)) {52 if (si && (inode_state_read_once(si) & I_NEW)) {
53 si->i_fop = &empty_fops;
52 si->i_mapping->a_ops = aops;54 si->i_mapping->a_ops = aops;
55 si->i_mode = 0444 | S_IFREG;
53 si->i_size = inode->i_size;56 si->i_size = inode->i_size;
54 unlock_new_inode(si);57 unlock_new_inode(si);
55 } else {58 } else {
@@ -65,7 +68,10 @@ bool erofs_ishare_fill_inode(struct inode *inode) · 9 unmodified lines
65 return false;68 return false;
66 }69 }
67 }70 }
68 vi->sharedinode = si;71 sd = d_obtain_alias(si); /* disconnected denties for sharedinodes */
72 if (IS_ERR(sd))
73 return false;
74 vi->sharedentry = sd;
69 INIT_LIST_HEAD(&vi->ishare_list);75 INIT_LIST_HEAD(&vi->ishare_list);
70 spin_lock(&EROFS_I(si)->ishare_lock);76 spin_lock(&EROFS_I(si)->ishare_lock);
71 list_add(&vi->ishare_list, &EROFS_I(si)->ishare_list);77 list_add(&vi->ishare_list, &EROFS_I(si)->ishare_list);
@@ -75,48 +81,40 @@ bool erofs_ishare_fill_inode(struct inode *inode) · 3 unmodified lines
75 81
76void erofs_ishare_free_inode(struct inode *inode)82void erofs_ishare_free_inode(struct inode *inode)
77{83{
78 struct erofs_inode *vi = EROFS_I(inode);84 struct erofs_inode *vi = EROFS_I(inode), *svi;
79 struct inode *sharedinode = vi->sharedinode;
80 85
81 if (!sharedinode)86 if (!vi->sharedentry)
82 return;87 return;
83 spin_lock(&EROFS_I(sharedinode)->ishare_lock);88 svi = EROFS_I(d_inode(vi->sharedentry));
89 spin_lock(&svi->ishare_lock);
84 list_del(&vi->ishare_list);90 list_del(&vi->ishare_list);
85 spin_unlock(&EROFS_I(sharedinode)->ishare_lock);91 spin_unlock(&svi->ishare_lock);
86 iput(sharedinode);92 dput(vi->sharedentry);
87 vi->sharedinode = NULL;93 vi->sharedentry = NULL;
88}94}
89 95
90static int erofs_ishare_file_open(struct inode *inode, struct file *file)96static int erofs_ishare_file_open(struct inode *inode, struct file *file)
91{97{
92 struct inode *sharedinode = EROFS_I(inode)->sharedinode;98 struct path sharedpath = {
93 struct file *realfile;99 .mnt = erofs_ishare_mnt,
100 .dentry = EROFS_I(inode)->sharedentry,
101 };
102 struct file *rf;
94 103
95 if (file->f_flags & O_DIRECT)104 if (file->f_flags & O_DIRECT)
96 return -EINVAL;105 return -EINVAL;
97 realfile = alloc_empty_backing_file(O_RDONLY|O_NOATIME, current_cred(),106
98 file);107 rf = backing_file_open(file, file->f_flags | O_NOATIME,
99 if (IS_ERR(realfile))108 &sharedpath, current_cred());
100 return PTR_ERR(realfile);109 if (IS_ERR(rf))
101 ihold(sharedinode);110 return PTR_ERR(rf);
102 realfile->f_op = &erofs_file_fops;111 file->private_data = rf;
103 realfile->f_inode = sharedinode;
104 realfile->f_mapping = sharedinode->i_mapping;
105 path_get(&file->f_path);
106 backing_file_set_user_path(realfile, &file->f_path);
107
108 file_ra_state_init(&realfile->f_ra, file->f_mapping);
109 realfile->private_data = EROFS_I(inode);
110 file->private_data = realfile;
111 return 0;112 return 0;
112}113}
113 114
114static int erofs_ishare_file_release(struct inode *inode, struct file *file)115static int erofs_ishare_file_release(struct inode *inode, struct file *file)
115{116{
116 struct file *realfile = file->private_data;117 fput(file->private_data);
117
118 iput(realfile->f_inode);
119 fput(realfile);
120 file->private_data = NULL;118 file->private_data = NULL;
121 return 0;119 return 0;
122}120}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t