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
189189 config EROFS_FS_PAGE_CACHE_SHARE
190190 bool "EROFS page cache share support (experimental)"
191191 depends on EROFS_FS && EROFS_FS_XATTR
192+ select FS_STACK
192193 help
193194 This enables page cache sharing among inodes with identical
194195 content fingerprints on the same machine.
fs/erofs/internal.h+2−2
@@ -288,8 +288,8 @@ struct erofs_inode { · 287 unmodified lines
288288 struct erofs_inode_fingerprint fingerprint;
289289 spinlock_t ishare_lock;
290290 };
291− /* for each real inode */
292− struct inode *sharedinode;
291+ /* for each real filesystem inode */
292+ struct dentry *sharedentry;
293293 };
294294 #endif
295295 /* the corresponding vfs inode */
fs/erofs/ishare.c+28−30
@@ -2,14 +2,13 @@ · 1 unmodified line
22 /*
33 * Copyright (C) 2024, Alibaba Cloud
44 */
5+#include <linux/backing-file.h>
56 #include <linux/xxhash.h>
67 #include <linux/mount.h>
78 #include <linux/security.h>
89 #include "internal.h"
910 #include "xattr.h"
1011
11−#include "../internal.h"
12−
1312 static struct vfsmount *erofs_ishare_mnt;
1413
1514 static 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
3332
3433 bool erofs_ishare_fill_inode(struct inode *inode)
3534 {
35+ static const struct file_operations empty_fops = {};
3636 struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);
3737 const struct address_space_operations *aops;
3838 struct erofs_inode *vi = EROFS_I(inode);
3939 struct erofs_inode_fingerprint fp;
40+ struct dentry *sd;
4041 struct inode *si;
4142
4243 aops = erofs_get_aops(inode);
@@ -49,7 +50,9 @@ bool erofs_ishare_fill_inode(struct inode *inode) · 6 unmodified lines
4950 xxh32(fp.opaque, fp.size, 0),
5051 erofs_ishare_iget5_eq, erofs_ishare_iget5_set, &fp);
5152 if (si && (inode_state_read_once(si) & I_NEW)) {
53+ si->i_fop = &empty_fops;
5254 si->i_mapping->a_ops = aops;
55+ si->i_mode = 0444 | S_IFREG;
5356 si->i_size = inode->i_size;
5457 unlock_new_inode(si);
5558 } else {
@@ -65,7 +68,10 @@ bool erofs_ishare_fill_inode(struct inode *inode) · 9 unmodified lines
6568 return false;
6669 }
6770 }
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;
6975 INIT_LIST_HEAD(&vi->ishare_list);
7076 spin_lock(&EROFS_I(si)->ishare_lock);
7177 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
7581
7682 void erofs_ishare_free_inode(struct inode *inode)
7783 {
78− struct erofs_inode *vi = EROFS_I(inode);
79− struct inode *sharedinode = vi->sharedinode;
84+ struct erofs_inode *vi = EROFS_I(inode), *svi;
8085
81− if (!sharedinode)
86+ if (!vi->sharedentry)
8287 return;
83− spin_lock(&EROFS_I(sharedinode)->ishare_lock);
88+ svi = EROFS_I(d_inode(vi->sharedentry));
89+ spin_lock(&svi->ishare_lock);
8490 list_del(&vi->ishare_list);
85− spin_unlock(&EROFS_I(sharedinode)->ishare_lock);
86− iput(sharedinode);
87− vi->sharedinode = NULL;
91+ spin_unlock(&svi->ishare_lock);
92+ dput(vi->sharedentry);
93+ vi->sharedentry = NULL;
8894 }
8995
9096 static int erofs_ishare_file_open(struct inode *inode, struct file *file)
9197 {
92− struct inode *sharedinode = EROFS_I(inode)->sharedinode;
93− struct file *realfile;
98+ struct path sharedpath = {
99+ .mnt = erofs_ishare_mnt,
100+ .dentry = EROFS_I(inode)->sharedentry,
101+ };
102+ struct file *rf;
94103
95104 if (file->f_flags & O_DIRECT)
96105 return -EINVAL;
97− realfile = alloc_empty_backing_file(O_RDONLY|O_NOATIME, current_cred(),
98− file);
99− if (IS_ERR(realfile))
100− return PTR_ERR(realfile);
101− ihold(sharedinode);
102− realfile->f_op = &erofs_file_fops;
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;
106+
107+ rf = backing_file_open(file, file->f_flags | O_NOATIME,
108+ &sharedpath, current_cred());
109+ if (IS_ERR(rf))
110+ return PTR_ERR(rf);
111+ file->private_data = rf;
111112 return 0;
112113 }
113114
114115 static int erofs_ishare_file_release(struct inode *inode, struct file *file)
115116 {
116− struct file *realfile = file->private_data;
117−
118− iput(realfile->f_inode);
119− fput(realfile);
117+ fput(file->private_data);
120118 file->private_data = NULL;
121119 return 0;
122120 }

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