Skip to content

Commit a22dd50

Browse files
committed
mm/hmm: remove hmm_mirror and related
The only two users of this are now converted to use mmu_interval_notifier, delete all the code and update hmm.rst. Link: https://lore.kernel.org/r/20191112202231.3856-14-jgg@ziepe.ca Reviewed-by: Jérôme Glisse <jglisse@redhat.com> Tested-by: Ralph Campbell <rcampbell@nvidia.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Jason Gunthorpe <jgg@mellanox.com>
1 parent 81fa1af commit a22dd50

4 files changed

Lines changed: 34 additions & 540 deletions

File tree

‎Documentation/vm/hmm.rst‎

Lines changed: 24 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -147,49 +147,16 @@ Address space mirroring implementation and API
147147
Address space mirroring's main objective is to allow duplication of a range of
148148
CPU page table into a device page table; HMM helps keep both synchronized. A
149149
device driver that wants to mirror a process address space must start with the
150-
registration of an hmm_mirror struct::
151-
152-
int hmm_mirror_register(struct hmm_mirror *mirror,
153-
struct mm_struct *mm);
154-
155-
The mirror struct has a set of callbacks that are used
156-
to propagate CPU page tables::
157-
158-
struct hmm_mirror_ops {
159-
/* release() - release hmm_mirror
160-
*
161-
* @mirror: pointer to struct hmm_mirror
162-
*
163-
* This is called when the mm_struct is being released. The callback
164-
* must ensure that all access to any pages obtained from this mirror
165-
* is halted before the callback returns. All future access should
166-
* fault.
167-
*/
168-
void (*release)(struct hmm_mirror *mirror);
169-
170-
/* sync_cpu_device_pagetables() - synchronize page tables
171-
*
172-
* @mirror: pointer to struct hmm_mirror
173-
* @update: update information (see struct mmu_notifier_range)
174-
* Return: -EAGAIN if update.blockable false and callback need to
175-
* block, 0 otherwise.
176-
*
177-
* This callback ultimately originates from mmu_notifiers when the CPU
178-
* page table is updated. The device driver must update its page table
179-
* in response to this callback. The update argument tells what action
180-
* to perform.
181-
*
182-
* The device driver must not return from this callback until the device
183-
* page tables are completely updated (TLBs flushed, etc); this is a
184-
* synchronous call.
185-
*/
186-
int (*sync_cpu_device_pagetables)(struct hmm_mirror *mirror,
187-
const struct hmm_update *update);
188-
};
189-
190-
The device driver must perform the update action to the range (mark range
191-
read only, or fully unmap, etc.). The device must complete the update before
192-
the driver callback returns.
150+
registration of a mmu_interval_notifier::
151+
152+
mni->ops = &driver_ops;
153+
int mmu_interval_notifier_insert(struct mmu_interval_notifier *mni,
154+
unsigned long start, unsigned long length,
155+
struct mm_struct *mm);
156+
157+
During the driver_ops->invalidate() callback the device driver must perform
158+
the update action to the range (mark range read only, or fully unmap,
159+
etc.). The device must complete the update before the driver callback returns.
193160

194161
When the device driver wants to populate a range of virtual addresses, it can
195162
use::
@@ -216,70 +183,46 @@ The usage pattern is::
216183
struct hmm_range range;
217184
...
218185

186+
range.notifier = &mni;
219187
range.start = ...;
220188
range.end = ...;
221189
range.pfns = ...;
222190
range.flags = ...;
223191
range.values = ...;
224192
range.pfn_shift = ...;
225-
hmm_range_register(&range, mirror);
226193

227-
/*
228-
* Just wait for range to be valid, safe to ignore return value as we
229-
* will use the return value of hmm_range_fault() below under the
230-
* mmap_sem to ascertain the validity of the range.
231-
*/
232-
hmm_range_wait_until_valid(&range, TIMEOUT_IN_MSEC);
194+
if (!mmget_not_zero(mni->notifier.mm))
195+
return -EFAULT;
233196

