Skip to content

Commit 8f86059

Browse files
Zhang, YanminLinus Torvalds
authored andcommitted
[PATCH] Enable mprotect on huge pages
2.6.16-rc3 uses hugetlb on-demand paging, but it doesn_t support hugetlb mprotect. From: David Gibson <david@gibson.dropbear.id.au> Remove a test from the mprotect() path which checks that the mprotect()ed range on a hugepage VMA is hugepage aligned (yes, really, the sense of is_aligned_hugepage_range() is the opposite of what you'd guess :-/). In fact, we don't need this test. If the given addresses match the beginning/end of a hugepage VMA they must already be suitably aligned. If they don't, then mprotect_fixup() will attempt to split the VMA. The very first test in split_vma() will check for a badly aligned address on a hugepage VMA and return -EINVAL if necessary. From: "Chen, Kenneth W" <kenneth.w.chen@intel.com> On i386 and x86-64, pte flag _PAGE_PSE collides with _PAGE_PROTNONE. The identify of hugetlb pte is lost when changing page protection via mprotect. A page fault occurs later will trigger a bug check in huge_pte_alloc(). The fix is to always make new pte a hugetlb pte and also to clean up legacy code where _PAGE_PRESENT is forced on in the pre-faulting day. Signed-off-by: Zhang Yanmin <yanmin.zhang@intel.com> Cc: David Gibson <david@gibson.dropbear.id.au> Cc: "David S. Miller" <davem@davemloft.net> Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org> Cc: Paul Mackerras <paulus@samba.org> Cc: William Lee Irwin III <wli@holomorphy.com> Signed-off-by: Ken Chen <kenneth.w.chen@intel.com> Signed-off-by: Nishanth Aravamudan <nacc@us.ibm.com> Cc: Andi Kleen <ak@muc.de> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent aed75ff commit 8f86059

6 files changed

Lines changed: 43 additions & 13 deletions

File tree

‎include/asm-i386/pgtable.h‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,12 @@ extern unsigned long pg0[];
219219
* The following only work if pte_present() is true.
220220
* Undefined behaviour if not..
221221
*/
222-
#define __LARGE_PTE (_PAGE_PSE | _PAGE_PRESENT)
223222
static inline int pte_user(pte_t pte) { return (pte).pte_low & _PAGE_USER; }
224223
static inline int pte_read(pte_t pte) { return (pte).pte_low & _PAGE_USER; }
225224
static inline int pte_dirty(pte_t pte) { return (pte).pte_low & _PAGE_DIRTY; }
226225
static inline int pte_young(pte_t pte) { return (pte).pte_low & _PAGE_ACCESSED; }
227226
static inline int pte_write(pte_t pte) { return (pte).pte_low & _PAGE_RW; }
228-
static inline int pte_huge(pte_t pte) { return ((pte).pte_low & __LARGE_PTE) == __LARGE_PTE; }
227+
static inline int pte_huge(pte_t pte) { return (pte).pte_low & _PAGE_PSE; }
229228

230229
/*
231230
* The following only works if pte_present() is not true.
@@ -242,7 +241,7 @@ static inline pte_t pte_mkexec(pte_t pte) { (pte).pte_low |= _PAGE_USER; return
242241
static inline pte_t pte_mkdirty(pte_t pte) { (pte).pte_low |= _PAGE_DIRTY; return pte; }
243242
static inline pte_t pte_mkyoung(pte_t pte) { (pte).pte_low |= _PAGE_ACCESSED; return pte; }
244243
static inline pte_t pte_mkwrite(pte_t pte) { (pte).pte_low |= _PAGE_RW; return pte; }
245-
static inline pte_t pte_mkhuge(pte_t pte) { (pte).pte_low |= __LARGE_PTE; return pte; }
244+
static inline pte_t pte_mkhuge(pte_t pte) { (pte).pte_low |= _PAGE_PSE; return pte; }
246245

247246
#ifdef CONFIG_X86_PAE
248247
# include <asm/pgtable-3level.h>

‎include/asm-ia64/pgtable.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ ia64_phys_addr_valid (unsigned long addr)
314314
#define pte_mkyoung(pte) (__pte(pte_val(pte) | _PAGE_A))
315315
#define pte_mkclean(pte) (__pte(pte_val(pte) & ~_PAGE_D))
316316
#define pte_mkdirty(pte) (__pte(pte_val(pte) | _PAGE_D))
317-
#define pte_mkhuge(pte) (__pte(pte_val(pte) | _PAGE_P))
317+
#define pte_mkhuge(pte) (__pte(pte_val(pte)))
318318

319319
/*
320320
* Macro to a page protection value as "uncacheable". Note that "protection" is really a

‎include/asm-x86_64/pgtable.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ static inline int pte_dirty(pte_t pte) { return pte_val(pte) & _PAGE_DIRTY; }
273273
static inline int pte_young(pte_t pte) { return pte_val(pte) & _PAGE_ACCESSED; }
274274
static inline int pte_write(pte_t pte) { return pte_val(pte) & _PAGE_RW; }
275275
static inline int pte_file(pte_t pte) { return pte_val(pte) & _PAGE_FILE; }
276-
static inline int pte_huge(pte_t pte) { return (pte_val(pte) & __LARGE_PTE) == __LARGE_PTE; }
276+
static inline int pte_huge(pte_t pte) { return pte_val(pte) & _PAGE_PSE; }
277277

278278
static inline pte_t pte_rdprotect(pte_t pte) { set_pte(&pte, __pte(pte_val(pte) & ~_PAGE_USER)); return pte; }
279279
static inline pte_t pte_exprotect(pte_t pte) { set_pte(&pte, __pte(pte_val(pte) & ~_PAGE_USER)); return pte; }
@@ -285,7 +285,7 @@ static inline pte_t pte_mkexec(pte_t pte) { set_pte(&pte, __pte(pte_val(pte) | _
285285
static inline pte_t pte_mkdirty(pte_t pte) { set_pte(&pte, __pte(pte_val(pte) | _PAGE_DIRTY)); return pte; }
286286
static inline pte_t pte_mkyoung(pte_t pte) { set_pte(&pte, __pte(pte_val(pte) | _PAGE_ACCESSED)); return pte; }
287287
static inline pte_t pte_mkwrite(pte_t pte) { set_pte(&pte, __pte(pte_val(pte) | _PAGE_RW)); return pte; }
288-
static inline pte_t pte_mkhuge(pte_t pte) { set_pte(&pte, __pte(pte_val(pte) | __LARGE_PTE)); return pte; }
288+
static inline pte_t pte_mkhuge(pte_t pte) { set_pte(&pte, __pte(pte_val(pte) | _PAGE_PSE)); return pte; }
289289

290290
struct vm_area_struct;
291291

‎include/linux/hugetlb.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ struct page *follow_huge_pmd(struct mm_struct *mm, unsigned long address,
4141
pmd_t *pmd, int write);
4242
int is_aligned_hugepage_range(unsigned long addr, unsigned long len);
4343
int pmd_huge(pmd_t pmd);
44+
void hugetlb_change_protection(struct vm_area_struct *vma,
45+
unsigned long address, unsigned long end, pgprot_t newprot);
4446

4547
#ifndef ARCH_HAS_HUGEPAGE_ONLY_RANGE
4648
#define is_hugepage_only_range(mm, addr, len) 0
@@ -101,6 +103,8 @@ static inline unsigned long hugetlb_total_pages(void)
101103
#define free_huge_page(p) ({ (void)(p); BUG(); })
102104
#define hugetlb_fault(mm, vma, addr, write) ({ BUG(); 0; })
103105

106+
#define hugetlb_change_protection(vma, address, end, newprot)
107+
104108
#ifndef HPAGE_MASK
105109
#define HPAGE_MASK PAGE_MASK /* Keep the compiler happy */
106110
#define HPAGE_SIZE PAGE_SIZE

‎mm/hugetlb.c‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,3 +565,32 @@ int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
565565

566566
return i;
567567
}
568+
569+
void hugetlb_change_protection(struct vm_area_struct *vma,
570+
unsigned long address, unsigned long end, pgprot_t newprot)
571+
{
572+
struct mm_struct *mm = vma->vm_mm;
573+
unsigned long start = address;
574+
pte_t *ptep;
575+
pte_t pte;
576+
577+
BUG_ON(address >= end);
578+
flush_cache_range(vma, address, end);
579+
580+
spin_lock(&mm->page_table_lock);
581+
for (; address < end; address += HPAGE_SIZE) {
582+
ptep = huge_pte_offset(mm, address);
583+
if (!ptep)
584+
continue;
585+
if (!pte_none(*ptep)) {
586+
pte = huge_ptep_get_and_clear(mm, address, ptep);
587+
pte = pte_mkhuge(pte_modify(pte, newprot));
588+
set_huge_pte_at(mm, address, ptep, pte);
589+
lazy_mmu_prot_update(pte);
590+
}
591+
}
592+
spin_unlock(&mm->page_table_lock);
593+
594+
flush_tlb_range(vma, start, end);
595+
}
596+

