Skip to content

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

David Hildenbrand dri-devel linux-alpha linux-arm-kernel linux-fsdevel linux-ia64 linux-kselftest linux-media linux-mips linux-mm linux-perf-users linux-rdma linux-samsung-soc linux-security-module linux-um linuxppc-dev lkml sparclinux 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest20 patches 36 repliesIn review 19 of 20 applied

Changes in v1 from the cover letter

* Use term "ptrace" instead of "debuggers" in patch descriptions
* Added ACK/Tested-by
* "mm/frame-vector: remove FOLL_FORCE usage"
-> Adjust description
* "mm: rename FOLL_FORCE to FOLL_PTRACE"
-> Added

Patches in v1 19 of 20 applied

#Subject and filesSizeRepliesApplied
1/20selftests/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 -221 replyApplied
2/20selftests/vm: cow: basic COW tests for non-anonymous pagestools/testing/selftests/vm/cow.c+337 -10 repliesApplied
3/20selftests/vm: cow: R/O long-term pinning reliability tests for non-anon pagestools/testing/selftests/vm/cow.c+27 -10 repliesApplied
4/20mm: add early FAULT_FLAG_UNSHARE consistency checksmm/huge_memory.c mm/hugetlb.c mm/memory.c+20 -111 replyApplied
5/20mm: add early FAULT_FLAG_WRITE consistency checksmm/memory.c+8 -01 replyApplied
6/20mm: rework handling in do_wp_page() based on private vs. shared mappingsmm/memory.c+17 -211 replyApplied
7/20mm: don't call vm_ops->huge_fault() in wp_huge_pmd()/wp_huge_pud() for private mappingsmm/memory.c+15 -91 replyApplied
8/20mm: extend FAULT_FLAG_UNSHARE support to anything in a COW mappinginclude/linux/mm_types.h mm/memory.c+4 -81 replyApplied
9/20mm/gup: reliable R/O long-term pinning in COW mappingsinclude/linux/mm.h mm/gup.c mm/huge_memory.c +1 more+34 -123 repliesApplied
10/20RDMA/umem: remove FOLL_FORCE usagedrivers/infiniband/core/umem.c+4 -41 replyApplied
11/20RDMA/usnic: remove FOLL_FORCE usagedrivers/infiniband/hw/usnic/usnic_uiom.c+4 -51 replyApplied
12/20RDMA/siw: remove FOLL_FORCE usagedrivers/infiniband/sw/siw/siw_mem.c+4 -51 replyApplied
13/20media: videobuf-dma-sg: remove FOLL_FORCE usagedrivers/media/v4l2-core/videobuf-dma-sg.c+5 -92 repliesApplied
14/20drm/etnaviv: remove FOLL_FORCE usagedrivers/gpu/drm/etnaviv/etnaviv_gem.c+5 -31 replyApplied
15/20media: pci/ivtv: remove FOLL_FORCE usagedrivers/media/pci/ivtv/ivtv-udma.c drivers/media/pci/ivtv/ivtv-yuv.c+3 -41 replyApplied
16/20mm/frame-vector: remove FOLL_FORCE usagedrivers/media/common/videobuf2/frame_vector.c+1 -112 repliesApplied
17/20drm/exynos: remove FOLL_FORCE usagedrivers/gpu/drm/exynos/exynos_drm_g2d.c+1 -11 replyApplied
18/20RDMA/hw/qib/qib_user_pages: remove FOLL_FORCE usagedrivers/infiniband/hw/qib/qib_user_pages.c+1 -10 repliesApplied
19/20habanalabs: remove FOLL_FORCE usagedrivers/misc/habanalabs/common/memory.c+1 -20 repliesApplied
20/20mm: rename FOLL_FORCE to FOLL_PTRACEarch/alpha/kernel/ptrace.c arch/arm64/kernel/mte.c arch/ia64/kernel/ptrace.c +19 more+61 -617 replies

Reviews trailers per patch

16 of 20 patches have a Reviewed-by or Acked-by.

PatchLeon RomanovskyOded GabbayDaniel VetterJason GunthorpeVlastimil BabkaHans VerkuilJohn Hubbard
1/20A-b
2/20
3/20
4/20R-b
5/20R-b
6/20R-b
7/20R-b
8/20R-b
9/20A-b R-b R-b
10/20T-b R-b
11/20R-b
12/20R-b
13/20R-b A-b
14/20R-b
15/20A-b
16/20R-b R-b A-b A-b
17/20R-b
18/20
19/20A-b
20/20

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 7 reviewers.

Read the v1 thread

Cover letter [PATCH mm-unstable v1 00/20] 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 and make FOLL_FORCE ptrace-specific (renaming it to FOLL_PTRACE). 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. Patch #20 renames FOLL_FORCE to FOLL_PTRACE.

I'm refraining from CCing all driver/arch maintainers on the whole patch set, but only CC them on the cover letter and the applicable patch (I know, I know, someone is always unhappy ... sorry).

RFC -> v1:
* Use term "ptrace" instead of "debuggers" in patch descriptions
* Added ACK/Tested-by
* "mm/frame-vector: remove FOLL_FORCE usage"
-> Adjust description
* "mm: rename FOLL_FORCE to FOLL_PTRACE"
-> Added

David Hildenbrand (20):
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
mm: rename FOLL_FORCE to FOLL_PTRACE

 arch/alpha/kernel/ptrace.c                    |   6 +-
 arch/arm64/kernel/mte.c                       |   2 +-
 arch/ia64/kernel/ptrace.c                     |  10 +-
 arch/mips/kernel/ptrace32.c                   |   4 +-
 arch/mips/math-emu/dsemul.c                   |   2 +-
 arch/powerpc/kernel/ptrace/ptrace32.c         |   4 +-
 arch/sparc/kernel/ptrace_32.c                 |   4 +-
 arch/sparc/kernel/ptrace_64.c                 |   8 +-
 arch/x86/kernel/step.c                        |   2 +-
 arch/x86/um/ptrace_32.c                       |   2 +-
 arch/x86/um/ptrace_64.c                       |   2 +-
 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 +-
 fs/exec.c                                     |   2 +-
 fs/proc/base.c                                |   2 +-
 include/linux/mm.h                            |  35 +-
 include/linux/mm_types.h                      |   8 +-
 kernel/events/uprobes.c                       |   4 +-
 kernel/ptrace.c                               |  12 +-
 mm/gup.c                                      |  38 +-
 mm/huge_memory.c                              |  13 +-
 mm/hugetlb.c                                  |  14 +-
 mm/memory.c                                   |  97 +++--
 mm/util.c                                     |   4 +-
 security/tomoyo/domain.c                      |   2 +-
 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 +-
 39 files changed, 575 insertions(+), 177 deletions(-)
 rename tools/testing/selftests/vm/{anon_cow.c => cow.c} (75%)
Signature · 1 line
2.38.1