Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1848232pxu; Fri, 27 Nov 2020 17:28:24 -0800 (PST) X-Google-Smtp-Source: ABdhPJzNiS5UM4QUeqRDPtu+HBAY5WqO5DOIK+RQMae3GkRSdR6xZlKsYk3HKot4FiAeYI3pZPPg X-Received: by 2002:a17:906:bcd4:: with SMTP id lw20mr10480036ejb.527.1606526904225; Fri, 27 Nov 2020 17:28:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606526904; cv=none; d=google.com; s=arc-20160816; b=sLfqLh0/iozeXrBidQU7if/+VJS718F/edKgCE+YGo8jpZNGbZOZUaNq11ZEhRvZqR A/a+VLoWmNEKfspehf5rIRfN34vpMXLvBDU07vm7tLHOCsKuIYf0MA+urxosb2rsVkPy UxrRPymWCqeOhVHqiv2FrGiFks7UdPr4kaDBYuxDHd7ois5anhxJXxERRUV7LgEdM9gb lnrTmxCyRW2dRW56mPpnHeVpOJYKFzSXCExFTTD47wamytsqqnI1l9YSP87+KvabOI7h fYoxVidIvu+43R7vE0CGggmqi8N+jmjNsz/pl5N+naKP2iEdQzOuDVhZRvq9/+JeoGyY qklA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=KFfdlT9oNUwho2w5XQHB2zyqSSAHo4d/LdFWl9C4gCA=; b=S6UCl1+Eom3Zhave3eNovxEXwu5XGPLOys6j07oXktWPYzMae7ddlYoeWGdw4em6sN FSKCOlsTJr9N8nsJpcZNzl8fjzXYGQwWZovGna/Q+W78ikH7qOOZgZPjXL+7wWvg7MdP Y9jpshMGoSAj/hKapFe/whPeB4XHFLXAsrYU4KETysqRjRDhXF0HCC23wYkgWMrJ2e8v tqC9/09bSubujuV3CyE3DWR1jWOphVugGo+qnYguX1BGNAv7gp0bn5/CUghAB52Lktas 519B+SQuaselRpvmhgqL1sxe3mlfIgeRHAOmf39e3EzfJVIyvXqfPWCHq0TcKLacwT+F DeIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=YsXt0CXw; 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=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e1si6360824ejf.335.2020.11.27.17.28.01; Fri, 27 Nov 2020 17:28:24 -0800 (PST) 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=@redhat.com header.s=mimecast20190719 header.b=YsXt0CXw; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731951AbgK1AnO (ORCPT + 99 others); Fri, 27 Nov 2020 19:43:14 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:56666 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730991AbgK1AeG (ORCPT ); Fri, 27 Nov 2020 19:34:06 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1606523626; 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: in-reply-to:in-reply-to:references:references; bh=KFfdlT9oNUwho2w5XQHB2zyqSSAHo4d/LdFWl9C4gCA=; b=YsXt0CXwQ1CHO72X73KBCShx4OYl15OWyrrKkt0JbcXIgUBnrXF+3njlsvWhG5z1JhJ9d1 a051cdOZM9Ir9lHjDZvCw7Ty3npyS9bje6zCDCSBdxDcOnqtjtPci3+WjKgULVH+y4kOYA nSyN5ChLTSxPIGbmzxnmYfEexW7J6Tw= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-489-Kqd8r88RMNacv7rPCQ94aA-1; Fri, 27 Nov 2020 19:33:45 -0500 X-MC-Unique: Kqd8r88RMNacv7rPCQ94aA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7607F10059A2; Sat, 28 Nov 2020 00:33:43 +0000 (UTC) Received: from mail (ovpn-112-118.rdu2.redhat.com [10.10.112.118]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3D5A96085D; Sat, 28 Nov 2020 00:33:40 +0000 (UTC) Date: Fri, 27 Nov 2020 19:33:39 -0500 From: Andrea Arcangeli To: Matthew Wilcox Cc: Peter Xu , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Hugh Dickins , Mike Rapoport Subject: Re: [PATCH] mm: Don't fault around userfaultfd-registered regions on reads Message-ID: References: <20201126222359.8120-1-peterx@redhat.com> <20201127122224.GX4327@casper.infradead.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201127122224.GX4327@casper.infradead.org> User-Agent: Mutt/2.0.2 (2020-11-20) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hello, On Fri, Nov 27, 2020 at 12:22:24PM +0000, Matthew Wilcox wrote: > On Thu, Nov 26, 2020 at 05:23:59PM -0500, Peter Xu wrote: > > For wr-protected mode uffds, errornously fault in those pages around could lead > > to threads accessing the pages without uffd server's awareness. For example, > > when punching holes on uffd-wp registered shmem regions, we'll first try to > > unmap all the pages before evicting the page cache but without locking the > > page (please refer to shmem_fallocate(), where unmap_mapping_range() is called > > before shmem_truncate_range()). When fault-around happens near a hole being > > punched, we might errornously fault in the "holes" right before it will be > > punched. Then there's a small window before the page cache was finally > > dropped, and after the page will be writable again (NOTE: the uffd-wp protect > > information is totally lost due to the pre-unmap in shmem_fallocate(), so the > > page can be writable within the small window). That's severe data loss. > > Sounds like you have a missing page_mkwrite implementation. If the real fault happened through the pagetable (which got dropped by the hole punching), a "missing type" userfault would be delivered to userland (because the pte would be none). Userland would invoke UFFDIO_COPY with the UFFDIO_COPY_MODE_WP flag. Such flag would then map the filled shmem page (not necessarily all zero and not necessarily the old content before the hole punch) with _PAGE_RW not set and _PAGE_UFFD_WP set, so the next write would also trigger a wrprotect userfault (this is what the uffd-wp app expects). filemap_map_pages doesn't notify userland when it fills a pte and it will map again the page read-write. However filemap_map_pages isn't capable to fill a hole and to undo the hole punch, all it can do are minor faults to re-fill the ptes from a not-yet-truncated inode page. Now it would be ok if filemap_map_pages re-filled the ptes, after they were zapped the first time by fallocate, and then the fallocate would zap them again before truncating the page, but I don't see a second pte zap... there's just the below single call of unmap_mapping_range: if ((u64)unmap_end > (u64)unmap_start) unmap_mapping_range(mapping, unmap_start, 1 + unmap_end - unmap_start, 0); shmem_truncate_range(inode, offset, offset + len - 1); So looking at filemap_map_pages in shmem, I'm really wondering if the non-uffd case is correct or not. Do we end up with ptes pointing to non pagecache, so then the virtual mapping is out of sync also with read/write syscalls that will then allocate another shmem page out of sync of the ptes? If a real page fault happened instead of filemap_map_pages, the shmem_fault() would block during fallocate punch hole by checking inode->i_private, as the comment says: * So refrain from * faulting pages into the hole while it's being punched. It's not immediately clear where filemap_map_pages refrains from faulting pages into the hole while it's being punched... given it's ignoring inode->i_private. So I'm not exactly sure how shmem can safely faulted in through filemap_map_pages, without going through shmem_fault... I suppose shmem simply is unsafe to use filemap_map_pages and it'd require a specific shmem_map_pages? If only filemap_map_pages was refraining re-faulting ptes of a shmem page that is about to be truncated (whose original ptes had _PAGE_RW unset and _PAGE_UFFD_WP set) there would be no problem with the uffd interaction. So a proper shmem_map_pages could co-exist with uffd, the userfaultfd_armed check would be only an optimization but it wouldn't be required to avoid userland memory corruption? From 8c6fb1b7dde309f0c8b5666a8e13557ae46369b4 Mon Sep 17 00:00:00 2001 From: Andrea Arcangeli Date: Fri, 27 Nov 2020 19:12:44 -0500 Subject: [PATCH 1/1] shmem: stop faulting in pages without checking inode->i_private Per shmem_fault comment shmem need to "refrain from faulting pages into the hole while it's being punched" and to do so it must check inode->i_private, which filemap_map_pages won't so it's unsafe to use in shmem because it can leave ptes pointing to non-pagecache pages in shmem backed vmas. Signed-off-by: Andrea Arcangeli --- mm/shmem.c | 1 - 1 file changed, 1 deletion(-) diff --git a/mm/shmem.c b/mm/shmem.c index 8e2b35ba93ad..f6f29b3e67c6 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -3942,7 +3942,6 @@ static const struct super_operations shmem_ops = { static const struct vm_operations_struct shmem_vm_ops = { .fault = shmem_fault, - .map_pages = filemap_map_pages, #ifdef CONFIG_NUMA .set_policy = shmem_set_policy, .get_policy = shmem_get_policy, Thanks, Andrea