Skip to content

Commit 84da111

Browse files
committed
Merge tag 'for-linus-hmm' of git://git.kernel.org/pub/scm/linux/kernel/git/rdma/rdma
Pull hmm updates from Jason Gunthorpe: "This is more cleanup and consolidation of the hmm APIs and the very strongly related mmu_notifier interfaces. Many places across the tree using these interfaces are touched in the process. Beyond that a cleanup to the page walker API and a few memremap related changes round out the series: - General improvement of hmm_range_fault() and related APIs, more documentation, bug fixes from testing, API simplification & consolidation, and unused API removal - Simplify the hmm related kconfigs to HMM_MIRROR and DEVICE_PRIVATE, and make them internal kconfig selects - Hoist a lot of code related to mmu notifier attachment out of drivers by using a refcount get/put attachment idiom and remove the convoluted mmu_notifier_unregister_no_release() and related APIs. - General API improvement for the migrate_vma API and revision of its only user in nouveau - Annotate mmu_notifiers with lockdep and sleeping region debugging Two series unrelated to HMM or mmu_notifiers came along due to dependencies: - Allow pagemap's memremap_pages family of APIs to work without providing a struct device - Make walk_page_range() and related use a constant structure for function pointers" * tag 'for-linus-hmm' of git://git.kernel.org/pub/scm/linux/kernel/git/rdma/rdma: (75 commits) libnvdimm: Enable unit test infrastructure compile checks mm, notifier: Catch sleeping/blocking for !blockable kernel.h: Add non_block_start/end() drm/radeon: guard against calling an unpaired radeon_mn_unregister() csky: add missing brackets in a macro for tlb.h pagewalk: use lockdep_assert_held for locking validation pagewalk: separate function pointers from iterator data mm: split out a new pagewalk.h header from mm.h mm/mmu_notifiers: annotate with might_sleep() mm/mmu_notifiers: prime lockdep mm/mmu_notifiers: add a lockdep map for invalidate_range_start/end mm/mmu_notifiers: remove the __mmu_notifier_invalidate_range_start/end exports mm/hmm: hmm_range_fault() infinite loop mm/hmm: hmm_range_fault() NULL pointer bug mm/hmm: fix hmm_range_fault()'s handling of swapped out pages mm/mmu_notifiers: remove unregister_no_release RDMA/odp: remove ib_ucontext from ib_umem RDMA/odp: use mmu_notifier_get/put for 'struct ib_ucontext_per_mm' RDMA/mlx5: Use odp instead of mr->umem in pagefault_mr RDMA/mlx5: Use ib_umem_start instead of umem.address ...
2 parents 227c3e9 + 62974fc commit 84da111

65 files changed

Lines changed: 1684 additions & 2184 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Documentation/vm/hmm.rst‎

Lines changed: 11 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,14 @@ read only, or fully unmap, etc.). The device must complete the update before
192192
the driver callback returns.
193193

194194
When the device driver wants to populate a range of virtual addresses, it can
195-
use either::
195+
use::
196196

197-
long hmm_range_snapshot(struct hmm_range *range);
198-
long hmm_range_fault(struct hmm_range *range, bool block);
197+
long hmm_range_fault(struct hmm_range *range, unsigned int flags);
199198

200-
The first one (hmm_range_snapshot()) will only fetch present CPU page table
199+
With the HMM_RANGE_SNAPSHOT flag, it will only fetch present CPU page table
201200
entries and will not trigger a page fault on missing or non-present entries.
202-
The second one does trigger a page fault on missing or read-only entries if
203-
write access is requested (see below). Page faults use the generic mm page
201+
Without that flag, it does trigger a page fault on missing or read-only entries
202+
if write access is requested (see below). Page faults use the generic mm page
204203
fault code path just like a CPU page fault.
205204

206205
Both functions copy CPU page table entries into their pfns array argument. Each
@@ -223,24 +222,24 @@ The usage pattern is::
223222
range.flags = ...;
224223
range.values = ...;
225224
range.pfn_shift = ...;
226-
hmm_range_register(&range);
225+
hmm_range_register(&range, mirror);
227226

