From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 844A1C433E0 for ; Tue, 11 Aug 2020 20:03:12 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 3B9B620774 for ; Tue, 11 Aug 2020 20:03:12 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="E6heO1TS" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3B9B620774 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 CCA136B0002; Tue, 11 Aug 2020 16:03:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C7B176B0005; Tue, 11 Aug 2020 16:03:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B68806B0006; Tue, 11 Aug 2020 16:03:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0209.hostedemail.com [216.40.44.209]) by kanga.kvack.org (Postfix) with ESMTP id 9BBA16B0002 for ; Tue, 11 Aug 2020 16:03:11 -0400 (EDT) Received: from smtpin10.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 2AEA3582D for ; Tue, 11 Aug 2020 20:03:11 +0000 (UTC) X-FDA: 77139361782.10.can43_470d36326fe5 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin10.hostedemail.com (Postfix) with ESMTP id E5C2F16A4A4 for ; Tue, 11 Aug 2020 20:03:10 +0000 (UTC) X-HE-Tag: can43_470d36326fe5 X-Filterd-Recvd-Size: 9246 Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by imf04.hostedemail.com (Postfix) with ESMTP for ; Tue, 11 Aug 2020 20:03:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1597176189; 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=jNptQhxyuodID2c61xEau00cpAGE8qA8J2RIxddJcM0=; b=E6heO1TSVViWEKco1JJMjtHBsLVaZP/3NeNLtI+F6r0pdrbpy/DX+lvLSgch4hSP28+XlW nyv0jQChsGEvqUfFrmjqm46KsliVnzGDPsCMDII8p7epf42CoeNpgd7Hv+phVS0w4Wj35Z wNfEPSaybzUDz74xJBbQfPdB3XH7znU= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-557-5l3DndeKOLK-2ODxu8TYgA-1; Tue, 11 Aug 2020 16:02:59 -0400 X-MC-Unique: 5l3DndeKOLK-2ODxu8TYgA-1 Received: by mail-qv1-f71.google.com with SMTP id l10so10378214qvw.22 for ; Tue, 11 Aug 2020 13:02:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=jNptQhxyuodID2c61xEau00cpAGE8qA8J2RIxddJcM0=; b=ftyaegUZ+NKWXh5JVDtkHn8Nqr/pDKS5XgsicT+Q9veu1dUlUkWPhRbb8PNciah1+D MxjKTzRsK5JAg6FbdmwDGRtHV27S3e1VWGzBskMvatqEU2qXNrxMQxZ752XPbid5WySX 8rkQGlCaWsFZUsjFQpN4FYJuCM2jqHmdxzAAb5A5LYwe2YX2jBaUkAU7LGZumZkIwtzg 7ciGYhpdwm3hdj7mmM1tHtJM85zc0kts0bkQzcSigoxUU95utj7LWoWGEzXLczWdwXUZ 1xakjVMFxqtD9F+JMMYplLEMB6uKMTQ+hQsWQHBc8ISxZYIieTEJnFfpom51/I7Arakk aXkA== X-Gm-Message-State: AOAM530FxjOmTkZRzs3Oum08A22AVZx0vSig5FQ3V1jBMK7RXOocH8EQ pxoUW7TsLWPukMMBC2sCfYGYR0E2h9aHjjd6z3CR9q+o+Fr2hVcLnpsamhihErdAlnGWJiyes/l 8L8APepSYO0E= X-Received: by 2002:ad4:438e:: with SMTP id s14mr3094791qvr.18.1597176178943; Tue, 11 Aug 2020 13:02:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzI0IGhFUZG7V4UiixOy1/PB6mY5Rqwr4p0YouLirqRFRy/mbq4l43/heNwgpaVqklKCfBMKQ== X-Received: by 2002:ad4:438e:: with SMTP id s14mr3094753qvr.18.1597176178618; Tue, 11 Aug 2020 13:02:58 -0700 (PDT) Received: from xz-x1 (bras-vprn-toroon474qw-lp130-11-70-53-122-15.dsl.bell.ca. [70.53.122.15]) by smtp.gmail.com with ESMTPSA id z197sm18466855qkb.66.2020.08.11.13.02.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Aug 2020 13:02:57 -0700 (PDT) Date: Tue, 11 Aug 2020 16:02:56 -0400 From: Peter Xu To: Jann Horn Cc: Linux-MM , kernel list , Andrew Morton , Marty Mcfadden , "Maya B . Gokhale" , Andrea Arcangeli , Linus Torvalds , Christoph Hellwig , Oleg Nesterov , Kirill Shutemov , Jan Kara Subject: Re: [PATCH v3] mm/gup: Allow real explicit breaking of COW Message-ID: <20200811200256.GC6353@xz-x1> References: <20200811183950.10603-1-peterx@redhat.com> MIME-Version: 1.0 In-Reply-To: Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline X-Rspamd-Queue-Id: E5C2F16A4A4 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam01 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: On Tue, Aug 11, 2020 at 09:07:17PM +0200, Jann Horn wrote: > On Tue, Aug 11, 2020 at 8:39 PM Peter Xu wrote: > > Starting from commit 17839856fd58 ("gup: document and work around "COW can > > break either way" issue", 2020-06-02), explicit copy-on-write behavior is > > enforced for private gup pages even if it's a read-only. It is achieved by > > always passing FOLL_WRITE to emulate a write. > > > > That should fix the COW issue that we were facing, however above commit could > > also break userfaultfd-wp and applications like umapsort [1,2]. > > > > One general routine of umap-like program is: userspace library will manage page > > allocations, and it will evict the least recently used pages from memory to > > external storages (e.g., file systems). Below are the general steps to evict > > an in-memory page in the uffd service thread when the page pool is full: > > > > (1) UFFDIO_WRITEPROTECT with mode=WP on some to-be-evicted page P, so that > > further writes to page P will block (keep page P clean) > > (2) Copy page P to external storage (e.g. file system) > > (3) MADV_DONTNEED to evict page P > > > > Here step (1) makes sure that the page to dump will always be up-to-date, so > > that the page snapshot in the file system is consistent with the one that was > > in the memory. However with commit 17839856fd58, step (2) can potentially hang > > itself because e.g. if we use write() to a file system fd to dump the page > > data, that will be a translated read gup request in the file system driver to > > read the page content, then the read gup will be translated to a write gup due > > to the new enforced COW behavior. This write gup will further trigger > > handle_userfault() and hang the uffd service thread itself. > > > > I think the problem will go away too if we replace the write() to the file > > system into a memory write to a mmaped region in the userspace library, because > > normal page faults will not enforce COW, only gup is affected. However we > > cannot forbid users to use write() or any form of kernel level read gup. > > > > One solution is actually already mentioned in commit 17839856fd58, which is to > > provide an explicit BREAK_COW scemantics for enforced COW. Then we can still > > use FAULT_FLAG_WRITE to identify whether this is a "real write request" or an > > "enfornced COW (read) request". > > > > With the enforced COW, we also need to inherit UFFD_WP bit during COW because > > now COW can happen with UFFD_WP enabled (previously, it cannot). > > > > Since at it, rename the variable in __handle_mm_fault() from "dirty" to "cow" > > to better suite its functionality. > [...] > > diff --git a/mm/memory.c b/mm/memory.c > [...] > > + * copied due to enfornced COW. When it happens, we > > (typo here and in the huge_memory version) Right.. > > [...] > > diff --git a/mm/gup.c b/mm/gup.c > > index d8a33dd1430d..c33e84ab9c36 100644 > > --- a/mm/gup.c > > +++ b/mm/gup.c > > @@ -870,6 +870,8 @@ static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma, > > return -ENOENT; > > if (*flags & FOLL_WRITE) > > fault_flags |= FAULT_FLAG_WRITE; > > + if (*flags & FOLL_BREAK_COW) > > + fault_flags |= FAULT_FLAG_BREAK_COW; > > if (*flags & FOLL_REMOTE) > > fault_flags |= FAULT_FLAG_REMOTE; > > if (locked) > > @@ -1076,7 +1078,7 @@ static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm, > > } > > if (is_vm_hugetlb_page(vma)) { > > if (should_force_cow_break(vma, foll_flags)) > > - foll_flags |= FOLL_WRITE; > > + foll_flags |= FOLL_BREAK_COW; > > How does this interact with the FOLL_WRITE check in follow_page_pte()? > If we want the COW to be broken, follow_page_pte() would have to treat > FOLL_BREAK_COW similarly to FOLL_WRITE, right? Good point... I did checked follow_page_mask() that FOLL_COW will still be set correctly after applying the patch, though I forgot the FOLL_WRITE part. Does below look good to you? diff --git a/mm/gup.c b/mm/gup.c index 9d1f44b01165..f4f2a69c6fe7 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -439,7 +439,8 @@ static struct page *follow_page_pte(struct vm_area_struct *vma, } if ((flags & FOLL_NUMA) && pte_protnone(pte)) goto no_page; - if ((flags & FOLL_WRITE) && !can_follow_write_pte(pte, flags)) { + if ((flags & (FOLL_WRITE | FOLL_BREAK_COW)) && + !can_follow_write_pte(pte, flags)) { pte_unmap_unlock(ptep, ptl); return NULL; } diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 4f192efef37c..edbd42c9d576 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1340,7 +1340,8 @@ struct page *follow_trans_huge_pmd(struct vm_area_struct *vma, assert_spin_locked(pmd_lockptr(mm, pmd)); - if (flags & FOLL_WRITE && !can_follow_write_pmd(*pmd, flags)) + if (flags & (FOLL_WRITE | FOLL_BREAK_COW) && + !can_follow_write_pmd(*pmd, flags)) goto out; /* Avoid dumping huge zero page */ Thanks, -- Peter Xu