path: root/fs/sysfs
diff options
authorDaniel Vetter <>2020-11-27 17:41:25 +0100
committerDaniel Vetter <>2021-01-12 14:26:31 +0100
commit74b30195395c406c787280a77ae55aed82dbbfc7 (patch)
tree968e22561759ef7cedcf464f054c64c307415862 /fs/sysfs
parent71a1d8ed900f8cf53151beff17e3e2ff8e9283a1 (diff)
sysfs: Support zapping of binary attr mmaps
We want to be able to revoke pci mmaps so that the same access rules applies as for /dev/kmem. Revoke support for devmem was added in 3234ac664a87 ("/dev/mem: Revoke mappings when a driver claims the region"). The simplest way to achieve this is by having the same filp->f_mapping for all mappings, so that unmap_mapping_range can find them all, no matter through which file they've been created. Since this must be set at open time we need sysfs support for this. Add an optional mapping parameter bin_attr, which is only consulted when there's also an mmap callback, since without mmap support allowing to adjust the ->f_mapping makes no sense. Reviewed-by: Greg Kroah-Hartman <> Signed-off-by: Daniel Vetter <> Cc: Jason Gunthorpe <> Cc: Kees Cook <> Cc: Dan Williams <> Cc: Andrew Morton <> Cc: John Hubbard <> Cc: Jérôme Glisse <> Cc: Jan Kara <> Cc: Dan Williams <> Cc: Cc: Cc: Cc: Cc: Bjorn Helgaas <> Cc: Cc: Greg Kroah-Hartman <> Cc: "Rafael J. Wysocki" <> Cc: Christian Brauner <> Cc: "David S. Miller" <> Cc: Michael Ellerman <> Cc: Sourabh Jain <> Cc: Daniel Vetter <> Cc: Mauro Carvalho Chehab <> Cc: Nayna Jain <> Signed-off-by: Daniel Vetter <> Link:
Diffstat (limited to 'fs/sysfs')
1 files changed, 11 insertions, 0 deletions
diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c
index 96d0da65e088..9aefa7779b29 100644
--- a/fs/sysfs/file.c
+++ b/fs/sysfs/file.c
@@ -170,6 +170,16 @@ static int sysfs_kf_bin_mmap(struct kernfs_open_file *of,
return battr->mmap(of->file, kobj, battr, vma);
+static int sysfs_kf_bin_open(struct kernfs_open_file *of)
+ struct bin_attribute *battr = of->kn->priv;
+ if (battr->mapping)
+ of->file->f_mapping = battr->mapping;
+ return 0;
void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr)
struct kernfs_node *kn = kobj->sd, *tmp;
@@ -241,6 +251,7 @@ static const struct kernfs_ops sysfs_bin_kfops_mmap = {
.read = sysfs_kf_bin_read,
.write = sysfs_kf_bin_write,
.mmap = sysfs_kf_bin_mmap,
+ .open = sysfs_kf_bin_open,
int sysfs_add_file_mode_ns(struct kernfs_node *parent,