Skip to content

RFC mm/gup: remove FOLL_FORCE usage from drivers (reliable R/O long-term pinning)

David Hildenbrand dri-devel linux-arm-kernel linux-kselftest linux-media linux-mm linux-rdma linux-samsung-soc lkml 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 RFC Latest19 patches 16 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/19selftests/vm: anon_cow: prepare for non-anonymous COW teststools/testing/selftests/vm/.gitignore tools/testing/selftests/vm/Makefile tools/testing/selftests/vm/check_config.sh +2 more+27 -220 replies
2/19selftests/vm: cow: basic COW tests for non-anonymous pagestools/testing/selftests/vm/cow.c+337 -10 replies
3/19selftests/vm: cow: R/O long-term pinning reliability tests for non-anon pagestools/testing/selftests/vm/cow.c+27 -10 replies
4/19mm: add early FAULT_FLAG_UNSHARE consistency checksmm/huge_memory.c mm/hugetlb.c mm/memory.c+20 -110 replies
5/19mm: add early FAULT_FLAG_WRITE consistency checksmm/memory.c+8 -03 replies
6/19mm: rework handling in do_wp_page() based on private vs. shared mappingsmm/memory.c+17 -210 replies
7/19mm: don't call vm_ops->huge_fault() in wp_huge_pmd()/wp_huge_pud() for private mappingsmm/memory.c+15 -90 replies
8/19mm: extend FAULT_FLAG_UNSHARE support to anything in a COW mappinginclude/linux/mm_types.h mm/memory.c+4 -80 replies
9/19mm/gup: reliable R/O long-term pinning in COW mappingsinclude/linux/mm.h mm/gup.c mm/huge_memory.c +1 more+34 -120 replies
10/19RDMA/umem: remove FOLL_FORCE usagedrivers/infiniband/core/umem.c+4 -41 reply
11/19RDMA/usnic: remove FOLL_FORCE usagedrivers/infiniband/hw/usnic/usnic_uiom.c+4 -50 replies
12/19RDMA/siw: remove FOLL_FORCE usagedrivers/infiniband/sw/siw/siw_mem.c+4 -50 replies
13/19media: videobuf-dma-sg: remove FOLL_FORCE usagedrivers/media/v4l2-core/videobuf-dma-sg.c+5 -90 replies
14/19drm/etnaviv: remove FOLL_FORCE usagedrivers/gpu/drm/etnaviv/etnaviv_gem.c+5 -30 replies
15/19media: pci/ivtv: remove FOLL_FORCE usagedrivers/media/pci/ivtv/ivtv-udma.c drivers/media/pci/ivtv/ivtv-yuv.c+3 -40 replies
16/19mm/frame-vector: remove FOLL_FORCE usagedrivers/media/common/videobuf2/frame_vector.c+1 -17 replies
17/19drm/exynos: remove FOLL_FORCE usagedrivers/gpu/drm/exynos/exynos_drm_g2d.c+1 -10 replies
18/19RDMA/hw/qib/qib_user_pages: remove FOLL_FORCE usagedrivers/infiniband/hw/qib/qib_user_pages.c+1 -10 replies
19/19habanalabs: remove FOLL_FORCE usagedrivers/misc/habanalabs/common/memory.c+1 -21 reply

Reviews trailers per patch

1 of 19 patches has a Reviewed-by or Acked-by.

PatchOded GabbayLeon Romanovsky
1/19
2/19
3/19
4/19
5/19
6/19
7/19
8/19
9/19
10/19T-b
11/19
12/19
13/19
14/19
15/19
16/19
17/19
18/19
19/19A-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v1 thread

Cover letter [PATCH RFC 00/19] mm/gup: remove FOLL_FORCE usage from drivers (reliable R/O long-term pinning)

For now, we did not support reliable R/O long-term pinning in COW mappings. That means, if we would trigger R/O long-term pinning in MAP_PRIVATE mapping, we could end up pinning the (R/O-mapped) shared zeropage or a pagecache page.

The next write access would trigger a write fault and replace the pinned page by an exclusive anonymous page in the process page table; whatever the process would write to that private page copy would not be visible by the owner of the previous page pin: for example, RDMA could read stale data. The end result is essentially an unexpected and hard-to-debug memory corruption.

Some drivers tried working around that limitation by using "FOLL_FORCE|FOLL_WRITE|FOLL_LONGTERM" for R/O long-term pinning for now. FOLL_WRITE would trigger a write fault, if required, and break COW before pinning the page. FOLL_FORCE is required because the VMA might lack write permissions, and drivers wanted to make that working as well, just like one would expect (no write access, but still triggering a write access to break COW).

