Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3426067pxf; Mon, 22 Mar 2021 06:16:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyuSiEDGStY/lmxOnh5O0PweAy9+4WJwhRPce1106BAe0IYNDl5x6h6tGJGKhdqQ0BJ4LSX X-Received: by 2002:a17:906:1c13:: with SMTP id k19mr19334436ejg.457.1616419002491; Mon, 22 Mar 2021 06:16:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616419002; cv=none; d=google.com; s=arc-20160816; b=CU4BsrVPGfTqAC3OnYUnRnM4IQfLe9fVvQMC0wQz2aGaI2NfiNSdFR7kR/zbNdK+QB w5E67IzaGinC3nc6lSbRzy52FT/3SPocbYx9YRApO3e5CiqZbfCbz3FCp48cQe9ij/TX uTl0vaNqUzCZWyZ6n4bQayEKz2bggnyWahxjVBDYVkmxydEj5Cq8DgqDmwuv0D4GMD4W 24Kfm3Q4/YE+PgTl8Sf83DvAJCzb1EpedbwO25EWhTjP6SJTmnsjSYG0h/f5ii0aWOYE xwW1eyJNkFUDUcbtsf+pG2UjJRnzudHgyDeahBM6ELnUBWNKm/Lg2TzqrYnoDJvHI35U lAag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=+oWSa13ztPV6lQqNXoJMY99jmBfB/lD8boeIyvrzGLY=; b=QTTlfTztQ2emvpCEttRB5oEnNu0+nSLtRoXdakADb6O4kreyc11GL98kij96cywxoq WD2Mh7gvUrHTcJtEi+KelhFufdp1Lsp/q7ea34QuRkAvGw74ipJSwzBYSFqfvrswr1TH IvVhxzZilOEMDQ3MxOsDU+RSNAP39jny1l959c/OL6S9kkcdgCOfPlLBYSJgz/VvkcVj b14uRyzJ5HXCXQKmE5RAm+Dc5nZjbcBX0fT7qQkimoTQwMYL52QW19qyZYvxMToyrHvn /HAUYHS3cwW2EORymD/slBg918+1JuPj409KMQgCSMsqxAXXoxvjxYT8kReijTSIZ//8 T8Pw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=J4EjrG0b; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bt5si10468217edb.506.2021.03.22.06.16.20; Mon, 22 Mar 2021 06:16:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=J4EjrG0b; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233081AbhCVNP0 (ORCPT + 99 others); Mon, 22 Mar 2021 09:15:26 -0400 Received: from mail.kernel.org ([198.145.29.99]:55048 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232819AbhCVNAp (ORCPT ); Mon, 22 Mar 2021 09:00:45 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 3E51060C3D; Mon, 22 Mar 2021 13:00:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1616418044; bh=6SAcRO0qYg3edBZ6Nl5uqmHSmdsG/Cbc/xFrvoFFuDE=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=J4EjrG0bjljNgV0UtyE8qwUzPFBRm26gliQFsU0gcVpujaNmPu60oYsKCOKJwCTQd 1RB3dQWr++7shv4lK5wknrzgFennM4ed58RJ5Pwcl1A42yqhBrEkTl9QpS61R5HnIX U0b6g8UFrC2qlwJsV0jRHZ94Qbno/jAf3N9dXqk22VlxqstMY1+L9WIa7oj5HFq2Zi LIrQNrLXn70I7qxurLHRrC6CV66KU82gM8F1rx8w4wx8eINXWWIyKz6vf+Te+f+z0E fBl4feSeG/FKt3kCmQOnte2V40OgcGswFD45NuY1PsCIb2cR6V3EL0fxF1xX/4CN2j 3ggkYu+Tmrdog== Date: Mon, 22 Mar 2021 15:00:37 +0200 From: Mike Rapoport To: Michal Hocko Cc: Bui Quang Minh , akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrea Arcangeli Subject: Re: [PATCH] userfaultfd: Write protect when virtual memory range has no page table entry Message-ID: References: <20210319152428.52683-1-minhquangbui99@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Mar 22, 2021 at 11:14:37AM +0100, Michal Hocko wrote: > Le'ts Andrea and Mike > > On Fri 19-03-21 22:24:28, Bui Quang Minh wrote: > > userfaultfd_writeprotect() use change_protection() to clear write bit in > > page table entries (pte/pmd). So, later write to this virtual address > > range causes a page fault, which is then handled by userspace program. > > However, change_protection() has no effect when there is no page table > > entries associated with that virtual memory range (a newly mapped memory > > range). As a result, later access to that memory range causes allocating a > > page table entry with write bit still set (due to VM_WRITE flag in > > vma->vm_flags). > > > > Add checks for VM_UFFD_WP in vma->vm_flags when allocating new page table > > entry in missing page table entry page fault path. > > From the above it is not really clear whether this is a usability > problem or a bug of the interface. I'd say it's usability/documentation clarity issue. Userspace can register an area with UFFDIO_REGISTER_MODE_MISSING | UFFDIO_REGISTER_MODE_WP and then it will be notified either when page table has no entry for a virtual address or when there is a write to a write protected address. > > Signed-off-by: Bui Quang Minh > > --- > > mm/huge_memory.c | 12 ++++++++++++ > > mm/memory.c | 10 ++++++++++ > > 2 files changed, 22 insertions(+) > > > > diff --git a/mm/huge_memory.c b/mm/huge_memory.c > > index ae907a9c2050..9bb16a55a48c 100644 > > --- a/mm/huge_memory.c > > +++ b/mm/huge_memory.c > > @@ -636,6 +636,11 @@ static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf, > > > > entry = mk_huge_pmd(page, vma->vm_page_prot); > > entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); > > + if (userfaultfd_wp(vma)) { > > + entry = pmd_wrprotect(entry); > > + entry = pmd_mkuffd_wp(entry); > > + } > > + > > page_add_new_anon_rmap(page, vma, haddr, true); > > lru_cache_add_inactive_or_unevictable(page, vma); > > pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, pgtable); > > @@ -643,6 +648,13 @@ static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf, > > update_mmu_cache_pmd(vma, vmf->address, vmf->pmd); > > add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR); > > mm_inc_nr_ptes(vma->vm_mm); > > + > > + if (userfaultfd_huge_pmd_wp(vma, *vmf->pmd)) { > > + spin_unlock(vmf->ptl); > > + count_vm_event(THP_FAULT_ALLOC); > > + count_memcg_event_mm(vma->vm_mm, THP_FAULT_ALLOC); > > + return handle_userfault(vmf, VM_UFFD_WP); > > + } > > spin_unlock(vmf->ptl); > > count_vm_event(THP_FAULT_ALLOC); > > count_memcg_event_mm(vma->vm_mm, THP_FAULT_ALLOC); > > diff --git a/mm/memory.c b/mm/memory.c > > index 5efa07fb6cdc..b835746545bf 100644 > > --- a/mm/memory.c > > +++ b/mm/memory.c > > @@ -3564,6 +3564,11 @@ static vm_fault_t do_anonymous_page(struct vm_fault *vmf) > > if (vma->vm_flags & VM_WRITE) > > entry = pte_mkwrite(pte_mkdirty(entry)); > > > > + if (userfaultfd_wp(vma)) { > > + entry = pte_wrprotect(entry); > > + entry = pte_mkuffd_wp(entry); > > + } > > + > > vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, > > &vmf->ptl); > > if (!pte_none(*vmf->pte)) { > > @@ -3590,6 +3595,11 @@ static vm_fault_t do_anonymous_page(struct vm_fault *vmf) > > > > /* No need to invalidate - it was non-present before */ > > update_mmu_cache(vma, vmf->address, vmf->pte); > > + > > + if (userfaultfd_pte_wp(vma, *vmf->pte)) { > > + pte_unmap_unlock(vmf->pte, vmf->ptl); > > + return handle_userfault(vmf, VM_UFFD_WP); > > + } > > unlock: > > pte_unmap_unlock(vmf->pte, vmf->ptl); > > return ret; > > -- > > 2.25.1 > > -- > Michal Hocko > SUSE Labs -- Sincerely yours, Mike.