234197
again:
198+
range.notifier_seq = mmu_interval_read_begin(&mni);
235199
down_read(&mm->mmap_sem);
236200
ret = hmm_range_fault(&range, HMM_RANGE_SNAPSHOT);
237201
if (ret) {
238202
up_read(&mm->mmap_sem);
239-
if (ret == -EBUSY) {
240-
/*
241-
* No need to check hmm_range_wait_until_valid() return value
242-
* on retry we will get proper error with hmm_range_fault()
243-
*/
244-
hmm_range_wait_until_valid(&range, TIMEOUT_IN_MSEC);
245-
goto again;
246-
}
247-
hmm_range_unregister(&range);
203+
if (ret == -EBUSY)
204+
goto again;
248205
return ret;
249206
}
207+
up_read(&mm->mmap_sem);
208+
250209
take_lock(driver->update);
251-
if (!hmm_range_valid(&range)) {
210+
if (mmu_interval_read_retry(&ni, range.notifier_seq) {
252211
release_lock(driver->update);
253-
up_read(&mm->mmap_sem);
254212
goto again;
255213
}
256214

257-
// Use pfns array content to update device page table
215+
/* Use pfns array content to update device page table,
216+
* under the update lock */
258217

259-
hmm_range_unregister(&range);
260218
release_lock(driver->update);
261-
up_read(&mm->mmap_sem);
262219
return 0;
263220
}
264221

265222
The driver->update lock is the same lock that the driver takes inside its
266-
sync_cpu_device_pagetables() callback. That lock must be held before calling
267-
hmm_range_valid() to avoid any race with a concurrent CPU page table update.
268-
269-
HMM implements all this on top of the mmu_notifier API because we wanted a
270-
simpler API and also to be able to perform optimizations latter on like doing
271-
concurrent device updates in multi-devices scenario.
272-
273-
HMM also serves as an impedance mismatch between how CPU page table updates
274-
are done (by CPU write to the page table and TLB flushes) and how devices
275-
update their own page table. Device updates are a multi-step process. First,
276-
appropriate commands are written to a buffer, then this buffer is scheduled for
277-
execution on the device. It is only once the device has executed commands in
278-
the buffer that the update is done. Creating and scheduling the update command
279-
buffer can happen concurrently for multiple devices. Waiting for each device to
280-
report commands as executed is serialized (there is no point in doing this
281-
concurrently).
282-
223+
invalidate() callback. That lock must be held before calling
224+
mmu_interval_read_retry() to avoid any race with a concurrent CPU page table
225+
update.
283226

284227
Leverage default_flags and pfn_flags_mask
285228
=========================================

‎include/linux/hmm.h‎

Lines changed: 2 additions & 181 deletions
Original file line numberDiff line numberDiff line change
@@ -68,29 +68,6 @@
6868
#include <linux/completion.h>
6969
#include <linux/mmu_notifier.h>
7070

71-
72-
/*
73-
* struct hmm - HMM per mm struct
74-
*
75-
* @mm: mm struct this HMM struct is bound to
76-
* @lock: lock protecting ranges list
77-
* @ranges: list of range being snapshotted
78-
* @mirrors: list of mirrors for this mm
79-
* @mmu_notifier: mmu notifier to track updates to CPU page table
80-
* @mirrors_sem: read/write semaphore protecting the mirrors list
81-
* @wq: wait queue for user waiting on a range invalidation
82-
* @notifiers: count of active mmu notifiers
83-
*/
84-
struct hmm {
85-
struct mmu_notifier mmu_notifier;
86-
spinlock_t ranges_lock;
87-
struct list_head ranges;
88-
struct list_head mirrors;
89-
struct rw_semaphore mirrors_sem;
90-
wait_queue_head_t wq;
91-
long notifiers;
92-
};
93-
9471
/*
9572
* hmm_pfn_flag_e - HMM flag enums
9673
*
@@ -143,9 +120,8 @@ enum hmm_pfn_value_e {
143120
/*
144121
* struct hmm_range - track invalidation lock on virtual address range
145122
*
146-
* @notifier: an optional mmu_interval_notifier
147-
* @notifier_seq: when notifier is used this is the result of
148-
* mmu_interval_read_begin()
123+
* @notifier: a mmu_interval_notifier that includes the start/end
124+
* @notifier_seq: result of mmu_interval_read_begin()
149125
* @hmm: the core HMM structure this range is active against
150126
* @vma: the vm area struct for the range
151127
* @list: all range lock are on a list
@@ -162,8 +138,6 @@ enum hmm_pfn_value_e {
162138
struct hmm_range {
163139
struct mmu_interval_notifier *notifier;
164140
unsigned long notifier_seq;
165-
struct hmm *hmm;
166-
struct list_head list;
167141
unsigned long start;
168142
unsigned long end;
169143
uint64_t *pfns;
@@ -172,32 +146,8 @@ struct hmm_range {
172146
uint64_t default_flags;
173147
uint64_t pfn_flags_mask;
174148
uint8_t pfn_shift;
175-
bool valid;
176149
};
177150

178-
/*
179-
* hmm_range_wait_until_valid() - wait for range to be valid
180-
* @range: range affected by invalidation to wait on
181-
* @timeout: time out for wait in ms (ie abort wait after that period of time)
182-
* Return: true if the range is valid, false otherwise.
183-
*/
184-
static inline bool hmm_range_wait_until_valid(struct hmm_range *range,
185-
unsigned long timeout)
186-
{
187-
return wait_event_timeout(range->hmm->wq, range->valid,
188-
msecs_to_jiffies(timeout)) != 0;
189-
}
190-
191-
/*
192-
* hmm_range_valid() - test if a range is valid or not
193-
* @range: range
194-
* Return: true if the range is valid, false otherwise.
195-
*/
196-
static inline bool hmm_range_valid(struct hmm_range *range)
197-
{
198-
return range->valid;
199-
}
200-
201151
/*
202152
* hmm_device_entry_to_page() - return struct page pointed to by a device entry
203153
* @range: range use to decode device entry value
@@ -267,111 +217,6 @@ static inline uint64_t hmm_device_entry_from_pfn(const struct hmm_range *range,
267217
range->flags[HMM_PFN_VALID];
268218
}
269219

270-
/*
271-
* Mirroring: how to synchronize device page table with CPU page table.
272-
*
273-
* A device driver that is participating in HMM mirroring must always
274-
* synchronize with CPU page table updates. For this, device drivers can either
275-
* directly use mmu_notifier APIs or they can use the hmm_mirror API. Device
276-
* drivers can decide to register one mirror per device per process, or just
277-
* one mirror per process for a group of devices. The pattern is:
278-
*
279-
* int device_bind_address_space(..., struct mm_struct *mm, ...)
280-
* {
281-
* struct device_address_space *das;
282-
*
283-
* // Device driver specific initialization, and allocation of das
284-
* // which contains an hmm_mirror struct as one of its fields.
285-
* ...
286-
*
287-
* ret = hmm_mirror_register(&das->mirror, mm, &device_mirror_ops);
288-
* if (ret) {
289-
* // Cleanup on error
290-
* return ret;
291-
* }
292-
*
293-
* // Other device driver specific initialization
294-
* ...
295-
* }
296-
*
297-
* Once an hmm_mirror is registered for an address space, the device driver
298-
* will get callbacks through sync_cpu_device_pagetables() operation (see
299-
* hmm_mirror_ops struct).
300-
*
301-
* Device driver must not free the struct containing the hmm_mirror struct
302-
* before calling hmm_mirror_unregister(). The expected usage is to do that when
303-
* the device driver is unbinding from an address space.
304-
*
305-
*
306-
* void device_unbind_address_space(struct device_address_space *das)
307-
* {
308-
* // Device driver specific cleanup
309-
* ...
310-
*
311-
* hmm_mirror_unregister(&das->mirror);
312-
*
313-
* // Other device driver specific cleanup, and now das can be freed
314-
* ...
315-
* }
316-
*/
317-
318-
struct hmm_mirror;
319-
320-
/*
321-
* struct hmm_mirror_ops - HMM mirror device operations callback
322-
*
323-
* @update: callback to update range on a device
324-
*/
325-
struct hmm_mirror_ops {
326-
/* release() - release hmm_mirror
327-
*
328-
* @mirror: pointer to struct hmm_mirror
329-
*
330-
* This is called when the mm_struct is being released. The callback
331-
* must ensure that all access to any pages obtained from this mirror
332-
* is halted before the callback returns. All future access should
333-
* fault.
334-
*/
335-
void (*release)(struct hmm_mirror *mirror);
336-
337-
/* sync_cpu_device_pagetables() - synchronize page tables
338-
*
339-
* @mirror: pointer to struct hmm_mirror
340-
* @update: update information (see struct mmu_notifier_range)
341-
* Return: -EAGAIN if mmu_notifier_range_blockable(update) is false
342-
* and callback needs to block, 0 otherwise.
343-
*
344-
* This callback ultimately originates from mmu_notifiers when the CPU
345-
* page table is updated. The device driver must update its page table
346-
* in response to this callback. The update argument tells what action
347-
* to perform.
348-
*
349-
* The device driver must not return from this callback until the device
350-
* page tables are completely updated (TLBs flushed, etc); this is a
351-
* synchronous call.
352-
*/
353-
int (*sync_cpu_device_pagetables)(
354-
struct hmm_mirror *mirror,
355-
const struct mmu_notifier_range *update);
356-
};
357-
358-
/*
359-
* struct hmm_mirror - mirror struct for a device driver
360-
*
361-
* @hmm: pointer to struct hmm (which is unique per mm_struct)
362-
* @ops: device driver callback for HMM mirror operations
363-
* @list: for list of mirrors of a given mm
364-
*
365-
* Each address space (mm_struct) being mirrored by a device must register one
366-
* instance of an hmm_mirror struct with HMM. HMM will track the list of all
367-
* mirrors for each mm_struct.
368-
*/
369-
struct hmm_mirror {
370-
struct hmm *hmm;
371-
const struct hmm_mirror_ops *ops;
372-
struct list_head list;
373-
};
374-
375220
/*
376221
* Retry fault if non-blocking, drop mmap_sem and return -EAGAIN in that case.
377222
*/
@@ -381,15 +226,9 @@ struct hmm_mirror {
381226
#define HMM_FAULT_SNAPSHOT (1 << 1)
382227

383228
#ifdef CONFIG_HMM_MIRROR
384-
int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm);
385-
void hmm_mirror_unregister(struct hmm_mirror *mirror);
386-
387229
/*
388230
* Please see Documentation/vm/hmm.rst for how to use the range API.
389231
*/
390-
int hmm_range_register(struct hmm_range *range, struct hmm_mirror *mirror);
391-
void hmm_range_unregister(struct hmm_range *range);
392-
393232
long hmm_range_fault(struct hmm_range *range, unsigned int flags);
394233

395234
long hmm_range_dma_map(struct hmm_range *range,
@@ -401,24 +240,6 @@ long hmm_range_dma_unmap(struct hmm_range *range,
401240
dma_addr_t *daddrs,
402241
bool dirty);
403242
#else
404-
int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm)
405-
{
406-
return -EOPNOTSUPP;
407-
}
408-
409-
void hmm_mirror_unregister(struct hmm_mirror *mirror)
410-
{
411-
}
412-
413-
int hmm_range_register(struct hmm_range *range, struct hmm_mirror *mirror)
414-
{
415-
return -EOPNOTSUPP;
416-
}
417-
418-
void hmm_range_unregister(struct hmm_range *range)
419-
{
420-
}
421-
422243
static inline long hmm_range_fault(struct hmm_range *range, unsigned int flags)
423244
{
424245
return -EOPNOTSUPP;

‎mm/Kconfig‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,6 @@ config DEV_PAGEMAP_OPS
675675
config HMM_MIRROR
676676
bool
677677
depends on MMU
678-
depends on MMU_NOTIFIER
679678

680679
config DEVICE_PRIVATE
681680
bool "Unaddressable device memory (GPU memory, ...)"

0 commit comments

Comments
 (0)