Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp223294imu; Sun, 4 Nov 2018 23:05:24 -0800 (PST) X-Google-Smtp-Source: AJdET5enU4cJIa4zG5PXTddPbSXP02HEsGs5ONAtabtc7w5Ijuxt1xkuOnYM9vfKlya/3kkcgX+E X-Received: by 2002:a65:60c9:: with SMTP id r9-v6mr18906215pgv.285.1541401524126; Sun, 04 Nov 2018 23:05:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541401524; cv=none; d=google.com; s=arc-20160816; b=tmBSQetPdHNfDBJgdr6KGPogw6v1Ek9yu08rA9dpYQrEtEQ8sCjpX92FUfUshGS/QK EnxRln2aFBp9+5xprxH85iU4sYnxSbJtEvpzwUF1zh+1GwWgKO/z/zNX9vaFfwKkrsu6 SwejE6bTnmPxZHEFhV37zC5rrbY5EdbrdxPhzxDmKbBPnaSCTtuFNAOzw5EVgPs3E8Ie LkZK48TfCHHSM3GqIt8J+Nv9LUSUmB+fsdgaGjetjrkn2vxeJeHd8qLHiI3KNnajP6Vq LRindz59oFpkKh+bXOXHs9vUL00dQlZha1kdty/9AEriE0y34mxXdtVhyXDSo7ybQDQh ZtKg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=wTJJuCIh981c/podzqokFm77abQxsRUvn3nOyTQ9lic=; b=XJvEsrkUeqIamjAawNPb5Pt8m/A/kVsVPc+5Z9HR0Q8TTtOoEyYFtDRLoDtknM4Uc4 PvWi6uLGNM4mfKsYblFzkhALtDyKETyYoJw36rBZbxf9ffd6iuf0TGvg67VKF3JMT6jF bJ0lhkaCjbvAzbp92+/XeetFE8pJTpnoOl+vIEO4uWwRzbFu5XJlfuhZzxBJFVoOs/B5 5Z3GYomJl/M44Kv3GaLI0XBihIpFafwctQ98R5ODdKRPV964wjmdA9+8l90uWBUMgd7O 2Vl37+P2hzuBTHOUTkwc6m6MafSMUoc6tmPW0l2V70A3UEvzGw6Lhec+U1mABNb38B+n 0SDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=JbvS1Js0; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e92-v6si42720624pld.45.2018.11.04.23.05.08; Sun, 04 Nov 2018 23:05:24 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=JbvS1Js0; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729483AbeKEQW7 (ORCPT + 99 others); Mon, 5 Nov 2018 11:22:59 -0500 Received: from mail-wr1-f67.google.com ([209.85.221.67]:41518 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729000AbeKEQW7 (ORCPT ); Mon, 5 Nov 2018 11:22:59 -0500 Received: by mail-wr1-f67.google.com with SMTP id x12-v6so8171044wrw.8 for ; Sun, 04 Nov 2018 23:04:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=wTJJuCIh981c/podzqokFm77abQxsRUvn3nOyTQ9lic=; b=JbvS1Js0bQAVhZ3NPshUyGy5EeQsED/mgXOZeLb6/+5mlYx+IYAw/pCqZwSMSCpPR4 h1051htvcNuQqsR7xwwQLiT+GAjzs4yMfb85ff7cW1Vubo1yShlEQ9aX9LrIXPNijK/d BhSSCBgGcNknMqGe4YX4eGUQ9VzWLelA0O20K7QN2GbBrsOKq8BlHiVYWiKdU8Xi8CFL IRQhxJ6kWfPKIDiO6KBBu24zuOaWOhmXkzUThdawUr2O56IqMoTfLGKDma0B0Ss2+Rg2 gOekolpSph2qAWxWU8RAHjaL3SuLatOTYAlu9qXlEjFq+l1pXHNg20U/9ksvyx+LMXgD OFpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=wTJJuCIh981c/podzqokFm77abQxsRUvn3nOyTQ9lic=; b=Tbv1tSB6KiL34XN0E4YRrplD9aMVZsCnN+UqQHLySuX27YEqmH1phzpNkktXS9P3Lz QRz7Lu+kdpieatovHL4UoJU9XTX9VZjjtATodPiFtd75YKCiADbST5sxPNnEUzYQ2VjN NXAzjoEFpiarZ02KDdkD0zn1nd2xtqRUbuuYUr9+cgprMpIwhf9Al8LkNbOt3i1azF7I e+ACYypkRKTkDGDk92ApNCk+3t6ZnvOuQTX9K9hkd9z5Tu8gcSn0XF46tU2fIdPLcYtj 3O/4/g0LVNTg7vpM5aoLOruS7yl03WHxNlhonNFyY5mAVFXbfIcIbJfb2XEwiD37CWu9 kJwg== X-Gm-Message-State: AGRZ1gJsV5caWoL8NgdnMLlIsVF3IDgBXdFp4ZfMHLzITCjE4fmjZ3eN /B0Plx0ubRPBUGQ4Lyk62nwQmCtH1MihkZvWV2Q= X-Received: by 2002:adf:aa05:: with SMTP id p5-v6mr19549114wrd.56.1541401484893; Sun, 04 Nov 2018 23:04:44 -0800 (PST) MIME-Version: 1.0 References: <1526555193-7242-1-git-send-email-ldufour@linux.vnet.ibm.com> <1526555193-7242-11-git-send-email-ldufour@linux.vnet.ibm.com> In-Reply-To: <1526555193-7242-11-git-send-email-ldufour@linux.vnet.ibm.com> From: vinayak menon Date: Mon, 5 Nov 2018 12:34:33 +0530 Message-ID: Subject: Re: [PATCH v11 10/26] mm: protect VMA modifications using VMA sequence count To: Laurent Dufour Cc: Andrew Morton , Michal Hocko , Peter Zijlstra , kirill@shutemov.name, ak@linux.intel.com, dave@stgolabs.net, jack@suse.cz, Matthew Wilcox , khandual@linux.vnet.ibm.com, aneesh.kumar@linux.vnet.ibm.com, benh@kernel.crashing.org, mpe@ellerman.id.au, paulus@samba.org, Thomas Gleixner , Ingo Molnar , hpa@zytor.com, Will Deacon , Sergey Senozhatsky , sergey.senozhatsky.work@gmail.com, Andrea Arcangeli , Alexei Starovoitov , kemi.wang@intel.com, Daniel Jordan , David Rientjes , Jerome Glisse , Ganesh Mahendran , Minchan Kim , punitagrawal@gmail.com, yang.shi@linux.alibaba.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, haren@linux.vnet.ibm.com, npiggin@gmail.com, Balbir Singh , Paul McKenney , Tim Chen , linuxppc-dev@lists.ozlabs.org, x86@kernel.org, Vinayak Menon Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Laurent, On Thu, May 17, 2018 at 4:37 PM Laurent Dufour wrote: > > The VMA sequence count has been introduced to allow fast detection of > VMA modification when running a page fault handler without holding > the mmap_sem. > > This patch provides protection against the VMA modification done in : > - madvise() > - mpol_rebind_policy() > - vma_replace_policy() > - change_prot_numa() > - mlock(), munlock() > - mprotect() > - mmap_region() > - collapse_huge_page() > - userfaultd registering services > > In addition, VMA fields which will be read during the speculative fault > path needs to be written using WRITE_ONCE to prevent write to be split > and intermediate values to be pushed to other CPUs. > > Signed-off-by: Laurent Dufour > --- > fs/proc/task_mmu.c | 5 ++++- > fs/userfaultfd.c | 17 +++++++++++++---- > mm/khugepaged.c | 3 +++ > mm/madvise.c | 6 +++++- > mm/mempolicy.c | 51 ++++++++++++++++++++++++++++++++++----------------- > mm/mlock.c | 13 ++++++++----- > mm/mmap.c | 22 +++++++++++++--------- > mm/mprotect.c | 4 +++- > mm/swap_state.c | 8 ++++++-- > 9 files changed, 89 insertions(+), 40 deletions(-) > > struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask, > struct vm_fault *vmf) > @@ -665,9 +669,9 @@ static inline void swap_ra_clamp_pfn(struct vm_area_struct *vma, > unsigned long *start, > unsigned long *end) > { > - *start = max3(lpfn, PFN_DOWN(vma->vm_start), > + *start = max3(lpfn, PFN_DOWN(READ_ONCE(vma->vm_start)), > PFN_DOWN(faddr & PMD_MASK)); > - *end = min3(rpfn, PFN_DOWN(vma->vm_end), > + *end = min3(rpfn, PFN_DOWN(READ_ONCE(vma->vm_end)), > PFN_DOWN((faddr & PMD_MASK) + PMD_SIZE)); > } > > -- > 2.7.4 > I have got a crash on 4.14 kernel with speculative page faults enabled and here is my analysis of the problem. The issue was reported only once. [23409.303395] el1_da+0x24/0x84 [23409.303400] __radix_tree_lookup+0x8/0x90 [23409.303407] find_get_entry+0x64/0x14c [23409.303410] pagecache_get_page+0x5c/0x27c [23409.303416] __read_swap_cache_async+0x80/0x260 [23409.303420] swap_vma_readahead+0x264/0x37c [23409.303423] swapin_readahead+0x5c/0x6c [23409.303428] do_swap_page+0x128/0x6e4 [23409.303431] handle_pte_fault+0x230/0xca4 [23409.303435] __handle_speculative_fault+0x57c/0x7c8 [23409.303438] do_page_fault+0x228/0x3e8 [23409.303442] do_translation_fault+0x50/0x6c [23409.303445] do_mem_abort+0x5c/0xe0 [23409.303447] el0_da+0x20/0x24 Process A accesses address ADDR (part of VMA A) and that results in a translation fault. Kernel enters __handle_speculative_fault to fix the fault. Process A enters do_swap_page->swapin_readahead->swap_vma_readahead from speculative path. During this time, another process B which shares the same mm, does a mprotect from another CPU which follows mprotect_fixup->__split_vma, and it splits VMA A into VMAs A and B. After the split, ADDR falls into VMA B, but process A is still using VMA A. Now ADDR is greater than VMA_A->vm_start and VMA_A->vm_end. swap_vma_readahead->swap_ra_info uses start and end of vma to calculate ptes and nr_pte, which goes wrong due to this and finally resulting in wrong "entry" passed to swap_vma_readahead->__read_swap_cache_async, and in turn causing invalid swapper_space being passed to __read_swap_cache_async->find_get_page, causing an abort. The fix I have tried is to cache vm_start and vm_end also in vmf and use it in swap_ra_clamp_pfn. Let me know your thoughts on this. I can send the patch I am a using if you feel that is the right thing to do. Thanks, Vinayak