From patchwork Thu Feb 20 16:30:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 11394541 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 80D17109A for ; Thu, 20 Feb 2020 16:31:26 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4BCE224673 for ; Thu, 20 Feb 2020 16:31:26 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="i6fiPmWA" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4BCE224673 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 6FB7E6B000A; Thu, 20 Feb 2020 11:31:25 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 685806B000C; Thu, 20 Feb 2020 11:31:25 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5264F6B000D; Thu, 20 Feb 2020 11:31:25 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0072.hostedemail.com [216.40.44.72]) by kanga.kvack.org (Postfix) with ESMTP id 385856B000A for ; Thu, 20 Feb 2020 11:31:25 -0500 (EST) Received: from smtpin08.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id C80F9824999B for ; Thu, 20 Feb 2020 16:31:24 +0000 (UTC) X-FDA: 76511045688.08.soap23_29124619f8e28 X-Spam-Summary: 1,0,0,,d41d8cd98f00b204,peterx@redhat.com,::linux-kernel@vger.kernel.org:bgeffon@google.com:xemul@virtuozzo.com:mike.kravetz@oracle.com:david@redhat.com:peterx@redhat.com:cracauer@cons.org:aarcange@redhat.com:mgorman@suse.de:bobbypowers@gmail.com:rppt@linux.vnet.ibm.com:kirill@shutemov.name:gokhale2@llnl.gov:hannes@cmpxchg.org:mcfadden8@llnl.gov:dplotnikov@virtuozzo.com:hughd@google.com:dgilbert@redhat.com:jglisse@redhat.com:shli@fb.com,RULES_HIT:30003:30054:30064,0,RBL:205.139.110.61:@redhat.com:.lbl8.mailshell.net-66.10.201.10 62.18.0.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:ft,MSBL:0,DNSBL:neutral,Custom_rules:0:1:0,LFtime:25,LUA_SUMMARY:none X-HE-Tag: soap23_29124619f8e28 X-Filterd-Recvd-Size: 7155 Received: from us-smtp-delivery-1.mimecast.com (us-smtp-2.mimecast.com [205.139.110.61]) by imf43.hostedemail.com (Postfix) with ESMTP for ; Thu, 20 Feb 2020 16:31:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582216281; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dDrE+2gIwc+ob8o3L/XP4p2YfgW3g9IncxOH86Yj45E=; b=i6fiPmWAneuwFSYe+NM1KUTf6hNKmo1fY5NjNaHGl2nwnL0l6Zoy1342EDH5NXMYfPw72v BvYBUpZIOM2gpltkARhTm8WIEfXZvpjtagIplXI4qe0hLhn4YbMZM9agQMYnH9IB/o20ib SxlrdhhMTezQ5f5eB33rFESMhyTqGdU= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-70-rIKYr1sMObOfVEh0M-Gz9Q-1; Thu, 20 Feb 2020 11:31:19 -0500 X-MC-Unique: rIKYr1sMObOfVEh0M-Gz9Q-1 Received: by mail-qk1-f198.google.com with SMTP id n126so3084944qkc.18 for ; Thu, 20 Feb 2020 08:31:19 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=CHUFDqjtneZv80jBBtiyIEM9rxGSYmYj1xgCDRa8zzo=; b=dWcuZ+5HUlChk658zCpEbmWnY+fuetTqYhnl1NouKZJdzCC4XiowujhsxgIF2+hHfX UVf+PdizakWAGdqLkPOmFnlbwZmnA5Md+BzHP+KfWSDY/oh1ElG0MLj5+ycngCLJ/xJC bArExYZjEZlBMLTf591GjrtPf8ekpxK1GcY86ANNnx98+Ws0ygmQrsTAQMXBIaRatXi3 8hAFX0qpcr5PBvbx8X8mrupOuCaTDJ4AN8kehfcAfqETdIuTytzpLtbRt+TJmOQ4hh39 p7z4UzM3kU/QbsVu6C87y9eeSH1nswb+3AVLSNV23bubQ3Rm8CBd2hxrsIXQcPKQLWic SdYQ== X-Gm-Message-State: APjAAAX3g+Lj9fPHzQhAUz3Bsx5RVScmxjT6ujBsjzHur4quOAgTTDGG 5a6KENoyFHSz7KCWq9UkccDPPyx7s35JAFIH71UkS8hLXzDHmvw3r9dovSvD8ScIZhxqZrTCwBk dbeh/JwLsMj4= X-Received: by 2002:a37:9c10:: with SMTP id f16mr29744444qke.275.1582216278867; Thu, 20 Feb 2020 08:31:18 -0800 (PST) X-Google-Smtp-Source: APXvYqx4qYyI240EJ4iaY8NxDR5Afaf1dkjtOJEaaFOxolIwXiqPaZSbmCZWqp6IyApgQc2eCSyiFA== X-Received: by 2002:a37:9c10:: with SMTP id f16mr29744392qke.275.1582216278582; Thu, 20 Feb 2020 08:31:18 -0800 (PST) Received: from xz-x1.redhat.com ([104.156.64.75]) by smtp.gmail.com with ESMTPSA id l19sm42366qkl.3.2020.02.20.08.31.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Feb 2020 08:31:17 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Brian Geffon , Pavel Emelyanov , Mike Kravetz , David Hildenbrand , peterx@redhat.com, Martin Cracauer , Andrea Arcangeli , Mel Gorman , Bobby Powers , Mike Rapoport , "Kirill A . Shutemov" , Maya Gokhale , Johannes Weiner , Marty McFadden , Denis Plotnikov , Hugh Dickins , "Dr . David Alan Gilbert" , Jerome Glisse , Shaohua Li Subject: [PATCH v6 02/19] userfaultfd: wp: hook userfault handler to write protection fault Date: Thu, 20 Feb 2020 11:30:55 -0500 Message-Id: <20200220163112.11409-3-peterx@redhat.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200220163112.11409-1-peterx@redhat.com> References: <20200220163112.11409-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Andrea Arcangeli There are several cases write protection fault happens. It could be a write to zero page, swaped page or userfault write protected page. When the fault happens, there is no way to know if userfault write protect the page before. Here we just blindly issue a userfault notification for vma with VM_UFFD_WP regardless if app write protects it yet. Application should be ready to handle such wp fault. v1: From: Shaohua Li v2: Handle the userfault in the common do_wp_page. If we get there a pagetable is present and readonly so no need to do further processing until we solve the userfault. In the swapin case, always swapin as readonly. This will cause false positive userfaults. We need to decide later if to eliminate them with a flag like soft-dirty in the swap entry (see _PAGE_SWP_SOFT_DIRTY). hugetlbfs wouldn't need to worry about swapouts but and tmpfs would be handled by a swap entry bit like anonymous memory. The main problem with no easy solution to eliminate the false positives, will be if/when userfaultfd is extended to real filesystem pagecache. When the pagecache is freed by reclaim we can't leave the radix tree pinned if the inode and in turn the radix tree is reclaimed as well. The estimation is that full accuracy and lack of false positives could be easily provided only to anonymous memory (as long as there's no fork or as long as MADV_DONTFORK is used on the userfaultfd anonymous range) tmpfs and hugetlbfs, it's most certainly worth to achieve it but in a later incremental patch. v3: Add hooking point for THP wrprotect faults. CC: Shaohua Li Signed-off-by: Andrea Arcangeli [peterx: don't conditionally drop FAULT_FLAG_WRITE in do_swap_page] Reviewed-by: Mike Rapoport Reviewed-by: Jerome Glisse Signed-off-by: Peter Xu --- mm/memory.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/mm/memory.c b/mm/memory.c index 0bccc622e482..3d8c7e8652e9 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -2733,6 +2733,11 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf) { struct vm_area_struct *vma = vmf->vma; + if (userfaultfd_wp(vma)) { + pte_unmap_unlock(vmf->pte, vmf->ptl); + return handle_userfault(vmf, VM_UFFD_WP); + } + vmf->page = vm_normal_page(vma, vmf->address, vmf->orig_pte); if (!vmf->page) { /* @@ -3930,8 +3935,11 @@ static inline vm_fault_t create_huge_pmd(struct vm_fault *vmf) /* `inline' is required to avoid gcc 4.1.2 build error */ static inline vm_fault_t wp_huge_pmd(struct vm_fault *vmf, pmd_t orig_pmd) { - if (vma_is_anonymous(vmf->vma)) + if (vma_is_anonymous(vmf->vma)) { + if (userfaultfd_wp(vmf->vma)) + return handle_userfault(vmf, VM_UFFD_WP); return do_huge_pmd_wp_page(vmf, orig_pmd); + } if (vmf->vma->vm_ops->huge_fault) return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD);