Skip to content

Commit aecd6f4

Browse files
kiryltorvalds
authored andcommitted
mm: close race between do_fault_around() and fault_around_bytes_set()
Things can go wrong if fault_around_bytes will be changed under do_fault_around(): between fault_around_mask() and fault_around_pages(). Let's read fault_around_bytes only once during do_fault_around() and calculate mask based on the reading. Note: fault_around_bytes can only be updated via debug interface. Also I've tried but was not able to trigger a bad behaviour without the patch. So I would not consider this patch as urgent. Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Cc: Dave Hansen <dave.hansen@intel.com> Cc: Andrey Ryabinin <a.ryabinin@samsung.com> Cc: Sasha Levin <sasha.levin@oracle.com> Cc: David Rientjes <rientjes@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 2ab051e commit aecd6f4

1 file changed

Lines changed: 7 additions & 14 deletions

File tree

‎mm/memory.c‎

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2768,16 +2768,6 @@ void do_set_pte(struct vm_area_struct *vma, unsigned long address,
27682768

27692769
static unsigned long fault_around_bytes = rounddown_pow_of_two(65536);
27702770

2771-
static inline unsigned long fault_around_pages(void)
2772-
{
2773-
return fault_around_bytes >> PAGE_SHIFT;
2774-
}
2775-
2776-
static inline unsigned long fault_around_mask(void)
2777-
{
2778-
return ~(fault_around_bytes - 1) & PAGE_MASK;
2779-
}
2780-
27812771
#ifdef CONFIG_DEBUG_FS
27822772
static int fault_around_bytes_get(void *data, u64 *val)
27832773
{
@@ -2842,12 +2832,15 @@ late_initcall(fault_around_debugfs);
28422832
static void do_fault_around(struct vm_area_struct *vma, unsigned long address,
28432833
pte_t *pte, pgoff_t pgoff, unsigned int flags)
28442834
{
2845-
unsigned long start_addr;
2835+
unsigned long start_addr, nr_pages, mask;
28462836
pgoff_t max_pgoff;
28472837
struct vm_fault vmf;
28482838
int off;
28492839

2850-
start_addr = max(address & fault_around_mask(), vma->vm_start);
2840+
nr_pages = ACCESS_ONCE(fault_around_bytes) >> PAGE_SHIFT;
2841+
mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK;
2842+
2843+
start_addr = max(address & mask, vma->vm_start);
28512844
off = ((address - start_addr) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
28522845
pte -= off;
28532846
pgoff -= off;
@@ -2859,7 +2852,7 @@ static void do_fault_around(struct vm_area_struct *vma, unsigned long address,
28592852
max_pgoff = pgoff - ((start_addr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) +
28602853
PTRS_PER_PTE - 1;
28612854
max_pgoff = min3(max_pgoff, vma_pages(vma) + vma->vm_pgoff - 1,
2862-
pgoff + fault_around_pages() - 1);
2855+
pgoff + nr_pages - 1);
28632856

28642857
/* Check if it makes any sense to call ->map_pages */
28652858
while (!pte_none(*pte)) {
@@ -2894,7 +2887,7 @@ static int do_read_fault(struct mm_struct *mm, struct vm_area_struct *vma,
28942887
* something).
28952888
*/
28962889
if (vma->vm_ops->map_pages && !(flags & FAULT_FLAG_NONLINEAR) &&
2897-
fault_around_pages() > 1) {
2890+
fault_around_bytes >> PAGE_SHIFT > 1) {
28982891
pte = pte_offset_map_lock(mm, pmd, address, &ptl);
28992892
do_fault_around(vma, address, pte, pgoff, flags);
29002893
if (!pte_same(*pte, orig_pte))

0 commit comments

Comments
 (0)