However, that is not a practical solution, because
(1) Drivers that don't stick to that undocumented and debatable pattern would still run into that issue. For example, VFIO only uses FOLL_LONGTERM for R/O long-term pinning.
(2) Using FOLL_WRITE just to work around a COW mapping + page pinning limitation is unintuitive. FOLL_WRITE would, for example, mark the page softdirty or trigger uffd-wp, even though, there actually isn't going to be any write access.
(3) The purpose of FOLL_FORCE is debug access, not access without lack of VMA permissions by arbitrarty drivers.

So instead, make R/O long-term pinning work as expected, by breaking COW in a COW mapping early, such that we can remove any FOLL_FORCE usage from drivers. More details in patch #8.

Patches #1--#3 add COW tests for non-anonymous pages.
Patches #4--#7 prepare core MM for extended FAULT_FLAG_UNSHARE support in COW mappings.
Patch #8 implements reliable R/O long-term pinning in COW mappings Patches #9--#19 remove any FOLL_FORCE usage from drivers.

I'm refraining from CCing all driver maintainers on the whole patch set.

Cc Linus Torvalds <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc John Hubbard <[email protected]>
Cc Peter Xu <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Nadav Amit <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Matthew Wilcox (Oracle) <[email protected]>
Cc Mike Kravetz <[email protected]>
Cc Muchun Song <[email protected]>
Cc Shuah Khan <[email protected]
Cc Lucas Stach <[email protected]>
Cc David Airlie <[email protected]>
Cc Oded Gabbay <[email protected]>
Cc Arnd Bergmann <[email protected]>

David Hildenbrand (19):
selftests/vm: anon_cow: prepare for non-anonymous COW tests selftests/vm: cow: basic COW tests for non-anonymous pages selftests/vm: cow: R/O long-term pinning reliability tests for
non-anon pages
mm: add early FAULT_FLAG_UNSHARE consistency checks
mm: add early FAULT_FLAG_WRITE consistency checks
mm: rework handling in do_wp_page() based on private vs. shared
mappings
mm: don't call vm_ops->huge_fault() in wp_huge_pmd()/wp_huge_pud() for
private mappings
mm: extend FAULT_FLAG_UNSHARE support to anything in a COW mapping mm/gup: reliable R/O long-term pinning in COW mappings RDMA/umem: remove FOLL_FORCE usage
RDMA/usnic: remove FOLL_FORCE usage
RDMA/siw: remove FOLL_FORCE usage
media: videobuf-dma-sg: remove FOLL_FORCE usage drm/etnaviv: remove FOLL_FORCE usage
media: pci/ivtv: remove FOLL_FORCE usage
mm/frame-vector: remove FOLL_FORCE usage
drm/exynos: remove FOLL_FORCE usage RDMA/hw/qib/qib_user_pages: remove FOLL_FORCE usage
habanalabs: remove FOLL_FORCE usage

 drivers/gpu/drm/etnaviv/etnaviv_gem.c         |   8 +-
 drivers/gpu/drm/exynos/exynos_drm_g2d.c       |   2 +-
 drivers/infiniband/core/umem.c                |   8 +-
 drivers/infiniband/hw/qib/qib_user_pages.c    |   2 +-
 drivers/infiniband/hw/usnic/usnic_uiom.c      |   9 +-
 drivers/infiniband/sw/siw/siw_mem.c           |   9 +-
 drivers/media/common/videobuf2/frame_vector.c |   2 +-
 drivers/media/pci/ivtv/ivtv-udma.c            |   2 +-
 drivers/media/pci/ivtv/ivtv-yuv.c             |   5 +-
 drivers/media/v4l2-core/videobuf-dma-sg.c     |  14 +-
 drivers/misc/habanalabs/common/memory.c       |   3 +-
 include/linux/mm.h                            |  27 +-
 include/linux/mm_types.h                      |   8 +-
 mm/gup.c                                      |  10 +-
 mm/huge_memory.c                              |   5 +-
 mm/hugetlb.c                                  |  12 +-
 mm/memory.c                                   |  97 +++--
 tools/testing/selftests/vm/.gitignore         |   2 +-
 tools/testing/selftests/vm/Makefile           |  10 +-
 tools/testing/selftests/vm/check_config.sh    |   4 +-
 .../selftests/vm/{anon_cow.c => cow.c}        | 387 +++++++++++++++++-
 tools/testing/selftests/vm/run_vmtests.sh     |   8 +-
 22 files changed, 516 insertions(+), 118 deletions(-)
 rename tools/testing/selftests/vm/{anon_cow.c => cow.c} (74%)
Signature · 1 line
2.38.1