Skip to content

mm,kernfs,proc: Unmap mmaps of removed files via file->f_mapping

Krzysztof Wilczyński driver-core kexec linux-mm linux-pci linux-sound 2 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest3 patches 6 repliesIn review
  2. v12 patches 2 repliesSuperseded

Changes in v2 from the cover letter

https://lore.kernel.org/linux-pci/[email protected]/

- Moved the handling of files with and without a swapped f_mapping inside unmap_mapping_file(), so that callers no longer need to tell the two apart, as per David Hildenbrand's feedback.
- Updated the kernel-doc for unmap_mapping_file() to describe unmapping folios within the VMAs, rather than unmapping the mmaps and VMAs themselves, as per David Hildenbrand's feedback.
- Dropped the EXPORT_SYMBOL_GPL() for unmap_mapping_file(), as both callers are built-in and there is no modular user.
- Added a should_zap_file_vma() helper to select the VMAs to zap, following the existing should_zap_cows() and should_zap_folio() convention.
- Added a WARN_ON_ONCE() to skip HugeTLB VMAs, as zapping those takes i_mmap_rwsem for write in hugetlb_zap_begin() and would deadlock against the read lock held over the walk.
- Added a cond_resched() to the walk, as the VMAs skipped by the filter do not reach the reschedule point in the zap path.
- Added a new patch to unmap the mappings made through the /proc/bus/pci device files, as these also survive device removal, along with a new pde_is_removed() helper used to tell a rundown from a regular close().
- Updated the commit logs across the series, so that each patch describes the f_mapping swap and its effect the same way.
- Added the /proc/vmcore and ALSA maintainers to the list of recipients for visibility.

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/3mm: Add unmap_mapping_file() helperinclude/linux/mm.h mm/memory.c+50 -02 replies
2/3kernfs: Unmap mmaps of removed files via file->f_mappingfs/kernfs/file.c+1 -30 replies
3/3proc: Unmap mmaps of removed files via file->f_mappingfs/proc/generic.c fs/proc/inode.c fs/proc/internal.h+9 -20 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v2 yet.

Read the v2 thread

Cover letter [PATCH v2 0/3] mm,kernfs,proc: Unmap mmaps of removed files via file->f_mapping

Hello,

The PCI resource files in sysfs and the /proc/bus/pci device files swap their f_mapping to the shared iomem address space at open time, so that revoke_iomem() can unmap userspace mappings when a driver claims a region, see commit 636b21b50152 ("PCI: Revoke mappings like devmem").

Their VMAs are therefore attached to the shared address space, which neither removal path reaches: kernfs_drain_open_files() unmaps the sysfs inode's own mapping, which contains none of them, and proc_entry_rundown() does not unmap anything at all.

As a result, userspace mappings of PCI BARs survive device removal, and also survive a BAR resize on the sysfs side, keeping stale PTEs into physical address space that the kernel may have reassigned since. A mapping made before the device is removed still returns the previous register value after the device has been released, through both interfaces.

This series adds unmap_mapping_file(), which unmaps the pages of the VMAs created through a given file, rather than a page offset window of an address space, and then uses it in the kernfs drain and in the procfs rundown. Mappings of unrelated files on the shared address space are left intact, and the claim-time revocation through revoke_iomem() keeps working, as the VMAs stay on the shared address space.

For kernfs this restores the behaviour these files had before the f_mapping swap was introduced. For procfs the unmapping is new behaviour, which matches kernfs. Besides /proc/bus/pci, only /proc/vmcore, the ALSA information entries and /proc/powerpc/systemcfg set a proc_mmap() hook. Mappings of /proc/vmcore are now also removed when the entry goes away, while the ALSA proc_mmap() hook calls a per-entry callback that no entry implements, so these cannot be mapped. The systemcfg entry has no proc_release() hook, so it is not tracked and is not affected.

A BAR resize is covered by the same path on the sysfs side, as the resize removes the resource groups and thus drains the open files. Mappings of the same BAR made through /dev/mem are left alone, as these are unmapped by physical range when a driver claims the region.

Krzysztof

---
Changes in v2:
https://lore.kernel.org/linux-pci/[email protected]/

- Moved the handling of files with and without a swapped f_mapping inside unmap_mapping_file(), so that callers no longer need to tell the two apart, as per David Hildenbrand's feedback.
- Updated the kernel-doc for unmap_mapping_file() to describe unmapping folios within the VMAs, rather than unmapping the mmaps and VMAs themselves, as per David Hildenbrand's feedback.
- Dropped the EXPORT_SYMBOL_GPL() for unmap_mapping_file(), as both callers are built-in and there is no modular user.
- Added a should_zap_file_vma() helper to select the VMAs to zap, following the existing should_zap_cows() and should_zap_folio() convention.
- Added a WARN_ON_ONCE() to skip HugeTLB VMAs, as zapping those takes i_mmap_rwsem for write in hugetlb_zap_begin() and would deadlock against the read lock held over the walk.
- Added a cond_resched() to the walk, as the VMAs skipped by the filter do not reach the reschedule point in the zap path.
- Added a new patch to unmap the mappings made through the /proc/bus/pci device files, as these also survive device removal, along with a new pde_is_removed() helper used to tell a rundown from a regular close().
- Updated the commit logs across the series, so that each patch describes the f_mapping swap and its effect the same way.
- Added the /proc/vmcore and ALSA maintainers to the list of recipients for visibility.

Krzysztof Wilczyński (3):
mm: Add unmap_mapping_file() helper
kernfs: Unmap mmaps of removed files via file->f_mapping
proc: Unmap mmaps of removed files via file->f_mapping

 fs/kernfs/file.c   |  4 +---
 fs/proc/generic.c  |  4 ++--
 fs/proc/inode.c    |  2 ++
 fs/proc/internal.h |  5 +++++
 include/linux/mm.h |  2 ++
 mm/memory.c        | 48 ++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 60 insertions(+), 5 deletions(-)
Signature · 1 line
2.55.0