‎mm/mprotect.c‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
124124
* a MAP_NORESERVE private mapping to writable will now reserve.
125125
*/
126126
if (newflags & VM_WRITE) {
127-
if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_SHARED|VM_HUGETLB))) {
127+
if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_SHARED))) {
128128
charged = nrpages;
129129
if (security_vm_enough_memory(charged))
130130
return -ENOMEM;
@@ -166,7 +166,10 @@ mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
166166
*/
167167
vma->vm_flags = newflags;
168168
vma->vm_page_prot = newprot;
169-
change_protection(vma, start, end, newprot);
169+
if (is_vm_hugetlb_page(vma))
170+
hugetlb_change_protection(vma, start, end, newprot);
171+
else
172+
change_protection(vma, start, end, newprot);
170173
vm_stat_account(mm, oldflags, vma->vm_file, -nrpages);
171174
vm_stat_account(mm, newflags, vma->vm_file, nrpages);
172175
return 0;
@@ -240,11 +243,6 @@ sys_mprotect(unsigned long start, size_t len, unsigned long prot)
240243

241244
/* Here we know that vma->vm_start <= nstart < vma->vm_end. */
242245

243-
if (is_vm_hugetlb_page(vma)) {
244-
error = -EACCES;
245-
goto out;
246-
}
247-
248246
newflags = vm_flags | (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
249247

250248
/* newflags >> 4 shift VM_MAY% in place of VM_% */

0 commit comments

Comments
 (0)