mm: expand mmap_prepare functionality and usage
Subsystems (20)
Lorenzo Stoakes (Oracle)
maintainerv2 cover mm: expand mmap_prepare functionality and usage
This series expands the mmap_prepare functionality, which is intended to replace the deprecated f_op->mmap hook which has been the source of bugs and security issues for some time.
This series starts with some cleanup of existing mmap_prepare logic, then adds documentation for the mmap_prepare call to make it easier for filesystem and driver writers to understand how it works.
It then importantly adds a vm_ops->mapped hook, a key feature that was missing from mmap_prepare previously - this is invoked when a driver which specifies mmap_prepare has successfully been mapped but not merged with another VMA.
mmap_prepare is invoked prior to a merge being attempted, so you cannot manipulate state such as reference counts as if it were a new mapping.
The vm_ops->mapped hook allows a driver to perform tasks required at this stage, and provides symmetry against subsequent vm_ops->open,close calls.
The series uses this to correct the afs implementation which wrongly manipulated reference count at mmap_prepare time.
It then adds an mmap_prepare equivalent of vm_iomap_memory() - mmap_action_simple_ioremap(), then uses this to update a number of drivers.
It then splits out the mmap_prepare compatibility layer (which allows for invocation of mmap_prepare hooks in an mmap() hook) in such a way as to allow for more incremental implementation of mmap_prepare hooks.
It then uses this to extend mmap_prepare usage in drivers.
Finally it adds an mmap_prepare equivalent of vm_map_pages(), which lays the foundation for future work which will extend mmap_prepare to DMA coherent mappings.
v2:
* Rebased on
https://lore.kernel.org/all/[email protected]/ to make Andrew's life easier :)
* Folded all interim fixes into series (thanks Randy for many doc fixes!))
* As per Suren, removed a comment about allocations too small to fail.
* As per Randy, fixed up typo in documentation for vm_area_desc.
* Fixed mmap_action_prepare() not returning if invalid action->type specified, as updated from Andrew's interim fix (thanks!) and also reported by kernel test bot.
* Updated mmap_action_prepare() and specific prepare functions to only pass vm_area_desc parameter as per Suren.
* Fixed up whitespace as per Suren.
* Updated vm_op->open comment in vm_operations_struct to reference forking as per Suren.
* Added a commit to check that input range is within VMA on remap as per Suren (this also covers I/O remap and all other cases already asserted).
* Updated AFS to not incorrectly reference count on mmap prepare as per Usama.
* Also updated various static AFS functions to be consistent with each other.
* Updated AFS commit message to reflect mmap_prepare being before any VMA merging as per Suren.
* Updated __compat_vma_mapped() to check for NULL vm_ops as per Usama.
* Updated __compat_vma_mapped() to not reference an unmapped VMA's fields as per Usama.
* Updated __vma_check_mmap_hook() to check for NULL vm_ops as per Usama.
* Dropped comment about preferring mmap_prepare as seems overly confusing, as per Suren.
* Updated the mmap lock assert in unmap_vma_locked() to a write lock assert as per Suren.
* Copied vm_ops->open comment over to VMA tests in appropriate patch as per Suren.
* Updated mmap_prepare documentation to reflect the fact that no resources should be allocated upon mmap_prepare.
* Updated mmap_prepare documentation to reference the vm_ops->mapped callback.
* Fixed stray markdown '## How to use' in documentation.
* Fixed bug reported by kernel test bot re: overlooked vma_desc_test_flags() -> vma_desc_test() in MTD driver for nommu.
v1:
https://lore.kernel.org/linux-mm/[email protected]/
Lorenzo Stoakes (Oracle) (16):
mm: various small mmap_prepare cleanups
mm: add documentation for the mmap_prepare file operation callback
mm: document vm_operations_struct->open the same as close()
mm: add vm_ops->mapped hook
fs: afs: correctly drop reference count on mapping failure
mm: add mmap_action_simple_ioremap()
misc: open-dice: replace deprecated mmap hook with mmap_prepare
hpet: replace deprecated mmap hook with mmap_prepare
mtdchar: replace deprecated mmap hook with mmap_prepare, clean up
stm: replace deprecated mmap hook with mmap_prepare
staging: vme_user: replace deprecated mmap hook with mmap_prepare
mm: allow handling of stacked mmap_prepare hooks in more drivers
drivers: hv: vmbus: replace deprecated mmap hook with mmap_prepare
uio: replace deprecated mmap hook with mmap_prepare in uio_info
mm: add mmap_action_map_kernel_pages[_full]()
mm: on remap assert that input range within the proposed VMA
Documentation/driver-api/vme.rst | 2 +- Documentation/filesystems/index.rst | 1 + Documentation/filesystems/mmap_prepare.rst | 168 ++++++++++++++++ drivers/char/hpet.c | 12 +- drivers/hv/hyperv_vmbus.h | 4 +- drivers/hv/vmbus_drv.c | 27 ++- drivers/hwtracing/stm/core.c | 31 ++- drivers/misc/open-dice.c | 19 +- drivers/mtd/mtdchar.c | 21 +- drivers/staging/vme_user/vme.c | 20 +- drivers/staging/vme_user/vme.h | 2 +- drivers/staging/vme_user/vme_user.c | 51 +++-- drivers/target/target_core_user.c | 26 ++- drivers/uio/uio.c | 10 +- drivers/uio/uio_hv_generic.c | 11 +- fs/afs/file.c | 36 +++- include/linux/fs.h | 14 +- include/linux/hyperv.h | 4 +- include/linux/mm.h | 158 +++++++++++++-- include/linux/mm_types.h | 17 +- include/linux/uio_driver.h | 4 +- mm/internal.h | 37 ++-- mm/memory.c | 177 ++++++++++++----- mm/util.c | 219 +++++++++++++++------ mm/vma.c | 59 ++++-- mm/vma.h | 2 +- tools/testing/vma/include/dup.h | 141 +++++++++---- tools/testing/vma/include/stubs.h | 8 +- 28 files changed, 970 insertions(+), 311 deletions(-) create mode 100644 Documentation/filesystems/mmap_prepare.rst
--
2.53.0
Lorenzo Stoakes (Oracle)
maintainerPATCH v2 1/16 mm: various small mmap_prepare cleanups
Rather than passing arbitrary fields, pass a vm_area_desc pointer to mmap prepare functions to mmap prepare, and an action and vma pointer to mmap complete in order to put all the action-specific logic in the function actually doing the work.
Additionally, allow mmap prepare functions to return an error so we can error out as soon as possible if there is something logically incorrect in the input.
Update remap_pfn_range_prepare() to properly check the input range for the CoW case.
While we're here, make remap_pfn_range_prepare_vma() a little neater, and pass mmap_action directly to call_action_complete().
Then, update compat_vma_mmap() to perform its logic directly, as __compat_vma_map() is not used by anything so we don't need to export it.
Also update compat_vma_mmap() to use vfs_mmap_prepare() rather than calling the mmap_prepare op directly.
Finally, update the VMA userland tests to reflect the changes.
--- include/linux/fs.h | 2 - include/linux/mm.h | 7 +- mm/internal.h | 27 ++++--- mm/memory.c | 45 +++++++---- mm/util.c | 119 +++++++++++++----------------- mm/vma.c | 24 +++--- tools/testing/vma/include/dup.h | 7 +- tools/testing/vma/include/stubs.h | 8 +- 8 files changed, 123 insertions(+), 116 deletions(-)
include/linux/fs.h +0 -2
@@ -2058,8 +2058,6 @@ static inline bool can_mmap_file(struct file *file)return true; } -int __compat_vma_mmap (const struct file_operations *f_op, -struct file *file,struct vm_area_struct *vma);int compat_vma_mmap (struct file *file,struct vm_area_struct *vma);static inline int vfs_mmap (struct file *file,struct vm_area_struct *vma)
include/linux/mm.h +3 -4
@@ -4320,10 +4320,9 @@ static inline void mmap_action_ioremap_full(struct vm_area_desc *desc,mmap_action_ioremap (desc, desc->start, start_pfn,vma_desc_size (desc)); } -void mmap_action_prepare (struct mmap_action *action, -struct vm_area_desc *desc); -int mmap_action_complete (struct mmap_action *action, -struct vm_area_struct *vma); +int mmap_action_prepare (struct vm_area_desc *desc); +int mmap_action_complete (struct vm_area_struct *vma, +struct mmap_action *action);/* Look up the first VMA which exactly match the interval vm_start ... vm_end */ static inline struct vm_area_struct *find_exact_vma(struct mm_struct *mm,
mm/internal.h +16 -11
@@ -1793,26 +1793,31 @@ int walk_page_range_debug(struct mm_struct *mm, unsigned long start,void dup_mm_exe_file (struct mm_struct *mm,struct mm_struct *oldmm);int dup_mmap (struct mm_struct *mm,struct mm_struct *oldmm); -void remap_pfn_range_prepare (struct vm_area_desc *desc,unsigned long pfn); -int remap_pfn_range_complete (struct vm_area_struct *vma,unsigned long addr, -unsigned long pfn,unsigned long size,pgprot_t pgprot); +int remap_pfn_range_prepare (struct vm_area_desc *desc); +int remap_pfn_range_complete (struct vm_area_struct *vma, +struct mmap_action *action); -static inline void io_remap_pfn_range_prepare (struct vm_area_desc *desc, -unsigned long orig_pfn,unsigned long size) +static inline int io_remap_pfn_range_prepare (struct vm_area_desc *desc) { +struct mmap_action *action = &desc->action; +const unsigned long orig_pfn = action->remap.start_pfn; +const unsigned long size = action->remap.size;const unsigned long pfn =io_remap_pfn_range_pfn (orig_pfn, size); -return remap_pfn_range_prepare (desc, pfn); + action->remap.start_pfn = pfn; +return remap_pfn_range_prepare (desc); }static inline int io_remap_pfn_range_complete (struct vm_area_struct *vma, -unsigned long addr,unsigned long orig_pfn,unsigned long size, -pgprot_t orig_prot) +struct mmap_action *action) { -const unsigned long pfn =io_remap_pfn_range_pfn (orig_pfn, size); -const pgprot_t prot =pgprot_decrypted (orig_prot); +const unsigned long size = action->remap.size; +const unsigned long orig_pfn = action->remap.start_pfn; +const pgprot_t orig_prot = vma->vm_page_prot; -return remap_pfn_range_complete (vma, addr, pfn, size, prot); + action->remap.pgprot =pgprot_decrypted (orig_prot); + action->remap.start_pfn =io_remap_pfn_range_pfn (orig_pfn, size); +return remap_pfn_range_complete (vma, action); }#ifdef CONFIG_MMU_NOTIFIER
mm/memory.c +29 -16
65 lines not on this page · raw message
mm/util.c +52 -67
190 lines not on this page · raw message
mm/vma.c +14 -10
@@ -2640,15 +2640,18 @@ static void __mmap_complete(struct mmap_state *map, struct vm_area_struct *vma)vma_set_page_prot (vma); } -static void call_action_prepare (struct mmap_state *map, -struct vm_area_desc *desc) +static int call_action_prepare (struct mmap_state *map, +struct vm_area_desc *desc) { -struct mmap_action *action = &desc->action; +int err; -mmap_action_prepare (action, desc); + err =mmap_action_prepare (desc); +if (err) +return err; -if (action->hide_from_rmap_until_complete) +if (desc->action.hide_from_rmap_until_complete) map->hold_file_rmap_lock = true; +return 0 ; }/*
@@ -2672,7 +2675,9 @@ static int call_mmap_prepare(struct mmap_state *map,if (err)return err; -call_action_prepare (map, desc); + err =call_action_prepare (map, desc); +if (err) +return err;/* Update fields permitted to be changed. */ map->pgoff = desc->pgoff;
@@ -2727,13 +2732,12 @@ static bool can_set_ksm_flags_early(struct mmap_state *map) }static int call_action_complete (struct mmap_state *map, -struct vm_area_desc *desc, +struct mmap_action *action,struct vm_area_struct *vma) { -struct mmap_action *action = &desc->action;int ret; - ret =mmap_action_complete (action, vma); + ret =mmap_action_complete (vma, action);/* If we held the file rmap we need to release it. */ if (map->hold_file_rmap_lock) {
@@ -2795,7 +2799,7 @@ static unsigned long __mmap_region(struct file *file, unsigned long addr,__mmap_complete (&map, vma);if (have_mmap_prepare && allocated_new) { - error =call_action_complete (&map, &desc, vma); + error =call_action_complete (&map, &desc.action, vma);if (error)return error;
tools/testing/vma/include/dup.h +5 -2
@@ -1271,9 +1271,12 @@ static inline int __compat_vma_mmap(const struct file_operations *f_op,if (err)return err; -mmap_action_prepare (&desc.action, &desc); + err =mmap_action_prepare (&desc); +if (err) +return err; +set_vma_from_desc (vma, &desc); -return mmap_action_complete (&desc.action, vma); +return mmap_action_complete (vma, &desc.action); } static inline int compat_vma_mmap(struct file *file,
tools/testing/vma/include/stubs.h +4 -4
@@ -81,13 +81,13 @@ static inline void free_anon_vma_name(struct vm_area_struct *vma) { } -static inline void mmap_action_prepare (struct mmap_action *action, -struct vm_area_desc *desc) +static inline int mmap_action_prepare (struct vm_area_desc *desc) { +return 0 ; } -static inline int mmap_action_complete (struct mmap_action *action, -struct vm_area_struct *vma) +static inline int mmap_action_complete (struct vm_area_struct *vma, +struct mmap_action *action) {return 0 ; }
Signature · 1 line
2.53.0
Lorenzo Stoakes (Oracle)
maintainerPATCH v2 2/16 mm: add documentation for the mmap_prepare file operation callback
In reply to Lorenzo Stoakes (Oracle)This documentation makes it easier for a driver/file system implementer to correctly use this callback.
It covers the fundamentals, whilst intentionally leaving the less lovely possible actions one might take undocumented (for instance - the success_hook, error_hook fields in mmap_action).
The document also covers the new VMA flags implementation which is the only one which will work correctly with mmap_prepare.
--- Documentation/filesystems/index.rst | 1 + Documentation/filesystems/mmap_prepare.rst | 142 +++++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 Documentation/filesystems/mmap_prepare.rst
Documentation/filesystems/index.rst +1 -0
@@ -29,6 +29,7 @@ algorithms work. fiemap files locks + mmap_prepare multigrain-ts mount_api quota
Documentation/filesystems/mmap_prepare.rst new file +142 -0
142 lines not on this page · raw message
Signature · 1 line
2.53.0
Lorenzo Stoakes (Oracle)
maintainerPATCH v2 3/16 mm: document vm_operations_struct->open the same as close()
In reply to Lorenzo Stoakes (Oracle)Describe when the operation is invoked and the context in which it is invoked, matching the description already added for vm_op->close().
While we're here, update all outdated references to an 'area' field for VMAs to the more consistent 'vma'.
--- include/linux/mm.h | 15 ++++++++++----- tools/testing/vma/include/dup.h | 5 +++++ 2 files changed, 15 insertions(+), 5 deletions(-)
include/linux/mm.h +10 -5
@@ -766,15 +766,20 @@ struct vm_uffd_ops;* to the functions called when a no-page or a wp-page exception occurs. */ struct vm_operations_struct { -void (*open)(struct vm_area_struct * area); +/** +* @open: Called when a VMA is remapped, split or forked. Not called +* upon first mapping a VMA. +* Context: User context. May sleep. Caller holds mmap_lock. +*/ +void (*open)(struct vm_area_struct *vma);/** * @close: Called when the VMA is being removed from the MM. * Context: User context. May sleep. Caller holds mmap_lock. */ -void (*close)(struct vm_area_struct * area); +void (*close)(struct vm_area_struct *vma);/* Called any time before splitting to check if it's allowed */ -int (*may_split)(struct vm_area_struct *area,unsigned long addr); -int (*mremap)(struct vm_area_struct *area); +int (*may_split)(struct vm_area_struct *vma,unsigned long addr); +int (*mremap)(struct vm_area_struct *vma);/* * Called by mprotect() to make driver-specific permission * checks before mprotect() is finalised. The VMA must not
@@ -786,7 +791,7 @@ struct vm_operations_struct {vm_fault_t (*huge_fault)(struct vm_fault *vmf,unsigned int order);vm_fault_t (*map_pages)(struct vm_fault *vmf,pgoff_t start_pgoff,pgoff_t end_pgoff); -unsigned long (*pagesize)(struct vm_area_struct * area); +unsigned long (*pagesize)(struct vm_area_struct *vma);/* notification that a previously read-only page is about to become * writable, if an error is returned it will cause a SIGBUS */
tools/testing/vma/include/dup.h +5 -0
@@ -632,6 +632,11 @@ struct vm_area_struct { } __randomize_layout;struct vm_operations_struct { +/** +* @open: Called when a VMA is remapped, split or forked. Not called +* upon first mapping a VMA. +* Context: User context. May sleep. Caller holds mmap_lock. +*/ void (*open)(struct vm_area_struct * area);/** * @close: Called when the VMA is being removed from the MM.
Signature · 1 line
2.53.0
Lorenzo Stoakes (Oracle)
maintainerPATCH v2 4/16 mm: add vm_ops->mapped hook
In reply to Lorenzo Stoakes (Oracle)Previously, when a driver needed to do something like establish a reference count, it could do so in the mmap hook in the knowledge that the mapping would succeed.
With the introduction of f_op->mmap_prepare this is no longer the case, as it is invoked prior to actually establishing the mapping.
mmap_prepare is not appropriate for this kind of thing as it is called before any merge might take place, and after which an error might occur meaning resources could be leaked.
To take this into account, introduce a new vm_ops->mapped callback which is invoked when the VMA is first mapped (though notably - not when it is merged - which is correct and mirrors existing mmap/open/close behaviour).
We do better that vm_ops->open() here, as this callback can return an error, at which point the VMA will be unmapped.
Note that vm_ops->mapped() is invoked after any mmap action is complete (such as I/O remapping).
We intentionally do not expose the VMA at this point, exposing only the fields that could be used, and an output parameter in case the operation needs to update the vma->vm_private_data field.
In order to deal with stacked filesystems which invoke inner filesystem's mmap() invocations, add __compat_vma_mapped() and invoke it on vfs_mmap() (via compat_vma_mmap()) to ensure that the mapped callback is handled when an mmap() caller invokes a nested filesystem's mmap_prepare() callback.
We can now also remove call_action_complete() and invoke mmap_action_complete() directly, as we separate out the rmap lock logic to be called in __mmap_region() instead via maybe_drop_file_rmap_lock().
We also abstract unmapping of a VMA on mmap action completion into its own helper function, unmap_vma_locked().
Update the mmap_prepare documentation to describe the mapped hook and make it clear what its intended use is.
Additionally, update VMA userland test headers to reflect the change.
--- Documentation/filesystems/mmap_prepare.rst | 15 ++++ include/linux/fs.h | 9 ++- include/linux/mm.h | 17 +++++ mm/internal.h | 8 ++ mm/util.c | 85 ++++++++++++++++------ mm/vma.c | 41 ++++++++--- tools/testing/vma/include/dup.h | 27 ++++++- 7 files changed, 164 insertions(+), 38 deletions(-)
Documentation/filesystems/mmap_prepare.rst +15 -0
@@ -25,6 +25,21 @@ That is - no resources should be allocated nor state updated to reflect that a mapping has been established, as the mapping may either be merged, or fail to be mapped after the callback is complete. +Mapped callback +--------------- + +If resources need to be allocated per-mapping, or state such as a reference +count needs to be manipulated, this should be done using the ``vm_ops->mapped`` +hook, which itself should be set by the >mmap_prepare hook. + +This callback is only invoked if a new mapping has been established and was not +merged with any other, and is invoked at a point where no error may occur before +the mapping is established. + +You may return an error to the callback itself, which will cause the mapping to +become unmapped and an error returned to the mmap() caller. This is useful if +resources need to be allocated, and that allocation might fail. + How To Use ==========
include/linux/fs.h +8 -1
@@ -2059,13 +2059,20 @@ static inline bool can_mmap_file(struct file *file) }int compat_vma_mmap (struct file *file,struct vm_area_struct *vma); +int __vma_check_mmap_hook (struct vm_area_struct *vma);static inline int vfs_mmap (struct file *file,struct vm_area_struct *vma) { +int err; +if (file->f_op->mmap_prepare)return compat_vma_mmap (file, vma); -return file->f_op->mmap (file, vma); + err = file->f_op->mmap (file, vma); +if (err) +return err; + +return __vma_check_mmap_hook (vma); }static inline int vfs_mmap_prepare (struct file *file,struct vm_area_desc *desc)
include/linux/mm.h +17 -0
@@ -777,6 +777,23 @@ struct vm_operations_struct {* Context: User context. May sleep. Caller holds mmap_lock. */ void (*close)(struct vm_area_struct *vma); +/** +* @mapped: Called when the VMA is first mapped in the MM. Not called if +* the new VMA is merged with an adjacent VMA. +* +* The @vm_private_data field is an output field allowing the user to +* modify vma->vm_private_data as necessary. +* +* ONLY valid if set from f_op->mmap_prepare. Will result in an error if +* set from f_op->mmap. +* +* Returns %0 on success, or an error otherwise. On error, the VMA will +* be unmapped. +* +* Context: User context. May sleep. Caller holds mmap_lock. +*/ +int (*mapped)(unsigned long start,unsigned long end,pgoff_t pgoff, +const struct file *file,void **vm_private_data);/* Called any time before splitting to check if it's allowed */ int (*may_split)(struct vm_area_struct *vma,unsigned long addr);int (*mremap)(struct vm_area_struct *vma);
mm/internal.h +8 -0
@@ -202,6 +202,14 @@ static inline void vma_close(struct vm_area_struct *vma)/* unmap_vmas is in mm/memory.c */ void unmap_vmas (struct mmu_gather *tlb,struct unmap_desc *unmap); +static inline void unmap_vma_locked (struct vm_area_struct *vma) +{ +const size_t len =vma_pages (vma) <<PAGE_SHIFT ; + +mmap_assert_write_locked (vma->vm_mm); +do_munmap (vma->vm_mm, vma->vm_start, len,NULL ); +} +#ifdef CONFIG_MMU static inline void get_anon_vma (struct anon_vma *anon_vma)
mm/util.c +61 -24
111 lines not on this page · raw message
mm/vma.c +29 -12
58 lines not on this page · raw message
tools/testing/vma/include/dup.h +26 -1
@@ -642,7 +642,24 @@ struct vm_operations_struct {* @close: Called when the VMA is being removed from the MM. * Context: User context. May sleep. Caller holds mmap_lock. */ -void (*close)(struct vm_area_struct * area); +void (*close)(struct vm_area_struct *vma); +/** +* @mapped: Called when the VMA is first mapped in the MM. Not called if +* the new VMA is merged with an adjacent VMA. +* +* The @vm_private_data field is an output field allowing the user to +* modify vma->vm_private_data as necessary. +* +* ONLY valid if set from f_op->mmap_prepare. Will result in an error if +* set from f_op->mmap. +* +* Returns %0 on success, or an error otherwise. On error, the VMA will +* be unmapped. +* +* Context: User context. May sleep. Caller holds mmap_lock. +*/ +int (*mapped)(unsigned long start,unsigned long end,pgoff_t pgoff, +const struct file *file,void **vm_private_data);/* Called any time before splitting to check if it's allowed */ int (*may_split)(struct vm_area_struct *area,unsigned long addr);int (*mremap)(struct vm_area_struct *area);
@@ -1500,3 +1517,11 @@ static inline pgprot_t vma_get_page_prot(vma_flags_t vma_flags)return vm_get_page_prot (vm_flags); } + +static inline void unmap_vma_locked (struct vm_area_struct *vma) +{ +const size_t len =vma_pages (vma) <<PAGE_SHIFT ; + +mmap_assert_write_locked (vma->vm_mm); +do_munmap (vma->vm_mm, vma->vm_start, len,NULL ); +}
Signature · 1 line
2.53.0
Lorenzo Stoakes (Oracle)
maintainerPATCH v2 5/16 fs: afs: correctly drop reference count on mapping failure
In reply to Lorenzo Stoakes (Oracle)Commit 9d5403b1036c ("fs: convert most other generic_file_*mmap() users to .mmap_prepare()") updated AFS to use the mmap_prepare callback in favour of the deprecated mmap callback.
However, it did not account for the fact that mmap_prepare is called pre-merge, and may then be merged, nor that mmap_prepare can fail to map due to an out of memory error.
Both of those are cases in which we should not be incrementing a reference count.
With the newly added vm_ops->mapped callback available, we can simply defer this operation to that callback which is only invoked once the mapping is successfully in place (but not yet visible to userspace as the mmap and VMA write locks are held).
Therefore add afs_mapped() to implement this callback for AFS, and remove the code doing so in afs_mmap_prepare().
Also update afs_vm_open(), afs_vm_close() and afs_vm_map_pages() to be consistent in how the vnode is accessed.
--- fs/afs/file.c | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-)
fs/afs/file.c +26 -10
@@ -28,6 +28,8 @@ static ssize_t afs_file_splice_read(struct file *in, loff_t *ppos,static void afs_vm_open (struct vm_area_struct *area);static void afs_vm_close (struct vm_area_struct *area);static vm_fault_t afs_vm_map_pages (struct vm_fault *vmf,pgoff_t start_pgoff,pgoff_t end_pgoff); +static int afs_mapped (unsigned long start,unsigned long end,pgoff_t pgoff, +const struct file *file,void **vm_private_data);const struct file_operations afs_file_operations = { .open = afs_open,
@@ -61,6 +63,7 @@ const struct address_space_operations afs_file_aops = { };static const struct vm_operations_struct afs_vm_ops = { + .mapped = afs_mapped, .open = afs_vm_open, .close = afs_vm_close, .fault = filemap_fault,
@@ -494,32 +497,45 @@ static void afs_drop_open_mmap(struct afs_vnode *vnode)*/ static int afs_file_mmap_prepare (struct vm_area_desc *desc) { -struct afs_vnode *vnode =AFS_FS_I (file_inode (desc->file));int ret; -afs_add_open_mmap (vnode); - ret =generic_file_mmap_prepare (desc); -if (ret ==0 ) - desc->vm_ops = &afs_vm_ops; -else -afs_drop_open_mmap (vnode); +if (ret) +return ret; + + desc->vm_ops = &afs_vm_ops;return ret; } +static int afs_mapped (unsigned long start,unsigned long end,pgoff_t pgoff, +const struct file *file,void **vm_private_data) +{ +struct afs_vnode *vnode =AFS_FS_I (file_inode (file)); + +afs_add_open_mmap (vnode); +return 0 ; +} +static void afs_vm_open (struct vm_area_struct *vma) { -afs_add_open_mmap (AFS_FS_I (file_inode (vma->vm_file))); +struct file *file = vma->vm_file; +struct afs_vnode *vnode =AFS_FS_I (file_inode (file)); + +afs_add_open_mmap (vnode); }static void afs_vm_close (struct vm_area_struct *vma) { -afs_drop_open_mmap (AFS_FS_I (file_inode (vma->vm_file))); +struct file *file = vma->vm_file; +struct afs_vnode *vnode =AFS_FS_I (file_inode (file)); + +afs_drop_open_mmap (vnode); }static vm_fault_t afs_vm_map_pages (struct vm_fault *vmf,pgoff_t start_pgoff,pgoff_t end_pgoff) { -struct afs_vnode *vnode =AFS_FS_I (file_inode (vmf->vma->vm_file)); +struct file *file = vmf->vma->vm_file; +struct afs_vnode *vnode =AFS_FS_I (file_inode (file));if (afs_check_validity (vnode))return filemap_map_pages (vmf, start_pgoff, end_pgoff);
Signature · 1 line
2.53.0
Lorenzo Stoakes (Oracle)
maintainerPATCH v2 6/16 mm: add mmap_action_simple_ioremap()
In reply to Lorenzo Stoakes (Oracle)Currently drivers use vm_iomap_memory() as a simple helper function for I/O remapping memory over a range starting at a specified physical address over a specified length.
In order to utilise this from mmap_prepare, separate out the core logic into __simple_ioremap_prep(), update vm_iomap_memory() to use it, and add simple_ioremap_prepare() to do the same with a VMA descriptor object.
We also add MMAP_SIMPLE_IO_REMAP and relevant fields to the struct mmap_action type to permit this operation also.
We use mmap_action_ioremap() to set up the actual I/O remap operation once we have checked and figured out the parameters, which makes simple_ioremap_prepare() easy to implement.
We then add mmap_action_simple_ioremap() to allow drivers to make use of this mode.
We update the mmap_prepare documentation to describe this mode.
Finally, we update the VMA tests to reflect this change.
--- Documentation/filesystems/mmap_prepare.rst | 3 + include/linux/mm.h | 24 +++++- include/linux/mm_types.h | 6 +- mm/internal.h | 2 + mm/memory.c | 87 +++++++++++++++------- mm/util.c | 12 +++ tools/testing/vma/include/dup.h | 6 +- 7 files changed, 112 insertions(+), 28 deletions(-)
Documentation/filesystems/mmap_prepare.rst +3 -0
@@ -153,5 +153,8 @@ pointer. These are: * mmap_action_ioremap_full() - Same as mmap_action_ioremap(), only remaps the entire mapping from ``start_pfn`` onward. +* mmap_action_simple_ioremap() - Sets up an I/O remap from a specified + physical address and over a specified length. + **NOTE:** The ``action`` field should never normally be manipulated directly, rather you ought to use one of these helpers.
include/linux/mm.h +23 -1
@@ -4337,11 +4337,33 @@ static inline void mmap_action_ioremap(struct vm_area_desc *desc,* @start_pfn: The first PFN in the range to remap. */ static inline void mmap_action_ioremap_full (struct vm_area_desc *desc, -unsigned long start_pfn) +unsigned long start_pfn) {mmap_action_ioremap (desc, desc->start, start_pfn,vma_desc_size (desc)); } +/** +* mmap_action_simple_ioremap - helper for mmap_prepare hook to specify that the +* physical range in [start_phys_addr, start_phys_addr + size) should be I/O +* remapped. +* @desc: The VMA descriptor for the VMA requiring remap. +* @start_phys_addr: Start of the physical memory to be mapped. +* @size: Size of the area to map. +* +* NOTE: Some drivers might want to tweak desc->page_prot for purposes of +* write-combine or similar. +*/ +static inline void mmap_action_simple_ioremap (struct vm_area_desc *desc, +phys_addr_t start_phys_addr, +unsigned long size) +{ +struct mmap_action *action = &desc->action; + + action->simple_ioremap.start_phys_addr = start_phys_addr; + action->simple_ioremap.size = size; + action->type =MMAP_SIMPLE_IO_REMAP ; +} +int mmap_action_prepare (struct vm_area_desc *desc);int mmap_action_complete (struct vm_area_struct *vma,struct mmap_action *action);
include/linux/mm_types.h +5 -1
@@ -814,6 +814,7 @@ enum mmap_action_type {MMAP_NOTHING ,/* Mapping is complete, no further action. */ MMAP_REMAP_PFN ,/* Remap PFN range. */ MMAP_IO_REMAP_PFN ,/* I/O remap PFN range. */ + MMAP_SIMPLE_IO_REMAP, /* I/O remap with guardrails. */ };/*
@@ -822,13 +823,16 @@ enum mmap_action_type {*/ struct mmap_action {union { -/* Remap range. */ struct {unsigned long start;unsigned long start_pfn;unsigned long size;pgprot_t pgprot; } remap; +struct { +phys_addr_t start_phys_addr; +unsigned long size; + } simple_ioremap; };enum mmap_action_type type;
mm/internal.h +2 -0
@@ -1804,6 +1804,8 @@ int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm);int remap_pfn_range_prepare (struct vm_area_desc *desc);int remap_pfn_range_complete (struct vm_area_struct *vma,struct mmap_action *action); +int simple_ioremap_prepare (struct vm_area_desc *desc); +/* No simple_ioremap_complete, is ultimately handled by remap complete. */ static inline int io_remap_pfn_range_prepare (struct vm_area_desc *desc) {
mm/memory.c +62 -25
100 lines not on this page · raw message
mm/util.c +12 -0
@@ -1390,6 +1390,8 @@ int mmap_action_prepare(struct vm_area_desc *desc)return remap_pfn_range_prepare (desc);case MMAP_IO_REMAP_PFN :return io_remap_pfn_range_prepare (desc); +case MMAP_SIMPLE_IO_REMAP : +return simple_ioremap_prepare (desc); }WARN_ON_ONCE (1 );
@@ -1421,6 +1423,14 @@ int mmap_action_complete(struct vm_area_struct *vma,case MMAP_IO_REMAP_PFN : err =io_remap_pfn_range_complete (vma, action);break ; +case MMAP_SIMPLE_IO_REMAP : +/* +* The simple I/O remap should have been delegated to an I/O +* remap. +*/ +WARN_ON_ONCE (1 ); + err = -EINVAL ; +break ; }return mmap_action_finish (vma, action, err);
@@ -1434,6 +1444,7 @@ int mmap_action_prepare(struct vm_area_desc *desc)break ;case MMAP_REMAP_PFN :case MMAP_IO_REMAP_PFN : +case MMAP_SIMPLE_IO_REMAP :WARN_ON_ONCE (1 );/* nommu cannot handle these. */ break ; }
@@ -1452,6 +1463,7 @@ int mmap_action_complete(struct vm_area_struct *vma,break ;case MMAP_REMAP_PFN :case MMAP_IO_REMAP_PFN : +case MMAP_SIMPLE_IO_REMAP :WARN_ON_ONCE (1 );/* nommu cannot handle this. */ err = -EINVAL ;
tools/testing/vma/include/dup.h +5 -1
@@ -453,6 +453,7 @@ enum mmap_action_type {MMAP_NOTHING ,/* Mapping is complete, no further action. */ MMAP_REMAP_PFN ,/* Remap PFN range. */ MMAP_IO_REMAP_PFN ,/* I/O remap PFN range. */ + MMAP_SIMPLE_IO_REMAP, /* I/O remap with guardrails. */ };/*
@@ -461,13 +462,16 @@ enum mmap_action_type {*/ struct mmap_action {union { -/* Remap range. */ struct {unsigned long start;unsigned long start_pfn;unsigned long size;pgprot_t pgprot; } remap; +struct { +phys_addr_t start; +unsigned long len; + } simple_ioremap; };enum mmap_action_type type;
Signature · 1 line
2.53.0
Lorenzo Stoakes (Oracle)
maintainerPATCH v2 7/16 misc: open-dice: replace deprecated mmap hook with mmap_prepare
In reply to Lorenzo Stoakes (Oracle)The f_op->mmap interface is deprecated, so update driver to use its successor, mmap_prepare.
The driver previously used vm_iomap_memory(), so this change replaces it with its mmap_prepare equivalent, mmap_action_simple_ioremap().
--- drivers/misc/open-dice.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)
drivers/misc/open-dice.c +11 -8
@@ -86,29 +86,32 @@ static ssize_t open_dice_write(struct file *filp, const char __user *ptr,/* * Creates a mapping of the reserved memory region in user address space. */ -static int open_dice_mmap (struct file *filp,struct vm_area_struct *vma) +static int open_dice_mmap_prepare (struct vm_area_desc *desc) { +struct file *filp = desc->file;struct open_dice_drvdata *drvdata =to_open_dice_drvdata (filp); -if (vma->vm_flags &VM_MAYSHARE ) { +if (vma_desc_test (desc,VMA_MAYSHARE_BIT )) {/* Do not allow userspace to modify the underlying data. */ -if (vma->vm_flags &VM_WRITE ) +if (vma_desc_test (desc,VMA_WRITE_BIT ))return -EPERM ;/* Ensure userspace cannot acquire VM_WRITE later. */ -vm_flags_clear (vma,VM_MAYWRITE ); +vma_desc_clear_flags (desc,VMA_MAYWRITE_BIT ); }/* Create write-combine mapping so all clients observe a wipe. */ - vma->vm_page_prot =pgprot_writecombine (vma->vm_page_prot); -vm_flags_set (vma,VM_DONTCOPY |VM_DONTDUMP ); -return vm_iomap_memory (vma, drvdata->rmem->base, drvdata->rmem->size); + desc->page_prot =pgprot_writecombine (desc->page_prot); +vma_desc_set_flags (desc,VMA_DONTCOPY_BIT ,VMA_DONTDUMP_BIT ); +mmap_action_simple_ioremap (desc, drvdata->rmem->base, + drvdata->rmem->size); +return 0 ; }static const struct file_operations open_dice_fops = { .owner =THIS_MODULE , .read = open_dice_read, .write = open_dice_write, - .mmap = open_dice_mmap, + .mmap_prepare = open_dice_mmap_prepare, };static int __init open_dice_probe (struct platform_device *pdev)
Signature · 1 line
2.53.0
Lorenzo Stoakes (Oracle)
maintainerPATCH v2 8/16 hpet: replace deprecated mmap hook with mmap_prepare
In reply to Lorenzo Stoakes (Oracle)The f_op->mmap interface is deprecated, so update driver to use its successor, mmap_prepare.
The driver previously used vm_iomap_memory(), so this change replaces it with its mmap_prepare equivalent, mmap_action_simple_ioremap().
--- drivers/char/hpet.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/char/hpet.c +7 -5
@@ -354,8 +354,9 @@ static __init int hpet_mmap_enable(char *str) }__setup ("hpet_mmap=" , hpet_mmap_enable); -static int hpet_mmap (struct file *file,struct vm_area_struct *vma) +static int hpet_mmap_prepare (struct vm_area_desc *desc) { +struct file *file = desc->file;struct hpet_dev *devp;unsigned long addr;
@@ -368,11 +369,12 @@ static int hpet_mmap(struct file *file, struct vm_area_struct *vma)if (addr & (PAGE_SIZE -1 ))return -ENOSYS ; - vma->vm_page_prot =pgprot_noncached (vma->vm_page_prot); - return vm_iomap_memory(vma, addr, PAGE_SIZE); + desc->page_prot =pgprot_noncached (desc->page_prot); +mmap_action_simple_ioremap (desc, addr,PAGE_SIZE ); + return 0; }#else -static int hpet_mmap (struct file *file,struct vm_area_struct *vma) +static int hpet_mmap_prepare (struct vm_area_desc *desc) {return -ENOSYS ; }
@@ -710,7 +712,7 @@ static const struct file_operations hpet_fops = { .open = hpet_open, .release = hpet_release, .fasync = hpet_fasync, - .mmap = hpet_mmap, + .mmap_prepare = hpet_mmap_prepare, };static int hpet_is_known (struct hpet_data *hdp)
Signature · 1 line
2.53.0
Lorenzo Stoakes (Oracle)
maintainerPATCH v2 9/16 mtdchar: replace deprecated mmap hook with mmap_prepare, clean up
In reply to Lorenzo Stoakes (Oracle)Replace the deprecated mmap callback with mmap_prepare.
Commit f5cf8f07423b ("mtd: Disable mtdchar mmap on MMU systems") commented out the CONFIG_MMU part of this function back in 2012, so after ~14 years it's probably reasonable to remove this altogether rather than updating dead code.
--- drivers/mtd/mtdchar.c | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-)
drivers/mtd/mtdchar.c +3 -18
@@ -1376,27 +1376,12 @@ static unsigned mtdchar_mmap_capabilities(struct file *file)/* * set up a mapping for shared memory segments */ -static int mtdchar_mmap (struct file *file,struct vm_area_struct *vma) +static int mtdchar_mmap_prepare (struct vm_area_desc *desc) {#ifdef CONFIG_MMU -struct mtd_file_info *mfi = file->private_data; -struct mtd_info *mtd = mfi->mtd; -struct map_info *map = mtd->priv; - -/* This is broken because it assumes the MTD device is map-based -and that mtd->priv is a valid struct map_info. It should be -replaced with something that uses the mtd_get_unmapped_area() -operation properly. */ -if (0 /*mtd->type == MTD_RAM || mtd->type == MTD_ROM*/ ) { -#ifdef pgprot_noncached -if (file->f_flags &O_DSYNC || map->phys >=__pa (high_memory)) - vma->vm_page_prot =pgprot_noncached (vma->vm_page_prot); -#endif -return vm_iomap_memory (vma, map->phys, map->size); - }return -ENODEV ;#else -return vma->vm_flags &VM_SHARED ?0 : -EACCES ; +return vma_desc_test (desc,VMA_SHARED_BIT ) ?0 : -EACCES ;#endif }
@@ -1411,7 +1396,7 @@ static const struct file_operations mtd_fops = {#endif .open = mtdchar_open, .release = mtdchar_close, - .mmap = mtdchar_mmap, + .mmap_prepare = mtdchar_mmap_prepare, #ifndef CONFIG_MMU .get_unmapped_area = mtdchar_get_unmapped_area, .mmap_capabilities = mtdchar_mmap_capabilities,
Signature · 1 line
2.53.0
Lorenzo Stoakes (Oracle)
maintainerPATCH v2 10/16 stm: replace deprecated mmap hook with mmap_prepare
In reply to Lorenzo Stoakes (Oracle)The f_op->mmap interface is deprecated, so update driver to use its successor, mmap_prepare.
The driver previously used vm_iomap_memory(), so this change replaces it with its mmap_prepare equivalent, mmap_action_simple_ioremap().
Also, in order to correctly maintain reference counting, add a vm_ops->mapped callback to increment the reference count when successfully mapped.
--- drivers/hwtracing/stm/core.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-)
drivers/hwtracing/stm/core.c +21 -10
@@ -666,6 +666,16 @@ static ssize_t stm_char_write(struct file *file, const char __user *buf,return count; } +static int stm_mmap_mapped (unsigned long start,unsigned long end,pgoff_t pgoff, +const struct file *file,void **vm_private_data) +{ +struct stm_file *stmf = file->private_data; +struct stm_device *stm = stmf->stm; + +pm_runtime_get_sync (&stm->dev); +return 0 ; +} +static void stm_mmap_open (struct vm_area_struct *vma) {struct stm_file *stmf = vma->vm_file->private_data;
@@ -684,12 +694,14 @@ static void stm_mmap_close(struct vm_area_struct *vma) }static const struct vm_operations_struct stm_mmap_vmops = { + .mapped = stm_mmap_mapped, .open = stm_mmap_open, .close = stm_mmap_close, }; -static int stm_char_mmap (struct file *file,struct vm_area_struct *vma) +static int stm_char_mmap_prepare (struct vm_area_desc *desc) { +struct file *file = desc->file;struct stm_file *stmf = file->private_data;struct stm_device *stm = stmf->stm;unsigned long size, phys;
@@ -697,10 +709,10 @@ static int stm_char_mmap(struct file *file, struct vm_area_struct *vma)if (!stm->data->mmio_addr)return -EOPNOTSUPP ; -if (vma->vm_pgoff) +if (desc->pgoff)return -EINVAL ; - size = vma->vm_end - vma->vm_start; + size =vma_desc_size (desc);if (stmf->output.nr_chans * stm->data->sw_mmiosz != size)return -EINVAL ;
@@ -712,13 +724,12 @@ static int stm_char_mmap(struct file *file, struct vm_area_struct *vma)if (!phys)return -EINVAL ; -pm_runtime_get_sync (&stm->dev); - - vma->vm_page_prot =pgprot_noncached (vma->vm_page_prot); -vm_flags_set (vma,VM_IO |VM_DONTEXPAND |VM_DONTDUMP ); - vma->vm_ops = &stm_mmap_vmops; -vm_iomap_memory (vma, phys, size); + desc->page_prot =pgprot_noncached (desc->page_prot); +vma_desc_set_flags (desc,VMA_IO_BIT ,VMA_DONTEXPAND_BIT , +VMA_DONTDUMP_BIT ); + desc->vm_ops = &stm_mmap_vmops; +mmap_action_simple_ioremap (desc, phys, size);return 0 ; }
@@ -836,7 +847,7 @@ static const struct file_operations stm_fops = { .open = stm_char_open, .release = stm_char_release, .write = stm_char_write, - .mmap = stm_char_mmap, + .mmap_prepare = stm_char_mmap_prepare, .unlocked_ioctl = stm_char_ioctl, .compat_ioctl = compat_ptr_ioctl, };
Signature · 1 line
2.53.0
Lorenzo Stoakes (Oracle)
maintainerPATCH v2 11/16 staging: vme_user: replace deprecated mmap hook with mmap_prepare
In reply to Lorenzo Stoakes (Oracle)The f_op->mmap interface is deprecated, so update driver to use its successor, mmap_prepare.
The driver previously used vm_iomap_memory(), so this change replaces it with its mmap_prepare equivalent, mmap_action_simple_ioremap().
Functions that wrap mmap() are also converted to wrap mmap_prepare() instead.
Also update the documentation accordingly.
--- Documentation/driver-api/vme.rst | 2 +- drivers/staging/vme_user/vme.c | 20 +++++------ drivers/staging/vme_user/vme.h | 2 +- drivers/staging/vme_user/vme_user.c | 51 +++++++++++++++++------------ 4 files changed, 42 insertions(+), 33 deletions(-)
Documentation/driver-api/vme.rst +1 -1
@@ -107,7 +107,7 @@ The function :c:func:`vme_master_read` can be used to read from and In addition to simple reads and writes, :c:func:`vme_master_rmw` is provided to do a read-modify-write transaction. Parts of a VME window can also be mapped -into user space memory using :c:func:`vme_master_mmap`. +into user space memory using :c:func:`vme_master_mmap_prepare`. Slave windows
drivers/staging/vme_user/vme.c +10 -10
@@ -735,9 +735,9 @@ unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask,EXPORT_SYMBOL (vme_master_rmw);/** -* vme_master_mmap - Mmap region of VME master window. +* vme_master_mmap_prepare - Mmap region of VME master window. * @resource: Pointer to VME master resource. -* @vma: Pointer to definition of user mapping. +* @desc: Pointer to descriptor of user mapping. * * Memory map a region of the VME master window into user space. *
@@ -745,12 +745,13 @@ EXPORT_SYMBOL(vme_master_rmw);* resource or -EFAULT if map exceeds window size. Other generic mmap * errors may also be returned. */ -int vme_master_mmap (struct vme_resource *resource,struct vm_area_struct *vma) +int vme_master_mmap_prepare (struct vme_resource *resource, +struct vm_area_desc *desc) { +const unsigned long vma_size =vma_desc_size (desc);struct vme_bridge *bridge =find_bridge (resource);struct vme_master_resource *image;phys_addr_t phys_addr; -unsigned long vma_size;if (resource->type !=VME_MASTER ) {dev_err (bridge->parent,"Not a master resource\n" );
@@ -758,19 +759,18 @@ int vme_master_mmap(struct vme_resource *resource, struct vm_area_struct *vma) } image = list_entry(resource->entry, struct vme_master_resource, list); - phys_addr = image->bus_resource.start + (vma->vm_pgoff <<PAGE_SHIFT ); - vma_size = vma->vm_end - vma->vm_start; + phys_addr = image->bus_resource.start + (desc->pgoff <<PAGE_SHIFT );if (phys_addr + vma_size > image->bus_resource.end +1 ) {dev_err (bridge->parent,"Map size cannot exceed the window size\n" );return -EFAULT ; } - vma->vm_page_prot =pgprot_noncached (vma->vm_page_prot); - -return vm_iomap_memory (vma, phys_addr, vma->vm_end - vma->vm_start); + desc->page_prot =pgprot_noncached (desc->page_prot); +mmap_action_simple_ioremap (desc, phys_addr, vma_size); +return 0 ; } -EXPORT_SYMBOL (vme_master_mmap); +EXPORT_SYMBOL (vme_master_mmap_prepare);/** * vme_master_free - Free VME master window
drivers/staging/vme_user/vme.h +1 -1
@@ -151,7 +151,7 @@ ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count,ssize_t vme_master_write (struct vme_resource *resource,void *buf,size_t count,loff_t offset);unsigned int vme_master_rmw (struct vme_resource *resource,unsigned int mask,unsigned int compare,unsigned int swap,loff_t offset); -int vme_master_mmap (struct vme_resource *resource,struct vm_area_struct *vma); +int vme_master_mmap_prepare (struct vme_resource *resource,struct vm_area_desc *desc);void vme_master_free (struct vme_resource *resource);struct vme_resource *vme_dma_request (struct vme_dev *vdev,u32 route);
drivers/staging/vme_user/vme_user.c +30 -21
83 lines not on this page · raw message
Signature · 1 line
2.53.0
Lorenzo Stoakes (Oracle)
maintainerPATCH v2 12/16 mm: allow handling of stacked mmap_prepare hooks in more drivers
In reply to Lorenzo Stoakes (Oracle)While the conversion of mmap hooks to mmap_prepare is underway, we wil encounter situations where mmap hooks need to invoke nested mmap_prepare hooks.
The nesting of mmap hooks is termed 'stacking'. In order to flexibly facilitate the conversion of custom mmap hooks in drivers which stack, we must split up the existing compat_vma_mapped() function into two separate functions:
* compat_set_desc_from_vma() - This allows the setting of a vm_area_desc object's fields to the relevant fields of a VMA.
* __compat_vma_mmap() - Once an mmap_prepare hook has been executed upon a vm_area_desc object, this function performs any mmap actions specified by the mmap_prepare hook and then invokes its vm_ops->mapped() hook if any were specified.
In ordinary cases, where a file's f_op->mmap_prepare() hook simply needs to be invoked in a stacked mmap() hook, compat_vma_mmap() can be used.
However some drivers define their own nested hooks, which are invoked in turn by another hook.
A concrete example is vmbus_channel->mmap_ring_buffer(), which is invoked in turn by bin_attribute->mmap():
vmbus_channel->mmap_ring_buffer() has a signature of:
int (*mmap_ring_buffer)(struct vmbus_channel *channel,
struct vm_area_struct *vma);
And bin_attribute->mmap() has a signature of:
int (*mmap)(struct file *, struct kobject *,
const struct bin_attribute *attr, struct vm_area_struct *vma);
And so compat_vma_mmap() cannot be used here for incremental conversion of hooks from mmap() to mmap_prepare().
There are many such instances like this, where conversion to mmap_prepare would otherwise cascade to a huge change set due to nesting of this kind.
The changes in this patch mean we could now instead convert vmbus_channel->mmap_ring_buffer() to vmbus_channel->mmap_prepare_ring_buffer(), and implement something like:
struct vm_area_desc desc; int err; compat_set_desc_from_vm(&desc, file, vma); err = channel->mmap_prepare_ring_buffer(channel, &desc); if (err) return err; return __compat_vma_mmap(&desc, vma);
Allowing us to incrementally update this logic, and other logic like it.
Unfortunately, as part of this change, we need to be able to flexibly assign to the VMA descriptor, so have to remove some of the const declarations within the structure.
Also update the VMA tests to reflect the changes.
--- include/linux/fs.h | 3 + include/linux/mm_types.h | 4 +- mm/util.c | 111 +++++++++++++++++++++++--------- mm/vma.h | 2 +- tools/testing/vma/include/dup.h | 111 ++++++++++++++++++++------------ 5 files changed, 157 insertions(+), 74 deletions(-)
include/linux/fs.h +3 -0
@@ -2058,6 +2058,9 @@ static inline bool can_mmap_file(struct file *file)return true; } +void compat_set_desc_from_vma (struct vm_area_desc *desc,const struct file *file, +const struct vm_area_struct *vma); +int __compat_vma_mmap (struct vm_area_desc *desc,struct vm_area_struct *vma);int compat_vma_mmap (struct file *file,struct vm_area_struct *vma);int __vma_check_mmap_hook (struct vm_area_struct *vma);
include/linux/mm_types.h +2 -2
@@ -891,8 +891,8 @@ static __always_inline bool vma_flags_empty(vma_flags_t *flags)*/ struct vm_area_desc {/* Immutable state. */ -const struct mm_struct *const mm; -struct file *const file;/* May vary from vm_file in stacked callers. */ +struct mm_struct *mm; +struct file *file;/* May vary from vm_file in stacked callers. */ unsigned long start;unsigned long end;
mm/util.c +80 -31
150 lines not on this page · raw message
mm/vma.h +1 -1
@@ -300,7 +300,7 @@ static inline int vma_iter_store_gfp(struct vma_iterator *vmi,* f_op->mmap() but which might have an underlying file system which implements * f_op->mmap_prepare(). */ -static inline void set_vma_from_desc (struct vm_area_struct *vma, +static inline void compat_set_vma_from_desc (struct vm_area_struct *vma,struct vm_area_desc *desc) {/*
tools/testing/vma/include/dup.h +71 -40
155 lines not on this page · raw message
Signature · 1 line
2.53.0
Lorenzo Stoakes (Oracle)
maintainerPATCH v2 13/16 drivers: hv: vmbus: replace deprecated mmap hook with mmap_prepare
In reply to Lorenzo Stoakes (Oracle)The f_op->mmap interface is deprecated, so update the vmbus driver to use its successor, mmap_prepare.
This updates all callbacks which referenced the function pointer hv_mmap_ring_buffer to instead reference hv_mmap_prepare_ring_buffer, utilising the newly introduced compat_set_desc_from_vma() and __compat_vma_mmap() to be able to implement this change.
The UIO HV generic driver is the only user of hv_create_ring_sysfs(), which is the only function which references vmbus_channel->mmap_prepare_ring_buffer which, in turn, is the only external interface to hv_mmap_prepare_ring_buffer.
This patch therefore updates this caller to use mmap_prepare instead, which also previously used vm_iomap_memory(), so this change replaces it with its mmap_prepare equivalent, mmap_action_simple_ioremap().
--- drivers/hv/hyperv_vmbus.h | 4 ++-- drivers/hv/vmbus_drv.c | 27 +++++++++++++++++---------- drivers/uio/uio_hv_generic.c | 11 ++++++----- include/linux/hyperv.h | 4 ++-- 4 files changed, 27 insertions(+), 19 deletions(-)
drivers/hv/hyperv_vmbus.h +2 -2
@@ -545,8 +545,8 @@ static inline int hv_debug_add_dev_dir(struct hv_device *dev)/* Create and remove sysfs entry for memory mapped ring buffers for a channel */ int hv_create_ring_sysfs (struct vmbus_channel *channel, -int (*hv_mmap_ring_buffer)(struct vmbus_channel *channel, -struct vm_area_struct *vma)); +int (*hv_mmap_prepare_ring_buffer)(struct vmbus_channel *channel, +struct vm_area_desc *desc));int hv_remove_ring_sysfs (struct vmbus_channel *channel);#endif /* _HYPERV_VMBUS_H */
drivers/hv/vmbus_drv.c +17 -10
@@ -1951,12 +1951,19 @@ static int hv_mmap_ring_buffer_wrapper(struct file *filp, struct kobject *kobj, struct vm_area_struct *vma) { struct vmbus_channel *channel = container_of(kobj, struct vmbus_channel, kobj); +struct vm_area_desc desc; +int err;/* * hv_(create|remove)_ring_sysfs implementation ensures that mmap_ring_buffer * is not NULL. */ -return channel->mmap_ring_buffer (channel, vma); +compat_set_desc_from_vma (&desc, filp, vma); + err = channel->mmap_prepare_ring_buffer (channel, &desc); +if (err) +return err; + +return __compat_vma_mmap (&desc, vma); }static struct bin_attribute chan_attr_ring_buffer = {
@@ -2048,13 +2055,13 @@ static const struct kobj_type vmbus_chan_ktype = {/** * hv_create_ring_sysfs() - create "ring" sysfs entry corresponding to ring buffers for a channel. * @channel: Pointer to vmbus_channel structure -* @hv_mmap_ring_buffer: function pointer for initializing the function to be called on mmap of +* @hv_mmap_ring_buffer: function pointer for initializing the function to be called on mmap * channel's "ring" sysfs node, which is for the ring buffer of that channel. * Function pointer is of below type: -* int (*hv_mmap_ring_buffer)(struct vmbus_channel *channel, -* struct vm_area_struct *vma)) -* This has a pointer to the channel and a pointer to vm_area_struct, -* used for mmap, as arguments. +* int (*hv_mmap_prepare_ring_buffer)(struct vmbus_channel *channel, +* struct vm_area_desc *desc)) +* This has a pointer to the channel and a pointer to vm_area_desc, +* used for mmap_prepare, as arguments. * * Sysfs node for ring buffer of a channel is created along with other fields, however its * visibility is disabled by default. Sysfs creation needs to be controlled when the use-case
@@ -2071,12 +2078,12 @@ static const struct kobj_type vmbus_chan_ktype = {* Returns 0 on success or error code on failure. */ int hv_create_ring_sysfs (struct vmbus_channel *channel, -int (*hv_mmap_ring_buffer)(struct vmbus_channel *channel, -struct vm_area_struct *vma)) +int (*hv_mmap_prepare_ring_buffer)(struct vmbus_channel *channel, +struct vm_area_desc *desc)) {struct kobject *kobj = &channel->kobj; - channel->mmap_ring_buffer = hv_mmap_ring_buffer; + channel->mmap_prepare_ring_buffer = hv_mmap_prepare_ring_buffer; channel->ring_sysfs_visible = true;return sysfs_update_group (kobj, &vmbus_chan_group);
@@ -2098,7 +2105,7 @@ int hv_remove_ring_sysfs(struct vmbus_channel *channel) channel->ring_sysfs_visible = false; ret =sysfs_update_group (kobj, &vmbus_chan_group); - channel->mmap_ring_buffer =NULL ; + channel->mmap_prepare_ring_buffer =NULL ;return ret; }EXPORT_SYMBOL_GPL (hv_remove_ring_sysfs);
drivers/uio/uio_hv_generic.c +6 -5
@@ -154,15 +154,16 @@ static void hv_uio_rescind(struct vmbus_channel *channel)* The ring buffer is allocated as contiguous memory by vmbus_open */ static int -hv_uio_ring_mmap (struct vmbus_channel *channel,struct vm_area_struct *vma) +hv_uio_ring_mmap_prepare (struct vmbus_channel *channel,struct vm_area_desc *desc) {void *ring_buffer =page_address (channel->ringbuffer_page);if (channel->state !=CHANNEL_OPENED_STATE )return -ENODEV ; -return vm_iomap_memory (vma,virt_to_phys (ring_buffer), - channel->ringbuffer_pagecount <<PAGE_SHIFT ); +mmap_action_simple_ioremap (desc,virt_to_phys (ring_buffer), + channel->ringbuffer_pagecount <<PAGE_SHIFT ); +return 0 ; }/* Callback from VMBUS subsystem when new channel created. */
@@ -183,7 +184,7 @@ hv_uio_new_channel(struct vmbus_channel *new_sc) }set_channel_read_mode (new_sc,HV_CALL_ISR ); - ret =hv_create_ring_sysfs (new_sc, hv_uio_ring_mmap); + ret =hv_create_ring_sysfs (new_sc, hv_uio_ring_mmap_prepare);if (ret) {dev_err (device,"sysfs create ring bin file failed; %d\n" , ret);vmbus_close (new_sc);
@@ -366,7 +367,7 @@ hv_uio_probe(struct hv_device *dev,* or decoupled from uio_hv_generic probe. Userspace programs can make use of inotify * APIs to make sure that ring is created. */ -hv_create_ring_sysfs (channel, hv_uio_ring_mmap); +hv_create_ring_sysfs (channel, hv_uio_ring_mmap_prepare);hv_set_drvdata (dev, pdata);
include/linux/hyperv.h +2 -2
@@ -1015,8 +1015,8 @@ struct vmbus_channel {/* The max size of a packet on this channel */ u32 max_pkt_size; -/* function to mmap ring buffer memory to the channel's sysfs ring attribute */ -int (*mmap_ring_buffer)(struct vmbus_channel *channel,struct vm_area_struct *vma); +/* function to mmap_prepare ring buffer memory to the channel's sysfs ring attribute */ +int (*mmap_prepare_ring_buffer)(struct vmbus_channel *channel,struct vm_area_desc *desc);/* boolean to control visibility of sysfs for ring buffer */ bool ring_sysfs_visible;
Signature · 1 line
2.53.0
Lorenzo Stoakes (Oracle)
maintainerPATCH v2 14/16 uio: replace deprecated mmap hook with mmap_prepare in uio_info
In reply to Lorenzo Stoakes (Oracle)The f_op->mmap interface is deprecated, so update uio_info to use its successor, mmap_prepare.
Therefore, replace the uio_info->mmap hook with a new uio_info->mmap_perepare hook, and update its one user, target_core_user, to both specify this new mmap_prepare hook and also to use the new vm_ops->mapped() hook to continue to maintain a correct udev->kref refcount.
Then update uio_mmap() to utilise the mmap_prepare compatibility layer to invoke this callback from the uio mmap invocation.
--- drivers/target/target_core_user.c | 26 ++++++++++++++++++-------- drivers/uio/uio.c | 10 ++++++++-- include/linux/uio_driver.h | 4 ++-- 3 files changed, 28 insertions(+), 12 deletions(-)
drivers/target/target_core_user.c +18 -8
@@ -1860,6 +1860,17 @@ static struct page *tcmu_try_get_data_page(struct tcmu_dev *udev, uint32_t dpi)return NULL ; } +static int tcmu_vma_mapped (unsigned long start,unsigned long end,pgoff_t pgoff, +const struct file *file,void **vm_private_data) +{ +struct tcmu_dev *udev = *vm_private_data; + +pr_debug ("vma_open\n" ); + +kref_get (&udev->kref); +return 0 ; +} +static void tcmu_vma_open (struct vm_area_struct *vma) {struct tcmu_dev *udev = vma->vm_private_data;
@@ -1919,26 +1930,25 @@ static vm_fault_t tcmu_vma_fault(struct vm_fault *vmf) }static const struct vm_operations_struct tcmu_vm_ops = { + .mapped = tcmu_vma_mapped, .open = tcmu_vma_open, .close = tcmu_vma_close, .fault = tcmu_vma_fault, }; -static int tcmu_mmap (struct uio_info *info,struct vm_area_struct *vma) +static int tcmu_mmap_prepare (struct uio_info *info,struct vm_area_desc *desc) { struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); -vm_flags_set (vma,VM_DONTEXPAND |VM_DONTDUMP ); - vma->vm_ops = &tcmu_vm_ops; +vma_desc_set_flags (desc,VMA_DONTEXPAND_BIT ,VMA_DONTDUMP_BIT ); + desc->vm_ops = &tcmu_vm_ops; - vma->vm_private_data = udev; + desc->private_data = udev;/* Ensure the mmap is exactly the right size */ -if (vma_pages (vma) != udev->mmap_pages) +if (vma_desc_pages (desc) != udev->mmap_pages)return -EINVAL ; -tcmu_vma_open (vma); -return 0 ; }
@@ -2253,7 +2263,7 @@ static int tcmu_configure_device(struct se_device *dev) info->irqcontrol = tcmu_irqcontrol; info->irq =UIO_IRQ_CUSTOM ; - info->mmap = tcmu_mmap; + info->mmap_prepare = tcmu_mmap_prepare; info->open = tcmu_open; info->release = tcmu_release;
drivers/uio/uio.c +8 -2
@@ -850,8 +850,14 @@ static int uio_mmap(struct file *filep, struct vm_area_struct *vma) goto out; } -if (idev->info->mmap) { - ret = idev->info->mmap (idev->info, vma); +if (idev->info->mmap_prepare) { +struct vm_area_desc desc; + +compat_set_desc_from_vma (&desc, filep, vma); + ret = idev->info->mmap_prepare (idev->info, &desc); +if (ret) + goto out; + ret =__compat_vma_mmap (&desc, vma); goto out; }
include/linux/uio_driver.h +2 -2
@@ -97,7 +97,7 @@ struct uio_device {* @irq_flags: flags for request_irq() * @priv: optional private data * @handler: the device's irq handler -* @mmap: mmap operation for this uio device +* @mmap_prepare: mmap_pepare operation for this uio device * @open: open operation for this uio device * @release: release operation for this uio device * @irqcontrol: disable/enable irqs when 0/1 is written to /dev/uioX
@@ -112,7 +112,7 @@ struct uio_info {unsigned long irq_flags;void *priv;irqreturn_t (*handler)(int irq,struct uio_info *dev_info); -int (*mmap)(struct uio_info *info,struct vm_area_struct *vma); +int (*mmap_prepare)(struct uio_info *info,struct vm_area_desc *desc);int (*open)(struct uio_info *info,struct inode *inode);int (*release)(struct uio_info *info,struct inode *inode);int (*irqcontrol)(struct uio_info *info,s32 irq_on);
Signature · 1 line
2.53.0
Lorenzo Stoakes (Oracle)
maintainerPATCH v2 15/16 mm: add mmap_action_map_kernel_pages[_full]()
In reply to Lorenzo Stoakes (Oracle)A user can invoke mmap_action_map_kernel_pages() to specify that the mapping should map kernel pages starting from desc->start of a specified number of pages specified in an array.
In order to implement this, adjust mmap_action_prepare() to be able to return an error code, as it makes sense to assert that the specified parameters are valid as quickly as possible as well as updating the VMA flags to include VMA_MIXEDMAP_BIT as necessary.
This provides an mmap_prepare equivalent of vm_insert_pages().
We additionally update the existing vm_insert_pages() code to use range_in_vma() and add a new range_in_vma_desc() helper function for the mmap_prepare case, sharing the code between the two in range_is_subset().
We add both mmap_action_map_kernel_pages() and mmap_action_map_kernel_pages_full() to allow for both partial and full VMA mappings.
We also add mmap_action_map_kernel_pages_discontig() to allow for discontiguous mapping of kernel pages should the need arise.
We update the documentation to reflect the new features.
Finally, we update the VMA tests accordingly to reflect the changes.
--- Documentation/filesystems/mmap_prepare.rst | 8 ++ include/linux/mm.h | 95 +++++++++++++++++++++- include/linux/mm_types.h | 7 ++ mm/memory.c | 42 +++++++++- mm/util.c | 6 ++ tools/testing/vma/include/dup.h | 7 ++ 6 files changed, 159 insertions(+), 6 deletions(-)
Documentation/filesystems/mmap_prepare.rst +8 -0
@@ -156,5 +156,13 @@ pointer. These are: * mmap_action_simple_ioremap() - Sets up an I/O remap from a specified physical address and over a specified length. +* mmap_action_map_kernel_pages() - Maps a specified array of `struct page` + pointers in the VMA from a specific offset. + +* mmap_action_map_kernel_pages_full() - Maps a specified array of `struct + page` pointers over the entire VMA. The caller must ensure there are + sufficient entries in the page array to cover the entire range of the + described VMA. + **NOTE:** The ``action`` field should never normally be manipulated directly, rather you ought to use one of these helpers.
include/linux/mm.h +93 -2
122 lines not on this page · raw message
include/linux/mm_types.h +7 -0
@@ -815,6 +815,7 @@ enum mmap_action_type {MMAP_REMAP_PFN ,/* Remap PFN range. */ MMAP_IO_REMAP_PFN ,/* I/O remap PFN range. */ MMAP_SIMPLE_IO_REMAP ,/* I/O remap with guardrails. */ + MMAP_MAP_KERNEL_PAGES, /* Map kernel page range from array. */ };/*
@@ -833,6 +834,12 @@ struct mmap_action {phys_addr_t start_phys_addr;unsigned long size; } simple_ioremap; +struct { +unsigned long start; +struct page **pages; +unsigned long nr_pages; +pgoff_t pgoff; + } map_kernel; };enum mmap_action_type type;
mm/memory.c +38 -4
57 lines not on this page · raw message
mm/util.c +6 -0
@@ -1441,6 +1441,8 @@ int mmap_action_prepare(struct vm_area_desc *desc)return io_remap_pfn_range_prepare (desc);case MMAP_SIMPLE_IO_REMAP :return simple_ioremap_prepare (desc); +case MMAP_MAP_KERNEL_PAGES : +return map_kernel_pages_prepare (desc); }WARN_ON_ONCE (1 );
@@ -1472,6 +1474,9 @@ int mmap_action_complete(struct vm_area_struct *vma,case MMAP_IO_REMAP_PFN : err =io_remap_pfn_range_complete (vma, action);break ; +case MMAP_MAP_KERNEL_PAGES : + err =map_kernel_pages_complete (vma, action); +break ;case MMAP_SIMPLE_IO_REMAP :/* * The simple I/O remap should have been delegated to an I/O
@@ -1494,6 +1499,7 @@ int mmap_action_prepare(struct vm_area_desc *desc)case MMAP_REMAP_PFN :case MMAP_IO_REMAP_PFN :case MMAP_SIMPLE_IO_REMAP : +case MMAP_MAP_KERNEL_PAGES :WARN_ON_ONCE (1 );/* nommu cannot handle these. */ break ; }
tools/testing/vma/include/dup.h +7 -0
@@ -454,6 +454,7 @@ enum mmap_action_type {MMAP_REMAP_PFN ,/* Remap PFN range. */ MMAP_IO_REMAP_PFN ,/* I/O remap PFN range. */ MMAP_SIMPLE_IO_REMAP ,/* I/O remap with guardrails. */ + MMAP_MAP_KERNEL_PAGES, /* Map kernel page range from an array. */ };/*
@@ -472,6 +473,12 @@ struct mmap_action {phys_addr_t start;unsigned long len; } simple_ioremap; +struct { +unsigned long start; +struct page **pages; +unsigned long num; +pgoff_t pgoff; + } map_kernel; };enum mmap_action_type type;
Signature · 1 line
2.53.0
Lorenzo Stoakes (Oracle)
maintainerPATCH v2 16/16 mm: on remap assert that input range within the proposed VMA
In reply to Lorenzo Stoakes (Oracle)Now we have range_in_vma_desc(), update remap_pfn_range_prepare() to check whether the input range in contained within the specified VMA, so we can fail at prepare time if an invalid range is specified.
This covers the I/O remap mmap actions also which ultimately call into this function, and other mmap action types either already span the full VMA or check this already.
--- mm/memory.c | 3 +++ 1 file changed, 3 insertions(+)
mm/memory.c +3 -0
@@ -3142,6 +3142,9 @@ int remap_pfn_range_prepare(struct vm_area_desc *desc)const bool is_cow =vma_desc_is_cow_mapping (desc);int err; +if (!range_in_vma_desc (desc, start, end)) +return -EFAULT ; + err =get_remap_pgoff (is_cow, start, end, desc->start, desc->end, pfn, &desc->pgoff);if (err)
Signature · 1 line
2.53.0
Andrew Morton
maintainerQuoted 3 lines · On Mon, 16 Mar 2026 21:11:56 +0000 "Lorenzo Stoakes (Oracle)" <[email protected]>…
This series expands the
mmap_preparefunctionality, which is intended to replace the deprecatedf_op->mmaphook which has been the source of bugs and security issues for some time.
Easy merge, thanks, all added to mm.git's mm-new branch.
Re: [PATCH v2 09/16] mtdchar: replace deprecated mmap hook with mmap_prepare, clean up
In reply to Lorenzo Stoakes (Oracle)----- Ursprüngliche Mail -----
Quoted 1 line
Von: "Lorenzo Stoakes (Oracle)" <[email protected]>
[...]
Quoted 9 lines
Commit
f5cf8f07423b("mtd: Disable mtdchar mmap on MMU systems") commented out theCONFIG_MMUpart of this function back in 2012, so after ~14 years it's probably reasonable to remove this altogether rather than updating dead code.Signed-off-by Lorenzo Stoakes (Oracle) <[email protected]>---
drivers/mtd/mtdchar.c| 21 +++------------------1 file changed, 3 insertions(+), 18 deletions(-)
Thanks,
//richard
Re: [PATCH v2 06/16] mm: add mmap_action_simple_ioremap()
In reply to Lorenzo Stoakes (Oracle)Quoted 16 lines · Lorenzo Stoakes (Oracle)
Currently drivers use
vm_iomap_memory()as a simple helper function for I/O remapping memory over a range starting at a specified physical address over a specified length.In order to utilise this from
mmap_prepare, separate out the core logic into__simple_ioremap_prep(), updatevm_iomap_memory()to use it, and addsimple_ioremap_prepare()to do the same with a VMA descriptor object.We also add
MMAP_SIMPLE_IO_REMAPand relevant fields to the structmmap_actiontype to permit this operation also.We use
mmap_action_ioremap()to set up the actual I/O remap operation once we have checked and figured out the parameters, which makessimple_ioremap_prepare()easy to implement.
Quoted 8 lines
We then add
mmap_action_simple_ioremap()to allow drivers to make use of this mode.We update the
mmap_preparedocumentation to describe this mode.Finally, we update the VMA tests to reflect this change.
Signed-off-by Lorenzo Stoakes (Oracle) <[email protected]>
A couple of nits, but otherwise LGTM.
Quoted 110 lines
--- Documentation/filesystems/mmap_prepare.rst | 3 + include/linux/mm.h | 24 +++++- include/linux/mm_types.h | 6 +- mm/internal.h | 2 + mm/memory.c | 87 +++++++++++++++------- mm/util.c | 12 +++ tools/testing/vma/include/dup.h | 6 +- 7 files changed, 112 insertions(+), 28 deletions(-)Documentation/filesystems/mmap_prepare.rst +3
-0@@ -153,5 +153,8 @@ pointer. These are: * mmap_action_ioremap_full() - Same as mmap_action_ioremap(), only remaps the entire mapping from ``start_pfn`` onward. +* mmap_action_simple_ioremap() - Sets up an I/O remap from a specified + physical address and over a specified length. + **NOTE:** The ``action`` field should never normally be manipulated directly, rather you ought to use one of these helpers.include/linux/mm.h +23
-1@@ -4337,11 +4337,33 @@ static inline void mmap_action_ioremap(struct vm_area_desc *desc,* @start_pfn: The first PFN in the range to remap. */ static inline void mmap_action_ioremap_full (struct vm_area_desc *desc, -unsigned long start_pfn) +unsigned long start_pfn) {mmap_action_ioremap (desc, desc->start, start_pfn,vma_desc_size (desc)); } +/** +* mmap_action_simple_ioremap - helper for mmap_prepare hook to specify that the +* physical range in [start_phys_addr, start_phys_addr + size) should be I/O +* remapped. +* @desc: The VMA descriptor for the VMA requiring remap. +* @start_phys_addr: Start of the physical memory to be mapped. +* @size: Size of the area to map. +* +* NOTE: Some drivers might want to tweak desc->page_prot for purposes of +* write-combine or similar. +*/ +static inline void mmap_action_simple_ioremap (struct vm_area_desc *desc, +phys_addr_t start_phys_addr, +unsigned long size) +{ +struct mmap_action *action = &desc->action; + + action->simple_ioremap.start_phys_addr = start_phys_addr; + action->simple_ioremap.size = size; + action->type =MMAP_SIMPLE_IO_REMAP ; +} +int mmap_action_prepare (struct vm_area_desc *desc);int mmap_action_complete (struct vm_area_struct *vma,struct mmap_action *action);include/linux/mm_types.h +5
-1@@ -814,6 +814,7 @@ enum mmap_action_type {MMAP_NOTHING ,/* Mapping is complete, no further action. */ MMAP_REMAP_PFN ,/* Remap PFN range. */ MMAP_IO_REMAP_PFN ,/* I/O remap PFN range. */ + MMAP_SIMPLE_IO_REMAP, /* I/O remap with guardrails. */ };/* @@ -822,13 +823,16 @@ enum mmap_action_type {*/ struct mmap_action {union { -/* Remap range. */ struct {unsigned long start;unsigned long start_pfn;unsigned long size;pgprot_t pgprot; } remap; +struct { +phys_addr_t start_phys_addr; +unsigned long size; + } simple_ioremap; };enum mmap_action_type type;mm/internal.h +2
-0@@ -1804,6 +1804,8 @@ int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm);int remap_pfn_range_prepare (struct vm_area_desc *desc);int remap_pfn_range_complete (struct vm_area_struct *vma,struct mmap_action *action); +int simple_ioremap_prepare (struct vm_area_desc *desc); +/* No simple_ioremap_complete, is ultimately handled by remap complete. */ static inline int io_remap_pfn_range_prepare (struct vm_area_desc *desc) {mm/memory.c +0
-0
mm/memory.c int remap_pfn_range_complete(struct vm_area_struct *vma, · line 3173
Quoted 4 lines
mm/memory.c +1
-0@@ -3170,6 +3170,59 @@ int remap_pfn_range_complete(struct vm_area_struct *vma,return do_remap_pfn_range (vma, start, pfn, size, prot); } +static int __simple_ioremap_prep(unsigned long vm_start, unsigned long vm_end,
nit: vm_start and vm_end are used only to calculate vm_len. You could reduce the number of arguments by just passing vm_len.
Quoted 39 lines
mm/memory.c +39
-0+ pgoff_t vm_pgoff, phys_addr_t start_phys, + unsigned long size, unsigned long *pfnp) +{ + const unsigned long vm_len = vm_end - vm_start; +unsigned long pfn, pages; + +/* Check that the physical memory area passed in looks valid */ +if (start_phys + size < start_phys) +return -EINVAL ; +/* +* You *really* shouldn't map things that aren't page-aligned, +* but we've historically allowed it because IO memory might +* just have smaller alignment. +*/ + size += start_phys & ~PAGE_MASK ; + pfn = start_phys >>PAGE_SHIFT ; + pages = (size + ~PAGE_MASK ) >>PAGE_SHIFT ; +if (pfn + pages < pfn) +return -EINVAL ; + +/* We start the mapping 'vm_pgoff' pages into the area */ +if (vm_pgoff > pages) +return -EINVAL ; + pfn += vm_pgoff; + pages -= vm_pgoff; + +/* Can we fit all of the mapping? */ +if ((vm_len >>PAGE_SHIFT ) > pages) +return -EINVAL ; + + *pfnp = pfn; +return 0 ; +} + +int simple_ioremap_prepare (struct vm_area_desc *desc) +{ +struct mmap_action *action = &desc->action; +const phys_addr_t start = action->simple_ioremap.start_phys_addr; +const unsigned long size = action->simple_ioremap.size;
mm/memory.c int remap_pfn_range_complete(struct vm_area_struct *vma, · line 3222
Quoted 10 lines
mm/memory.c +10
-0+unsigned long pfn; +int err; + + err =__simple_ioremap_prep (desc->start, desc->end, desc->pgoff, + start, size, &pfn); +if (err) +return err; + +/* The I/O remap logic does the heavy lifting. */ +mmap_action_ioremap (desc, desc->start, pfn,vma_desc_size (desc));
nit: Looks like a perfect opportunity to use mmap_action_ioremap_full() here.
mm/memory.c int remap_pfn_range_complete(struct vm_area_struct *vma, · line 3223
Quoted 1 line
mm/memory.c +1
-0+return mmap_action_prepare (desc);
Ok, so IIUC this uses recursion:mmap_action_prepare(MMAP_SIMPLE_IO_REMAP) -> simple_ioremap_prepare() -> mmap_action_prepare(MMAP_IO_REMAP_PFN).
Quoted 124 lines
mm/memory.c +11
-25+} +/** * vm_iomap_memory - remap memory to userspace * @vma: user vma to map to @@ -3187,32 +3240,16 @@ int remap_pfn_range_complete(struct vm_area_struct *vma,*/ int vm_iomap_memory (struct vm_area_struct *vma,phys_addr_t start,unsigned long len) { -unsigned long vm_len, pfn, pages; - -/* Check that the physical memory area passed in looks valid */ -if (start + len < start) -return -EINVAL ; -/* -* You *really* shouldn't map things that aren't page-aligned, -* but we've historically allowed it because IO memory might -* just have smaller alignment. -*/ - len += start & ~PAGE_MASK ; - pfn = start >>PAGE_SHIFT ; - pages = (len + ~PAGE_MASK ) >>PAGE_SHIFT ; -if (pfn + pages < pfn) -return -EINVAL ; - -/* We start the mapping 'vm_pgoff' pages into the area */ -if (vma->vm_pgoff > pages) -return -EINVAL ; - pfn += vma->vm_pgoff; - pages -= vma->vm_pgoff; +const unsigned long vm_start = vma->vm_start; +const unsigned long vm_end = vma->vm_end; +const unsigned long vm_len = vm_end - vm_start; +unsigned long pfn; +int err; -/* Can we fit all of the mapping? */ - vm_len = vma->vm_end - vma->vm_start; -if (vm_len >>PAGE_SHIFT > pages) -return -EINVAL ; + err =__simple_ioremap_prep (vm_start, vm_end, vma->vm_pgoff, start, + len, &pfn); +if (err) +return err;/* Ok, let it rip */ return io_remap_pfn_range (vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);mm/util.c +12
-0@@ -1390,6 +1390,8 @@ int mmap_action_prepare(struct vm_area_desc *desc)return remap_pfn_range_prepare (desc);case MMAP_IO_REMAP_PFN :return io_remap_pfn_range_prepare (desc); +case MMAP_SIMPLE_IO_REMAP : +return simple_ioremap_prepare (desc); }WARN_ON_ONCE (1 );@@ -1421,6 +1423,14 @@ int mmap_action_complete(struct vm_area_struct *vma,case MMAP_IO_REMAP_PFN : err =io_remap_pfn_range_complete (vma, action);break ; +case MMAP_SIMPLE_IO_REMAP : +/* +* The simple I/O remap should have been delegated to an I/O +* remap. +*/ +WARN_ON_ONCE (1 ); + err = -EINVAL ; +break ; }return mmap_action_finish (vma, action, err);@@ -1434,6 +1444,7 @@ int mmap_action_prepare(struct vm_area_desc *desc)break ;case MMAP_REMAP_PFN :case MMAP_IO_REMAP_PFN : +case MMAP_SIMPLE_IO_REMAP :WARN_ON_ONCE (1 );/* nommu cannot handle these. */ break ; }@@ -1452,6 +1463,7 @@ int mmap_action_complete(struct vm_area_struct *vma,break ;case MMAP_REMAP_PFN :case MMAP_IO_REMAP_PFN : +case MMAP_SIMPLE_IO_REMAP :WARN_ON_ONCE (1 );/* nommu cannot handle this. */ err = -EINVAL ;tools/testing/vma/include/dup.h +5
-2@@ -453,6 +453,7 @@ enum mmap_action_type {MMAP_NOTHING ,/* Mapping is complete, no further action. */ MMAP_REMAP_PFN ,/* Remap PFN range. */ MMAP_IO_REMAP_PFN ,/* I/O remap PFN range. */ + MMAP_SIMPLE_IO_REMAP, /* I/O remap with guardrails. */ };/* @@ -461,13 +462,16 @@ enum mmap_action_type {*/ struct mmap_action {union { -/* Remap range. */ struct {unsigned long start;unsigned long start_pfn;unsigned long size;pgprot_t pgprot; } remap; +struct { +phys_addr_t start; +unsigned long len; + } simple_ioremap; };enum mmap_action_type type; --2.53.0
Re: [PATCH v2 07/16] misc: open-dice: replace deprecated mmap hook with mmap_prepare
In reply to Lorenzo Stoakes (Oracle)Quoted 8 lines · Lorenzo Stoakes (Oracle)
The
f_op->mmapinterface is deprecated, so update driver to use its successor,mmap_prepare.The driver previously used
vm_iomap_memory(), so this change replaces it with itsmmap_prepareequivalent,mmap_action_simple_ioremap().Signed-off-by Lorenzo Stoakes (Oracle) <[email protected]>
Quoted 52 lines
--- drivers/misc/open-dice.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)drivers/misc/open-dice.c +11
-9@@ -86,29 +86,32 @@ static ssize_t open_dice_write(struct file *filp, const char __user *ptr,/* * Creates a mapping of the reserved memory region in user address space. */ -static int open_dice_mmap (struct file *filp,struct vm_area_struct *vma) +static int open_dice_mmap_prepare (struct vm_area_desc *desc) { +struct file *filp = desc->file;struct open_dice_drvdata *drvdata =to_open_dice_drvdata (filp); -if (vma->vm_flags &VM_MAYSHARE ) { +if (vma_desc_test (desc,VMA_MAYSHARE_BIT )) {/* Do not allow userspace to modify the underlying data. */ -if (vma->vm_flags &VM_WRITE ) +if (vma_desc_test (desc,VMA_WRITE_BIT ))return -EPERM ;/* Ensure userspace cannot acquire VM_WRITE later. */ -vm_flags_clear (vma,VM_MAYWRITE ); +vma_desc_clear_flags (desc,VMA_MAYWRITE_BIT ); }/* Create write-combine mapping so all clients observe a wipe. */ - vma->vm_page_prot =pgprot_writecombine (vma->vm_page_prot); -vm_flags_set (vma,VM_DONTCOPY |VM_DONTDUMP ); -return vm_iomap_memory (vma, drvdata->rmem->base, drvdata->rmem->size); + desc->page_prot =pgprot_writecombine (desc->page_prot); +vma_desc_set_flags (desc,VMA_DONTCOPY_BIT ,VMA_DONTDUMP_BIT ); +mmap_action_simple_ioremap (desc, drvdata->rmem->base, + drvdata->rmem->size); +return 0 ; }static const struct file_operations open_dice_fops = { .owner =THIS_MODULE , .read = open_dice_read, .write = open_dice_write, - .mmap = open_dice_mmap, + .mmap_prepare = open_dice_mmap_prepare, };static int __init open_dice_probe (struct platform_device *pdev) --2.53.0
Re: [PATCH v2 08/16] hpet: replace deprecated mmap hook with mmap_prepare
In reply to Lorenzo Stoakes (Oracle)Quoted 8 lines · Lorenzo Stoakes (Oracle)
The
f_op->mmapinterface is deprecated, so update driver to use its successor,mmap_prepare.The driver previously used
vm_iomap_memory(), so this change replaces it with itsmmap_prepareequivalent,mmap_action_simple_ioremap().Signed-off-by Lorenzo Stoakes (Oracle) <[email protected]>
Quoted 47 lines
--- drivers/char/hpet.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)drivers/char/hpet.c +7
-6@@ -354,8 +354,9 @@ static __init int hpet_mmap_enable(char *str) }__setup ("hpet_mmap=" , hpet_mmap_enable); -static int hpet_mmap (struct file *file,struct vm_area_struct *vma) +static int hpet_mmap_prepare (struct vm_area_desc *desc) { +struct file *file = desc->file;struct hpet_dev *devp;unsigned long addr;@@ -368,11 +369,12 @@ static int hpet_mmap(struct file *file, struct vm_area_struct *vma)if (addr & (PAGE_SIZE -1 ))return -ENOSYS ; - vma->vm_page_prot =pgprot_noncached (vma->vm_page_prot); - return vm_iomap_memory(vma, addr, PAGE_SIZE); + desc->page_prot =pgprot_noncached (desc->page_prot); +mmap_action_simple_ioremap (desc, addr,PAGE_SIZE ); + return 0; }#else -static int hpet_mmap (struct file *file,struct vm_area_struct *vma) +static int hpet_mmap_prepare (struct vm_area_desc *desc) {return -ENOSYS ; }@@ -710,7 +712,7 @@ static const struct file_operations hpet_fops = { .open = hpet_open, .release = hpet_release, .fasync = hpet_fasync, - .mmap = hpet_mmap, + .mmap_prepare = hpet_mmap_prepare, };static int hpet_is_known (struct hpet_data *hdp) --2.53.0
Re: [PATCH v2 10/16] stm: replace deprecated mmap hook with mmap_prepare
In reply to Lorenzo Stoakes (Oracle)Quoted 4 lines · Lorenzo Stoakes (Oracle)
The
f_op->mmapinterface is deprecated, so update driver to use its successor,mmap_prepare.
Quoted 8 lines
The driver previously used
vm_iomap_memory(), so this change replaces it with itsmmap_prepareequivalent,mmap_action_simple_ioremap().Also, in order to correctly maintain reference counting, add a
vm_ops->mappedcallback to increment the reference count when successfully mapped.Signed-off-by Lorenzo Stoakes (Oracle) <[email protected]>
Quoted 85 lines
--- drivers/hwtracing/stm/core.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-)drivers/hwtracing/stm/core.c +21
-11@@ -666,6 +666,16 @@ static ssize_t stm_char_write(struct file *file, const char __user *buf,return count; } +static int stm_mmap_mapped (unsigned long start,unsigned long end,pgoff_t pgoff, +const struct file *file,void **vm_private_data) +{ +struct stm_file *stmf = file->private_data; +struct stm_device *stm = stmf->stm; + +pm_runtime_get_sync (&stm->dev); +return 0 ; +} +static void stm_mmap_open (struct vm_area_struct *vma) {struct stm_file *stmf = vma->vm_file->private_data;@@ -684,12 +694,14 @@ static void stm_mmap_close(struct vm_area_struct *vma) }static const struct vm_operations_struct stm_mmap_vmops = { + .mapped = stm_mmap_mapped, .open = stm_mmap_open, .close = stm_mmap_close, }; -static int stm_char_mmap (struct file *file,struct vm_area_struct *vma) +static int stm_char_mmap_prepare (struct vm_area_desc *desc) { +struct file *file = desc->file;struct stm_file *stmf = file->private_data;struct stm_device *stm = stmf->stm;unsigned long size, phys;@@ -697,10 +709,10 @@ static int stm_char_mmap(struct file *file, struct vm_area_struct *vma)if (!stm->data->mmio_addr)return -EOPNOTSUPP ; -if (vma->vm_pgoff) +if (desc->pgoff)return -EINVAL ; - size = vma->vm_end - vma->vm_start; + size =vma_desc_size (desc);if (stmf->output.nr_chans * stm->data->sw_mmiosz != size)return -EINVAL ;@@ -712,13 +724,12 @@ static int stm_char_mmap(struct file *file, struct vm_area_struct *vma)if (!phys)return -EINVAL ; -pm_runtime_get_sync (&stm->dev); - - vma->vm_page_prot =pgprot_noncached (vma->vm_page_prot); -vm_flags_set (vma,VM_IO |VM_DONTEXPAND |VM_DONTDUMP ); - vma->vm_ops = &stm_mmap_vmops; -vm_iomap_memory (vma, phys, size); + desc->page_prot =pgprot_noncached (desc->page_prot); +vma_desc_set_flags (desc,VMA_IO_BIT ,VMA_DONTEXPAND_BIT , +VMA_DONTDUMP_BIT ); + desc->vm_ops = &stm_mmap_vmops; +mmap_action_simple_ioremap (desc, phys, size);return 0 ; }@@ -836,7 +847,7 @@ static const struct file_operations stm_fops = { .open = stm_char_open, .release = stm_char_release, .write = stm_char_write, - .mmap = stm_char_mmap, + .mmap_prepare = stm_char_mmap_prepare, .unlocked_ioctl = stm_char_ioctl, .compat_ioctl = compat_ptr_ioctl, }; --2.53.0
Suren Baghdasaryan
reviewerRe: [PATCH v2 11/16] staging: vme_user: replace deprecated mmap hook with mmap_prepare
In reply to Lorenzo Stoakes (Oracle)Quoted 122 lines · Lorenzo Stoakes (Oracle)
The
f_op->mmapinterface is deprecated, so update driver to use its successor,mmap_prepare.The driver previously used
vm_iomap_memory(), so this change replaces it with itsmmap_prepareequivalent,mmap_action_simple_ioremap().Functions that wrap
mmap()are also converted to wrapmmap_prepare()instead.Also update the documentation accordingly.
Signed-off-by Lorenzo Stoakes (Oracle) <[email protected]>--- Documentation/driver-api/vme.rst | 2 +- drivers/staging/vme_user/vme.c | 20 +++++------ drivers/staging/vme_user/vme.h | 2 +- drivers/staging/vme_user/vme_user.c | 51 +++++++++++++++++------------ 4 files changed, 42 insertions(+), 33 deletions(-)Documentation/driver-api/vme.rst +1
-1@@ -107,7 +107,7 @@ The function :c:func:`vme_master_read` can be used to read from and In addition to simple reads and writes, :c:func:`vme_master_rmw` is provided to do a read-modify-write transaction. Parts of a VME window can also be mapped -into user space memory using :c:func:`vme_master_mmap`. +into user space memory using :c:func:`vme_master_mmap_prepare`. Slave windowsdrivers/staging/vme_user/vme.c +10
-10@@ -735,9 +735,9 @@ unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask,EXPORT_SYMBOL (vme_master_rmw);/** -* vme_master_mmap - Mmap region of VME master window. +* vme_master_mmap_prepare - Mmap region of VME master window. * @resource: Pointer to VME master resource. -* @vma: Pointer to definition of user mapping. +* @desc: Pointer to descriptor of user mapping. * * Memory map a region of the VME master window into user space. * @@ -745,12 +745,13 @@ EXPORT_SYMBOL(vme_master_rmw);* resource or -EFAULT if map exceeds window size. Other generic mmap * errors may also be returned. */ -int vme_master_mmap (struct vme_resource *resource,struct vm_area_struct *vma) +int vme_master_mmap_prepare (struct vme_resource *resource, +struct vm_area_desc *desc) { +const unsigned long vma_size =vma_desc_size (desc);struct vme_bridge *bridge =find_bridge (resource);struct vme_master_resource *image;phys_addr_t phys_addr; -unsigned long vma_size;if (resource->type !=VME_MASTER ) {dev_err (bridge->parent,"Not a master resource\n" );@@ -758,19 +759,18 @@ int vme_master_mmap(struct vme_resource *resource, struct vm_area_struct *vma) } image = list_entry(resource->entry, struct vme_master_resource, list); - phys_addr = image->bus_resource.start + (vma->vm_pgoff <<PAGE_SHIFT ); - vma_size = vma->vm_end - vma->vm_start; + phys_addr = image->bus_resource.start + (desc->pgoff <<PAGE_SHIFT );if (phys_addr + vma_size > image->bus_resource.end +1 ) {dev_err (bridge->parent,"Map size cannot exceed the window size\n" );return -EFAULT ; } - vma->vm_page_prot =pgprot_noncached (vma->vm_page_prot); - -return vm_iomap_memory (vma, phys_addr, vma->vm_end - vma->vm_start); + desc->page_prot =pgprot_noncached (desc->page_prot); +mmap_action_simple_ioremap (desc, phys_addr, vma_size); +return 0 ; } -EXPORT_SYMBOL (vme_master_mmap); +EXPORT_SYMBOL (vme_master_mmap_prepare);/** * vme_master_free - Free VME master window drivers/staging/vme_user/vme.h +1
-1@@ -151,7 +151,7 @@ ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count,ssize_t vme_master_write (struct vme_resource *resource,void *buf,size_t count,loff_t offset);unsigned int vme_master_rmw (struct vme_resource *resource,unsigned int mask,unsigned int compare,unsigned int swap,loff_t offset); -int vme_master_mmap (struct vme_resource *resource,struct vm_area_struct *vma); +int vme_master_mmap_prepare (struct vme_resource *resource,struct vm_area_desc *desc);void vme_master_free (struct vme_resource *resource);struct vme_resource *vme_dma_request (struct vme_dev *vdev,u32 route);drivers/staging/vme_user/vme_user.c +3
-7@@ -446,24 +446,14 @@ static void vme_user_vm_close(struct vm_area_struct *vma)kfree (vma_priv); } -static const struct vm_operations_struct vme_user_vm_ops = { - .open = vme_user_vm_open, - .close = vme_user_vm_close, -}; - -static int vme_user_master_mmap (unsigned int minor,struct vm_area_struct *vma) +static int vme_user_vm_mapped (unsigned long start,unsigned long end,pgoff_t pgoff, +const struct file *file,void **vm_private_data) { -int err; +const unsigned int minor =iminor (file_inode (file));
drivers/staging/vme_user/vme_user.c static void vme_user_vm_close(struct vm_area_struct *vma) · line 466
Quoted 10 lines
drivers/staging/vme_user/vme_user.c +0
-6struct vme_user_vma_priv *vma_priv;mutex_lock (&image[minor].mutex); - err =vme_master_mmap (image[minor].resource, vma); -if (err) { -mutex_unlock (&image[minor].mutex); -return err; - } -
Ok, this changes the set of the operations performed under image[minor].mutex. Before we had:
mutex_lock(&image[minor].mutex);vme_master_mmap();
<some final adjustments>mutex_unlock(&image[minor].mutex);
Now we have:
mutex_lock(&image[minor].mutex);vme_master_mmap_prepare()mutex_unlock(&image[minor].mutex);vm_iomap_memory();mutex_lock(&image[minor].mutex);vme_user_vm_mapped(); // <some final adjustments>mutex_unlock(&image[minor].mutex);
I think as long as image[minor] does not change while we are not holding the mutex we should be safe, and looking at the code it seems to be the case. But I'm not familiar with this driver and might be wrong. Worth double-checking.
Quoted 64 lines
drivers/staging/vme_user/vme_user.c +27
-9vma_priv =kmalloc_obj (*vma_priv);if (!vma_priv) {mutex_unlock (&image[minor].mutex);@@ -472,22 +462,41 @@ static int vme_user_master_mmap(unsigned int minor, struct vm_area_struct *vma) vma_priv->minor = minor;refcount_set (&vma_priv->refcnt,1 ); - vma->vm_ops = &vme_user_vm_ops; - vma->vm_private_data = vma_priv; - + *vm_private_data = vma_priv; image[minor].mmap_count++;mutex_unlock (&image[minor].mutex); -return 0 ; } -static int vme_user_mmap (struct file *file,struct vm_area_struct *vma) +static const struct vm_operations_struct vme_user_vm_ops = { + .mapped = vme_user_vm_mapped, + .open = vme_user_vm_open, + .close = vme_user_vm_close, +}; + +static int vme_user_master_mmap_prepare (unsigned int minor, +struct vm_area_desc *desc) +{ +int err; + +mutex_lock (&image[minor].mutex); + + err =vme_master_mmap_prepare (image[minor].resource, desc); +if (!err) + desc->vm_ops = &vme_user_vm_ops; + +mutex_unlock (&image[minor].mutex); +return err; +} + +static int vme_user_mmap_prepare (struct vm_area_desc *desc) { -unsigned int minor =iminor (file_inode (file)); +const struct file *file = desc->file; +const unsigned int minor =iminor (file_inode (file));if (type[minor] ==MASTER_MINOR ) -return vme_user_master_mmap (minor, vma); +return vme_user_master_mmap_prepare (minor, desc);return -ENODEV ; }@@ -498,7 +507,7 @@ static const struct file_operations vme_user_fops = { .llseek = vme_user_llseek, .unlocked_ioctl = vme_user_unlocked_ioctl, .compat_ioctl = compat_ptr_ioctl, - .mmap = vme_user_mmap, + .mmap_prepare = vme_user_mmap_prepare, };static int vme_user_match (struct vme_dev *vdev) --2.53.0
Suren Baghdasaryan
reviewerRe: [PATCH v2 11/16] staging: vme_user: replace deprecated mmap hook with mmap_prepare
Quoted 153 lines · Suren Baghdasaryan
Quoted 122 lines · Lorenzo Stoakes (Oracle)
The
f_op->mmapinterface is deprecated, so update driver to use its successor,mmap_prepare.The driver previously used
vm_iomap_memory(), so this change replaces it with itsmmap_prepareequivalent,mmap_action_simple_ioremap().Functions that wrap
mmap()are also converted to wrapmmap_prepare()instead.Also update the documentation accordingly.
Signed-off-by Lorenzo Stoakes (Oracle) <[email protected]>--- Documentation/driver-api/vme.rst | 2 +- drivers/staging/vme_user/vme.c | 20 +++++------ drivers/staging/vme_user/vme.h | 2 +- drivers/staging/vme_user/vme_user.c | 51 +++++++++++++++++------------ 4 files changed, 42 insertions(+), 33 deletions(-)Documentation/driver-api/vme.rst +1
-1@@ -107,7 +107,7 @@ The function :c:func:`vme_master_read` can be used to read from and In addition to simple reads and writes, :c:func:`vme_master_rmw` is provided to do a read-modify-write transaction. Parts of a VME window can also be mapped -into user space memory using :c:func:`vme_master_mmap`. +into user space memory using :c:func:`vme_master_mmap_prepare`. Slave windowsdrivers/staging/vme_user/vme.c +10
-10@@ -735,9 +735,9 @@ unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask,EXPORT_SYMBOL (vme_master_rmw);/** -* vme_master_mmap - Mmap region of VME master window. +* vme_master_mmap_prepare - Mmap region of VME master window. * @resource: Pointer to VME master resource. -* @vma: Pointer to definition of user mapping. +* @desc: Pointer to descriptor of user mapping. * * Memory map a region of the VME master window into user space. * @@ -745,12 +745,13 @@ EXPORT_SYMBOL(vme_master_rmw);* resource or -EFAULT if map exceeds window size. Other generic mmap * errors may also be returned. */ -int vme_master_mmap (struct vme_resource *resource,struct vm_area_struct *vma) +int vme_master_mmap_prepare (struct vme_resource *resource, +struct vm_area_desc *desc) { +const unsigned long vma_size =vma_desc_size (desc);struct vme_bridge *bridge =find_bridge (resource);struct vme_master_resource *image;phys_addr_t phys_addr; -unsigned long vma_size;if (resource->type !=VME_MASTER ) {dev_err (bridge->parent,"Not a master resource\n" );@@ -758,19 +759,18 @@ int vme_master_mmap(struct vme_resource *resource, struct vm_area_struct *vma) } image = list_entry(resource->entry, struct vme_master_resource, list); - phys_addr = image->bus_resource.start + (vma->vm_pgoff <<PAGE_SHIFT ); - vma_size = vma->vm_end - vma->vm_start; + phys_addr = image->bus_resource.start + (desc->pgoff <<PAGE_SHIFT );if (phys_addr + vma_size > image->bus_resource.end +1 ) {dev_err (bridge->parent,"Map size cannot exceed the window size\n" );return -EFAULT ; } - vma->vm_page_prot =pgprot_noncached (vma->vm_page_prot); - -return vm_iomap_memory (vma, phys_addr, vma->vm_end - vma->vm_start); + desc->page_prot =pgprot_noncached (desc->page_prot); +mmap_action_simple_ioremap (desc, phys_addr, vma_size); +return 0 ; } -EXPORT_SYMBOL (vme_master_mmap); +EXPORT_SYMBOL (vme_master_mmap_prepare);/** * vme_master_free - Free VME master window drivers/staging/vme_user/vme.h +1
-1@@ -151,7 +151,7 @@ ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count,ssize_t vme_master_write (struct vme_resource *resource,void *buf,size_t count,loff_t offset);unsigned int vme_master_rmw (struct vme_resource *resource,unsigned int mask,unsigned int compare,unsigned int swap,loff_t offset); -int vme_master_mmap (struct vme_resource *resource,struct vm_area_struct *vma); +int vme_master_mmap_prepare (struct vme_resource *resource,struct vm_area_desc *desc);void vme_master_free (struct vme_resource *resource);struct vme_resource *vme_dma_request (struct vme_dev *vdev,u32 route);drivers/staging/vme_user/vme_user.c +3
-7@@ -446,24 +446,14 @@ static void vme_user_vm_close(struct vm_area_struct *vma)kfree (vma_priv); } -static const struct vm_operations_struct vme_user_vm_ops = { - .open = vme_user_vm_open, - .close = vme_user_vm_close, -}; - -static int vme_user_master_mmap (unsigned int minor,struct vm_area_struct *vma) +static int vme_user_vm_mapped (unsigned long start,unsigned long end,pgoff_t pgoff, +const struct file *file,void **vm_private_data) { -int err; +const unsigned int minor =iminor (file_inode (file));drivers/staging/vme_user/vme_user.c static void vme_user_vm_close(struct vm_area_struct *vma) · line 466
Quoted 10 lines
drivers/staging/vme_user/vme_user.c +0
-6struct vme_user_vma_priv *vma_priv;mutex_lock (&image[minor].mutex); - err =vme_master_mmap (image[minor].resource, vma); -if (err) { -mutex_unlock (&image[minor].mutex); -return err; - } -Ok, this changes the set of the operations performed under image[minor].mutex. Before we had:
mutex_lock(&image[minor].mutex);vme_master_mmap();
<some final adjustments>mutex_unlock(&image[minor].mutex);Now we have:
mutex_lock(&image[minor].mutex);vme_master_mmap_prepare()mutex_unlock(&image[minor].mutex);vm_iomap_memory();mutex_lock(&image[minor].mutex);vme_user_vm_mapped(); // <some final adjustments>mutex_unlock(&image[minor].mutex);
Quoted 4 lines
I think as long as image[minor] does not change while we are not holding the mutex we should be safe, and looking at the code it seems to be the case. But I'm not familiar with this driver and might be wrong. Worth double-checking.
A side note: if we had to hold the mutex across all those operations I think we would need to take the mutex in the vm_ops->mmap_prepare and add a vm_ops->map_failed hook or something along that line to drop the mutex in case mmap_action_complete() fails. Not sure if we will have such cases though...
Quoted 65 lines
Quoted 65 lines
drivers/staging/vme_user/vme_user.c +27
-9vma_priv =kmalloc_obj (*vma_priv);if (!vma_priv) {mutex_unlock (&image[minor].mutex);@@ -472,22 +462,41 @@ static int vme_user_master_mmap(unsigned int minor, struct vm_area_struct *vma) vma_priv->minor = minor;refcount_set (&vma_priv->refcnt,1 ); - vma->vm_ops = &vme_user_vm_ops; - vma->vm_private_data = vma_priv; - + *vm_private_data = vma_priv; image[minor].mmap_count++;mutex_unlock (&image[minor].mutex); -return 0 ; } -static int vme_user_mmap (struct file *file,struct vm_area_struct *vma) +static const struct vm_operations_struct vme_user_vm_ops = { + .mapped = vme_user_vm_mapped, + .open = vme_user_vm_open, + .close = vme_user_vm_close, +}; + +static int vme_user_master_mmap_prepare (unsigned int minor, +struct vm_area_desc *desc) +{ +int err; + +mutex_lock (&image[minor].mutex); + + err =vme_master_mmap_prepare (image[minor].resource, desc); +if (!err) + desc->vm_ops = &vme_user_vm_ops; + +mutex_unlock (&image[minor].mutex); +return err; +} + +static int vme_user_mmap_prepare (struct vm_area_desc *desc) { -unsigned int minor =iminor (file_inode (file)); +const struct file *file = desc->file; +const unsigned int minor =iminor (file_inode (file));if (type[minor] ==MASTER_MINOR ) -return vme_user_master_mmap (minor, vma); +return vme_user_master_mmap_prepare (minor, desc);return -ENODEV ; }@@ -498,7 +507,7 @@ static const struct file_operations vme_user_fops = { .llseek = vme_user_llseek, .unlocked_ioctl = vme_user_unlocked_ioctl, .compat_ioctl = compat_ptr_ioctl, - .mmap = vme_user_mmap, + .mmap_prepare = vme_user_mmap_prepare, };static int vme_user_match (struct vme_dev *vdev) --2.53.0
Suren Baghdasaryan
reviewerRe: [PATCH v2 12/16] mm: allow handling of stacked mmap_prepare hooks in more drivers
In reply to Lorenzo Stoakes (Oracle)Quoted 2 lines · Lorenzo Stoakes (Oracle)
While the conversion of mmap hooks to
mmap_prepareis underway, we wil
nit: s/wil/will
Quoted 56 lines
encounter situations where mmap hooks need to invoke nested
mmap_preparehooks.The nesting of mmap hooks is termed 'stacking'. In order to flexibly facilitate the conversion of custom mmap hooks in drivers which stack, we must split up the existing
compat_vma_mapped()function into two separate functions:*
compat_set_desc_from_vma()- This allows the setting of avm_area_descobject's fields to the relevant fields of a VMA.*
__compat_vma_mmap()- Once anmmap_preparehook has been executed upon avm_area_descobject, this function performs any mmap actions specified by themmap_preparehook and then invokes itsvm_ops->mapped()hook if any were specified.In ordinary cases, where a file's
f_op->mmap_prepare()hook simply needs to be invoked in a stackedmmap()hook,compat_vma_mmap()can be used.However some drivers define their own nested hooks, which are invoked in turn by another hook.
A concrete example is
vmbus_channel->mmap_ring_buffer(), which is invoked in turn bybin_attribute->mmap():
vmbus_channel->mmap_ring_buffer()has a signature of:int (*mmap_ring_buffer)(struct
vmbus_channel*channel,struct vm_area_struct *vma);And
bin_attribute->mmap()has a signature of:int (*mmap)(struct file *, struct kobject *,
const structbin_attribute*attr, structvm_area_struct*vma);And so
compat_vma_mmap()cannot be used here for incremental conversion of hooks frommmap()tommap_prepare().There are many such instances like this, where conversion to
mmap_preparewould otherwise cascade to a huge change set due to nesting of this kind.The changes in this patch mean we could now instead convert
vmbus_channel->mmap_ring_buffer()tovmbus_channel->mmap_prepare_ring_buffer(), and implement something like:struct vm_area_desc desc; int err; compat_set_desc_from_vm(&desc, file, vma); err = channel->mmap_prepare_ring_buffer(channel, &desc); if (err) return err; return __compat_vma_mmap(&desc, vma);
Quoted 1 line
Allowing us to incrementally update this logic, and other logic like it.
The way I understand this and the next 2 patches is that they are preperations for later replacement of mmap() with mmap_prepare() but they don't yet do that completely. Is that right?
To clarify what I mean, in [1] for example, you are replacing struct uio_info.mmap with uio_info.mmap_prepare but it's still being called from uio_mmap(). IOW, you are not replacing uio_mmap with uio_mmap_prepare. Is that the next step that's not yet implemented?
[1] https://lore.kernel.org/all/892a8b32e5ef64c69239ccc2d1bd364716fd7fdf.1773695307.git.ljs@kernel.org/
Quoted 383 lines
Unfortunately, as part of this change, we need to be able to flexibly assign to the VMA descriptor, so have to remove some of the const declarations within the structure.
Also update the VMA tests to reflect the changes.
Signed-off-by Lorenzo Stoakes (Oracle) <[email protected]>--- include/linux/fs.h | 3 + include/linux/mm_types.h | 4 +- mm/util.c | 111 +++++++++++++++++++++++--------- mm/vma.h | 2 +- tools/testing/vma/include/dup.h | 111 ++++++++++++++++++++------------ 5 files changed, 157 insertions(+), 74 deletions(-)include/linux/fs.h +3
-0@@ -2058,6 +2058,9 @@ static inline bool can_mmap_file(struct file *file)return true; } +void compat_set_desc_from_vma (struct vm_area_desc *desc,const struct file *file, +const struct vm_area_struct *vma); +int __compat_vma_mmap (struct vm_area_desc *desc,struct vm_area_struct *vma);int compat_vma_mmap (struct file *file,struct vm_area_struct *vma);int __vma_check_mmap_hook (struct vm_area_struct *vma);include/linux/mm_types.h +2
-2@@ -891,8 +891,8 @@ static __always_inline bool vma_flags_empty(vma_flags_t *flags)*/ struct vm_area_desc {/* Immutable state. */ -const struct mm_struct *const mm; -struct file *const file;/* May vary from vm_file in stacked callers. */ +struct mm_struct *mm; +struct file *file;/* May vary from vm_file in stacked callers. */ unsigned long start;unsigned long end;mm/util.c +80
-31@@ -1163,34 +1163,38 @@ void flush_dcache_folio(struct folio *folio)EXPORT_SYMBOL (flush_dcache_folio);#endif -static int __compat_vma_mmap (struct file *file,struct vm_area_struct *vma) +/** +* compat_set_desc_from_vma() - assigns VMA descriptor @desc fields from a VMA. +* @desc: A VMA descriptor whose fields need to be set. +* @file: The file object describing the file being mmap()'d. +* @vma: The VMA whose fields we wish to assign to @desc. +* +* This is a compatibility function to allow an mmap() hook to call +* mmap_prepare() hooks when drivers nest these. This function specifically +* allows the construction of a vm_area_desc value, @desc, from a VMA @vma for +* the purposes of doing this. +* +* Once the conversion of drivers is complete this function will no longer be +* required and will be removed. +*/ +void compat_set_desc_from_vma (struct vm_area_desc *desc, +const struct file *file, +const struct vm_area_struct *vma) { -struct vm_area_desc desc = { - .mm = vma->vm_mm, - .file = file, - .start = vma->vm_start, - .end = vma->vm_end, - - .pgoff = vma->vm_pgoff, - .vm_file = vma->vm_file, - .vma_flags = vma->flags, - .page_prot = vma->vm_page_prot, - - .action.type =MMAP_NOTHING ,/* Default */ - }; -int err; + desc->mm = vma->vm_mm; + desc->file = (struct file *)file; + desc->start = vma->vm_start; + desc->end = vma->vm_end; - err =vfs_mmap_prepare (file, &desc); -if (err) -return err; + desc->pgoff = vma->vm_pgoff; + desc->vm_file = vma->vm_file; + desc->vma_flags = vma->flags; + desc->page_prot = vma->vm_page_prot; - err =mmap_action_prepare (&desc); -if (err) -return err; - -set_vma_from_desc (vma, &desc); -return mmap_action_complete (vma, &desc.action); +/* Default. */ + desc->action.type =MMAP_NOTHING ; } +EXPORT_SYMBOL (compat_set_desc_from_vma);static int __compat_vma_mapped (struct file *file,struct vm_area_struct *vma) {@@ -1211,6 +1215,49 @@ static int __compat_vma_mapped(struct file *file, struct vm_area_struct *vma)return err; } +/** +* __compat_vma_mmap() - Similar to compat_vma_mmap(), only it allows +* flexibility as to how the mmap_prepare callback is invoked, which is useful +* for drivers which invoke nested mmap_prepare callbacks in an mmap() hook. +* @desc: A VMA descriptor upon which an mmap_prepare() hook has already been +* executed. +* @vma: The VMA to which @desc should be applied. +* +* The function assumes that you have obtained a VMA descriptor @desc from +* compt_set_desc_from_vma(), and already executed the mmap_prepare() hook upon +* it. +* +* It then performs any specified mmap actions, and invokes the vm_ops->mapped() +* hook if one is present. +* +* See the description of compat_vma_mmap() for more details. +* +* Once the conversion of drivers is complete this function will no longer be +* required and will be removed. +* +* Returns: 0 on success or error. +*/ +int __compat_vma_mmap (struct vm_area_desc *desc, +struct vm_area_struct *vma) +{ +int err; + +/* Perform any preparatory tasks for mmap action. */ + err =mmap_action_prepare (desc); +if (err) +return err; +/* Update the VMA from the descriptor. */ +compat_set_vma_from_desc (vma, desc); +/* Complete any specified mmap actions. */ + err =mmap_action_complete (vma, &desc->action); +if (err) +return err; + +/* Invoke vm_ops->mapped callback. */ +return __compat_vma_mapped (desc->file, vma); +} +EXPORT_SYMBOL (__compat_vma_mmap); +/** * compat_vma_mmap() - Apply the file's .mmap_prepare() hook to an * existing VMA and execute any requested actions. @@ -1218,10 +1265,10 @@ static int __compat_vma_mapped(struct file *file, struct vm_area_struct *vma)* @vma: The VMA to apply the .mmap_prepare() hook to. * * Ordinarily, .mmap_prepare() is invoked directly upon mmap(). However, certain -* stacked filesystems invoke a nested mmap hook of an underlying file. +* stacked drivers invoke a nested mmap hook of an underlying file. * -* Until all filesystems are converted to use .mmap_prepare(), we must be -* conservative and continue to invoke these stacked filesystems using the +* Until all drivers are converted to use .mmap_prepare(), we must be +* conservative and continue to invoke these stacked drivers using the * deprecated .mmap() hook. * * However we have a problem if the underlying file system possesses an @@ -1232,20 +1279,22 @@ static int __compat_vma_mapped(struct file *file, struct vm_area_struct *vma)* establishes a struct vm_area_desc descriptor, passes to the underlying * .mmap_prepare() hook and applies any changes performed by it. * -* Once the conversion of filesystems is complete this function will no longer -* be required and will be removed. +* Once the conversion of drivers is complete this function will no longer be +* required and will be removed. * * Returns: 0 on success or error. */ int compat_vma_mmap (struct file *file,struct vm_area_struct *vma) { +struct vm_area_desc desc;int err; - err =__compat_vma_mmap (file, vma); +compat_set_desc_from_vma (&desc, file, vma); + err =vfs_mmap_prepare (file, &desc);if (err)return err; -return __compat_vma_mapped (file, vma); +return __compat_vma_mmap (&desc, vma); }EXPORT_SYMBOL (compat_vma_mmap);mm/vma.h +1
-1@@ -300,7 +300,7 @@ static inline int vma_iter_store_gfp(struct vma_iterator *vmi,* f_op->mmap() but which might have an underlying file system which implements * f_op->mmap_prepare(). */ -static inline void set_vma_from_desc (struct vm_area_struct *vma, +static inline void compat_set_vma_from_desc (struct vm_area_struct *vma,struct vm_area_desc *desc) {/* tools/testing/vma/include/dup.h +71
-41@@ -519,8 +519,8 @@ enum vma_operation {*/ struct vm_area_desc {/* Immutable state. */ -const struct mm_struct *const mm; -struct file *const file;/* May vary from vm_file in stacked callers. */ +struct mm_struct *mm; +struct file *file;/* May vary from vm_file in stacked callers. */ unsigned long start;unsigned long end;@@ -1272,43 +1272,92 @@ static inline void vma_set_anonymous(struct vm_area_struct *vma) }/* Declared in vma.h. */ -static inline void set_vma_from_desc (struct vm_area_struct *vma, +static inline void compat_set_vma_from_desc (struct vm_area_struct *vma,struct vm_area_desc *desc); -static inline int __compat_vma_mmap (const struct file_operations *f_op, -struct file *file,struct vm_area_struct *vma) +static inline void compat_set_desc_from_vma (struct vm_area_desc *desc, +const struct file *file, +const struct vm_area_struct *vma) { -struct vm_area_desc desc = { - .mm = vma->vm_mm, - .file = file, - .start = vma->vm_start, - .end = vma->vm_end, + desc->mm = vma->vm_mm; + desc->file = (struct file *)file; + desc->start = vma->vm_start; + desc->end = vma->vm_end; - .pgoff = vma->vm_pgoff, - .vm_file = vma->vm_file, - .vma_flags = vma->flags, - .page_prot = vma->vm_page_prot, + desc->pgoff = vma->vm_pgoff; + desc->vm_file = vma->vm_file; + desc->vma_flags = vma->flags; + desc->page_prot = vma->vm_page_prot; - .action.type =MMAP_NOTHING ,/* Default */ - }; +/* Default. */ + desc->action.type =MMAP_NOTHING ; +} + +static inline unsigned long vma_pages (const struct vm_area_struct *vma) +{ +return (vma->vm_end - vma->vm_start) >>PAGE_SHIFT ; +} + +static inline void unmap_vma_locked (struct vm_area_struct *vma) +{ +const size_t len =vma_pages (vma) <<PAGE_SHIFT ; + +mmap_assert_write_locked (vma->vm_mm); +do_munmap (vma->vm_mm, vma->vm_start, len,NULL ); +} + +static inline int __compat_vma_mapped (struct file *file,struct vm_area_struct *vma) +{ +const struct vm_operations_struct *vm_ops = vma->vm_ops;int err; - err = f_op->mmap_prepare (&desc); +if (!vm_ops->mapped) +return 0 ; + + err = vm_ops->mapped (vma->vm_start, vma->vm_end, vma->vm_pgoff, file, + &vma->vm_private_data);if (err) -return err; +unmap_vma_locked (vma); +return err; +} - err =mmap_action_prepare (&desc); +static inline int __compat_vma_mmap (struct vm_area_desc *desc, +struct vm_area_struct *vma) +{ +int err; + +/* Perform any preparatory tasks for mmap action. */ + err =mmap_action_prepare (desc); +if (err) +return err; +/* Update the VMA from the descriptor. */ +compat_set_vma_from_desc (vma, desc); +/* Complete any specified mmap actions. */ + err =mmap_action_complete (vma, &desc->action);if (err)return err; -set_vma_from_desc (vma, &desc); -return mmap_action_complete (vma, &desc.action); +/* Invoke vm_ops->mapped callback. */ +return __compat_vma_mapped (desc->file, vma); +} + +static inline int vfs_mmap_prepare (struct file *file,struct vm_area_desc *desc) +{ +return file->f_op->mmap_prepare (desc); }static inline int compat_vma_mmap (struct file *file,struct vm_area_struct *vma) { -return __compat_vma_mmap (file->f_op, file, vma); +struct vm_area_desc desc; +int err; + +compat_set_desc_from_vma (&desc, file, vma); + err =vfs_mmap_prepare (file, &desc); +if (err) +return err; + +return __compat_vma_mmap (&desc, vma); }@@ -1318,11 +1367,6 @@ static inline void vma_iter_init(struct vma_iterator *vmi,mas_init (&vmi->mas, &mm->mm_mt, addr); } -static inline unsigned long vma_pages (struct vm_area_struct *vma) -{ -return (vma->vm_end - vma->vm_start) >>PAGE_SHIFT ; -} -static inline void mmap_assert_locked (struct mm_struct *); static inline struct vm_area_struct *find_vma_intersection(struct mm_struct *mm, unsigned long start_addr,@@ -1492,11 +1536,6 @@ static inline int vfs_mmap(struct file *file, struct vm_area_struct *vma)return file->f_op->mmap (file, vma); } -static inline int vfs_mmap_prepare (struct file *file,struct vm_area_desc *desc) -{ -return file->f_op->mmap_prepare (desc); -} -static inline void vma_set_file (struct vm_area_struct *vma,struct file *file) {/* Changing an anonymous vma with this is illegal */ @@ -1521,11 +1560,3 @@ static inline pgprot_t vma_get_page_prot(vma_flags_t vma_flags)return vm_get_page_prot (vm_flags); } - -static inline void unmap_vma_locked (struct vm_area_struct *vma) -{ -const size_t len =vma_pages (vma) <<PAGE_SHIFT ; - -mmap_assert_write_locked (vma->vm_mm); -do_munmap (vma->vm_mm, vma->vm_start, len,NULL ); -} --2.53.0
Re: [PATCH v2 15/16] mm: add mmap_action_map_kernel_pages[_full]()
In reply to Lorenzo Stoakes (Oracle)Quoted 20 lines · Lorenzo Stoakes (Oracle)
A user can invoke
mmap_action_map_kernel_pages()to specify that the mapping should map kernel pages starting fromdesc->startof a specified number of pages specified in an array.In order to implement this, adjust
mmap_action_prepare()to be able to return an error code, as it makes sense to assert that the specified parameters are valid as quickly as possible as well as updating the VMA flags to includeVMA_MIXEDMAP_BITas necessary.This provides an
mmap_prepareequivalent ofvm_insert_pages().We additionally update the existing
vm_insert_pages()code to userange_in_vma()and add a newrange_in_vma_desc()helper function for themmap_preparecase, sharing the code between the two inrange_is_subset().We add both
mmap_action_map_kernel_pages()andmmap_action_map_kernel_pages_full()to allow for both partial and full VMA mappings.
Quoted 8 lines
We also add
mmap_action_map_kernel_pages_discontig()to allow for discontiguous mapping of kernel pages should the need arise.We update the documentation to reflect the new features.
Finally, we update the VMA tests accordingly to reflect the changes.
Signed-off-by Lorenzo Stoakes (Oracle) <[email protected]>
With one nit,
Quoted 32 lines
--- Documentation/filesystems/mmap_prepare.rst | 8 ++ include/linux/mm.h | 95 +++++++++++++++++++++- include/linux/mm_types.h | 7 ++ mm/memory.c | 42 +++++++++- mm/util.c | 6 ++ tools/testing/vma/include/dup.h | 7 ++ 6 files changed, 159 insertions(+), 6 deletions(-)Documentation/filesystems/mmap_prepare.rst +8
-0@@ -156,5 +156,13 @@ pointer. These are: * mmap_action_simple_ioremap() - Sets up an I/O remap from a specified physical address and over a specified length. +* mmap_action_map_kernel_pages() - Maps a specified array of `struct page` + pointers in the VMA from a specific offset. + +* mmap_action_map_kernel_pages_full() - Maps a specified array of `struct + page` pointers over the entire VMA. The caller must ensure there are + sufficient entries in the page array to cover the entire range of the + described VMA. + **NOTE:** The ``action`` field should never normally be manipulated directly, rather you ought to use one of these helpers.include/linux/mm.h +0
-0
include/linux/mm.h static inline bool folio_maybe_mapped_shared(struct folio *folio) · line 2915
Quoted 5 lines
include/linux/mm.h +1
-1@@ -2912,7 +2912,7 @@ static inline bool folio_maybe_mapped_shared(struct folio *folio)* The caller must add any reference (e.g., from folio_try_get()) it might be * holding itself to the result. * -* Returns the expected folio refcount. +* Returns: the expected folio refcount.
nit: I see both "Returns:" and "Return:" being used in the codebase but this header file uses "Return:", so for consistency you should probably do the same. This also applies to later instances in this patch.
Quoted 267 lines
include/linux/mm.h +92
-1*/ static inline int folio_expected_ref_count (const struct folio *folio) {@@ -4364,6 +4364,45 @@ static inline void mmap_action_simple_ioremap(struct vm_area_desc *desc, action->type =MMAP_SIMPLE_IO_REMAP ; } +/** +* mmap_action_map_kernel_pages - helper for mmap_prepare hook to specify that +* @num kernel pages contained in the @pages array should be mapped to userland +* starting at virtual address @start. +* @desc: The VMA descriptor for the VMA requiring kernel pags to be mapped. +* @start: The virtual address from which to map them. +* @pages: An array of struct page pointers describing the memory to map. +* @nr_pages: The number of entries in the @pages aray. +*/ +static inline void mmap_action_map_kernel_pages (struct vm_area_desc *desc, +unsigned long start,struct page **pages, +unsigned long nr_pages) +{ +struct mmap_action *action = &desc->action; + + action->type =MMAP_MAP_KERNEL_PAGES ; + action->map_kernel.start = start; + action->map_kernel.pages = pages; + action->map_kernel.nr_pages = nr_pages; + action->map_kernel.pgoff = desc->pgoff; +} + +/** +* mmap_action_map_kernel_pages_full - helper for mmap_prepare hook to specify that +* kernel pages contained in the @pages array should be mapped to userland +* from @desc->start to @desc->end. +* @desc: The VMA descriptor for the VMA requiring kernel pags to be mapped. +* @pages: An array of struct page pointers describing the memory to map. +* +* The caller must ensure that @pages contains sufficient entries to cover the +* entire range described by @desc. +*/ +static inline void mmap_action_map_kernel_pages_full (struct vm_area_desc *desc, +struct page **pages) +{ +mmap_action_map_kernel_pages (desc, desc->start, pages, +vma_desc_pages (desc)); +} +int mmap_action_prepare (struct vm_area_desc *desc);int mmap_action_complete (struct vm_area_struct *vma,struct mmap_action *action);@@ -4380,10 +4419,59 @@ static inline struct vm_area_struct *find_exact_vma(struct mm_struct *mm,return vma; } +/** +* range_is_subset - Is the specified inner range a subset of the outer range? +* @outer_start: The start of the outer range. +* @outer_end: The exclusive end of the outer range. +* @inner_start: The start of the inner range. +* @inner_end: The exclusive end of the inner range. +* +* Returns: %true if [inner_start, inner_end) is a subset of [outer_start, +* outer_end), otherwise %false. +*/ +static inline bool range_is_subset (unsigned long outer_start, +unsigned long outer_end, +unsigned long inner_start, +unsigned long inner_end) +{ +return outer_start <= inner_start && inner_end <= outer_end; +} + +/** +* range_in_vma - is the specified [@start, @end) range a subset of the VMA? +* @vma: The VMA against which we want to check [@start, @end). +* @start: The start of the range we wish to check. +* @end: The exclusive end of the range we wish to check. +* +* Returns: %true if [@start, @end) is a subset of [@vma->vm_start, +* @vma->vm_end), %false otherwise. +*/ static inline bool range_in_vma (const struct vm_area_struct *vma,unsigned long start,unsigned long end) { -return (vma && vma->vm_start <= start && end <= vma->vm_end); +if (!vma) +return false; + +return range_is_subset (vma->vm_start, vma->vm_end, start, end); +} + +/** +* range_in_vma_desc - is the specified [@start, @end) range a subset of the VMA +* described by @desc, a VMA descriptor? +* @desc: The VMA descriptor against which we want to check [@start, @end). +* @start: The start of the range we wish to check. +* @end: The exclusive end of the range we wish to check. +* +* Returns: %true if [@start, @end) is a subset of [@desc->start, @desc->end), +* %false otherwise. +*/ +static inline bool range_in_vma_desc (const struct vm_area_desc *desc, +unsigned long start,unsigned long end) +{ +if (!desc) +return false; + +return range_is_subset (desc->start, desc->end, start, end); }#ifdef CONFIG_MMU @@ -4427,6 +4515,9 @@ int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,int vm_insert_page (struct vm_area_struct *,unsigned long addr,struct page *);int vm_insert_pages (struct vm_area_struct *vma,unsigned long addr,struct page **pages,unsigned long *num); +int map_kernel_pages_prepare (struct vm_area_desc *desc); +int map_kernel_pages_complete (struct vm_area_struct *vma, +struct mmap_action *action);int vm_map_pages (struct vm_area_struct *vma,struct page **pages,unsigned long num); int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages,include/linux/mm_types.h +7
-0@@ -815,6 +815,7 @@ enum mmap_action_type {MMAP_REMAP_PFN ,/* Remap PFN range. */ MMAP_IO_REMAP_PFN ,/* I/O remap PFN range. */ MMAP_SIMPLE_IO_REMAP ,/* I/O remap with guardrails. */ + MMAP_MAP_KERNEL_PAGES, /* Map kernel page range from array. */ };/* @@ -833,6 +834,12 @@ struct mmap_action {phys_addr_t start_phys_addr;unsigned long size; } simple_ioremap; +struct { +unsigned long start; +struct page **pages; +unsigned long nr_pages; +pgoff_t pgoff; + } map_kernel; };enum mmap_action_type type;mm/memory.c +38
-4@@ -2484,13 +2484,14 @@ static int insert_pages(struct vm_area_struct *vma, unsigned long addr,int vm_insert_pages (struct vm_area_struct *vma,unsigned long addr,struct page **pages,unsigned long *num) { -const unsigned long end_addr = addr + (*num *PAGE_SIZE ) -1 ; +const unsigned long nr_pages = *num; +const unsigned long end = addr +PAGE_SIZE * nr_pages; -if (addr < vma->vm_start || end_addr >= vma->vm_end) +if (!range_in_vma (vma, addr, end))return -EFAULT ;if (!(vma->vm_flags &VM_MIXEDMAP )) { -BUG_ON (mmap_read_trylock (vma->vm_mm)); -BUG_ON (vma->vm_flags &VM_PFNMAP ); +VM_WARN_ON_ONCE (mmap_read_trylock (vma->vm_mm)); +VM_WARN_ON_ONCE (vma->vm_flags &VM_PFNMAP );vm_flags_set (vma,VM_MIXEDMAP ); }/* Defer page refcount checking till we're about to map that page. */ @@ -2498,6 +2499,39 @@ int vm_insert_pages(struct vm_area_struct *vma, unsigned long addr, }EXPORT_SYMBOL (vm_insert_pages); +int map_kernel_pages_prepare (struct vm_area_desc *desc) +{ +const struct mmap_action *action = &desc->action; +const unsigned long addr = action->map_kernel.start; +unsigned long nr_pages, end; + +if (!vma_desc_test (desc,VMA_MIXEDMAP_BIT )) { +VM_WARN_ON_ONCE (mmap_read_trylock (desc->mm)); +VM_WARN_ON_ONCE (vma_desc_test (desc,VMA_PFNMAP_BIT )); +vma_desc_set_flags (desc,VMA_MIXEDMAP_BIT ); + } + + nr_pages = action->map_kernel.nr_pages; + end = addr +PAGE_SIZE * nr_pages; +if (!range_in_vma_desc (desc, addr, end)) +return -EFAULT ; + +return 0 ; +} +EXPORT_SYMBOL (map_kernel_pages_prepare); + +int map_kernel_pages_complete (struct vm_area_struct *vma, +struct mmap_action *action) +{ +unsigned long nr_pages; + + nr_pages = action->map_kernel.nr_pages; +return insert_pages (vma, action->map_kernel.start, + action->map_kernel.pages, + &nr_pages, vma->vm_page_prot); +} +EXPORT_SYMBOL (map_kernel_pages_complete); +/** * vm_insert_page - insert single page into user vma * @vma: user vma to map to mm/util.c +6
-0@@ -1441,6 +1441,8 @@ int mmap_action_prepare(struct vm_area_desc *desc)return io_remap_pfn_range_prepare (desc);case MMAP_SIMPLE_IO_REMAP :return simple_ioremap_prepare (desc); +case MMAP_MAP_KERNEL_PAGES : +return map_kernel_pages_prepare (desc); }WARN_ON_ONCE (1 );@@ -1472,6 +1474,9 @@ int mmap_action_complete(struct vm_area_struct *vma,case MMAP_IO_REMAP_PFN : err =io_remap_pfn_range_complete (vma, action);break ; +case MMAP_MAP_KERNEL_PAGES : + err =map_kernel_pages_complete (vma, action); +break ;case MMAP_SIMPLE_IO_REMAP :/* * The simple I/O remap should have been delegated to an I/O @@ -1494,6 +1499,7 @@ int mmap_action_prepare(struct vm_area_desc *desc)case MMAP_REMAP_PFN :case MMAP_IO_REMAP_PFN :case MMAP_SIMPLE_IO_REMAP : +case MMAP_MAP_KERNEL_PAGES :WARN_ON_ONCE (1 );/* nommu cannot handle these. */ break ; }tools/testing/vma/include/dup.h +7
-1@@ -454,6 +454,7 @@ enum mmap_action_type {MMAP_REMAP_PFN ,/* Remap PFN range. */ MMAP_IO_REMAP_PFN ,/* I/O remap PFN range. */ MMAP_SIMPLE_IO_REMAP ,/* I/O remap with guardrails. */ + MMAP_MAP_KERNEL_PAGES, /* Map kernel page range from an array. */ };/* @@ -472,6 +473,12 @@ struct mmap_action {phys_addr_t start;unsigned long len; } simple_ioremap; +struct { +unsigned long start; +struct page **pages; +unsigned long num; +pgoff_t pgoff; + } map_kernel; };enum mmap_action_type type; --2.53.0
Re: [PATCH v2 16/16] mm: on remap assert that input range within the proposed VMA
In reply to Lorenzo Stoakes (Oracle)Quoted 3 lines · Lorenzo Stoakes (Oracle)
Now we have
range_in_vma_desc(), updateremap_pfn_range_prepare()to check whether the input range in contained within the specified VMA, so we can
s/in contained/is contained
Quoted 7 lines
fail at prepare time if an invalid range is specified.
This covers the I/O remap mmap actions also which ultimately call into this function, and other mmap action types either already span the full VMA or check this already.
Signed-off-by Lorenzo Stoakes (Oracle) <[email protected]>
Quoted 21 lines
--- mm/memory.c | 3 +++ 1 file changed, 3 insertions(+)mm/memory.c +3
-1@@ -3142,6 +3142,9 @@ int remap_pfn_range_prepare(struct vm_area_desc *desc)const bool is_cow =vma_desc_is_cow_mapping (desc);int err; +if (!range_in_vma_desc (desc, start, end)) +return -EFAULT ; + err =get_remap_pgoff (is_cow, start, end, desc->start, desc->end, pfn, &desc->pgoff);if (err) --2.53.0
Lorenzo Stoakes (Oracle)
maintainerRe: [PATCH v2 06/16] mm: add mmap_action_simple_ioremap()
In reply to Suren BaghdasaryanQuoted 22 lines · Suren Baghdasaryan
Quoted 21 lines · Lorenzo Stoakes (Oracle)
Currently drivers use
vm_iomap_memory()as a simple helper function for I/O remapping memory over a range starting at a specified physical address over a specified length.In order to utilise this from
mmap_prepare, separate out the core logic into__simple_ioremap_prep(), updatevm_iomap_memory()to use it, and addsimple_ioremap_prepare()to do the same with a VMA descriptor object.We also add
MMAP_SIMPLE_IO_REMAPand relevant fields to the structmmap_actiontype to permit this operation also.We use
mmap_action_ioremap()to set up the actual I/O remap operation once we have checked and figured out the parameters, which makessimple_ioremap_prepare()easy to implement.We then add
mmap_action_simple_ioremap()to allow drivers to make use of this mode.We update the
mmap_preparedocumentation to describe this mode.
Quoted 7 lines
Quoted 3 lines
Finally, we update the VMA tests to reflect this change.
Signed-off-by Lorenzo Stoakes (Oracle) <[email protected]>A couple of nits, but otherwise LGTM.
Reviewed-by Suren Baghdasaryan <[email protected]>
Thanks!
Quoted 114 lines
Quoted 114 lines
--- Documentation/filesystems/mmap_prepare.rst | 3 + include/linux/mm.h | 24 +++++- include/linux/mm_types.h | 6 +- mm/internal.h | 2 + mm/memory.c | 87 +++++++++++++++------- mm/util.c | 12 +++ tools/testing/vma/include/dup.h | 6 +- 7 files changed, 112 insertions(+), 28 deletions(-)Documentation/filesystems/mmap_prepare.rst +3
-0@@ -153,5 +153,8 @@ pointer. These are: * mmap_action_ioremap_full() - Same as mmap_action_ioremap(), only remaps the entire mapping from ``start_pfn`` onward. +* mmap_action_simple_ioremap() - Sets up an I/O remap from a specified + physical address and over a specified length. + **NOTE:** The ``action`` field should never normally be manipulated directly, rather you ought to use one of these helpers.include/linux/mm.h +23
-1@@ -4337,11 +4337,33 @@ static inline void mmap_action_ioremap(struct vm_area_desc *desc,* @start_pfn: The first PFN in the range to remap. */ static inline void mmap_action_ioremap_full (struct vm_area_desc *desc, -unsigned long start_pfn) +unsigned long start_pfn) {mmap_action_ioremap (desc, desc->start, start_pfn,vma_desc_size (desc)); } +/** +* mmap_action_simple_ioremap - helper for mmap_prepare hook to specify that the +* physical range in [start_phys_addr, start_phys_addr + size) should be I/O +* remapped. +* @desc: The VMA descriptor for the VMA requiring remap. +* @start_phys_addr: Start of the physical memory to be mapped. +* @size: Size of the area to map. +* +* NOTE: Some drivers might want to tweak desc->page_prot for purposes of +* write-combine or similar. +*/ +static inline void mmap_action_simple_ioremap (struct vm_area_desc *desc, +phys_addr_t start_phys_addr, +unsigned long size) +{ +struct mmap_action *action = &desc->action; + + action->simple_ioremap.start_phys_addr = start_phys_addr; + action->simple_ioremap.size = size; + action->type =MMAP_SIMPLE_IO_REMAP ; +} +int mmap_action_prepare (struct vm_area_desc *desc);int mmap_action_complete (struct vm_area_struct *vma,struct mmap_action *action);include/linux/mm_types.h +5
-1@@ -814,6 +814,7 @@ enum mmap_action_type {MMAP_NOTHING ,/* Mapping is complete, no further action. */ MMAP_REMAP_PFN ,/* Remap PFN range. */ MMAP_IO_REMAP_PFN ,/* I/O remap PFN range. */ + MMAP_SIMPLE_IO_REMAP, /* I/O remap with guardrails. */ };/* @@ -822,13 +823,16 @@ enum mmap_action_type {*/ struct mmap_action {union { -/* Remap range. */ struct {unsigned long start;unsigned long start_pfn;unsigned long size;pgprot_t pgprot; } remap; +struct { +phys_addr_t start_phys_addr; +unsigned long size; + } simple_ioremap; };enum mmap_action_type type;mm/internal.h +2
-0@@ -1804,6 +1804,8 @@ int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm);int remap_pfn_range_prepare (struct vm_area_desc *desc);int remap_pfn_range_complete (struct vm_area_struct *vma,struct mmap_action *action); +int simple_ioremap_prepare (struct vm_area_desc *desc); +/* No simple_ioremap_complete, is ultimately handled by remap complete. */ static inline int io_remap_pfn_range_prepare (struct vm_area_desc *desc) {mm/memory.c +0
-0@@ -3170,6 +3170,59 @@ int remap_pfn_range_complete(struct vm_area_struct *vma,return do_remap_pfn_range (vma, start, pfn, size, prot); }
Quoted 4 lines
mm/memory.c int remap_pfn_range_complete(struct vm_area_struct *vma, · line 3173
Quoted 1 line
mm/memory.c +1
-0+static int __simple_ioremap_prep(unsigned long vm_start, unsigned long vm_end,nit:
vm_startandvm_endare used only to calculatevm_len. You could reduce the number of arguments by just passingvm_len.
Ack will fixup!
Quoted 44 lines
Quoted 44 lines
mm/memory.c +43
-0+ pgoff_t vm_pgoff, phys_addr_t start_phys, + unsigned long size, unsigned long *pfnp) +{ + const unsigned long vm_len = vm_end - vm_start; +unsigned long pfn, pages; + +/* Check that the physical memory area passed in looks valid */ +if (start_phys + size < start_phys) +return -EINVAL ; +/* +* You *really* shouldn't map things that aren't page-aligned, +* but we've historically allowed it because IO memory might +* just have smaller alignment. +*/ + size += start_phys & ~PAGE_MASK ; + pfn = start_phys >>PAGE_SHIFT ; + pages = (size + ~PAGE_MASK ) >>PAGE_SHIFT ; +if (pfn + pages < pfn) +return -EINVAL ; + +/* We start the mapping 'vm_pgoff' pages into the area */ +if (vm_pgoff > pages) +return -EINVAL ; + pfn += vm_pgoff; + pages -= vm_pgoff; + +/* Can we fit all of the mapping? */ +if ((vm_len >>PAGE_SHIFT ) > pages) +return -EINVAL ; + + *pfnp = pfn; +return 0 ; +} + +int simple_ioremap_prepare (struct vm_area_desc *desc) +{ +struct mmap_action *action = &desc->action; +const phys_addr_t start = action->simple_ioremap.start_phys_addr; +const unsigned long size = action->simple_ioremap.size; +unsigned long pfn; +int err; + + err = __simple_ioremap_prep(desc->start, desc->end, desc->pgoff,
Quoted 8 lines
mm/memory.c int remap_pfn_range_complete(struct vm_area_struct *vma, · line 3222
Quoted 6 lines
mm/memory.c +6
-0+ start, size, &pfn); +if (err) +return err; + +/* The I/O remap logic does the heavy lifting. */ +mmap_action_ioremap (desc, desc->start, pfn,vma_desc_size (desc));nit: Looks like a perfect opportunity to use
mmap_action_ioremap_full()here.
Yeah can do!
Quoted 6 lines
mm/memory.c int remap_pfn_range_complete(struct vm_area_struct *vma, · line 3223
Quoted 2 lines
mm/memory.c +1
-0+return mmap_action_prepare (desc);Ok, so IIUC this uses recursion:
mmap_action_prepare(MMAP_SIMPLE_IO_REMAP)->simple_ioremap_prepare()->mmap_action_prepare(MMAP_IO_REMAP_PFN).
Yep, it's one level, I think that should be ok? :)
Quoted 121 lines
Quoted 121 lines
mm/memory.c +11
-25+} +/** * vm_iomap_memory - remap memory to userspace * @vma: user vma to map to @@ -3187,32 +3240,16 @@ int remap_pfn_range_complete(struct vm_area_struct *vma,*/ int vm_iomap_memory (struct vm_area_struct *vma,phys_addr_t start,unsigned long len) { -unsigned long vm_len, pfn, pages; - -/* Check that the physical memory area passed in looks valid */ -if (start + len < start) -return -EINVAL ; -/* -* You *really* shouldn't map things that aren't page-aligned, -* but we've historically allowed it because IO memory might -* just have smaller alignment. -*/ - len += start & ~PAGE_MASK ; - pfn = start >>PAGE_SHIFT ; - pages = (len + ~PAGE_MASK ) >>PAGE_SHIFT ; -if (pfn + pages < pfn) -return -EINVAL ; - -/* We start the mapping 'vm_pgoff' pages into the area */ -if (vma->vm_pgoff > pages) -return -EINVAL ; - pfn += vma->vm_pgoff; - pages -= vma->vm_pgoff; +const unsigned long vm_start = vma->vm_start; +const unsigned long vm_end = vma->vm_end; +const unsigned long vm_len = vm_end - vm_start; +unsigned long pfn; +int err; -/* Can we fit all of the mapping? */ - vm_len = vma->vm_end - vma->vm_start; -if (vm_len >>PAGE_SHIFT > pages) -return -EINVAL ; + err =__simple_ioremap_prep (vm_start, vm_end, vma->vm_pgoff, start, + len, &pfn); +if (err) +return err;/* Ok, let it rip */ return io_remap_pfn_range (vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);mm/util.c +12
-0@@ -1390,6 +1390,8 @@ int mmap_action_prepare(struct vm_area_desc *desc)return remap_pfn_range_prepare (desc);case MMAP_IO_REMAP_PFN :return io_remap_pfn_range_prepare (desc); +case MMAP_SIMPLE_IO_REMAP : +return simple_ioremap_prepare (desc); }WARN_ON_ONCE (1 );@@ -1421,6 +1423,14 @@ int mmap_action_complete(struct vm_area_struct *vma,case MMAP_IO_REMAP_PFN : err =io_remap_pfn_range_complete (vma, action);break ; +case MMAP_SIMPLE_IO_REMAP : +/* +* The simple I/O remap should have been delegated to an I/O +* remap. +*/ +WARN_ON_ONCE (1 ); + err = -EINVAL ; +break ; }return mmap_action_finish (vma, action, err);@@ -1434,6 +1444,7 @@ int mmap_action_prepare(struct vm_area_desc *desc)break ;case MMAP_REMAP_PFN :case MMAP_IO_REMAP_PFN : +case MMAP_SIMPLE_IO_REMAP :WARN_ON_ONCE (1 );/* nommu cannot handle these. */ break ; }@@ -1452,6 +1463,7 @@ int mmap_action_complete(struct vm_area_struct *vma,break ;case MMAP_REMAP_PFN :case MMAP_IO_REMAP_PFN : +case MMAP_SIMPLE_IO_REMAP :WARN_ON_ONCE (1 );/* nommu cannot handle this. */ err = -EINVAL ;tools/testing/vma/include/dup.h +1
-0@@ -453,6 +453,7 @@ enum mmap_action_type {MMAP_NOTHING ,/* Mapping is complete, no further action. */ MMAP_REMAP_PFN ,/* Remap PFN range. */ MMAP_IO_REMAP_PFN ,/* I/O remap PFN range. */ + MMAP_SIMPLE_IO_REMAP, /* I/O remap with guardrails. */ };Quoted 1 line
Quoted 1 line
/*
tools/testing/vma/include/dup.h +4
-1@@ -461,13 +462,16 @@ enum mmap_action_type {*/ struct mmap_action {union { -/* Remap range. */ struct {unsigned long start;unsigned long start_pfn;unsigned long size;pgprot_t pgprot; } remap; +struct { +phys_addr_t start; +unsigned long len; + } simple_ioremap; };enum mmap_action_type type;
Quoted 3 lines
Quoted 3 lines
--
2.53.0
Cheers, Lorenzo
Re: [PATCH v2 12/16] mm: allow handling of stacked mmap_prepare hooks in more drivers
In reply to Lorenzo Stoakes (Oracle)Quoted 11 lines · On Mon, 16 Mar 2026 21:12:08 +0000 "Lorenzo Stoakes (Oracle)" <[email protected]>…
While the conversion of mmap hooks to
mmap_prepareis underway, we wil encounter situations where mmap hooks need to invoke nestedmmap_preparehooks.The nesting of mmap hooks is termed 'stacking'. In order to flexibly facilitate the conversion of custom mmap hooks in drivers which stack, we must split up the existing
compat_vma_mapped()function into two separate functions:*
compat_set_desc_from_vma()- This allows the setting of avm_area_descobject's fields to the relevant fields of a VMA.
Hello Lorenzo, I hope you are doing well!
Thank you for this patch. I was developing on top of mm-new today and had an error that I think was caused by this patch. I want to preface this by saying that I am not at all familiar with this area of the code, so please do forgive me if I've misinterpreted the crash and mistakenly pointed at this commit : -)
Here is the crash:
[ 1.083795] kernel tried to execute NX-protected page - exploit attempt? (uid: 0) [ 1.083883] BUG: unable to handle page fault for address: ffa00000048efbb8 [ 1.083957] #PF: supervisor instruction fetch in kernel mode [ 1.084030] #PF: error_code(0x0011) - permissions violation [ 1.084086] PGD 100000067 P4D 10035f067 PUD 100364067 PMD 441ed9067 PTE 80000004466a3163 [ 1.084162] Oops: Oops: 0011 [#1] SMP [ 1.084218] CPU: 0 UID: 0 PID: 305 Comm: mkdir Tainted: G W E 7.0.0-rc4-virtme-00442-ge53de5a0302f-dirty #85 PREEMPTLAZY
As you can see, it's on a QEMU instance. I don't think this makes a difference in the crash, though.
[ 1.084321] Tainted: [W]=WARN, [E]=UNSIGNED_MODULE [ 1.084369] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.3-5.el9 11/05/2023 [ 1.084450] RIP: 0010:0xffa00000048efbb8 [ 1.084489] Code: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 <40> 12 0e 00 01 00 11 ff d0 fa 8e 04 00 00 a0 ff 80 33 51 02 01 00 [ 1.084642] RSP: 0018:ffa00000048ef998 EFLAGS: 00010286 [ 1.084692] RAX: ffa00000048efbb8 RBX: ff11000102512cc0 RCX: 000000000000000d [ 1.084766] RDX: ffffffffa06247d0 RSI: ffa00000048efa18 RDI: ff11000102512cc0 [ 1.084826] RBP: ffa00000048ef9c8 R08: 0000000000000000 R09: 0000000000000007 [ 1.084889] R10: ff110001047d1f08 R11: 00007effdc3d0fff R12: ff110001047d3b00 [ 1.084954] R13: ff11000446cae600 R14: ff110001024efe00 R15: ff11000102510a80 [ 1.085021] FS: 0000000000000000(0000) GS:ff110004aae72000(0000) knlGS:0000000000000000 [ 1.085083] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 1.085136] CR2: ffa00000048efbb8 CR3: 0000000102667001 CR4: 0000000000771ef0 [ 1.085201] PKRU: 55555554 [ 1.085228] Call Trace: [ 1.085248] <TASK> [ 1.085274] ? __compat_vma_mmap+0x8e/0x130 [ 1.085318] ? compat_vma_mmap+0x76/0x80 [ 1.085354] ? mas_alloc_nodes+0xb2/0x110 [ 1.085390] ? backing_file_mmap+0xc3/0xf0 [ 1.085426] ? ovl_mmap+0x41/0x50 [ 1.085463] ? ovl_mmap+0x50/0x50 [ 1.085499] ? __mmap_region+0x7e8/0x1100 [ 1.085539] ? do_mmap+0x49f/0x5e0 [ 1.085573] ? vm_mmap_pgoff+0xef/0x1e0 [ 1.085609] ? ksys_mmap_pgoff+0x15c/0x1f0 [ 1.085647] ? do_syscall_64+0xab/0x980 [ 1.085684] ? entry_SYSCALL_64_after_hwframe+0x4b/0x53 [ 1.085730] </TASK> [ 1.085770] Modules linked in: virtio_mmio(E) 9pnet_virtio(E) 9p(E) 9pnet(E) netfs(E) [ 1.085838] CR2: ffa00000048efbb8 [ 1.085874] ---[ end trace 0000000000000000 ]--- [ 1.085875] kernel tried to execute NX-protected page - exploit attempt? (uid: 0) [ 1.085918] RIP: 0010:0xffa00000048efbb8 [ 1.085921] Code: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 <40> 12 0e 00 01 00 11 ff d0 fa 8e 04 00 00 a0 ff 80 33 51 02 01 00 [ 1.085988] BUG: unable to handle page fault for address: ffa00000048f7bb8 [ 1.086026] RSP: 0018:ffa00000048ef998 EFLAGS: 00010286 [ 1.086166] #PF: supervisor instruction fetch in kernel mode [ 1.086221] [ 1.086267] #PF: error_code(0x0011) - permissions violation [ 1.086321] RAX: ffa00000048efbb8 RBX: ff11000102512cc0 RCX: 000000000000000d [ 1.086348] PGD 100000067 [ 1.086394] RDX: ffffffffa06247d0 RSI: ffa00000048efa18 RDI: ff11000102512cc0 [ 1.086459] P4D 10035f067 [ 1.086486] RBP: ffa00000048ef9c8 R08: 0000000000000000 R09: 0000000000000007 [ 1.086550] PUD 100364067 [ 1.086577] R10: ff110001047d1f08 R11: 00007effdc3d0fff R12: ff110001047d3b00 [ 1.086641] PMD 441ed9067 [ 1.086668] R13: ff11000446cae600 R14: ff110001024efe00 R15: ff11000102510a80 [ 1.086731] PTE 80000004433d3163 [ 1.086764] FS: 0000000000000000(0000) GS:ff110004aae72000(0000) knlGS:0000000000000000 [ 1.086829] [ 1.086868] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 1.086931] Oops: Oops: 0011 [#2] SMP [ 1.086958] CR2: ffa00000048efbb8 CR3: 0000000102667001 CR4: 0000000000771ef0 [ 1.087015] CPU: 29 UID: 0 PID: 306 Comm: mount Tainted: G D W E 7.0.0-rc4-virtme-00442-ge53de5a0302f-dirty #85 PREEMPTLAZY [ 1.087050] PKRU: 55555554 [ 1.087115] Tainted: [D]=DIE, [W]=WARN, [E]=UNSIGNED_MODULE [ 1.087207] Kernel panic - not syncing: Fatal exception [ 2.158392] Shutting down cpus with NMI [ 2.158629] Kernel Offset: disabled [ 2.158668] ---[ end Kernel panic - not syncing: Fatal exception ]---
It crashes at compat_vma_mmap, and here is what I think could be the potential crash path:
- compat_vma_mmap() creates struct vm_area_desc desc;
- compat_set_desc_from_vma Doesn't initialize the struct, but instead modifies independent fields. I think this is where the behavior diverges, since before we would use the C initializer and uninitialized variables would be set to 0 (including ommitted ones, like action.success_hook or action.error_hook). But action.type = MMAP_NOTHING
- desc.action.success_hook remains uninitialized in vfs_mmap_prepare
- mmap_action_complete()
- Here, We've set action.type to be MMAP_NOTHING, so we have err = 0
- mmap_action_finish(action, vma, 0)
- And here, since err == 0, we check action->success_hook (which has garbage, therefore it's nonzero) and call action->success_hook(vma)
And I think action->success_hook(vma) where success_hook is uninitialized stack garbage gets me to where I am.
Again, I'm not too familiar with this area of the kernel, this is just based on the quick digging that I did. And aplogies again if I'm missing something ; -) I do think that the uninitialized members could be a problem though.
Thank you, I hope you have a great day Lorenzo!
Joshua
Lorenzo Stoakes (Oracle)
maintainerRe: [PATCH v2 12/16] mm: allow handling of stacked mmap_prepare hooks in more drivers
Quoted 98 lines · Joshua Hahn
Quoted 11 lines · On Mon, 16 Mar 2026 21:12:08 +0000 "Lorenzo Stoakes (Oracle)" <[email protected]>…
While the conversion of mmap hooks to
mmap_prepareis underway, we wil encounter situations where mmap hooks need to invoke nestedmmap_preparehooks.The nesting of mmap hooks is termed 'stacking'. In order to flexibly facilitate the conversion of custom mmap hooks in drivers which stack, we must split up the existing
compat_vma_mapped()function into two separate functions:*
compat_set_desc_from_vma()- This allows the setting of avm_area_descobject's fields to the relevant fields of a VMA.Hello Lorenzo, I hope you are doing well!
Thank you for this patch. I was developing on top of mm-new today and had an error that I think was caused by this patch. I want to preface this by saying that I am not at all familiar with this area of the code, so please do forgive me if I've misinterpreted the crash and mistakenly pointed at this commit : -)
Here is the crash:
[ 1.083795] kernel tried to execute NX-protected page - exploit attempt? (uid: 0) [ 1.083883] BUG: unable to handle page fault for address: ffa00000048efbb8 [ 1.083957] #PF: supervisor instruction fetch in kernel mode [ 1.084030] #PF: error_code(0x0011) - permissions violation [ 1.084086] PGD 100000067 P4D 10035f067 PUD 100364067 PMD 441ed9067 PTE 80000004466a3163 [ 1.084162] Oops: Oops: 0011 [#1] SMP [ 1.084218] CPU: 0 UID: 0 PID: 305 Comm: mkdir Tainted: G W E 7.0.0-rc4-virtme-00442-ge53de5a0302f-dirty #85 PREEMPTLAZYAs you can see, it's on a QEMU instance. I don't think this makes a difference in the crash, though.
[ 1.084321] Tainted: [W]=WARN, [E]=UNSIGNED_MODULE [ 1.084369] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.3-5.el9 11/05/2023 [ 1.084450] RIP: 0010:0xffa00000048efbb8 [ 1.084489] Code: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 <40> 12 0e 00 01 00 11 ff d0 fa 8e 04 00 00 a0 ff 80 33 51 02 01 00 [ 1.084642] RSP: 0018:ffa00000048ef998 EFLAGS: 00010286 [ 1.084692] RAX: ffa00000048efbb8 RBX: ff11000102512cc0 RCX: 000000000000000d [ 1.084766] RDX: ffffffffa06247d0 RSI: ffa00000048efa18 RDI: ff11000102512cc0 [ 1.084826] RBP: ffa00000048ef9c8 R08: 0000000000000000 R09: 0000000000000007 [ 1.084889] R10: ff110001047d1f08 R11: 00007effdc3d0fff R12: ff110001047d3b00 [ 1.084954] R13: ff11000446cae600 R14: ff110001024efe00 R15: ff11000102510a80 [ 1.085021] FS: 0000000000000000(0000) GS:ff110004aae72000(0000) knlGS:0000000000000000 [ 1.085083] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 1.085136] CR2: ffa00000048efbb8 CR3: 0000000102667001 CR4: 0000000000771ef0 [ 1.085201] PKRU: 55555554 [ 1.085228] Call Trace: [ 1.085248] <TASK> [ 1.085274] ? __compat_vma_mmap+0x8e/0x130 [ 1.085318] ? compat_vma_mmap+0x76/0x80 [ 1.085354] ? mas_alloc_nodes+0xb2/0x110 [ 1.085390] ? backing_file_mmap+0xc3/0xf0 [ 1.085426] ? ovl_mmap+0x41/0x50 [ 1.085463] ? ovl_mmap+0x50/0x50 [ 1.085499] ? __mmap_region+0x7e8/0x1100 [ 1.085539] ? do_mmap+0x49f/0x5e0 [ 1.085573] ? vm_mmap_pgoff+0xef/0x1e0 [ 1.085609] ? ksys_mmap_pgoff+0x15c/0x1f0 [ 1.085647] ? do_syscall_64+0xab/0x980 [ 1.085684] ? entry_SYSCALL_64_after_hwframe+0x4b/0x53 [ 1.085730] </TASK> [ 1.085770] Modules linked in: virtio_mmio(E) 9pnet_virtio(E) 9p(E) 9pnet(E) netfs(E) [ 1.085838] CR2: ffa00000048efbb8 [ 1.085874] ---[ end trace 0000000000000000 ]--- [ 1.085875] kernel tried to execute NX-protected page - exploit attempt? (uid: 0) [ 1.085918] RIP: 0010:0xffa00000048efbb8 [ 1.085921] Code: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 <40> 12 0e 00 01 00 11 ff d0 fa 8e 04 00 00 a0 ff 80 33 51 02 01 00 [ 1.085988] BUG: unable to handle page fault for address: ffa00000048f7bb8 [ 1.086026] RSP: 0018:ffa00000048ef998 EFLAGS: 00010286 [ 1.086166] #PF: supervisor instruction fetch in kernel mode [ 1.086221] [ 1.086267] #PF: error_code(0x0011) - permissions violation [ 1.086321] RAX: ffa00000048efbb8 RBX: ff11000102512cc0 RCX: 000000000000000d [ 1.086348] PGD 100000067 [ 1.086394] RDX: ffffffffa06247d0 RSI: ffa00000048efa18 RDI: ff11000102512cc0 [ 1.086459] P4D 10035f067 [ 1.086486] RBP: ffa00000048ef9c8 R08: 0000000000000000 R09: 0000000000000007 [ 1.086550] PUD 100364067 [ 1.086577] R10: ff110001047d1f08 R11: 00007effdc3d0fff R12: ff110001047d3b00 [ 1.086641] PMD 441ed9067 [ 1.086668] R13: ff11000446cae600 R14: ff110001024efe00 R15: ff11000102510a80 [ 1.086731] PTE 80000004433d3163 [ 1.086764] FS: 0000000000000000(0000) GS:ff110004aae72000(0000) knlGS:0000000000000000 [ 1.086829] [ 1.086868] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 1.086931] Oops: Oops: 0011 [#2] SMP [ 1.086958] CR2: ffa00000048efbb8 CR3: 0000000102667001 CR4: 0000000000771ef0 [ 1.087015] CPU: 29 UID: 0 PID: 306 Comm: mount Tainted: G D W E 7.0.0-rc4-virtme-00442-ge53de5a0302f-dirty #85 PREEMPTLAZY [ 1.087050] PKRU: 55555554 [ 1.087115] Tainted: [D]=DIE, [W]=WARN, [E]=UNSIGNED_MODULE [ 1.087207] Kernel panic - not syncing: Fatal exception [ 2.158392] Shutting down cpus with NMI [ 2.158629] Kernel Offset: disabled [ 2.158668] ---[ end Kernel panic - not syncing: Fatal exception ]---
Quoted 7 lines
It crashes at
compat_vma_mmap, and here is what I think could be the potential crash path:-
compat_vma_mmap()creates structvm_area_descdesc;
-compat_set_desc_from_vmaDoesn't initialize the struct, but instead modifies independent fields. I think this is where the behavior diverges, since before we would use the C initializer and uninitialized
Ah yeah you're right I'll fix that up!
Quoted 12 lines
variables would be set to 0 (including ommitted ones, like action.success_hook or action.error_hook). But action.type =
MMAP_NOTHING
- desc.action.success_hook remains uninitialized invfs_mmap_prepare
-mmap_action_complete()
- Here, We've set action.type to beMMAP_NOTHING, so we have err = 0
-mmap_action_finish(action, vma, 0)
- And here, since err == 0, we checkaction->success_hook(which has garbage, therefore it's nonzero) and callaction->success_hook(vma)And I think
action->success_hook(vma)wheresuccess_hookis uninitialized stack garbage gets me to where I am.
Quoted 7 lines
Again, I'm not too familiar with this area of the kernel, this is just based on the quick digging that I did. And aplogies again if I'm missing something ; -) I do think that the uninitialized members could be a problem though.
Thank you, I hope you have a great day Lorenzo!
Joshua
Thanks for the report and analysis, much appreciated, hope you have a great day too :)
Cheers, Lorenzo
Lorenzo Stoakes (Oracle)
maintainerRe: [PATCH v2 11/16] staging: vme_user: replace deprecated mmap hook with mmap_prepare
In reply to Suren BaghdasaryanQuoted 154 lines · Suren Baghdasaryan
Quoted 153 lines · Suren Baghdasaryan
Quoted 122 lines · Lorenzo Stoakes (Oracle)
The
f_op->mmapinterface is deprecated, so update driver to use its successor,mmap_prepare.The driver previously used
vm_iomap_memory(), so this change replaces it with itsmmap_prepareequivalent,mmap_action_simple_ioremap().Functions that wrap
mmap()are also converted to wrapmmap_prepare()instead.Also update the documentation accordingly.
Signed-off-by Lorenzo Stoakes (Oracle) <[email protected]>--- Documentation/driver-api/vme.rst | 2 +- drivers/staging/vme_user/vme.c | 20 +++++------ drivers/staging/vme_user/vme.h | 2 +- drivers/staging/vme_user/vme_user.c | 51 +++++++++++++++++------------ 4 files changed, 42 insertions(+), 33 deletions(-)Documentation/driver-api/vme.rst +1
-1@@ -107,7 +107,7 @@ The function :c:func:`vme_master_read` can be used to read from and In addition to simple reads and writes, :c:func:`vme_master_rmw` is provided to do a read-modify-write transaction. Parts of a VME window can also be mapped -into user space memory using :c:func:`vme_master_mmap`. +into user space memory using :c:func:`vme_master_mmap_prepare`. Slave windowsdrivers/staging/vme_user/vme.c +10
-10@@ -735,9 +735,9 @@ unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask,EXPORT_SYMBOL (vme_master_rmw);/** -* vme_master_mmap - Mmap region of VME master window. +* vme_master_mmap_prepare - Mmap region of VME master window. * @resource: Pointer to VME master resource. -* @vma: Pointer to definition of user mapping. +* @desc: Pointer to descriptor of user mapping. * * Memory map a region of the VME master window into user space. * @@ -745,12 +745,13 @@ EXPORT_SYMBOL(vme_master_rmw);* resource or -EFAULT if map exceeds window size. Other generic mmap * errors may also be returned. */ -int vme_master_mmap (struct vme_resource *resource,struct vm_area_struct *vma) +int vme_master_mmap_prepare (struct vme_resource *resource, +struct vm_area_desc *desc) { +const unsigned long vma_size =vma_desc_size (desc);struct vme_bridge *bridge =find_bridge (resource);struct vme_master_resource *image;phys_addr_t phys_addr; -unsigned long vma_size;if (resource->type !=VME_MASTER ) {dev_err (bridge->parent,"Not a master resource\n" );@@ -758,19 +759,18 @@ int vme_master_mmap(struct vme_resource *resource, struct vm_area_struct *vma) } image = list_entry(resource->entry, struct vme_master_resource, list); - phys_addr = image->bus_resource.start + (vma->vm_pgoff <<PAGE_SHIFT ); - vma_size = vma->vm_end - vma->vm_start; + phys_addr = image->bus_resource.start + (desc->pgoff <<PAGE_SHIFT );if (phys_addr + vma_size > image->bus_resource.end +1 ) {dev_err (bridge->parent,"Map size cannot exceed the window size\n" );return -EFAULT ; } - vma->vm_page_prot =pgprot_noncached (vma->vm_page_prot); - -return vm_iomap_memory (vma, phys_addr, vma->vm_end - vma->vm_start); + desc->page_prot =pgprot_noncached (desc->page_prot); +mmap_action_simple_ioremap (desc, phys_addr, vma_size); +return 0 ; } -EXPORT_SYMBOL (vme_master_mmap); +EXPORT_SYMBOL (vme_master_mmap_prepare);/** * vme_master_free - Free VME master window drivers/staging/vme_user/vme.h +1
-1@@ -151,7 +151,7 @@ ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count,ssize_t vme_master_write (struct vme_resource *resource,void *buf,size_t count,loff_t offset);unsigned int vme_master_rmw (struct vme_resource *resource,unsigned int mask,unsigned int compare,unsigned int swap,loff_t offset); -int vme_master_mmap (struct vme_resource *resource,struct vm_area_struct *vma); +int vme_master_mmap_prepare (struct vme_resource *resource,struct vm_area_desc *desc);void vme_master_free (struct vme_resource *resource);struct vme_resource *vme_dma_request (struct vme_dev *vdev,u32 route);drivers/staging/vme_user/vme_user.c +3
-7@@ -446,24 +446,14 @@ static void vme_user_vm_close(struct vm_area_struct *vma)kfree (vma_priv); } -static const struct vm_operations_struct vme_user_vm_ops = { - .open = vme_user_vm_open, - .close = vme_user_vm_close, -}; - -static int vme_user_master_mmap (unsigned int minor,struct vm_area_struct *vma) +static int vme_user_vm_mapped (unsigned long start,unsigned long end,pgoff_t pgoff, +const struct file *file,void **vm_private_data) { -int err; +const unsigned int minor =iminor (file_inode (file));drivers/staging/vme_user/vme_user.c static void vme_user_vm_close(struct vm_area_struct *vma) · line 466
Quoted 10 lines
drivers/staging/vme_user/vme_user.c +0
-6struct vme_user_vma_priv *vma_priv;mutex_lock (&image[minor].mutex); - err =vme_master_mmap (image[minor].resource, vma); -if (err) { -mutex_unlock (&image[minor].mutex); -return err; - } -Ok, this changes the set of the operations performed under image[minor].mutex. Before we had:
mutex_lock(&image[minor].mutex);vme_master_mmap();
<some final adjustments>mutex_unlock(&image[minor].mutex);Now we have:
mutex_lock(&image[minor].mutex);vme_master_mmap_prepare()mutex_unlock(&image[minor].mutex);vm_iomap_memory();mutex_lock(&image[minor].mutex);vme_user_vm_mapped(); // <some final adjustments>mutex_unlock(&image[minor].mutex);
Quoted 4 lines
Quoted 4 lines
I think as long as image[minor] does not change while we are not holding the mutex we should be safe, and looking at the code it seems to be the case. But I'm not familiar with this driver and might be wrong. Worth double-checking.
The file is pinned for the duration, the mutex is associated with the file, so there's no sane world in which that could be problematic.
Keeping in mind that we manipulate stuff on vme_user_vm_close() that directly acceses image[minor] at an arbitary time.
Quoted 6 lines
A side note: if we had to hold the mutex across all those operations I think we would need to take the mutex in the
vm_ops->mmap_prepareand add avm_ops->map_failedhook or something along that line to drop the mutex in casemmap_action_complete()fails. Not sure if we will have such cases though...
No, I don't want to do this if it can be at all avoided. You should in nearly any sane circumstance be able to defer things until the mapped hook anyway.
Also a merge can happen too after an .mmap_prepare, so we'd have to have some 'success' hook and I'm just not going there it'll end up open to abuse again.
(We do have success and error filtering hooks right now, sadly, but they're really for hugetlb and I plan to find a way to get rid of them).
The mmap_prepare is meant to essentially be as stateless as possible.
Anyway I don't think it's relevant here.
Quoted 62 lines
Quoted 62 lines
Quoted 62 lines
drivers/staging/vme_user/vme_user.c +27
-8vma_priv =kmalloc_obj (*vma_priv);if (!vma_priv) {mutex_unlock (&image[minor].mutex);@@ -472,22 +462,41 @@ static int vme_user_master_mmap(unsigned int minor, struct vm_area_struct *vma) vma_priv->minor = minor;refcount_set (&vma_priv->refcnt,1 ); - vma->vm_ops = &vme_user_vm_ops; - vma->vm_private_data = vma_priv; - + *vm_private_data = vma_priv; image[minor].mmap_count++;mutex_unlock (&image[minor].mutex); -return 0 ; } -static int vme_user_mmap (struct file *file,struct vm_area_struct *vma) +static const struct vm_operations_struct vme_user_vm_ops = { + .mapped = vme_user_vm_mapped, + .open = vme_user_vm_open, + .close = vme_user_vm_close, +}; + +static int vme_user_master_mmap_prepare (unsigned int minor, +struct vm_area_desc *desc) +{ +int err; + +mutex_lock (&image[minor].mutex); + + err =vme_master_mmap_prepare (image[minor].resource, desc); +if (!err) + desc->vm_ops = &vme_user_vm_ops; + +mutex_unlock (&image[minor].mutex); +return err; +} + +static int vme_user_mmap_prepare (struct vm_area_desc *desc) { -unsigned int minor =iminor (file_inode (file)); +const struct file *file = desc->file; +const unsigned int minor =iminor (file_inode (file));if (type[minor] ==MASTER_MINOR ) -return vme_user_master_mmap (minor, vma); +return vme_user_master_mmap_prepare (minor, desc);return -ENODEV ; }@@ -498,7 +507,7 @@ static const struct file_operations vme_user_fops = { .llseek = vme_user_llseek, .unlocked_ioctl = vme_user_unlocked_ioctl, .compat_ioctl = compat_ptr_ioctl, - .mmap = vme_user_mmap, + .mmap_prepare = vme_user_mmap_prepare, };
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/staging/vme_user/vme_user.c +0
-1static int vme_user_match (struct vme_dev *vdev) --2.53.0
Cheers, Lorenzo
Lorenzo Stoakes (Oracle)
maintainerRe: [PATCH v2 15/16] mm: add mmap_action_map_kernel_pages[_full]()
In reply to Suren BaghdasaryanQuoted 24 lines · Suren Baghdasaryan
Quoted 23 lines · Lorenzo Stoakes (Oracle)
A user can invoke
mmap_action_map_kernel_pages()to specify that the mapping should map kernel pages starting fromdesc->startof a specified number of pages specified in an array.In order to implement this, adjust
mmap_action_prepare()to be able to return an error code, as it makes sense to assert that the specified parameters are valid as quickly as possible as well as updating the VMA flags to includeVMA_MIXEDMAP_BITas necessary.This provides an
mmap_prepareequivalent ofvm_insert_pages().We additionally update the existing
vm_insert_pages()code to userange_in_vma()and add a newrange_in_vma_desc()helper function for themmap_preparecase, sharing the code between the two inrange_is_subset().We add both
mmap_action_map_kernel_pages()andmmap_action_map_kernel_pages_full()to allow for both partial and full VMA mappings.We also add
mmap_action_map_kernel_pages_discontig()to allow for discontiguous mapping of kernel pages should the need arise.
Quoted 8 lines
Quoted 5 lines
We update the documentation to reflect the new features.
Finally, we update the VMA tests accordingly to reflect the changes.
Signed-off-by Lorenzo Stoakes (Oracle) <[email protected]>With one nit,
Reviewed-by Suren Baghdasaryan <[email protected]>
Thanks!
Quoted 35 lines
Quoted 35 lines
--- Documentation/filesystems/mmap_prepare.rst | 8 ++ include/linux/mm.h | 95 +++++++++++++++++++++- include/linux/mm_types.h | 7 ++ mm/memory.c | 42 +++++++++- mm/util.c | 6 ++ tools/testing/vma/include/dup.h | 7 ++ 6 files changed, 159 insertions(+), 6 deletions(-)Documentation/filesystems/mmap_prepare.rst +8
-0@@ -156,5 +156,13 @@ pointer. These are: * mmap_action_simple_ioremap() - Sets up an I/O remap from a specified physical address and over a specified length. +* mmap_action_map_kernel_pages() - Maps a specified array of `struct page` + pointers in the VMA from a specific offset. + +* mmap_action_map_kernel_pages_full() - Maps a specified array of `struct + page` pointers over the entire VMA. The caller must ensure there are + sufficient entries in the page array to cover the entire range of the + described VMA. + **NOTE:** The ``action`` field should never normally be manipulated directly, rather you ought to use one of these helpers.include/linux/mm.h +0
-0@@ -2912,7 +2912,7 @@ static inline bool folio_maybe_mapped_shared(struct folio *folio)* The caller must add any reference (e.g., from folio_try_get()) it might be * holding itself to the result.
Quoted 8 lines
include/linux/mm.h static inline bool folio_maybe_mapped_shared(struct folio *folio) · line 2915
Quoted 3 lines
include/linux/mm.h +1
-1* -* Returns the expected folio refcount. +* Returns: the expected folio refcount. nit: I see both "Returns:" and "Return:" being used in the codebase but this header file uses "Return:", so for consistency you should probably do the same. This also applies to later instances in this patch.
Well here I'm just adding the colon, while I'm here (maybe have been an update in response to feedback actualy).
And this function that's not part of my change already uses 'Returns' and I'm pretty sure that's the correct form.
So I think not a big deal to keep using that?
Quoted 268 lines
Quoted 268 lines
include/linux/mm.h +92
-1*/ static inline int folio_expected_ref_count (const struct folio *folio) {@@ -4364,6 +4364,45 @@ static inline void mmap_action_simple_ioremap(struct vm_area_desc *desc, action->type =MMAP_SIMPLE_IO_REMAP ; } +/** +* mmap_action_map_kernel_pages - helper for mmap_prepare hook to specify that +* @num kernel pages contained in the @pages array should be mapped to userland +* starting at virtual address @start. +* @desc: The VMA descriptor for the VMA requiring kernel pags to be mapped. +* @start: The virtual address from which to map them. +* @pages: An array of struct page pointers describing the memory to map. +* @nr_pages: The number of entries in the @pages aray. +*/ +static inline void mmap_action_map_kernel_pages (struct vm_area_desc *desc, +unsigned long start,struct page **pages, +unsigned long nr_pages) +{ +struct mmap_action *action = &desc->action; + + action->type =MMAP_MAP_KERNEL_PAGES ; + action->map_kernel.start = start; + action->map_kernel.pages = pages; + action->map_kernel.nr_pages = nr_pages; + action->map_kernel.pgoff = desc->pgoff; +} + +/** +* mmap_action_map_kernel_pages_full - helper for mmap_prepare hook to specify that +* kernel pages contained in the @pages array should be mapped to userland +* from @desc->start to @desc->end. +* @desc: The VMA descriptor for the VMA requiring kernel pags to be mapped. +* @pages: An array of struct page pointers describing the memory to map. +* +* The caller must ensure that @pages contains sufficient entries to cover the +* entire range described by @desc. +*/ +static inline void mmap_action_map_kernel_pages_full (struct vm_area_desc *desc, +struct page **pages) +{ +mmap_action_map_kernel_pages (desc, desc->start, pages, +vma_desc_pages (desc)); +} +int mmap_action_prepare (struct vm_area_desc *desc);int mmap_action_complete (struct vm_area_struct *vma,struct mmap_action *action);@@ -4380,10 +4419,59 @@ static inline struct vm_area_struct *find_exact_vma(struct mm_struct *mm,return vma; } +/** +* range_is_subset - Is the specified inner range a subset of the outer range? +* @outer_start: The start of the outer range. +* @outer_end: The exclusive end of the outer range. +* @inner_start: The start of the inner range. +* @inner_end: The exclusive end of the inner range. +* +* Returns: %true if [inner_start, inner_end) is a subset of [outer_start, +* outer_end), otherwise %false. +*/ +static inline bool range_is_subset (unsigned long outer_start, +unsigned long outer_end, +unsigned long inner_start, +unsigned long inner_end) +{ +return outer_start <= inner_start && inner_end <= outer_end; +} + +/** +* range_in_vma - is the specified [@start, @end) range a subset of the VMA? +* @vma: The VMA against which we want to check [@start, @end). +* @start: The start of the range we wish to check. +* @end: The exclusive end of the range we wish to check. +* +* Returns: %true if [@start, @end) is a subset of [@vma->vm_start, +* @vma->vm_end), %false otherwise. +*/ static inline bool range_in_vma (const struct vm_area_struct *vma,unsigned long start,unsigned long end) { -return (vma && vma->vm_start <= start && end <= vma->vm_end); +if (!vma) +return false; + +return range_is_subset (vma->vm_start, vma->vm_end, start, end); +} + +/** +* range_in_vma_desc - is the specified [@start, @end) range a subset of the VMA +* described by @desc, a VMA descriptor? +* @desc: The VMA descriptor against which we want to check [@start, @end). +* @start: The start of the range we wish to check. +* @end: The exclusive end of the range we wish to check. +* +* Returns: %true if [@start, @end) is a subset of [@desc->start, @desc->end), +* %false otherwise. +*/ +static inline bool range_in_vma_desc (const struct vm_area_desc *desc, +unsigned long start,unsigned long end) +{ +if (!desc) +return false; + +return range_is_subset (desc->start, desc->end, start, end); }#ifdef CONFIG_MMU @@ -4427,6 +4515,9 @@ int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,int vm_insert_page (struct vm_area_struct *,unsigned long addr,struct page *);int vm_insert_pages (struct vm_area_struct *vma,unsigned long addr,struct page **pages,unsigned long *num); +int map_kernel_pages_prepare (struct vm_area_desc *desc); +int map_kernel_pages_complete (struct vm_area_struct *vma, +struct mmap_action *action);int vm_map_pages (struct vm_area_struct *vma,struct page **pages,unsigned long num); int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages,include/linux/mm_types.h +7
-0@@ -815,6 +815,7 @@ enum mmap_action_type {MMAP_REMAP_PFN ,/* Remap PFN range. */ MMAP_IO_REMAP_PFN ,/* I/O remap PFN range. */ MMAP_SIMPLE_IO_REMAP ,/* I/O remap with guardrails. */ + MMAP_MAP_KERNEL_PAGES, /* Map kernel page range from array. */ };/* @@ -833,6 +834,12 @@ struct mmap_action {phys_addr_t start_phys_addr;unsigned long size; } simple_ioremap; +struct { +unsigned long start; +struct page **pages; +unsigned long nr_pages; +pgoff_t pgoff; + } map_kernel; };enum mmap_action_type type;mm/memory.c +38
-4@@ -2484,13 +2484,14 @@ static int insert_pages(struct vm_area_struct *vma, unsigned long addr,int vm_insert_pages (struct vm_area_struct *vma,unsigned long addr,struct page **pages,unsigned long *num) { -const unsigned long end_addr = addr + (*num *PAGE_SIZE ) -1 ; +const unsigned long nr_pages = *num; +const unsigned long end = addr +PAGE_SIZE * nr_pages; -if (addr < vma->vm_start || end_addr >= vma->vm_end) +if (!range_in_vma (vma, addr, end))return -EFAULT ;if (!(vma->vm_flags &VM_MIXEDMAP )) { -BUG_ON (mmap_read_trylock (vma->vm_mm)); -BUG_ON (vma->vm_flags &VM_PFNMAP ); +VM_WARN_ON_ONCE (mmap_read_trylock (vma->vm_mm)); +VM_WARN_ON_ONCE (vma->vm_flags &VM_PFNMAP );vm_flags_set (vma,VM_MIXEDMAP ); }/* Defer page refcount checking till we're about to map that page. */ @@ -2498,6 +2499,39 @@ int vm_insert_pages(struct vm_area_struct *vma, unsigned long addr, }EXPORT_SYMBOL (vm_insert_pages); +int map_kernel_pages_prepare (struct vm_area_desc *desc) +{ +const struct mmap_action *action = &desc->action; +const unsigned long addr = action->map_kernel.start; +unsigned long nr_pages, end; + +if (!vma_desc_test (desc,VMA_MIXEDMAP_BIT )) { +VM_WARN_ON_ONCE (mmap_read_trylock (desc->mm)); +VM_WARN_ON_ONCE (vma_desc_test (desc,VMA_PFNMAP_BIT )); +vma_desc_set_flags (desc,VMA_MIXEDMAP_BIT ); + } + + nr_pages = action->map_kernel.nr_pages; + end = addr +PAGE_SIZE * nr_pages; +if (!range_in_vma_desc (desc, addr, end)) +return -EFAULT ; + +return 0 ; +} +EXPORT_SYMBOL (map_kernel_pages_prepare); + +int map_kernel_pages_complete (struct vm_area_struct *vma, +struct mmap_action *action) +{ +unsigned long nr_pages; + + nr_pages = action->map_kernel.nr_pages; +return insert_pages (vma, action->map_kernel.start, + action->map_kernel.pages, + &nr_pages, vma->vm_page_prot); +} +EXPORT_SYMBOL (map_kernel_pages_complete); +/** * vm_insert_page - insert single page into user vma * @vma: user vma to map to mm/util.c +6
-0@@ -1441,6 +1441,8 @@ int mmap_action_prepare(struct vm_area_desc *desc)return io_remap_pfn_range_prepare (desc);case MMAP_SIMPLE_IO_REMAP :return simple_ioremap_prepare (desc); +case MMAP_MAP_KERNEL_PAGES : +return map_kernel_pages_prepare (desc); }WARN_ON_ONCE (1 );@@ -1472,6 +1474,9 @@ int mmap_action_complete(struct vm_area_struct *vma,case MMAP_IO_REMAP_PFN : err =io_remap_pfn_range_complete (vma, action);break ; +case MMAP_MAP_KERNEL_PAGES : + err =map_kernel_pages_complete (vma, action); +break ;case MMAP_SIMPLE_IO_REMAP :/* * The simple I/O remap should have been delegated to an I/O @@ -1494,6 +1499,7 @@ int mmap_action_prepare(struct vm_area_desc *desc)case MMAP_REMAP_PFN :case MMAP_IO_REMAP_PFN :case MMAP_SIMPLE_IO_REMAP : +case MMAP_MAP_KERNEL_PAGES :WARN_ON_ONCE (1 );/* nommu cannot handle these. */ break ; }tools/testing/vma/include/dup.h +7
-1@@ -454,6 +454,7 @@ enum mmap_action_type {MMAP_REMAP_PFN ,/* Remap PFN range. */ MMAP_IO_REMAP_PFN ,/* I/O remap PFN range. */ MMAP_SIMPLE_IO_REMAP ,/* I/O remap with guardrails. */ + MMAP_MAP_KERNEL_PAGES, /* Map kernel page range from an array. */ };/* @@ -472,6 +473,12 @@ struct mmap_action {phys_addr_t start;unsigned long len; } simple_ioremap; +struct { +unsigned long start; +struct page **pages; +unsigned long num; +pgoff_t pgoff; + } map_kernel; };enum mmap_action_type type; --2.53.0
Lorenzo Stoakes (Oracle)
maintainerRe: [PATCH v2 12/16] mm: allow handling of stacked mmap_prepare hooks in more drivers
In reply to Suren BaghdasaryanQuoted 5 lines · Suren Baghdasaryan
Quoted 2 lines · Lorenzo Stoakes (Oracle)
While the conversion of mmap hooks to
mmap_prepareis underway, we wilnit: s/wil/will
Thanks, fixed.
Quoted 59 lines
Quoted 57 lines
encounter situations where mmap hooks need to invoke nested
mmap_preparehooks.The nesting of mmap hooks is termed 'stacking'. In order to flexibly facilitate the conversion of custom mmap hooks in drivers which stack, we must split up the existing
compat_vma_mapped()function into two separate functions:*
compat_set_desc_from_vma()- This allows the setting of avm_area_descobject's fields to the relevant fields of a VMA.*
__compat_vma_mmap()- Once anmmap_preparehook has been executed upon avm_area_descobject, this function performs any mmap actions specified by themmap_preparehook and then invokes itsvm_ops->mapped()hook if any were specified.In ordinary cases, where a file's
f_op->mmap_prepare()hook simply needs to be invoked in a stackedmmap()hook,compat_vma_mmap()can be used.However some drivers define their own nested hooks, which are invoked in turn by another hook.
A concrete example is
vmbus_channel->mmap_ring_buffer(), which is invoked in turn bybin_attribute->mmap():
vmbus_channel->mmap_ring_buffer()has a signature of:int (*mmap_ring_buffer)(struct
vmbus_channel*channel,struct vm_area_struct *vma);And
bin_attribute->mmap()has a signature of:int (*mmap)(struct file *, struct kobject *,
const structbin_attribute*attr, structvm_area_struct*vma);And so
compat_vma_mmap()cannot be used here for incremental conversion of hooks frommmap()tommap_prepare().There are many such instances like this, where conversion to
mmap_preparewould otherwise cascade to a huge change set due to nesting of this kind.The changes in this patch mean we could now instead convert
vmbus_channel->mmap_ring_buffer()tovmbus_channel->mmap_prepare_ring_buffer(), and implement something like:struct vm_area_desc desc; int err; compat_set_desc_from_vm(&desc, file, vma); err = channel->mmap_prepare_ring_buffer(channel, &desc); if (err) return err; return __compat_vma_mmap(&desc, vma);Quoted 1 line
Allowing us to incrementally update this logic, and other logic like it.
Quoted 7 lines
The way I understand this and the next 2 patches is that they are preperations for later replacement of
mmap()withmmap_prepare()but they don't yet do that completely. Is that right?
To clarify what I mean, in [1] for example, you are replacing struct uio_info.mmap with uio_info.mmap_prepare but it's still being called fromuio_mmap(). IOW, you are not replacinguio_mmapwithuio_mmap_prepare. Is that the next step that's not yet implemented?
Yeah, there were 12 more patches I didn't send :) because I feel they'd make more sense separate and wanted to test/develop them more.
This is all laying the groundwork for having mmap_prepare DMA cache mappings ultimately, while expanding functionality as we go.
The intent here though isn't _just_ that, it's more - in general - when we have an e.g.:
int some_special_mmap(struct some_type *blah, struct file *filp /* sometimes */,
struct vm_area_struct *vma)
{
...
}Or some say custom ops for something like this, where in the existing code callers hook .mmap(), grab some specific struct (like a device pointer or a state pointer) from the file private data and then delegate to another helper.
In this situation, we are able to use the compatibility layer to change say the ops to be .mmap_prepare instead while the overarching caller is .mmap.
This allows for iterative conversion to .mmap_prepare without having to amend 100 files at once in a multi-thousand line patch touching dozens of drivers or some hellish notion like that.
This is vital to sensibly being able to implement these changes bit-by-bit.
Cheers, Lorenzo
Quoted 386 lines
[1] https://lore.kernel.org/all/892a8b32e5ef64c69239ccc2d1bd364716fd7fdf.1773695307.git.ljs@kernel.org/
Quoted 383 lines
Unfortunately, as part of this change, we need to be able to flexibly assign to the VMA descriptor, so have to remove some of the const declarations within the structure.
Also update the VMA tests to reflect the changes.
Signed-off-by Lorenzo Stoakes (Oracle) <[email protected]>--- include/linux/fs.h | 3 + include/linux/mm_types.h | 4 +- mm/util.c | 111 +++++++++++++++++++++++--------- mm/vma.h | 2 +- tools/testing/vma/include/dup.h | 111 ++++++++++++++++++++------------ 5 files changed, 157 insertions(+), 74 deletions(-)include/linux/fs.h +3
-0@@ -2058,6 +2058,9 @@ static inline bool can_mmap_file(struct file *file)return true; } +void compat_set_desc_from_vma (struct vm_area_desc *desc,const struct file *file, +const struct vm_area_struct *vma); +int __compat_vma_mmap (struct vm_area_desc *desc,struct vm_area_struct *vma);int compat_vma_mmap (struct file *file,struct vm_area_struct *vma);int __vma_check_mmap_hook (struct vm_area_struct *vma);include/linux/mm_types.h +2
-2@@ -891,8 +891,8 @@ static __always_inline bool vma_flags_empty(vma_flags_t *flags)*/ struct vm_area_desc {/* Immutable state. */ -const struct mm_struct *const mm; -struct file *const file;/* May vary from vm_file in stacked callers. */ +struct mm_struct *mm; +struct file *file;/* May vary from vm_file in stacked callers. */ unsigned long start;unsigned long end;mm/util.c +80
-31@@ -1163,34 +1163,38 @@ void flush_dcache_folio(struct folio *folio)EXPORT_SYMBOL (flush_dcache_folio);#endif -static int __compat_vma_mmap (struct file *file,struct vm_area_struct *vma) +/** +* compat_set_desc_from_vma() - assigns VMA descriptor @desc fields from a VMA. +* @desc: A VMA descriptor whose fields need to be set. +* @file: The file object describing the file being mmap()'d. +* @vma: The VMA whose fields we wish to assign to @desc. +* +* This is a compatibility function to allow an mmap() hook to call +* mmap_prepare() hooks when drivers nest these. This function specifically +* allows the construction of a vm_area_desc value, @desc, from a VMA @vma for +* the purposes of doing this. +* +* Once the conversion of drivers is complete this function will no longer be +* required and will be removed. +*/ +void compat_set_desc_from_vma (struct vm_area_desc *desc, +const struct file *file, +const struct vm_area_struct *vma) { -struct vm_area_desc desc = { - .mm = vma->vm_mm, - .file = file, - .start = vma->vm_start, - .end = vma->vm_end, - - .pgoff = vma->vm_pgoff, - .vm_file = vma->vm_file, - .vma_flags = vma->flags, - .page_prot = vma->vm_page_prot, - - .action.type =MMAP_NOTHING ,/* Default */ - }; -int err; + desc->mm = vma->vm_mm; + desc->file = (struct file *)file; + desc->start = vma->vm_start; + desc->end = vma->vm_end; - err =vfs_mmap_prepare (file, &desc); -if (err) -return err; + desc->pgoff = vma->vm_pgoff; + desc->vm_file = vma->vm_file; + desc->vma_flags = vma->flags; + desc->page_prot = vma->vm_page_prot; - err =mmap_action_prepare (&desc); -if (err) -return err; - -set_vma_from_desc (vma, &desc); -return mmap_action_complete (vma, &desc.action); +/* Default. */ + desc->action.type =MMAP_NOTHING ; } +EXPORT_SYMBOL (compat_set_desc_from_vma);static int __compat_vma_mapped (struct file *file,struct vm_area_struct *vma) {@@ -1211,6 +1215,49 @@ static int __compat_vma_mapped(struct file *file, struct vm_area_struct *vma)return err; } +/** +* __compat_vma_mmap() - Similar to compat_vma_mmap(), only it allows +* flexibility as to how the mmap_prepare callback is invoked, which is useful +* for drivers which invoke nested mmap_prepare callbacks in an mmap() hook. +* @desc: A VMA descriptor upon which an mmap_prepare() hook has already been +* executed. +* @vma: The VMA to which @desc should be applied. +* +* The function assumes that you have obtained a VMA descriptor @desc from +* compt_set_desc_from_vma(), and already executed the mmap_prepare() hook upon +* it. +* +* It then performs any specified mmap actions, and invokes the vm_ops->mapped() +* hook if one is present. +* +* See the description of compat_vma_mmap() for more details. +* +* Once the conversion of drivers is complete this function will no longer be +* required and will be removed. +* +* Returns: 0 on success or error. +*/ +int __compat_vma_mmap (struct vm_area_desc *desc, +struct vm_area_struct *vma) +{ +int err; + +/* Perform any preparatory tasks for mmap action. */ + err =mmap_action_prepare (desc); +if (err) +return err; +/* Update the VMA from the descriptor. */ +compat_set_vma_from_desc (vma, desc); +/* Complete any specified mmap actions. */ + err =mmap_action_complete (vma, &desc->action); +if (err) +return err; + +/* Invoke vm_ops->mapped callback. */ +return __compat_vma_mapped (desc->file, vma); +} +EXPORT_SYMBOL (__compat_vma_mmap); +/** * compat_vma_mmap() - Apply the file's .mmap_prepare() hook to an * existing VMA and execute any requested actions. @@ -1218,10 +1265,10 @@ static int __compat_vma_mapped(struct file *file, struct vm_area_struct *vma)* @vma: The VMA to apply the .mmap_prepare() hook to. * * Ordinarily, .mmap_prepare() is invoked directly upon mmap(). However, certain -* stacked filesystems invoke a nested mmap hook of an underlying file. +* stacked drivers invoke a nested mmap hook of an underlying file. * -* Until all filesystems are converted to use .mmap_prepare(), we must be -* conservative and continue to invoke these stacked filesystems using the +* Until all drivers are converted to use .mmap_prepare(), we must be +* conservative and continue to invoke these stacked drivers using the * deprecated .mmap() hook. * * However we have a problem if the underlying file system possesses an @@ -1232,20 +1279,22 @@ static int __compat_vma_mapped(struct file *file, struct vm_area_struct *vma)* establishes a struct vm_area_desc descriptor, passes to the underlying * .mmap_prepare() hook and applies any changes performed by it. * -* Once the conversion of filesystems is complete this function will no longer -* be required and will be removed. +* Once the conversion of drivers is complete this function will no longer be +* required and will be removed. * * Returns: 0 on success or error. */ int compat_vma_mmap (struct file *file,struct vm_area_struct *vma) { +struct vm_area_desc desc;int err; - err =__compat_vma_mmap (file, vma); +compat_set_desc_from_vma (&desc, file, vma); + err =vfs_mmap_prepare (file, &desc);if (err)return err; -return __compat_vma_mapped (file, vma); +return __compat_vma_mmap (&desc, vma); }EXPORT_SYMBOL (compat_vma_mmap);mm/vma.h +1
-1@@ -300,7 +300,7 @@ static inline int vma_iter_store_gfp(struct vma_iterator *vmi,* f_op->mmap() but which might have an underlying file system which implements * f_op->mmap_prepare(). */ -static inline void set_vma_from_desc (struct vm_area_struct *vma, +static inline void compat_set_vma_from_desc (struct vm_area_struct *vma,struct vm_area_desc *desc) {/* tools/testing/vma/include/dup.h +71
-41@@ -519,8 +519,8 @@ enum vma_operation {*/ struct vm_area_desc {/* Immutable state. */ -const struct mm_struct *const mm; -struct file *const file;/* May vary from vm_file in stacked callers. */ +struct mm_struct *mm; +struct file *file;/* May vary from vm_file in stacked callers. */ unsigned long start;unsigned long end;@@ -1272,43 +1272,92 @@ static inline void vma_set_anonymous(struct vm_area_struct *vma) }/* Declared in vma.h. */ -static inline void set_vma_from_desc (struct vm_area_struct *vma, +static inline void compat_set_vma_from_desc (struct vm_area_struct *vma,struct vm_area_desc *desc); -static inline int __compat_vma_mmap (const struct file_operations *f_op, -struct file *file,struct vm_area_struct *vma) +static inline void compat_set_desc_from_vma (struct vm_area_desc *desc, +const struct file *file, +const struct vm_area_struct *vma) { -struct vm_area_desc desc = { - .mm = vma->vm_mm, - .file = file, - .start = vma->vm_start, - .end = vma->vm_end, + desc->mm = vma->vm_mm; + desc->file = (struct file *)file; + desc->start = vma->vm_start; + desc->end = vma->vm_end; - .pgoff = vma->vm_pgoff, - .vm_file = vma->vm_file, - .vma_flags = vma->flags, - .page_prot = vma->vm_page_prot, + desc->pgoff = vma->vm_pgoff; + desc->vm_file = vma->vm_file; + desc->vma_flags = vma->flags; + desc->page_prot = vma->vm_page_prot; - .action.type =MMAP_NOTHING ,/* Default */ - }; +/* Default. */ + desc->action.type =MMAP_NOTHING ; +} + +static inline unsigned long vma_pages (const struct vm_area_struct *vma) +{ +return (vma->vm_end - vma->vm_start) >>PAGE_SHIFT ; +} + +static inline void unmap_vma_locked (struct vm_area_struct *vma) +{ +const size_t len =vma_pages (vma) <<PAGE_SHIFT ; + +mmap_assert_write_locked (vma->vm_mm); +do_munmap (vma->vm_mm, vma->vm_start, len,NULL ); +} + +static inline int __compat_vma_mapped (struct file *file,struct vm_area_struct *vma) +{ +const struct vm_operations_struct *vm_ops = vma->vm_ops;int err; - err = f_op->mmap_prepare (&desc); +if (!vm_ops->mapped) +return 0 ; + + err = vm_ops->mapped (vma->vm_start, vma->vm_end, vma->vm_pgoff, file, + &vma->vm_private_data);if (err) -return err; +unmap_vma_locked (vma); +return err; +} - err =mmap_action_prepare (&desc); +static inline int __compat_vma_mmap (struct vm_area_desc *desc, +struct vm_area_struct *vma) +{ +int err; + +/* Perform any preparatory tasks for mmap action. */ + err =mmap_action_prepare (desc); +if (err) +return err; +/* Update the VMA from the descriptor. */ +compat_set_vma_from_desc (vma, desc); +/* Complete any specified mmap actions. */ + err =mmap_action_complete (vma, &desc->action);if (err)return err; -set_vma_from_desc (vma, &desc); -return mmap_action_complete (vma, &desc.action); +/* Invoke vm_ops->mapped callback. */ +return __compat_vma_mapped (desc->file, vma); +} + +static inline int vfs_mmap_prepare (struct file *file,struct vm_area_desc *desc) +{ +return file->f_op->mmap_prepare (desc); }static inline int compat_vma_mmap (struct file *file,struct vm_area_struct *vma) { -return __compat_vma_mmap (file->f_op, file, vma); +struct vm_area_desc desc; +int err; + +compat_set_desc_from_vma (&desc, file, vma); + err =vfs_mmap_prepare (file, &desc); +if (err) +return err; + +return __compat_vma_mmap (&desc, vma); }@@ -1318,11 +1367,6 @@ static inline void vma_iter_init(struct vma_iterator *vmi,mas_init (&vmi->mas, &mm->mm_mt, addr); } -static inline unsigned long vma_pages (struct vm_area_struct *vma) -{ -return (vma->vm_end - vma->vm_start) >>PAGE_SHIFT ; -} -static inline void mmap_assert_locked (struct mm_struct *); static inline struct vm_area_struct *find_vma_intersection(struct mm_struct *mm, unsigned long start_addr,@@ -1492,11 +1536,6 @@ static inline int vfs_mmap(struct file *file, struct vm_area_struct *vma)return file->f_op->mmap (file, vma); } -static inline int vfs_mmap_prepare (struct file *file,struct vm_area_desc *desc) -{ -return file->f_op->mmap_prepare (desc); -} -static inline void vma_set_file (struct vm_area_struct *vma,struct file *file) {/* Changing an anonymous vma with this is illegal */ @@ -1521,11 +1560,3 @@ static inline pgprot_t vma_get_page_prot(vma_flags_t vma_flags)return vm_get_page_prot (vm_flags); } - -static inline void unmap_vma_locked (struct vm_area_struct *vma) -{ -const size_t len =vma_pages (vma) <<PAGE_SHIFT ; - -mmap_assert_write_locked (vma->vm_mm); -do_munmap (vma->vm_mm, vma->vm_start, len,NULL ); -} --2.53.0
Suren Baghdasaryan
reviewerRe: [PATCH v2 15/16] mm: add mmap_action_map_kernel_pages[_full]()
In reply to Lorenzo Stoakes (Oracle)Quoted 81 lines · Lorenzo Stoakes (Oracle)
Quoted 24 lines · Suren Baghdasaryan
Quoted 23 lines · Lorenzo Stoakes (Oracle)
A user can invoke
mmap_action_map_kernel_pages()to specify that the mapping should map kernel pages starting fromdesc->startof a specified number of pages specified in an array.In order to implement this, adjust
mmap_action_prepare()to be able to return an error code, as it makes sense to assert that the specified parameters are valid as quickly as possible as well as updating the VMA flags to includeVMA_MIXEDMAP_BITas necessary.This provides an
mmap_prepareequivalent ofvm_insert_pages().We additionally update the existing
vm_insert_pages()code to userange_in_vma()and add a newrange_in_vma_desc()helper function for themmap_preparecase, sharing the code between the two inrange_is_subset().We add both
mmap_action_map_kernel_pages()andmmap_action_map_kernel_pages_full()to allow for both partial and full VMA mappings.We also add
mmap_action_map_kernel_pages_discontig()to allow for discontiguous mapping of kernel pages should the need arise.Quoted 8 lines
Quoted 5 lines
We update the documentation to reflect the new features.
Finally, we update the VMA tests accordingly to reflect the changes.
Signed-off-by Lorenzo Stoakes (Oracle) <[email protected]>With one nit,
Reviewed-by Suren Baghdasaryan <[email protected]>Thanks!
Quoted 35 lines
Quoted 35 lines
--- Documentation/filesystems/mmap_prepare.rst | 8 ++ include/linux/mm.h | 95 +++++++++++++++++++++- include/linux/mm_types.h | 7 ++ mm/memory.c | 42 +++++++++- mm/util.c | 6 ++ tools/testing/vma/include/dup.h | 7 ++ 6 files changed, 159 insertions(+), 6 deletions(-)Documentation/filesystems/mmap_prepare.rst +8
-0@@ -156,5 +156,13 @@ pointer. These are: * mmap_action_simple_ioremap() - Sets up an I/O remap from a specified physical address and over a specified length. +* mmap_action_map_kernel_pages() - Maps a specified array of `struct page` + pointers in the VMA from a specific offset. + +* mmap_action_map_kernel_pages_full() - Maps a specified array of `struct + page` pointers over the entire VMA. The caller must ensure there are + sufficient entries in the page array to cover the entire range of the + described VMA. + **NOTE:** The ``action`` field should never normally be manipulated directly, rather you ought to use one of these helpers.include/linux/mm.h +0
-0@@ -2912,7 +2912,7 @@ static inline bool folio_maybe_mapped_shared(struct folio *folio)* The caller must add any reference (e.g., from folio_try_get()) it might be * holding itself to the result. Quoted 8 lines
include/linux/mm.h static inline bool folio_maybe_mapped_shared(struct folio *folio) · line 2915
Quoted 3 lines
include/linux/mm.h +1
-1* -* Returns the expected folio refcount. +* Returns: the expected folio refcount. nit: I see both "Returns:" and "Return:" being used in the codebase but this header file uses "Return:", so for consistency you should probably do the same. This also applies to later instances in this patch.
Quoted 7 lines
Well here I'm just adding the colon, while I'm here (maybe have been an update in response to feedback actualy).
And this function that's not part of my change already uses 'Returns' and I'm pretty sure that's the correct form.
So I think not a big deal to keep using that?
Correct. Anything I mark as "nit:" is not critical and can be ignored.
Quoted 269 lines
Quoted 269 lines
Quoted 269 lines
include/linux/mm.h +92
-1*/ static inline int folio_expected_ref_count (const struct folio *folio) {@@ -4364,6 +4364,45 @@ static inline void mmap_action_simple_ioremap(struct vm_area_desc *desc, action->type =MMAP_SIMPLE_IO_REMAP ; } +/** +* mmap_action_map_kernel_pages - helper for mmap_prepare hook to specify that +* @num kernel pages contained in the @pages array should be mapped to userland +* starting at virtual address @start. +* @desc: The VMA descriptor for the VMA requiring kernel pags to be mapped. +* @start: The virtual address from which to map them. +* @pages: An array of struct page pointers describing the memory to map. +* @nr_pages: The number of entries in the @pages aray. +*/ +static inline void mmap_action_map_kernel_pages (struct vm_area_desc *desc, +unsigned long start,struct page **pages, +unsigned long nr_pages) +{ +struct mmap_action *action = &desc->action; + + action->type =MMAP_MAP_KERNEL_PAGES ; + action->map_kernel.start = start; + action->map_kernel.pages = pages; + action->map_kernel.nr_pages = nr_pages; + action->map_kernel.pgoff = desc->pgoff; +} + +/** +* mmap_action_map_kernel_pages_full - helper for mmap_prepare hook to specify that +* kernel pages contained in the @pages array should be mapped to userland +* from @desc->start to @desc->end. +* @desc: The VMA descriptor for the VMA requiring kernel pags to be mapped. +* @pages: An array of struct page pointers describing the memory to map. +* +* The caller must ensure that @pages contains sufficient entries to cover the +* entire range described by @desc. +*/ +static inline void mmap_action_map_kernel_pages_full (struct vm_area_desc *desc, +struct page **pages) +{ +mmap_action_map_kernel_pages (desc, desc->start, pages, +vma_desc_pages (desc)); +} +int mmap_action_prepare (struct vm_area_desc *desc);int mmap_action_complete (struct vm_area_struct *vma,struct mmap_action *action);@@ -4380,10 +4419,59 @@ static inline struct vm_area_struct *find_exact_vma(struct mm_struct *mm,return vma; } +/** +* range_is_subset - Is the specified inner range a subset of the outer range? +* @outer_start: The start of the outer range. +* @outer_end: The exclusive end of the outer range. +* @inner_start: The start of the inner range. +* @inner_end: The exclusive end of the inner range. +* +* Returns: %true if [inner_start, inner_end) is a subset of [outer_start, +* outer_end), otherwise %false. +*/ +static inline bool range_is_subset (unsigned long outer_start, +unsigned long outer_end, +unsigned long inner_start, +unsigned long inner_end) +{ +return outer_start <= inner_start && inner_end <= outer_end; +} + +/** +* range_in_vma - is the specified [@start, @end) range a subset of the VMA? +* @vma: The VMA against which we want to check [@start, @end). +* @start: The start of the range we wish to check. +* @end: The exclusive end of the range we wish to check. +* +* Returns: %true if [@start, @end) is a subset of [@vma->vm_start, +* @vma->vm_end), %false otherwise. +*/ static inline bool range_in_vma (const struct vm_area_struct *vma,unsigned long start,unsigned long end) { -return (vma && vma->vm_start <= start && end <= vma->vm_end); +if (!vma) +return false; + +return range_is_subset (vma->vm_start, vma->vm_end, start, end); +} + +/** +* range_in_vma_desc - is the specified [@start, @end) range a subset of the VMA +* described by @desc, a VMA descriptor? +* @desc: The VMA descriptor against which we want to check [@start, @end). +* @start: The start of the range we wish to check. +* @end: The exclusive end of the range we wish to check. +* +* Returns: %true if [@start, @end) is a subset of [@desc->start, @desc->end), +* %false otherwise. +*/ +static inline bool range_in_vma_desc (const struct vm_area_desc *desc, +unsigned long start,unsigned long end) +{ +if (!desc) +return false; + +return range_is_subset (desc->start, desc->end, start, end); }#ifdef CONFIG_MMU @@ -4427,6 +4515,9 @@ int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,int vm_insert_page (struct vm_area_struct *,unsigned long addr,struct page *);int vm_insert_pages (struct vm_area_struct *vma,unsigned long addr,struct page **pages,unsigned long *num); +int map_kernel_pages_prepare (struct vm_area_desc *desc); +int map_kernel_pages_complete (struct vm_area_struct *vma, +struct mmap_action *action);int vm_map_pages (struct vm_area_struct *vma,struct page **pages,unsigned long num); int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages,include/linux/mm_types.h +7
-0@@ -815,6 +815,7 @@ enum mmap_action_type {MMAP_REMAP_PFN ,/* Remap PFN range. */ MMAP_IO_REMAP_PFN ,/* I/O remap PFN range. */ MMAP_SIMPLE_IO_REMAP ,/* I/O remap with guardrails. */ + MMAP_MAP_KERNEL_PAGES, /* Map kernel page range from array. */ };/* @@ -833,6 +834,12 @@ struct mmap_action {phys_addr_t start_phys_addr;unsigned long size; } simple_ioremap; +struct { +unsigned long start; +struct page **pages; +unsigned long nr_pages; +pgoff_t pgoff; + } map_kernel; };enum mmap_action_type type;mm/memory.c +38
-4@@ -2484,13 +2484,14 @@ static int insert_pages(struct vm_area_struct *vma, unsigned long addr,int vm_insert_pages (struct vm_area_struct *vma,unsigned long addr,struct page **pages,unsigned long *num) { -const unsigned long end_addr = addr + (*num *PAGE_SIZE ) -1 ; +const unsigned long nr_pages = *num; +const unsigned long end = addr +PAGE_SIZE * nr_pages; -if (addr < vma->vm_start || end_addr >= vma->vm_end) +if (!range_in_vma (vma, addr, end))return -EFAULT ;if (!(vma->vm_flags &VM_MIXEDMAP )) { -BUG_ON (mmap_read_trylock (vma->vm_mm)); -BUG_ON (vma->vm_flags &VM_PFNMAP ); +VM_WARN_ON_ONCE (mmap_read_trylock (vma->vm_mm)); +VM_WARN_ON_ONCE (vma->vm_flags &VM_PFNMAP );vm_flags_set (vma,VM_MIXEDMAP ); }/* Defer page refcount checking till we're about to map that page. */ @@ -2498,6 +2499,39 @@ int vm_insert_pages(struct vm_area_struct *vma, unsigned long addr, }EXPORT_SYMBOL (vm_insert_pages); +int map_kernel_pages_prepare (struct vm_area_desc *desc) +{ +const struct mmap_action *action = &desc->action; +const unsigned long addr = action->map_kernel.start; +unsigned long nr_pages, end; + +if (!vma_desc_test (desc,VMA_MIXEDMAP_BIT )) { +VM_WARN_ON_ONCE (mmap_read_trylock (desc->mm)); +VM_WARN_ON_ONCE (vma_desc_test (desc,VMA_PFNMAP_BIT )); +vma_desc_set_flags (desc,VMA_MIXEDMAP_BIT ); + } + + nr_pages = action->map_kernel.nr_pages; + end = addr +PAGE_SIZE * nr_pages; +if (!range_in_vma_desc (desc, addr, end)) +return -EFAULT ; + +return 0 ; +} +EXPORT_SYMBOL (map_kernel_pages_prepare); + +int map_kernel_pages_complete (struct vm_area_struct *vma, +struct mmap_action *action) +{ +unsigned long nr_pages; + + nr_pages = action->map_kernel.nr_pages; +return insert_pages (vma, action->map_kernel.start, + action->map_kernel.pages, + &nr_pages, vma->vm_page_prot); +} +EXPORT_SYMBOL (map_kernel_pages_complete); +/** * vm_insert_page - insert single page into user vma * @vma: user vma to map to mm/util.c +6
-0@@ -1441,6 +1441,8 @@ int mmap_action_prepare(struct vm_area_desc *desc)return io_remap_pfn_range_prepare (desc);case MMAP_SIMPLE_IO_REMAP :return simple_ioremap_prepare (desc); +case MMAP_MAP_KERNEL_PAGES : +return map_kernel_pages_prepare (desc); }WARN_ON_ONCE (1 );@@ -1472,6 +1474,9 @@ int mmap_action_complete(struct vm_area_struct *vma,case MMAP_IO_REMAP_PFN : err =io_remap_pfn_range_complete (vma, action);break ; +case MMAP_MAP_KERNEL_PAGES : + err =map_kernel_pages_complete (vma, action); +break ;case MMAP_SIMPLE_IO_REMAP :/* * The simple I/O remap should have been delegated to an I/O @@ -1494,6 +1499,7 @@ int mmap_action_prepare(struct vm_area_desc *desc)case MMAP_REMAP_PFN :case MMAP_IO_REMAP_PFN :case MMAP_SIMPLE_IO_REMAP : +case MMAP_MAP_KERNEL_PAGES :WARN_ON_ONCE (1 );/* nommu cannot handle these. */ break ; }tools/testing/vma/include/dup.h +7
-1@@ -454,6 +454,7 @@ enum mmap_action_type {MMAP_REMAP_PFN ,/* Remap PFN range. */ MMAP_IO_REMAP_PFN ,/* I/O remap PFN range. */ MMAP_SIMPLE_IO_REMAP ,/* I/O remap with guardrails. */ + MMAP_MAP_KERNEL_PAGES, /* Map kernel page range from an array. */ };/* @@ -472,6 +473,12 @@ struct mmap_action {phys_addr_t start;unsigned long len; } simple_ioremap; +struct { +unsigned long start; +struct page **pages; +unsigned long num; +pgoff_t pgoff; + } map_kernel; };enum mmap_action_type type; --2.53.0
Suren Baghdasaryan
reviewerRe: [PATCH v2 11/16] staging: vme_user: replace deprecated mmap hook with mmap_prepare
In reply to Lorenzo Stoakes (Oracle)Quoted 161 lines · Lorenzo Stoakes (Oracle)
Quoted 154 lines · Suren Baghdasaryan
Quoted 153 lines · Suren Baghdasaryan
Quoted 122 lines · Lorenzo Stoakes (Oracle)
The
f_op->mmapinterface is deprecated, so update driver to use its successor,mmap_prepare.The driver previously used
vm_iomap_memory(), so this change replaces it with itsmmap_prepareequivalent,mmap_action_simple_ioremap().Functions that wrap
mmap()are also converted to wrapmmap_prepare()instead.Also update the documentation accordingly.
Signed-off-by Lorenzo Stoakes (Oracle) <[email protected]>--- Documentation/driver-api/vme.rst | 2 +- drivers/staging/vme_user/vme.c | 20 +++++------ drivers/staging/vme_user/vme.h | 2 +- drivers/staging/vme_user/vme_user.c | 51 +++++++++++++++++------------ 4 files changed, 42 insertions(+), 33 deletions(-)Documentation/driver-api/vme.rst +1
-1@@ -107,7 +107,7 @@ The function :c:func:`vme_master_read` can be used to read from and In addition to simple reads and writes, :c:func:`vme_master_rmw` is provided to do a read-modify-write transaction. Parts of a VME window can also be mapped -into user space memory using :c:func:`vme_master_mmap`. +into user space memory using :c:func:`vme_master_mmap_prepare`. Slave windowsdrivers/staging/vme_user/vme.c +10
-10@@ -735,9 +735,9 @@ unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask,EXPORT_SYMBOL (vme_master_rmw);/** -* vme_master_mmap - Mmap region of VME master window. +* vme_master_mmap_prepare - Mmap region of VME master window. * @resource: Pointer to VME master resource. -* @vma: Pointer to definition of user mapping. +* @desc: Pointer to descriptor of user mapping. * * Memory map a region of the VME master window into user space. * @@ -745,12 +745,13 @@ EXPORT_SYMBOL(vme_master_rmw);* resource or -EFAULT if map exceeds window size. Other generic mmap * errors may also be returned. */ -int vme_master_mmap (struct vme_resource *resource,struct vm_area_struct *vma) +int vme_master_mmap_prepare (struct vme_resource *resource, +struct vm_area_desc *desc) { +const unsigned long vma_size =vma_desc_size (desc);struct vme_bridge *bridge =find_bridge (resource);struct vme_master_resource *image;phys_addr_t phys_addr; -unsigned long vma_size;if (resource->type !=VME_MASTER ) {dev_err (bridge->parent,"Not a master resource\n" );@@ -758,19 +759,18 @@ int vme_master_mmap(struct vme_resource *resource, struct vm_area_struct *vma) } image = list_entry(resource->entry, struct vme_master_resource, list); - phys_addr = image->bus_resource.start + (vma->vm_pgoff <<PAGE_SHIFT ); - vma_size = vma->vm_end - vma->vm_start; + phys_addr = image->bus_resource.start + (desc->pgoff <<PAGE_SHIFT );if (phys_addr + vma_size > image->bus_resource.end +1 ) {dev_err (bridge->parent,"Map size cannot exceed the window size\n" );return -EFAULT ; } - vma->vm_page_prot =pgprot_noncached (vma->vm_page_prot); - -return vm_iomap_memory (vma, phys_addr, vma->vm_end - vma->vm_start); + desc->page_prot =pgprot_noncached (desc->page_prot); +mmap_action_simple_ioremap (desc, phys_addr, vma_size); +return 0 ; } -EXPORT_SYMBOL (vme_master_mmap); +EXPORT_SYMBOL (vme_master_mmap_prepare);/** * vme_master_free - Free VME master window drivers/staging/vme_user/vme.h +1
-1@@ -151,7 +151,7 @@ ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count,ssize_t vme_master_write (struct vme_resource *resource,void *buf,size_t count,loff_t offset);unsigned int vme_master_rmw (struct vme_resource *resource,unsigned int mask,unsigned int compare,unsigned int swap,loff_t offset); -int vme_master_mmap (struct vme_resource *resource,struct vm_area_struct *vma); +int vme_master_mmap_prepare (struct vme_resource *resource,struct vm_area_desc *desc);void vme_master_free (struct vme_resource *resource);struct vme_resource *vme_dma_request (struct vme_dev *vdev,u32 route);drivers/staging/vme_user/vme_user.c +3
-7@@ -446,24 +446,14 @@ static void vme_user_vm_close(struct vm_area_struct *vma)kfree (vma_priv); } -static const struct vm_operations_struct vme_user_vm_ops = { - .open = vme_user_vm_open, - .close = vme_user_vm_close, -}; - -static int vme_user_master_mmap (unsigned int minor,struct vm_area_struct *vma) +static int vme_user_vm_mapped (unsigned long start,unsigned long end,pgoff_t pgoff, +const struct file *file,void **vm_private_data) { -int err; +const unsigned int minor =iminor (file_inode (file));drivers/staging/vme_user/vme_user.c static void vme_user_vm_close(struct vm_area_struct *vma) · line 466
Quoted 10 lines
drivers/staging/vme_user/vme_user.c +0
-6struct vme_user_vma_priv *vma_priv;mutex_lock (&image[minor].mutex); - err =vme_master_mmap (image[minor].resource, vma); -if (err) { -mutex_unlock (&image[minor].mutex); -return err; - } -Ok, this changes the set of the operations performed under image[minor].mutex. Before we had:
mutex_lock(&image[minor].mutex);vme_master_mmap();
<some final adjustments>mutex_unlock(&image[minor].mutex);Now we have:
mutex_lock(&image[minor].mutex);vme_master_mmap_prepare()mutex_unlock(&image[minor].mutex);vm_iomap_memory();mutex_lock(&image[minor].mutex);vme_user_vm_mapped(); // <some final adjustments>mutex_unlock(&image[minor].mutex);Quoted 4 lines
Quoted 4 lines
I think as long as image[minor] does not change while we are not holding the mutex we should be safe, and looking at the code it seems to be the case. But I'm not familiar with this driver and might be wrong. Worth double-checking.
Quoted 5 lines
The file is pinned for the duration, the mutex is associated with the file, so there's no sane world in which that could be problematic.
Keeping in mind that we manipulate stuff on
vme_user_vm_close()that directly acceses image[minor] at an arbitary time.
That was my understanding as well. Thanks for confirming.
Quoted 12 lines
Quoted 7 lines
A side note: if we had to hold the mutex across all those operations I think we would need to take the mutex in the
vm_ops->mmap_prepareand add avm_ops->map_failedhook or something along that line to drop the mutex in casemmap_action_complete()fails. Not sure if we will have such cases though...No, I don't want to do this if it can be at all avoided. You should in nearly any sane circumstance be able to defer things until the mapped hook anyway.
Quoted 8 lines
Also a merge can happen too after an .mmap_prepare, so we'd have to have some 'success' hook and I'm just not going there it'll end up open to abuse again.
(We do have success and error filtering hooks right now, sadly, but they're really for hugetlb and I plan to find a way to get rid of them).
The
mmap_prepareis meant to essentially be as stateless as possible.
Yes, I also hope we won't encounter cases requiring us to keep any state information between the mmap_prepare and mapped stages.
Quoted 71 lines
Anyway I don't think it's relevant here.
Quoted 62 lines
Quoted 62 lines
Quoted 62 lines
drivers/staging/vme_user/vme_user.c +27
-8vma_priv =kmalloc_obj (*vma_priv);if (!vma_priv) {mutex_unlock (&image[minor].mutex);@@ -472,22 +462,41 @@ static int vme_user_master_mmap(unsigned int minor, struct vm_area_struct *vma) vma_priv->minor = minor;refcount_set (&vma_priv->refcnt,1 ); - vma->vm_ops = &vme_user_vm_ops; - vma->vm_private_data = vma_priv; - + *vm_private_data = vma_priv; image[minor].mmap_count++;mutex_unlock (&image[minor].mutex); -return 0 ; } -static int vme_user_mmap (struct file *file,struct vm_area_struct *vma) +static const struct vm_operations_struct vme_user_vm_ops = { + .mapped = vme_user_vm_mapped, + .open = vme_user_vm_open, + .close = vme_user_vm_close, +}; + +static int vme_user_master_mmap_prepare (unsigned int minor, +struct vm_area_desc *desc) +{ +int err; + +mutex_lock (&image[minor].mutex); + + err =vme_master_mmap_prepare (image[minor].resource, desc); +if (!err) + desc->vm_ops = &vme_user_vm_ops; + +mutex_unlock (&image[minor].mutex); +return err; +} + +static int vme_user_mmap_prepare (struct vm_area_desc *desc) { -unsigned int minor =iminor (file_inode (file)); +const struct file *file = desc->file; +const unsigned int minor =iminor (file_inode (file));if (type[minor] ==MASTER_MINOR ) -return vme_user_master_mmap (minor, vma); +return vme_user_master_mmap_prepare (minor, desc);return -ENODEV ; }@@ -498,7 +507,7 @@ static const struct file_operations vme_user_fops = { .llseek = vme_user_llseek, .unlocked_ioctl = vme_user_unlocked_ioctl, .compat_ioctl = compat_ptr_ioctl, - .mmap = vme_user_mmap, + .mmap_prepare = vme_user_mmap_prepare, };Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/staging/vme_user/vme_user.c +0
-1static int vme_user_match (struct vme_dev *vdev) --2.53.0
Cheers, Lorenzo
Re: [PATCH v2 11/16] staging: vme_user: replace deprecated mmap hook with mmap_prepare
Quoted 14 lines · Suren Baghdasaryan
Quoted 12 lines · Lorenzo Stoakes (Oracle)
Quoted 10 lines · Suren Baghdasaryan
Quoted 9 lines · Suren Baghdasaryan
Quoted 7 lines · Lorenzo Stoakes (Oracle)
The
f_op->mmapinterface is deprecated, so update driver to use its successor,mmap_prepare.The driver previously used
vm_iomap_memory(), so this change replaces it with itsmmap_prepareequivalent,mmap_action_simple_ioremap().
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Functions that wrap
mmap()are also converted to wrapmmap_prepare()instead.Also update the documentation accordingly.
Signed-off-by Lorenzo Stoakes (Oracle) <[email protected]>
Quoted 246 lines
Quoted 143 lines
Quoted 138 lines
Quoted 138 lines
Quoted 109 lines
--- Documentation/driver-api/vme.rst | 2 +- drivers/staging/vme_user/vme.c | 20 +++++------ drivers/staging/vme_user/vme.h | 2 +- drivers/staging/vme_user/vme_user.c | 51 +++++++++++++++++------------ 4 files changed, 42 insertions(+), 33 deletions(-)Documentation/driver-api/vme.rst +1
-1@@ -107,7 +107,7 @@ The function :c:func:`vme_master_read` can be used to read from and In addition to simple reads and writes, :c:func:`vme_master_rmw` is provided to do a read-modify-write transaction. Parts of a VME window can also be mapped -into user space memory using :c:func:`vme_master_mmap`. +into user space memory using :c:func:`vme_master_mmap_prepare`. Slave windowsdrivers/staging/vme_user/vme.c +10
-10@@ -735,9 +735,9 @@ unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask,EXPORT_SYMBOL (vme_master_rmw);/** -* vme_master_mmap - Mmap region of VME master window. +* vme_master_mmap_prepare - Mmap region of VME master window. * @resource: Pointer to VME master resource. -* @vma: Pointer to definition of user mapping. +* @desc: Pointer to descriptor of user mapping. * * Memory map a region of the VME master window into user space. * @@ -745,12 +745,13 @@ EXPORT_SYMBOL(vme_master_rmw);* resource or -EFAULT if map exceeds window size. Other generic mmap * errors may also be returned. */ -int vme_master_mmap (struct vme_resource *resource,struct vm_area_struct *vma) +int vme_master_mmap_prepare (struct vme_resource *resource, +struct vm_area_desc *desc) { +const unsigned long vma_size =vma_desc_size (desc);struct vme_bridge *bridge =find_bridge (resource);struct vme_master_resource *image;phys_addr_t phys_addr; -unsigned long vma_size;if (resource->type !=VME_MASTER ) {dev_err (bridge->parent,"Not a master resource\n" );@@ -758,19 +759,18 @@ int vme_master_mmap(struct vme_resource *resource, struct vm_area_struct *vma) } image = list_entry(resource->entry, struct vme_master_resource, list); - phys_addr = image->bus_resource.start + (vma->vm_pgoff <<PAGE_SHIFT ); - vma_size = vma->vm_end - vma->vm_start; + phys_addr = image->bus_resource.start + (desc->pgoff <<PAGE_SHIFT );if (phys_addr + vma_size > image->bus_resource.end +1 ) {dev_err (bridge->parent,"Map size cannot exceed the window size\n" );return -EFAULT ; } - vma->vm_page_prot =pgprot_noncached (vma->vm_page_prot); - -return vm_iomap_memory (vma, phys_addr, vma->vm_end - vma->vm_start); + desc->page_prot =pgprot_noncached (desc->page_prot); +mmap_action_simple_ioremap (desc, phys_addr, vma_size); +return 0 ; } -EXPORT_SYMBOL (vme_master_mmap); +EXPORT_SYMBOL (vme_master_mmap_prepare);/** * vme_master_free - Free VME master window drivers/staging/vme_user/vme.h +1
-1@@ -151,7 +151,7 @@ ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count,ssize_t vme_master_write (struct vme_resource *resource,void *buf,size_t count,loff_t offset);unsigned int vme_master_rmw (struct vme_resource *resource,unsigned int mask,unsigned int compare,unsigned int swap,loff_t offset); -int vme_master_mmap (struct vme_resource *resource,struct vm_area_struct *vma); +int vme_master_mmap_prepare (struct vme_resource *resource,struct vm_area_desc *desc);void vme_master_free (struct vme_resource *resource);struct vme_resource *vme_dma_request (struct vme_dev *vdev,u32 route);drivers/staging/vme_user/vme_user.c +3
-7@@ -446,24 +446,14 @@ static void vme_user_vm_close(struct vm_area_struct *vma)kfree (vma_priv); } -static const struct vm_operations_struct vme_user_vm_ops = { - .open = vme_user_vm_open, - .close = vme_user_vm_close, -}; - -static int vme_user_master_mmap (unsigned int minor,struct vm_area_struct *vma) +static int vme_user_vm_mapped (unsigned long start,unsigned long end,pgoff_t pgoff, +const struct file *file,void **vm_private_data) { -int err; +const unsigned int minor =iminor (file_inode (file));drivers/staging/vme_user/vme_user.c static void vme_user_vm_close(struct vm_area_struct *vma) · line 466
Quoted 10 lines
drivers/staging/vme_user/vme_user.c +0
-6struct vme_user_vma_priv *vma_priv;mutex_lock (&image[minor].mutex); - err =vme_master_mmap (image[minor].resource, vma); -if (err) { -mutex_unlock (&image[minor].mutex); -return err; - } -Ok, this changes the set of the operations performed under image[minor].mutex. Before we had:
mutex_lock(&image[minor].mutex);vme_master_mmap();
<some final adjustments>mutex_unlock(&image[minor].mutex);Now we have:
mutex_lock(&image[minor].mutex);vme_master_mmap_prepare()mutex_unlock(&image[minor].mutex);vm_iomap_memory();mutex_lock(&image[minor].mutex);vme_user_vm_mapped(); // <some final adjustments>mutex_unlock(&image[minor].mutex);Quoted 4 lines
Quoted 4 lines
I think as long as image[minor] does not change while we are not holding the mutex we should be safe, and looking at the code it seems to be the case. But I'm not familiar with this driver and might be wrong. Worth double-checking.
Quoted 5 lines
The file is pinned for the duration, the mutex is associated with the file, so there's no sane world in which that could be problematic.
Keeping in mind that we manipulate stuff on
vme_user_vm_close()that directly acceses image[minor] at an arbitary time.That was my understanding as well. Thanks for confirming.
Quoted 12 lines
Quoted 7 lines
A side note: if we had to hold the mutex across all those operations I think we would need to take the mutex in the
vm_ops->mmap_prepareand add avm_ops->map_failedhook or something along that line to drop the mutex in casemmap_action_complete()fails. Not sure if we will have such cases though...No, I don't want to do this if it can be at all avoided. You should in nearly any sane circumstance be able to defer things until the mapped hook anyway.
Quoted 8 lines
Also a merge can happen too after an .mmap_prepare, so we'd have to have some 'success' hook and I'm just not going there it'll end up open to abuse again.
(We do have success and error filtering hooks right now, sadly, but they're really for hugetlb and I plan to find a way to get rid of them).
The
mmap_prepareis meant to essentially be as stateless as possible.Yes, I also hope we won't encounter cases requiring us to keep any state information between the
mmap_prepareand mapped stages.Quoted 71 lines
Anyway I don't think it's relevant here.
Quoted 62 lines
Quoted 62 lines
Quoted 62 lines
drivers/staging/vme_user/vme_user.c +27
-8vma_priv =kmalloc_obj (*vma_priv);if (!vma_priv) {mutex_unlock (&image[minor].mutex);@@ -472,22 +462,41 @@ static int vme_user_master_mmap(unsigned int minor, struct vm_area_struct *vma) vma_priv->minor = minor;refcount_set (&vma_priv->refcnt,1 ); - vma->vm_ops = &vme_user_vm_ops; - vma->vm_private_data = vma_priv; - + *vm_private_data = vma_priv; image[minor].mmap_count++;mutex_unlock (&image[minor].mutex); -return 0 ; } -static int vme_user_mmap (struct file *file,struct vm_area_struct *vma) +static const struct vm_operations_struct vme_user_vm_ops = { + .mapped = vme_user_vm_mapped, + .open = vme_user_vm_open, + .close = vme_user_vm_close, +}; + +static int vme_user_master_mmap_prepare (unsigned int minor, +struct vm_area_desc *desc) +{ +int err; + +mutex_lock (&image[minor].mutex); + + err =vme_master_mmap_prepare (image[minor].resource, desc); +if (!err) + desc->vm_ops = &vme_user_vm_ops; + +mutex_unlock (&image[minor].mutex); +return err; +} + +static int vme_user_mmap_prepare (struct vm_area_desc *desc) { -unsigned int minor =iminor (file_inode (file)); +const struct file *file = desc->file; +const unsigned int minor =iminor (file_inode (file));if (type[minor] ==MASTER_MINOR ) -return vme_user_master_mmap (minor, vma); +return vme_user_master_mmap_prepare (minor, desc);return -ENODEV ; }@@ -498,7 +507,7 @@ static const struct file_operations vme_user_fops = { .llseek = vme_user_llseek, .unlocked_ioctl = vme_user_unlocked_ioctl, .compat_ioctl = compat_ptr_ioctl, - .mmap = vme_user_mmap, + .mmap_prepare = vme_user_mmap_prepare, };Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/staging/vme_user/vme_user.c +0
-1static int vme_user_match (struct vme_dev *vdev) --2.53.0
Cheers, Lorenzo
Lorenzo Stoakes (Oracle)
maintainerRe: [PATCH v2 06/16] mm: add mmap_action_simple_ioremap()
In reply to Lorenzo Stoakes (Oracle)Quoted 22 lines · Lorenzo Stoakes (Oracle)
Quoted 9 lines · Suren Baghdasaryan
Quoted 9 lines
mm/memory.c +9
-0+int simple_ioremap_prepare (struct vm_area_desc *desc) +{ +struct mmap_action *action = &desc->action; +const phys_addr_t start = action->simple_ioremap.start_phys_addr; +const unsigned long size = action->simple_ioremap.size; +unsigned long pfn; +int err; + + err = __simple_ioremap_prep(desc->start, desc->end, desc->pgoff,Quoted 8 lines
mm/memory.c int remap_pfn_range_complete(struct vm_area_struct *vma, · line 3222
Quoted 6 lines
mm/memory.c +6
-0+ start, size, &pfn); +if (err) +return err; + +/* The I/O remap logic does the heavy lifting. */ +mmap_action_ioremap (desc, desc->start, pfn,vma_desc_size (desc));nit: Looks like a perfect opportunity to use
mmap_action_ioremap_full()here.Yeah can do!
Quoted 1 line
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
+ return
mmap_action_prepare(desc);Ok, so IIUC this uses recursion:
mmap_action_prepare(MMAP_SIMPLE_IO_REMAP)->simple_ioremap_prepare()->mmap_action_prepare(MMAP_IO_REMAP_PFN).Yep, it's one level, I think that should be ok? :)
On second thoughts, it's silly not just to call io_remap_pfn_range_prepare() direct so will change it to do that!
Cheers, Lorenzo