Commit 2aee09d8 authored by Jérôme Glisse's avatar Jérôme Glisse Committed by Linus Torvalds

mm/hmm: change hmm_vma_fault() to allow write fault on page basis

This changes hmm_vma_fault() to not take a global write fault flag for a
range but instead rely on caller to populate HMM pfns array with proper
fault flag ie HMM_PFN_VALID if driver want read fault for that address or
HMM_PFN_VALID and HMM_PFN_WRITE for write.

Moreover by setting HMM_PFN_DEVICE_PRIVATE the device driver can ask for
device private memory to be migrated back to system memory through page
fault.

This is more flexible API and it better reflects how device handles and
reports fault.

Link: http://lkml.kernel.org/r/20180323005527.758-15-jglisse@redhat.comSigned-off-by: default avatarJérôme Glisse <jglisse@redhat.com>
Cc: Evgeny Baskakov <ebaskakov@nvidia.com>
Cc: Ralph Campbell <rcampbell@nvidia.com>
Cc: Mark Hairgrove <mhairgrove@nvidia.com>
Cc: John Hubbard <jhubbard@nvidia.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 53f5c3f4
...@@ -317,7 +317,7 @@ bool hmm_vma_range_done(struct hmm_range *range); ...@@ -317,7 +317,7 @@ bool hmm_vma_range_done(struct hmm_range *range);
* *
* See the function description in mm/hmm.c for further documentation. * See the function description in mm/hmm.c for further documentation.
*/ */
int hmm_vma_fault(struct hmm_range *range, bool write, bool block); int hmm_vma_fault(struct hmm_range *range, bool block);
#endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */ #endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */
......
...@@ -299,12 +299,10 @@ struct hmm_vma_walk { ...@@ -299,12 +299,10 @@ struct hmm_vma_walk {
unsigned long last; unsigned long last;
bool fault; bool fault;
bool block; bool block;
bool write;
}; };
static int hmm_vma_do_fault(struct mm_walk *walk, static int hmm_vma_do_fault(struct mm_walk *walk, unsigned long addr,
unsigned long addr, bool write_fault, uint64_t *pfn)
uint64_t *pfn)
{ {
unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_REMOTE; unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_REMOTE;
struct hmm_vma_walk *hmm_vma_walk = walk->private; struct hmm_vma_walk *hmm_vma_walk = walk->private;
...@@ -312,7 +310,7 @@ static int hmm_vma_do_fault(struct mm_walk *walk, ...@@ -312,7 +310,7 @@ static int hmm_vma_do_fault(struct mm_walk *walk,
int r; int r;
flags |= hmm_vma_walk->block ? 0 : FAULT_FLAG_ALLOW_RETRY; flags |= hmm_vma_walk->block ? 0 : FAULT_FLAG_ALLOW_RETRY;
flags |= hmm_vma_walk->write ? FAULT_FLAG_WRITE : 0; flags |= write_fault ? FAULT_FLAG_WRITE : 0;
r = handle_mm_fault(vma, addr, flags); r = handle_mm_fault(vma, addr, flags);
if (r & VM_FAULT_RETRY) if (r & VM_FAULT_RETRY)
return -EBUSY; return -EBUSY;
...@@ -344,15 +342,17 @@ static int hmm_pfns_bad(unsigned long addr, ...@@ -344,15 +342,17 @@ static int hmm_pfns_bad(unsigned long addr,
* hmm_vma_walk_hole() - handle a range lacking valid pmd or pte(s) * hmm_vma_walk_hole() - handle a range lacking valid pmd or pte(s)
* @start: range virtual start address (inclusive) * @start: range virtual start address (inclusive)
* @end: range virtual end address (exclusive) * @end: range virtual end address (exclusive)
* @fault: should we fault or not ?
* @write_fault: write fault ?
* @walk: mm_walk structure * @walk: mm_walk structure
* Returns: 0 on success, -EAGAIN after page fault, or page fault error * Returns: 0 on success, -EAGAIN after page fault, or page fault error
* *
* This function will be called whenever pmd_none() or pte_none() returns true, * This function will be called whenever pmd_none() or pte_none() returns true,
* or whenever there is no page directory covering the virtual address range. * or whenever there is no page directory covering the virtual address range.
*/ */
static int hmm_vma_walk_hole(unsigned long addr, static int hmm_vma_walk_hole_(unsigned long addr, unsigned long end,
unsigned long end, bool fault, bool write_fault,
struct mm_walk *walk) struct mm_walk *walk)
{ {
struct hmm_vma_walk *hmm_vma_walk = walk->private; struct hmm_vma_walk *hmm_vma_walk = walk->private;
struct hmm_range *range = hmm_vma_walk->range; struct hmm_range *range = hmm_vma_walk->range;
...@@ -363,16 +363,89 @@ static int hmm_vma_walk_hole(unsigned long addr, ...@@ -363,16 +363,89 @@ static int hmm_vma_walk_hole(unsigned long addr,
i = (addr - range->start) >> PAGE_SHIFT; i = (addr - range->start) >> PAGE_SHIFT;
for (; addr < end; addr += PAGE_SIZE, i++) { for (; addr < end; addr += PAGE_SIZE, i++) {
pfns[i] = 0; pfns[i] = 0;
if (hmm_vma_walk->fault) { if (fault || write_fault) {
int ret; int ret;
ret = hmm_vma_do_fault(walk, addr, &pfns[i]); ret = hmm_vma_do_fault(walk, addr, write_fault,
&pfns[i]);
if (ret != -EAGAIN) if (ret != -EAGAIN)
return ret; return ret;
} }
} }
return hmm_vma_walk->fault ? -EAGAIN : 0; return (fault || write_fault) ? -EAGAIN : 0;
}
static inline void hmm_pte_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
uint64_t pfns, uint64_t cpu_flags,
bool *fault, bool *write_fault)
{
*fault = *write_fault = false;
if (!hmm_vma_walk->fault)
return;
/* We aren't ask to do anything ... */
if (!(pfns & HMM_PFN_VALID))
return;
/* If CPU page table is not valid then we need to fault */
*fault = cpu_flags & HMM_PFN_VALID;
/* Need to write fault ? */
if ((pfns & HMM_PFN_WRITE) && !(cpu_flags & HMM_PFN_WRITE)) {
*fault = *write_fault = false;
return;
}
/* Do we fault on device memory ? */
if ((pfns & HMM_PFN_DEVICE_PRIVATE) &&
(cpu_flags & HMM_PFN_DEVICE_PRIVATE)) {
*write_fault = pfns & HMM_PFN_WRITE;
*fault = true;
}
}
static void hmm_range_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
const uint64_t *pfns, unsigned long npages,
uint64_t cpu_flags, bool *fault,
bool *write_fault)
{
unsigned long i;
if (!hmm_vma_walk->fault) {
*fault = *write_fault = false;
return;
}
for (i = 0; i < npages; ++i) {
hmm_pte_need_fault(hmm_vma_walk, pfns[i], cpu_flags,
fault, write_fault);
if ((*fault) || (*write_fault))
return;
}
}
static int hmm_vma_walk_hole(unsigned long addr, unsigned long end,
struct mm_walk *walk)
{
struct hmm_vma_walk *hmm_vma_walk = walk->private;
struct hmm_range *range = hmm_vma_walk->range;
bool fault, write_fault;
unsigned long i, npages;
uint64_t *pfns;
i = (addr - range->start) >> PAGE_SHIFT;
npages = (end - addr) >> PAGE_SHIFT;
pfns = &range->pfns[i];
hmm_range_need_fault(hmm_vma_walk, pfns, npages,
0, &fault, &write_fault);
return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
}
static inline uint64_t pmd_to_hmm_pfn_flags(pmd_t pmd)
{
if (pmd_protnone(pmd))
return 0;
return pmd_write(pmd) ? HMM_PFN_VALID |
HMM_PFN_WRITE :
HMM_PFN_VALID;
} }
static int hmm_vma_handle_pmd(struct mm_walk *walk, static int hmm_vma_handle_pmd(struct mm_walk *walk,
...@@ -382,14 +455,17 @@ static int hmm_vma_handle_pmd(struct mm_walk *walk, ...@@ -382,14 +455,17 @@ static int hmm_vma_handle_pmd(struct mm_walk *walk,
pmd_t pmd) pmd_t pmd)
{ {
struct hmm_vma_walk *hmm_vma_walk = walk->private; struct hmm_vma_walk *hmm_vma_walk = walk->private;
unsigned long pfn, i; unsigned long pfn, npages, i;
uint64_t flag = 0; uint64_t flag = 0, cpu_flags;
bool fault, write_fault;
if (pmd_protnone(pmd)) npages = (end - addr) >> PAGE_SHIFT;
return hmm_vma_walk_hole(addr, end, walk); cpu_flags = pmd_to_hmm_pfn_flags(pmd);
hmm_range_need_fault(hmm_vma_walk, pfns, npages, cpu_flags,
&fault, &write_fault);
if ((hmm_vma_walk->fault & hmm_vma_walk->write) && !pmd_write(pmd)) if (pmd_protnone(pmd) || fault || write_fault)
return hmm_vma_walk_hole(addr, end, walk); return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
pfn = pmd_pfn(pmd) + pte_index(addr); pfn = pmd_pfn(pmd) + pte_index(addr);
flag |= pmd_write(pmd) ? HMM_PFN_WRITE : 0; flag |= pmd_write(pmd) ? HMM_PFN_WRITE : 0;
...@@ -399,19 +475,32 @@ static int hmm_vma_handle_pmd(struct mm_walk *walk, ...@@ -399,19 +475,32 @@ static int hmm_vma_handle_pmd(struct mm_walk *walk,
return 0; return 0;
} }
static inline uint64_t pte_to_hmm_pfn_flags(pte_t pte)
{
if (pte_none(pte) || !pte_present(pte))
return 0;
return pte_write(pte) ? HMM_PFN_VALID |
HMM_PFN_WRITE :
HMM_PFN_VALID;
}
static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr, static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
unsigned long end, pmd_t *pmdp, pte_t *ptep, unsigned long end, pmd_t *pmdp, pte_t *ptep,
uint64_t *pfn) uint64_t *pfn)
{ {
struct hmm_vma_walk *hmm_vma_walk = walk->private; struct hmm_vma_walk *hmm_vma_walk = walk->private;
struct vm_area_struct *vma = walk->vma; struct vm_area_struct *vma = walk->vma;
bool fault, write_fault;
uint64_t cpu_flags;
pte_t pte = *ptep; pte_t pte = *ptep;
*pfn = 0; *pfn = 0;
cpu_flags = pte_to_hmm_pfn_flags(pte);
hmm_pte_need_fault(hmm_vma_walk, *pfn, cpu_flags,
&fault, &write_fault);
if (pte_none(pte)) { if (pte_none(pte)) {
*pfn = 0; if (fault || write_fault)
if (hmm_vma_walk->fault)
goto fault; goto fault;
return 0; return 0;
} }
...@@ -420,7 +509,7 @@ static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr, ...@@ -420,7 +509,7 @@ static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
swp_entry_t entry = pte_to_swp_entry(pte); swp_entry_t entry = pte_to_swp_entry(pte);
if (!non_swap_entry(entry)) { if (!non_swap_entry(entry)) {
if (hmm_vma_walk->fault) if (fault || write_fault)
goto fault; goto fault;
return 0; return 0;
} }
...@@ -430,21 +519,20 @@ static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr, ...@@ -430,21 +519,20 @@ static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
* device and report anything else as error. * device and report anything else as error.
*/ */
if (is_device_private_entry(entry)) { if (is_device_private_entry(entry)) {
cpu_flags = HMM_PFN_VALID | HMM_PFN_DEVICE_PRIVATE;
cpu_flags |= is_write_device_private_entry(entry) ?
HMM_PFN_WRITE : 0;
*pfn = hmm_pfn_from_pfn(swp_offset(entry)); *pfn = hmm_pfn_from_pfn(swp_offset(entry));
if (is_write_device_private_entry(entry)) {
*pfn |= HMM_PFN_WRITE;
} else if ((hmm_vma_walk->fault & hmm_vma_walk->write))
goto fault;
*pfn |= HMM_PFN_DEVICE_PRIVATE; *pfn |= HMM_PFN_DEVICE_PRIVATE;
return 0; return 0;
} }
if (is_migration_entry(entry)) { if (is_migration_entry(entry)) {
if (hmm_vma_walk->fault) { if (fault || write_fault) {
pte_unmap(ptep); pte_unmap(ptep);
hmm_vma_walk->last = addr; hmm_vma_walk->last = addr;
migration_entry_wait(vma->vm_mm, migration_entry_wait(vma->vm_mm,
pmdp, addr); pmdp, addr);
return -EAGAIN; return -EAGAIN;
} }
return 0; return 0;
...@@ -455,17 +543,16 @@ static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr, ...@@ -455,17 +543,16 @@ static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
return -EFAULT; return -EFAULT;
} }
if ((hmm_vma_walk->fault & hmm_vma_walk->write) && !pte_write(pte)) if (fault || write_fault)
goto fault; goto fault;
*pfn = hmm_pfn_from_pfn(pte_pfn(pte)); *pfn = hmm_pfn_from_pfn(pte_pfn(pte)) | cpu_flags;
*pfn |= pte_write(pte) ? HMM_PFN_WRITE : 0;
return 0; return 0;
fault: fault:
pte_unmap(ptep); pte_unmap(ptep);
/* Fault any virtual address we were asked to fault */ /* Fault any virtual address we were asked to fault */
return hmm_vma_walk_hole(addr, end, walk); return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
} }
static int hmm_vma_walk_pmd(pmd_t *pmdp, static int hmm_vma_walk_pmd(pmd_t *pmdp,
...@@ -686,7 +773,6 @@ EXPORT_SYMBOL(hmm_vma_range_done); ...@@ -686,7 +773,6 @@ EXPORT_SYMBOL(hmm_vma_range_done);
/* /*
* hmm_vma_fault() - try to fault some address in a virtual address range * hmm_vma_fault() - try to fault some address in a virtual address range
* @range: range being faulted * @range: range being faulted
* @write: is it a write fault
* @block: allow blocking on fault (if true it sleeps and do not drop mmap_sem) * @block: allow blocking on fault (if true it sleeps and do not drop mmap_sem)
* Returns: 0 success, error otherwise (-EAGAIN means mmap_sem have been drop) * Returns: 0 success, error otherwise (-EAGAIN means mmap_sem have been drop)
* *
...@@ -731,7 +817,7 @@ EXPORT_SYMBOL(hmm_vma_range_done); ...@@ -731,7 +817,7 @@ EXPORT_SYMBOL(hmm_vma_range_done);
* *
* YOU HAVE BEEN WARNED ! * YOU HAVE BEEN WARNED !
*/ */
int hmm_vma_fault(struct hmm_range *range, bool write, bool block) int hmm_vma_fault(struct hmm_range *range, bool block)
{ {
struct vm_area_struct *vma = range->vma; struct vm_area_struct *vma = range->vma;
unsigned long start = range->start; unsigned long start = range->start;
...@@ -779,7 +865,6 @@ int hmm_vma_fault(struct hmm_range *range, bool write, bool block) ...@@ -779,7 +865,6 @@ int hmm_vma_fault(struct hmm_range *range, bool write, bool block)
spin_unlock(&hmm->lock); spin_unlock(&hmm->lock);
hmm_vma_walk.fault = true; hmm_vma_walk.fault = true;
hmm_vma_walk.write = write;
hmm_vma_walk.block = block; hmm_vma_walk.block = block;
hmm_vma_walk.range = range; hmm_vma_walk.range = range;
mm_walk.private = &hmm_vma_walk; mm_walk.private = &hmm_vma_walk;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment