Skip to content

Commit 5180da4

Browse files
Suresh Siddhatorvalds
authored andcommitted
x86, pat: separate the pfn attribute tracking for remap_pfn_range and vm_insert_pfn
With PAT enabled, vm_insert_pfn() looks up the existing pfn memory attribute and uses it. Expectation is that the driver reserves the memory attributes for the pfn before calling vm_insert_pfn(). remap_pfn_range() (when called for the whole vma) will setup a new attribute (based on the prot argument) for the specified pfn range. This addresses the legacy usage which typically calls remap_pfn_range() with a desired memory attribute. For ranges smaller than the vma size (which is typically not the case), remap_pfn_range() will use the existing memory attribute for the pfn range. Expose two different API's for these different behaviors. track_pfn_insert() for tracking the pfn attribute set by vm_insert_pfn() and track_pfn_remap() for the remap_pfn_range(). This cleanup also prepares the ground for the track/untrack pfn vma routines to take over the ownership of setting PAT specific vm_flag in the 'vma'. [khlebnikov@openvz.org: Clear checks in track_pfn_remap()] [akpm@linux-foundation.org: tweak a few comments] Signed-off-by: Suresh Siddha <suresh.b.siddha@intel.com> Signed-off-by: Konstantin Khlebnikov <khlebnikov@openvz.org> Cc: Venkatesh Pallipadi <venki@google.com> Cc: H. Peter Anvin <hpa@zytor.com> Cc: Nick Piggin <npiggin@kernel.dk> Cc: Ingo Molnar <mingo@redhat.com> Cc: Alexander Viro <viro@zeniv.linux.org.uk> Cc: Carsten Otte <cotte@de.ibm.com> Cc: Chris Metcalf <cmetcalf@tilera.com> Cc: Cyrill Gorcunov <gorcunov@openvz.org> Cc: Eric Paris <eparis@redhat.com> Cc: Hugh Dickins <hughd@google.com> Cc: James Morris <james.l.morris@oracle.com> Cc: Jason Baron <jbaron@redhat.com> Cc: Kentaro Takeda <takedakn@nttdata.co.jp> Cc: Konstantin Khlebnikov <khlebnikov@openvz.org> Cc: Matt Helsley <matthltc@us.ibm.com> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Robert Richter <robert.richter@amd.com> Cc: Suresh Siddha <suresh.b.siddha@intel.com> Cc: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Acked-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent b1a86e1 commit 5180da4

File tree

3 files changed

+73
-42
lines changed

3 files changed

+73
-42
lines changed

arch/x86/mm/pat.c

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -664,13 +664,13 @@ static void free_pfn_range(u64 paddr, unsigned long size)
664664
}
665665

666666
/*
667-
* track_pfn_vma_copy is called when vma that is covering the pfnmap gets
667+
* track_pfn_copy is called when vma that is covering the pfnmap gets
668668
* copied through copy_page_range().
669669
*
670670
* If the vma has a linear pfn mapping for the entire range, we get the prot
671671
* from pte and reserve the entire vma range with single reserve_pfn_range call.
672672
*/
673-
int track_pfn_vma_copy(struct vm_area_struct *vma)
673+
int track_pfn_copy(struct vm_area_struct *vma)
674674
{
675675
resource_size_t paddr;
676676
unsigned long prot;
@@ -694,15 +694,12 @@ int track_pfn_vma_copy(struct vm_area_struct *vma)
694694
}
695695

