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=-5.5 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 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 EC6EFC5517A for ; Thu, 22 Oct 2020 19:58:20 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 2E20D2417D for ; Thu, 22 Oct 2020 19:58:19 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=nvidia.com header.i=@nvidia.com header.b="bs3xt+dz" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2E20D2417D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=nvidia.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 5D67C6B005D; Thu, 22 Oct 2020 15:58:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 560BF6B0062; Thu, 22 Oct 2020 15:58:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 428296B0068; Thu, 22 Oct 2020 15:58:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0079.hostedemail.com [216.40.44.79]) by kanga.kvack.org (Postfix) with ESMTP id 12EEE6B005D for ; Thu, 22 Oct 2020 15:58:19 -0400 (EDT) Received: from smtpin10.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id ACD6E1EE6 for ; Thu, 22 Oct 2020 19:58:18 +0000 (UTC) X-FDA: 77400623076.10.north72_2b12a0d27253 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin10.hostedemail.com (Postfix) with ESMTP id 6F81916A4B1 for ; Thu, 22 Oct 2020 19:58:18 +0000 (UTC) X-HE-Tag: north72_2b12a0d27253 X-Filterd-Recvd-Size: 5124 Received: from hqnvemgate24.nvidia.com (hqnvemgate24.nvidia.com [216.228.121.143]) by imf32.hostedemail.com (Postfix) with ESMTP for ; Thu, 22 Oct 2020 19:58:17 +0000 (UTC) Received: from hqmail.nvidia.com (Not Verified[216.228.121.13]) by hqnvemgate24.nvidia.com (using TLS: TLSv1.2, AES256-SHA) id ; Thu, 22 Oct 2020 12:56:42 -0700 Received: from [10.2.54.36] (10.124.1.5) by HQMAIL107.nvidia.com (172.20.187.13) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Thu, 22 Oct 2020 19:58:14 +0000 Subject: Re: [RFCv2 08/16] KVM: Use GUP instead of copy_from/to_user() to access guest memory To: Matthew Wilcox CC: "Kirill A. Shutemov" , Dave Hansen , Andy Lutomirski , "Peter Zijlstra" , Paolo Bonzini , "Sean Christopherson" , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , David Rientjes , Andrea Arcangeli , Kees Cook , Will Drewry , "Edgecombe, Rick P" , "Kleen, Andi" , "Liran Alon" , Mike Rapoport , , , , , "Kirill A. Shutemov" References: <20201020061859.18385-1-kirill.shutemov@linux.intel.com> <20201020061859.18385-9-kirill.shutemov@linux.intel.com> <20201022114946.GR20115@casper.infradead.org> From: John Hubbard Message-ID: <30ce6691-fd70-76a2-8b61-86d207c88713@nvidia.com> Date: Thu, 22 Oct 2020 12:58:14 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.12.0 MIME-Version: 1.0 In-Reply-To: <20201022114946.GR20115@casper.infradead.org> Content-Type: text/plain; charset="utf-8"; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.124.1.5] X-ClientProxiedBy: HQMAIL105.nvidia.com (172.20.187.12) To HQMAIL107.nvidia.com (172.20.187.13) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1603396602; bh=SDDfdPapwaAGPN6G5NGvPgR723v4Q3ngUYge890SCck=; h=Subject:To:CC:References:From:Message-ID:Date:User-Agent: MIME-Version:In-Reply-To:Content-Type:Content-Language: Content-Transfer-Encoding:X-Originating-IP:X-ClientProxiedBy; b=bs3xt+dzMIEc1iPoqfF/PiRE0RAm4lrjLAAOpczF8TnctMMyV4Q6U4C16Gw/BxU00 7D0s51rhGaNO1lWwWg1UNN5wmXuNlG2QNHfTbbZxkpe8Yd8/FwKxpdss1g1AZ4bReG /Lg5LHvimXwF5n3bsNikgJ8lN6fbTTyz4NqL2mcGXn41IoQ2lmRaqx1bp+/171WoDZ b+F+ihCsmrVAtPDqwRPIj0cNvYeQ1uMbGVK00N+kUXU/mdTBBViG6uWtt+N5kwf53P xVAASH+PC4G9wiuXgn3Rf/c0FTJFfb0x1cK1SXN38pgZeEQwQth9jFXkRQj3NJUxOR nMrdMYnlmyBbg== 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 10/22/20 4:49 AM, Matthew Wilcox wrote: > On Tue, Oct 20, 2020 at 01:25:59AM -0700, John Hubbard wrote: >> Should copy_to_guest() use pin_user_pages_unlocked() instead of gup_unlocked? >> We wrote a "Case 5" in Documentation/core-api/pin_user_pages.rst, just for this >> situation, I think: >> >> >> CASE 5: Pinning in order to write to the data within the page >> ------------------------------------------------------------- >> Even though neither DMA nor Direct IO is involved, just a simple case of "pin, >> write to a page's data, unpin" can cause a problem. Case 5 may be considered a >> superset of Case 1, plus Case 2, plus anything that invokes that pattern. In >> other words, if the code is neither Case 1 nor Case 2, it may still require >> FOLL_PIN, for patterns like this: >> >> Correct (uses FOLL_PIN calls): >> pin_user_pages() >> write to the data within the pages >> unpin_user_pages() > > Case 5 is crap though. That bug should have been fixed by getting > the locking right. ie: > > get_user_pages_fast(); > lock_page(); > kmap(); > set_bit(); > kunmap(); > set_page_dirty() > unlock_page(); > > I should have vetoed that patch at the time, but I was busy with other things. > It does seem like lock_page() is better, for now at least, because it forces the kind of synchronization with file system writeback that is still yet to be implemented for pin_user_pages(). Long term though, Case 5 provides an alternative way to do this pattern--without using lock_page(). Also, note that Case 5, *in general*, need not be done page-at-a-time, unlike the lock_page() approach. Therefore, Case 5 might potentially help at some call sites, either for deadlock avoidance or performance improvements. In other words, once the other half of the pin_user_pages() plan is implemented, either of these approaches should work. Or, are you thinking that there is never a situation in which Case 5 is valid? thanks, -- John Hubbard NVIDIA