228227
/*
229228
* Just wait for range to be valid, safe to ignore return value as we
230-
* will use the return value of hmm_range_snapshot() below under the
229+
* will use the return value of hmm_range_fault() below under the
231230
* mmap_sem to ascertain the validity of the range.
232231
*/
233232
hmm_range_wait_until_valid(&range, TIMEOUT_IN_MSEC);
234233

235234
again:
236235
down_read(&mm->mmap_sem);
237-
ret = hmm_range_snapshot(&range);
236+
ret = hmm_range_fault(&range, HMM_RANGE_SNAPSHOT);
238237
if (ret) {
239238
up_read(&mm->mmap_sem);
240239
if (ret == -EBUSY) {
241240
/*
242241
* No need to check hmm_range_wait_until_valid() return value
243-
* on retry we will get proper error with hmm_range_snapshot()
242+
* on retry we will get proper error with hmm_range_fault()
244243
*/
245244
hmm_range_wait_until_valid(&range, TIMEOUT_IN_MSEC);
246245
goto again;
@@ -340,58 +339,8 @@ Migration to and from device memory
340339
===================================
341340

342341
Because the CPU cannot access device memory, migration must use the device DMA
343-
engine to perform copy from and to device memory. For this we need a new
344-
migration helper::
345-
346-
int migrate_vma(const struct migrate_vma_ops *ops,
347-
struct vm_area_struct *vma,
348-
unsigned long mentries,
349-
unsigned long start,
350-
unsigned long end,
351-
unsigned long *src,
352-
unsigned long *dst,
353-
void *private);
354-
355-
Unlike other migration functions it works on a range of virtual address, there
356-
are two reasons for that. First, device DMA copy has a high setup overhead cost
357-
and thus batching multiple pages is needed as otherwise the migration overhead
358-
makes the whole exercise pointless. The second reason is because the
359-
migration might be for a range of addresses the device is actively accessing.
360-
361-
The migrate_vma_ops struct defines two callbacks. First one (alloc_and_copy())
362-
controls destination memory allocation and copy operation. Second one is there
363-
to allow the device driver to perform cleanup operations after migration::
364-
365-
struct migrate_vma_ops {
366-
void (*alloc_and_copy)(struct vm_area_struct *vma,
367-
const unsigned long *src,
368-
unsigned long *dst,
369-
unsigned long start,
370-
unsigned long end,
371-
void *private);
372-
void (*finalize_and_map)(struct vm_area_struct *vma,
373-
const unsigned long *src,
374-
const unsigned long *dst,
375-
unsigned long start,
376-
unsigned long end,
377-
void *private);
378-
};
379-
380-
It is important to stress that these migration helpers allow for holes in the
381-
virtual address range. Some pages in the range might not be migrated for all
382-
the usual reasons (page is pinned, page is locked, ...). This helper does not
383-
fail but just skips over those pages.
384-
385-
The alloc_and_copy() might decide to not migrate all pages in the
386-
range (for reasons under the callback control). For those, the callback just
387-
has to leave the corresponding dst entry empty.
388-
389-
Finally, the migration of the struct page might fail (for file backed page) for
390-
various reasons (failure to freeze reference, or update page cache, ...). If
391-
that happens, then the finalize_and_map() can catch any pages that were not
392-
migrated. Note those pages were still copied to a new page and thus we wasted
393-
bandwidth but this is considered as a rare event and a price that we are
394-
willing to pay to keep all the code simpler.
342+
engine to perform copy from and to device memory. For this we need to use
343+
migrate_vma_setup(), migrate_vma_pages(), and migrate_vma_finalize() helpers.
395344

396345

397346
Memory cgroup (memcg) and rss accounting

‎arch/csky/include/asm/tlb.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
#define tlb_start_vma(tlb, vma) \
1010
do { \
11-
if (!tlb->fullmm) \
12-
flush_cache_range(vma, vma->vm_start, vma->vm_end); \
11+
if (!(tlb)->fullmm) \
12+
flush_cache_range(vma, (vma)->vm_start, (vma)->vm_end); \
1313
} while (0)
1414

1515
#define tlb_end_vma(tlb, vma) \
1616
do { \
17-
if (!tlb->fullmm) \
18-
flush_tlb_range(vma, vma->vm_start, vma->vm_end); \
17+
if (!(tlb)->fullmm) \
18+
flush_tlb_range(vma, (vma)->vm_start, (vma)->vm_end); \
1919
} while (0)
2020

2121
#define tlb_flush(tlb) flush_tlb_mm((tlb)->mm)

‎arch/openrisc/kernel/dma.c‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
#include <linux/dma-noncoherent.h>
19+
#include <linux/pagewalk.h>
1920

2021
#include <asm/cpuinfo.h>
2122
#include <asm/spr_defs.h>
@@ -43,6 +44,10 @@ page_set_nocache(pte_t *pte, unsigned long addr,
4344
return 0;
4445
}
4546

47+
static const struct mm_walk_ops set_nocache_walk_ops = {
48+
.pte_entry = page_set_nocache,
49+
};
50+
4651
static int
4752
page_clear_nocache(pte_t *pte, unsigned long addr,
4853
unsigned long next, struct mm_walk *walk)
@@ -58,6 +63,10 @@ page_clear_nocache(pte_t *pte, unsigned long addr,
5863
return 0;
5964
}
6065

66+
static const struct mm_walk_ops clear_nocache_walk_ops = {
67+
.pte_entry = page_clear_nocache,
68+
};
69+
6170
/*
6271
* Alloc "coherent" memory, which for OpenRISC means simply uncached.
6372
*
@@ -80,10 +89,6 @@ arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
8089
{
8190
unsigned long va;
8291
void *page;
83-
struct mm_walk walk = {
84-
.pte_entry = page_set_nocache,
85-
.mm = &init_mm
86-
};
8792

8893
page = alloc_pages_exact(size, gfp | __GFP_ZERO);
8994
if (!page)
@@ -98,7 +103,8 @@ arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
98103
* We need to iterate through the pages, clearing the dcache for
99104
* them and setting the cache-inhibit bit.
100105
*/
101-
if (walk_page_range(va, va + size, &walk)) {
106+
if (walk_page_range(&init_mm, va, va + size, &set_nocache_walk_ops,
107+
NULL)) {
102108
free_pages_exact(page, size);
103109
return NULL;
104110
}
@@ -111,13 +117,10 @@ arch_dma_free(struct device *dev, size_t size, void *vaddr,
111117
dma_addr_t dma_handle, unsigned long attrs)
112118
{
113119
unsigned long va = (unsigned long)vaddr;
114-
struct mm_walk walk = {
115-
.pte_entry = page_clear_nocache,
116-
.mm = &init_mm
117-
};
118120

119121
/* walk_page_range shouldn't be able to fail here */
120-
WARN_ON(walk_page_range(va, va + size, &walk));
122+
WARN_ON(walk_page_range(&init_mm, va, va + size,
123+
&clear_nocache_walk_ops, NULL));
121124

122125
free_pages_exact(vaddr, size);
123126
}

‎arch/powerpc/mm/book3s64/subpage_prot.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <linux/kernel.h>
88
#include <linux/gfp.h>
99
#include <linux/types.h>
10-
#include <linux/mm.h>
10+
#include <linux/pagewalk.h>
1111
#include <linux/hugetlb.h>
1212
#include <linux/syscalls.h>
1313

@@ -139,14 +139,14 @@ static int subpage_walk_pmd_entry(pmd_t *pmd, unsigned long addr,
139139
return 0;
140140
}
141141

142+
static const struct mm_walk_ops subpage_walk_ops = {
143+
.pmd_entry = subpage_walk_pmd_entry,
144+
};
145+
142146
static void subpage_mark_vma_nohuge(struct mm_struct *mm, unsigned long addr,
143147
unsigned long len)
144148
{
145149
struct vm_area_struct *vma;
146-
struct mm_walk subpage_proto_walk = {
147-
.mm = mm,
148-
.pmd_entry = subpage_walk_pmd_entry,
149-
};
150150

151151
/*
152152
* We don't try too hard, we just mark all the vma in that range
@@ -163,7 +163,7 @@ static void subpage_mark_vma_nohuge(struct mm_struct *mm, unsigned long addr,
163163
if (vma->vm_start >= (addr + len))
164164
break;
165165
vma->vm_flags |= VM_NOHUGEPAGE;
166-
walk_page_vma(vma, &subpage_proto_walk);
166+
walk_page_vma(vma, &subpage_walk_ops, NULL);
167167
vma = vma->vm_next;
168168
}
169169
}

‎arch/s390/mm/gmap.c‎

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010

1111
#include <linux/kernel.h>
12-
#include <linux/mm.h>
12+
#include <linux/pagewalk.h>
1313
#include <linux/swap.h>
1414
#include <linux/smp.h>
1515
#include <linux/spinlock.h>
@@ -2521,13 +2521,9 @@ static int __zap_zero_pages(pmd_t *pmd, unsigned long start,
25212521
return 0;
25222522
}
25232523

2524-
static inline void zap_zero_pages(struct mm_struct *mm)
2525-
{
2526-
struct mm_walk walk = { .pmd_entry = __zap_zero_pages };
2527-
2528-
walk.mm = mm;
2529-
walk_page_range(0, TASK_SIZE, &walk);
2530-
}
2524+
static const struct mm_walk_ops zap_zero_walk_ops = {
2525+
.pmd_entry = __zap_zero_pages,
2526+
};
25312527

25322528
/*
25332529
* switch on pgstes for its userspace process (for kvm)
@@ -2546,7 +2542,7 @@ int s390_enable_sie(void)
25462542
mm->context.has_pgste = 1;
25472543
/* split thp mappings and disable thp for future mappings */
25482544
thp_split_mm(mm);
2549-
zap_zero_pages(mm);
2545+
walk_page_range(mm, 0, TASK_SIZE, &zap_zero_walk_ops, NULL);
25502546
up_write(&mm->mmap_sem);
25512547
return 0;
25522548
}
@@ -2589,12 +2585,13 @@ static int __s390_enable_skey_hugetlb(pte_t *pte, unsigned long addr,
25892585
return 0;
25902586
}
25912587

2588+
static const struct mm_walk_ops enable_skey_walk_ops = {
2589+
.hugetlb_entry = __s390_enable_skey_hugetlb,
2590+
.pte_entry = __s390_enable_skey_pte,
2591+
};
2592+
25922593
int s390_enable_skey(void)
25932594
{
2594-
struct mm_walk walk = {
2595-
.hugetlb_entry = __s390_enable_skey_hugetlb,
2596-
.pte_entry = __s390_enable_skey_pte,
2597-
};
25982595
struct mm_struct *mm = current->mm;
25992596
struct vm_area_struct *vma;
26002597
int rc = 0;
@@ -2614,8 +2611,7 @@ int s390_enable_skey(void)
26142611
}
26152612
mm->def_flags &= ~VM_MERGEABLE;
26162613

2617-
walk.mm = mm;
2618-
walk_page_range(0, TASK_SIZE, &walk);
2614+
walk_page_range(mm, 0, TASK_SIZE, &enable_skey_walk_ops, NULL);
26192615

26202616
out_up:
26212617
up_write(&mm->mmap_sem);
@@ -2633,13 +2629,14 @@ static int __s390_reset_cmma(pte_t *pte, unsigned long addr,
26332629
return 0;
26342630
}
26352631

2632+
static const struct mm_walk_ops reset_cmma_walk_ops = {
2633+
.pte_entry = __s390_reset_cmma,
2634+
};
2635+
26362636
void s390_reset_cmma(struct mm_struct *mm)
26372637
{
2638-
struct mm_walk walk = { .pte_entry = __s390_reset_cmma };
2639-
26402638
down_write(&mm->mmap_sem);
2641-
walk.mm = mm;
2642-
walk_page_range(0, TASK_SIZE, &walk);
2639+
walk_page_range(mm, 0, TASK_SIZE, &reset_cmma_walk_ops, NULL);
26432640
up_write(&mm->mmap_sem);
26442641
}
26452642
EXPORT_SYMBOL_GPL(s390_reset_cmma);

‎drivers/gpu/drm/amd/amdgpu/Kconfig‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ config DRM_AMDGPU_CIK
2727
config DRM_AMDGPU_USERPTR
2828
bool "Always enable userptr write support"
2929
depends on DRM_AMDGPU
30-
depends on HMM_MIRROR
30+
depends on MMU
31+
select HMM_MIRROR
32+
select MMU_NOTIFIER
3133
help
3234
This option selects CONFIG_HMM and CONFIG_HMM_MIRROR if it
3335
isn't already selected to enabled full userptr support.

‎drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <linux/pm_runtime.h>
3636
#include <linux/vga_switcheroo.h>
3737
#include <drm/drm_probe_helper.h>
38+
#include <linux/mmu_notifier.h>
3839

3940
#include "amdgpu.h"
4041
#include "amdgpu_irq.h"
@@ -1469,6 +1470,7 @@ static void __exit amdgpu_exit(void)
14691470
amdgpu_unregister_atpx_handler();
14701471
amdgpu_sync_fini();
14711472
amdgpu_fence_slab_fini();
1473+
mmu_notifier_synchronize();
14721474
}
14731475

14741476
module_init(amdgpu_init);

‎drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,14 @@ static void amdgpu_mn_invalidate_node(struct amdgpu_mn_node *node,
195195
* Block for operations on BOs to finish and mark pages as accessed and
196196
* potentially dirty.
197197
*/
198-
static int amdgpu_mn_sync_pagetables_gfx(struct hmm_mirror *mirror,
199-
const struct hmm_update *update)
198+
static int
199+
amdgpu_mn_sync_pagetables_gfx(struct hmm_mirror *mirror,
200+
const struct mmu_notifier_range *update)
200201
{
201202
struct amdgpu_mn *amn = container_of(mirror, struct amdgpu_mn, mirror);
202203
unsigned long start = update->start;
203204
unsigned long end = update->end;
204-
bool blockable = update->blockable;
205+
bool blockable = mmu_notifier_range_blockable(update);
205206
struct interval_tree_node *it;
206207

207208
/* notification is exclusive, but interval is inclusive */
@@ -243,13 +244,14 @@ static int amdgpu_mn_sync_pagetables_gfx(struct hmm_mirror *mirror,
243244
* necessitates evicting all user-mode queues of the process. The BOs
244245
* are restorted in amdgpu_mn_invalidate_range_end_hsa.
245246
*/
246-
static int amdgpu_mn_sync_pagetables_hsa(struct hmm_mirror *mirror,
247-
const struct hmm_update *update)
247+
static int
248+
amdgpu_mn_sync_pagetables_hsa(struct hmm_mirror *mirror,
249+
const struct mmu_notifier_range *update)
248250
{
249251
struct amdgpu_mn *amn = container_of(mirror, struct amdgpu_mn, mirror);
250252
unsigned long start = update->start;
251253
unsigned long end = update->end;
252-
bool blockable = update->blockable;
254+
bool blockable = mmu_notifier_range_blockable(update);
253255
struct interval_tree_node *it;
254256

255257
/* notification is exclusive, but interval is inclusive */
@@ -482,6 +484,5 @@ void amdgpu_hmm_init_range(struct hmm_range *range)
482484
range->flags = hmm_range_flags;
483485
range->values = hmm_range_values;
484486
range->pfn_shift = PAGE_SHIFT;
485-
INIT_LIST_HEAD(&range->list);
486487
}
487488
}

0 commit comments

Comments
 (0)