696696
/*
697-
* track_pfn_vma_new is called when a _new_ pfn mapping is being established
698-
* for physical range indicated by pfn and size.
699-
*
700697
* prot is passed in as a parameter for the new mapping. If the vma has a
701698
* linear pfn mapping for the entire range reserve the entire vma range with
702699
* single reserve_pfn_range call.
703700
*/
704-
int track_pfn_vma_new(struct vm_area_struct *vma, pgprot_t *prot,
705-
unsigned long pfn, unsigned long size)
701+
int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot,
702+
unsigned long pfn, unsigned long size)
706703
{
707704
resource_size_t paddr = (resource_size_t)pfn << PAGE_SHIFT;
708705
unsigned long flags;
@@ -714,21 +711,49 @@ int track_pfn_vma_new(struct vm_area_struct *vma, pgprot_t *prot,
714711
if (!pat_enabled)
715712
return 0;
716713

717-
/* for vm_insert_pfn and friends, we set prot based on lookup */
714+
/*
715+
* For anything smaller than the vma size we set prot based on the
716+
* lookup.
717+
*/
718718
flags = lookup_memtype(paddr);
719+
720+
/* Check memtype for the remaining pages */
721+
while (size > PAGE_SIZE) {
722+
size -= PAGE_SIZE;
723+
paddr += PAGE_SIZE;
724+
if (flags != lookup_memtype(paddr))
725+
return -EINVAL;
726+
}
727+
728+
*prot = __pgprot((pgprot_val(vma->vm_page_prot) & (~_PAGE_CACHE_MASK)) |
729+
flags);
730+
731+
return 0;
732+
}
733+
734+
int track_pfn_insert(struct vm_area_struct *vma, pgprot_t *prot,
735+
unsigned long pfn)
736+
{
737+
unsigned long flags;
738+
739+
if (!pat_enabled)
740+
return 0;
741+
742+
/* Set prot based on lookup */
743+
flags = lookup_memtype((resource_size_t)pfn << PAGE_SHIFT);
719744
*prot = __pgprot((pgprot_val(vma->vm_page_prot) & (~_PAGE_CACHE_MASK)) |
720745
flags);
721746

722747
return 0;
723748
}
724749

725750
/*
726-
* untrack_pfn_vma is called while unmapping a pfnmap for a region.
751+
* untrack_pfn is called while unmapping a pfnmap for a region.
727752
* untrack can be called for a specific region indicated by pfn and size or
728753
* can be for the entire vma (in which case pfn, size are zero).
729754
*/
730-
void untrack_pfn_vma(struct vm_area_struct *vma, unsigned long pfn,
731-
unsigned long size)
755+
void untrack_pfn(struct vm_area_struct *vma, unsigned long pfn,
756+
unsigned long size)
732757
{
733758
resource_size_t paddr;
734759
unsigned long prot;

include/asm-generic/pgtable.h

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -381,48 +381,57 @@ static inline void ptep_modify_prot_commit(struct mm_struct *mm,
381381

382382
#ifndef __HAVE_PFNMAP_TRACKING
383383
/*
384-
* Interface that can be used by architecture code to keep track of
385-
* memory type of pfn mappings (remap_pfn_range, vm_insert_pfn)
386-
*
387-
* track_pfn_vma_new is called when a _new_ pfn mapping is being established
388-
* for physical range indicated by pfn and size.
384+
* Interfaces that can be used by architecture code to keep track of
385+
* memory type of pfn mappings specified by the remap_pfn_range,
386+
* vm_insert_pfn.
387+
*/
388+
389+
/*
390+
* track_pfn_remap is called when a _new_ pfn mapping is being established
391+
* by remap_pfn_range() for physical range indicated by pfn and size.
389392
*/
390-
static inline int track_pfn_vma_new(struct vm_area_struct *vma, pgprot_t *prot,
391-
unsigned long pfn, unsigned long size)
393+
static inline int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot,
394+
unsigned long pfn, unsigned long size)
392395
{
393396
return 0;
394397
}
395398

396399
/*
397-
* Interface that can be used by architecture code to keep track of
398-
* memory type of pfn mappings (remap_pfn_range, vm_insert_pfn)
399-
*
400-
* track_pfn_vma_copy is called when vma that is covering the pfnmap gets
400+
* track_pfn_insert is called when a _new_ single pfn is established
401+
* by vm_insert_pfn().
402+
*/
403+
static inline int track_pfn_insert(struct vm_area_struct *vma, pgprot_t *prot,
404+
unsigned long pfn)
405+
{
406+
return 0;
407+
}
408+
409+
/*
410+
* track_pfn_copy is called when vma that is covering the pfnmap gets
401411
* copied through copy_page_range().
402412
*/
403-
static inline int track_pfn_vma_copy(struct vm_area_struct *vma)
413+
static inline int track_pfn_copy(struct vm_area_struct *vma)
404414
{
405415
return 0;
406416
}
407417

408418
/*
409-
* Interface that can be used by architecture code to keep track of
410-
* memory type of pfn mappings (remap_pfn_range, vm_insert_pfn)
411-
*
412419
* untrack_pfn_vma is called while unmapping a pfnmap for a region.
413420
* untrack can be called for a specific region indicated by pfn and size or
414-
* can be for the entire vma (in which case size can be zero).
421+
* can be for the entire vma (in which case pfn, size are zero).
415422
*/
416-
static inline void untrack_pfn_vma(struct vm_area_struct *vma,
417-
unsigned long pfn, unsigned long size)
423+
static inline void untrack_pfn(struct vm_area_struct *vma,
424+
unsigned long pfn, unsigned long size)
418425
{
419426
}
420427
#else
421-
extern int track_pfn_vma_new(struct vm_area_struct *vma, pgprot_t *prot,
422-
unsigned long pfn, unsigned long size);
423-
extern int track_pfn_vma_copy(struct vm_area_struct *vma);
424-
extern void untrack_pfn_vma(struct vm_area_struct *vma, unsigned long pfn,
425-
unsigned long size);
428+
extern int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot,
429+
unsigned long pfn, unsigned long size);
430+
extern int track_pfn_insert(struct vm_area_struct *vma, pgprot_t *prot,
431+
unsigned long pfn);
432+
extern int track_pfn_copy(struct vm_area_struct *vma);
433+
extern void untrack_pfn(struct vm_area_struct *vma, unsigned long pfn,
434+
unsigned long size);
426435
#endif
427436

428437
#ifdef CONFIG_MMU

mm/memory.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@ int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
10601060
* We do not free on error cases below as remove_vma
10611061
* gets called on error from higher level routine
10621062
*/
1063-
ret = track_pfn_vma_copy(vma);
1063+
ret = track_pfn_copy(vma);
10641064
if (ret)
10651065
return ret;
10661066
}
@@ -1328,7 +1328,7 @@ static void unmap_single_vma(struct mmu_gather *tlb,
13281328
uprobe_munmap(vma, start, end);
13291329

13301330
if (unlikely(is_pfn_mapping(vma)))
1331-
untrack_pfn_vma(vma, 0, 0);
1331+
untrack_pfn(vma, 0, 0);
13321332

13331333
if (start != end) {
13341334
if (unlikely(is_vm_hugetlb_page(vma))) {
@@ -2162,14 +2162,11 @@ int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
21622162

21632163
if (addr < vma->vm_start || addr >= vma->vm_end)
21642164
return -EFAULT;
2165-
if (track_pfn_vma_new(vma, &pgprot, pfn, PAGE_SIZE))
2165+
if (track_pfn_insert(vma, &pgprot, pfn))
21662166
return -EINVAL;
21672167

21682168
ret = insert_pfn(vma, addr, pfn, pgprot);
21692169

2170-
if (ret)
2171-
untrack_pfn_vma(vma, pfn, PAGE_SIZE);
2172-
21732170
return ret;
21742171
}
21752172
EXPORT_SYMBOL(vm_insert_pfn);
@@ -2311,7 +2308,7 @@ int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
23112308

23122309
vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP;
23132310

2314-
err = track_pfn_vma_new(vma, &prot, pfn, PAGE_ALIGN(size));
2311+
err = track_pfn_remap(vma, &prot, pfn, PAGE_ALIGN(size));
23152312
if (err) {
23162313
/*
23172314
* To indicate that track_pfn related cleanup is not
@@ -2335,7 +2332,7 @@ int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
23352332
} while (pgd++, addr = next, addr != end);
23362333

23372334
if (err)
2338-
untrack_pfn_vma(vma, pfn, PAGE_ALIGN(size));
2335+
untrack_pfn(vma, pfn, PAGE_ALIGN(size));
23392336

23402337
return err;
23412338
}

0 commit comments

Comments
 (0)