Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp4635370imm; Mon, 14 May 2018 10:18:17 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqTFi5En/xriqRGFMS6mt40iJv00/W0peRULCOh36TDdDPh35/22JmUQZky+DitWIN2MCX9 X-Received: by 2002:a17:902:24e:: with SMTP id 72-v6mr10625624plc.87.1526318297699; Mon, 14 May 2018 10:18:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526318297; cv=none; d=google.com; s=arc-20160816; b=a/iRWtQrvSbri4M7hu3HcPLppQ4JZp0fyta9TXGiQvxSfTDUSwQW5zJ08G5Bh+/6ZN GsjA9dtHzwRULISp+yRCOWc7PM2JpdsWgUZfhYriTpb1bp3VjbYnbTt8AKsK+L0aheXx 0CI8B8/XuF5hysdca3KFLshSdn6LY6SNB923qyauQycOzuW+qoPzQIE1b+IhdlD2JX6o ZAIjHZ7m4dbTd5mOEOP77MZy9yrwmp+6Qs3Yl7RDuXKFDtIdlDHhoFDfVdLCXMwAh3hD k8xE2iTj2P8sOuyeJDWJ9W6e9Jas9/1jrU7Ua84MpSSlFCteJlf5EZXLDQPRaJxE7NZS Ghbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date:from :references:cc:to:subject:arc-authentication-results; bh=wAKqrlbEo5KUbylKvlVj3yZ5G3OqXQQXY74YcspdfDE=; b=ltcrZgcH8PDkGJpbQ+X/BsvFTO78j7IcET4Dl5biIZoFYgTg7Mqup6vVumiJDGPBDI rYBvcivYF2VTeCPLkuQhhoVQtFAJdDBLowUnK/AM6MKEjWmTCQH8E/JpHEuImjYAQS2Y at8RY+1Yd3phdOldWit+ZK+bpuSOz4w/R9T3A+dFuOMPRyIzt8uzRj03Q+KbvGewBZUZ jm/4l+JdDEMogLJzp2Uq+M+5lYoevO/WbH/vp5pIPEw3GMtzpCabJ5uCRJIXrywzBemd svaKOzzEMuX1ls3Y7uSG37HCq9p17VX9AFbhXZ7CX5pfk4xLCmX48dmRR/fY6TV2qxYw REig== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g8-v6si10860304pli.75.2018.05.14.10.18.03; Mon, 14 May 2018 10:18:17 -0700 (PDT) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932747AbeENPZT (ORCPT + 99 others); Mon, 14 May 2018 11:25:19 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:56472 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932486AbeENPZR (ORCPT ); Mon, 14 May 2018 11:25:17 -0400 Received: from pps.filterd (m0098409.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w4EFJEYw099124 for ; Mon, 14 May 2018 11:25:17 -0400 Received: from e06smtp15.uk.ibm.com (e06smtp15.uk.ibm.com [195.75.94.111]) by mx0a-001b2d01.pphosted.com with ESMTP id 2hyannyqaj-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 14 May 2018 11:25:17 -0400 Received: from localhost by e06smtp15.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 14 May 2018 16:25:13 +0100 Received: from b06cxnps4074.portsmouth.uk.ibm.com (9.149.109.196) by e06smtp15.uk.ibm.com (192.168.101.145) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 14 May 2018 16:25:04 +0100 Received: from d06av21.portsmouth.uk.ibm.com (d06av21.portsmouth.uk.ibm.com [9.149.105.232]) by b06cxnps4074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w4EFP3ek44630196; Mon, 14 May 2018 15:25:03 GMT Received: from d06av21.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id C25505204D; Mon, 14 May 2018 15:15:14 +0100 (BST) Received: from [9.101.4.33] (unknown [9.101.4.33]) by d06av21.portsmouth.uk.ibm.com (Postfix) with ESMTP id 4A90A52043; Mon, 14 May 2018 15:15:13 +0100 (BST) Subject: Re: [PATCH v10 09/25] mm: protect VMA modifications using VMA sequence count To: Minchan Kim Cc: akpm@linux-foundation.org, mhocko@kernel.org, peterz@infradead.org, kirill@shutemov.name, ak@linux.intel.com, dave@stgolabs.net, jack@suse.cz, Matthew Wilcox , benh@kernel.crashing.org, mpe@ellerman.id.au, paulus@samba.org, Thomas Gleixner , Ingo Molnar , hpa@zytor.com, Will Deacon , Sergey Senozhatsky , Andrea Arcangeli , Alexei Starovoitov , kemi.wang@intel.com, sergey.senozhatsky.work@gmail.com, Daniel Jordan , David Rientjes , Jerome Glisse , Ganesh Mahendran , linux-kernel@vger.kernel.org, linux-mm@kvack.org, haren@linux.vnet.ibm.com, khandual@linux.vnet.ibm.com, npiggin@gmail.com, bsingharora@gmail.com, paulmck@linux.vnet.ibm.com, Tim Chen , linuxppc-dev@lists.ozlabs.org, x86@kernel.org References: <1523975611-15978-1-git-send-email-ldufour@linux.vnet.ibm.com> <1523975611-15978-10-git-send-email-ldufour@linux.vnet.ibm.com> <20180423071941.GD114098@rodete-desktop-imager.corp.google.com> From: Laurent Dufour Date: Mon, 14 May 2018 17:25:02 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: <20180423071941.GD114098@rodete-desktop-imager.corp.google.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-TM-AS-GCONF: 00 x-cbid: 18051415-0020-0000-0000-0000041D1A40 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18051415-0021-0000-0000-000042B22A18 Message-Id: <3f655c5b-bc0d-1766-d0a0-5645a8bf27ab@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2018-05-14_04:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1709140000 definitions=main-1805140157 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 23/04/2018 09:19, Minchan Kim wrote: > On Tue, Apr 17, 2018 at 04:33:15PM +0200, 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(-) >> >> diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c >> index c486ad4b43f0..aeb417f28839 100644 >> --- a/fs/proc/task_mmu.c >> +++ b/fs/proc/task_mmu.c >> @@ -1136,8 +1136,11 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf, >> goto out_mm; >> } >> for (vma = mm->mmap; vma; vma = vma->vm_next) { >> - vma->vm_flags &= ~VM_SOFTDIRTY; >> + vm_write_begin(vma); >> + WRITE_ONCE(vma->vm_flags, >> + vma->vm_flags & ~VM_SOFTDIRTY); >> vma_set_page_prot(vma); >> + vm_write_end(vma); > > trivial: > > I think It's tricky to maintain that VMA fields to be read during SPF should be > (READ|WRITE_ONCE). I think we need some accessor to read/write them rather than > raw accessing like like vma_set_page_prot. Maybe spf prefix would be helpful. > > vma_spf_set_value(vma, vm_flags, val); > > We also add some markers in vm_area_struct's fileds to indicate that > people shouldn't access those fields directly. > > Just a thought. At the beginning I was liking that idea but... I'm not sure this will change a lot the code, most of the time the vm_write_begin()/end() are surrounding part of code larger than one VMA structure's field change. For this particular case and few others this will be applicable but that's not the majority. Thanks, Laurent. > > >> } >> downgrade_write(&mm->mmap_sem); > > >> diff --git a/mm/swap_state.c b/mm/swap_state.c >> index fe079756bb18..8a8a402ed59f 100644 >> --- a/mm/swap_state.c >> +++ b/mm/swap_state.c >> @@ -575,6 +575,10 @@ static unsigned long swapin_nr_pages(unsigned long offset) >> * the readahead. >> * >> * Caller must hold down_read on the vma->vm_mm if vmf->vma is not NULL. >> + * This is needed to ensure the VMA will not be freed in our back. In the case >> + * of the speculative page fault handler, this cannot happen, even if we don't >> + * hold the mmap_sem. Callees are assumed to take care of reading VMA's fields > > I guess reader would be curious on *why* is safe with SPF. > Comment about the why could be helpful for reviewer. > >> + * using READ_ONCE() to read consistent values. >> */ >> struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask, >> struct vm_fault *vmf) >> @@ -668,9 +672,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 >> >