Skip to content

Commit 2efaca9

Browse files
ozbenhtorvalds
authored andcommitted
mm/futex: fix futex writes on archs with SW tracking of dirty & young
I haven't reproduced it myself but the fail scenario is that on such machines (notably ARM and some embedded powerpc), if you manage to hit that futex path on a writable page whose dirty bit has gone from the PTE, you'll livelock inside the kernel from what I can tell. It will go in a loop of trying the atomic access, failing, trying gup to "fix it up", getting succcess from gup, go back to the atomic access, failing again because dirty wasn't fixed etc... So I think you essentially hang in the kernel. The scenario is probably rare'ish because affected architecture are embedded and tend to not swap much (if at all) so we probably rarely hit the case where dirty is missing or young is missing, but I think Shan has a piece of SW that can reliably reproduce it using a shared writable mapping & fork or something like that. On archs who use SW tracking of dirty & young, a page without dirty is effectively mapped read-only and a page without young unaccessible in the PTE. Additionally, some architectures might lazily flush the TLB when relaxing write protection (by doing only a local flush), and expect a fault to invalidate the stale entry if it's still present on another processor. The futex code assumes that if the "in_atomic()" access -EFAULT's, it can "fix it up" by causing get_user_pages() which would then be equivalent to taking the fault. However that isn't the case. get_user_pages() will not call handle_mm_fault() in the case where the PTE seems to have the right permissions, regardless of the dirty and young state. It will eventually update those bits ... in the struct page, but not in the PTE. Additionally, it will not handle the lazy TLB flushing that can be required by some architectures in the fault case. Basically, gup is the wrong interface for the job. The patch provides a more appropriate one which boils down to just calling handle_mm_fault() since what we are trying to do is simulate a real page fault. The futex code currently attempts to write to user memory within a pagefault disabled section, and if that fails, tries to fix it up using get_user_pages(). This doesn't work on archs where the dirty and young bits are maintained by software, since they will gate access permission in the TLB, and will not be updated by gup(). In addition, there's an expectation on some archs that a spurious write fault triggers a local TLB flush, and that is missing from the picture as well. I decided that adding those "features" to gup() would be too much for this already too complex function, and instead added a new simpler fixup_user_fault() which is essentially a wrapper around handle_mm_fault() which the futex code can call. [[email protected]: coding-style fixes] [[email protected]: fix some nits Darren saw, fiddle comment layout] Signed-off-by: Benjamin Herrenschmidt <[email protected]> Reported-by: Shan Hai <[email protected]> Tested-by: Shan Hai <[email protected]> Cc: David Laight <[email protected]> Acked-by: Peter Zijlstra <[email protected]> Cc: Darren Hart <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 72c4783 commit 2efaca9

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

include/linux/mm.h

+2
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,8 @@ int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
988988
int get_user_pages_fast(unsigned long start, int nr_pages, int write,
989989
struct page **pages);
990990
struct page *get_dump_page(unsigned long addr);
991+
extern int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
992+
unsigned long address, unsigned int fault_flags);
991993

992994
extern int try_to_release_page(struct page * page, gfp_t gfp_mask);
993995
extern void do_invalidatepage(struct page *page, unsigned long offset);

kernel/futex.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,8 @@ static int fault_in_user_writeable(u32 __user *uaddr)
355355
int ret;
356356

357357
down_read(&mm->mmap_sem);
358-
ret = get_user_pages(current, mm, (unsigned long)uaddr,
359-
1, 1, 0, NULL, NULL);
358+
ret = fixup_user_fault(current, mm, (unsigned long)uaddr,
359+
FAULT_FLAG_WRITE);
360360
up_read(&mm->mmap_sem);
361361

362362
return ret < 0 ? ret : 0;

mm/memory.c

+57-1
Original file line numberDiff line numberDiff line change
@@ -1805,7 +1805,63 @@ int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
18051805
}
18061806
EXPORT_SYMBOL(__get_user_pages);
18071807

1808-
/**
1808+
/*
1809+
* fixup_user_fault() - manually resolve a user page fault
1810+
* @tsk: the task_struct to use for page fault accounting, or
1811+
* NULL if faults are not to be recorded.
1812+
* @mm: mm_struct of target mm
1813+
* @address: user address
1814+
* @fault_flags:flags to pass down to handle_mm_fault()
1815+
*
1816+
* This is meant to be called in the specific scenario where for locking reasons
1817+
* we try to access user memory in atomic context (within a pagefault_disable()
1818+
* section), this returns -EFAULT, and we want to resolve the user fault before
1819+
* trying again.
1820+
*
1821+
* Typically this is meant to be used by the futex code.
1822+
*
1823+
* The main difference with get_user_pages() is that this function will
1824+
* unconditionally call handle_mm_fault() which will in turn perform all the
1825+
* necessary SW fixup of the dirty and young bits in the PTE, while
1826+
* handle_mm_fault() only guarantees to update these in the struct page.
1827+
*
1828+
* This is important for some architectures where those bits also gate the
1829+
* access permission to the page because they are maintained in software. On
1830+
* such architectures, gup() will not be enough to make a subsequent access
1831+
* succeed.
1832+
*
1833+
* This should be called with the mm_sem held for read.
1834+
*/
1835+
int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
1836+
unsigned long address, unsigned int fault_flags)
1837+
{
1838+
struct vm_area_struct *vma;
1839+
int ret;
1840+
1841+
vma = find_extend_vma(mm, address);
1842+
if (!vma || address < vma->vm_start)
1843+
return -EFAULT;
1844+
1845+
ret = handle_mm_fault(mm, vma, address, fault_flags);
1846+
if (ret & VM_FAULT_ERROR) {
1847+
if (ret & VM_FAULT_OOM)
1848+
return -ENOMEM;
1849+
if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
1850+
return -EHWPOISON;
1851+
if (ret & VM_FAULT_SIGBUS)
1852+
return -EFAULT;
1853+
BUG();
1854+
}
1855+
if (tsk) {
1856+
if (ret & VM_FAULT_MAJOR)
1857+
tsk->maj_flt++;
1858+
else
1859+
tsk->min_flt++;
1860+
}
1861+
return 0;
1862+
}
1863+
1864+
/*
18091865
* get_user_pages() - pin user pages in memory
18101866
* @tsk: the task_struct to use for page fault accounting, or
18111867
* NULL if faults are not to be recorded.

0 commit comments

Comments
 (0)