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 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 687E6C7EE25 for ; Wed, 3 May 2023 07:09:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229627AbjECHJp (ORCPT ); Wed, 3 May 2023 03:09:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52396 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229674AbjECHJl (ORCPT ); Wed, 3 May 2023 03:09:41 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 17C982736 for ; Wed, 3 May 2023 00:08:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1683097738; 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=pHJN0MwZt1Qkyg0FwWKVsL71HOfF8cRCNsnTVzqCki0=; b=D0/wd8rnEWmWlMr/XtAwPijjPgbMq3yz/a2EVkmuofSTffQGzLgpGF8mN/V2Jxn3wkdOyB 5d3gacQGLWuGX6k6KvcUmopGW4FLfyRmQHYnAiy73XmV+Kas85MRoiu6d1EQRrLTHdUvRJ rHz1AFHTLD06s9XnbCrv+pJXicnhUJs= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-7-cd9VFXBpNsGUWQcj4dKqoA-1; Wed, 03 May 2023 03:08:57 -0400 X-MC-Unique: cd9VFXBpNsGUWQcj4dKqoA-1 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-30641258c9eso141607f8f.2 for ; Wed, 03 May 2023 00:08:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683097736; x=1685689736; h=content-transfer-encoding:in-reply-to:subject:organization:from :content-language:references:cc:to:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=pHJN0MwZt1Qkyg0FwWKVsL71HOfF8cRCNsnTVzqCki0=; b=S2d/vehPdwSMeEtTG5FaYg1ljzaDKBz0OnUWChgclGdq1qBoNESjg9aNLSOP7rsWAf YD0HdqbbKtejb+oIlqU+Cj5DaZT9qOpLtZPbs1QiEfsNpYIGO7C56ComkzHtl488xvOZ 1ja7UTaLnhHjiVtoExbCSH3e6XagjTy/uYpsMZ4P14r0ebIx8wcbmJrteZs404TxjiGp eV5R+RIA9EpGRroakBylquMi5a9y3DuvOSmvJW0+LJ+loVMFx4LJi+PtKCrF1epMWoRC 93zNwq6HE77K56fae+9BXzSs43nnlyHWsczU9PIU2U0Q8YrrX0xrSP/Xi2gxJj93mRhC f2Qw== X-Gm-Message-State: AC+VfDxedV0+z+TpL3HfPcXzgijd8yehZyx5SFGyleL68gjySYlTbTlI Q6UyfGr4n57J+jB6lTpH6PaNLvV37hG69YWCFGnr+BIbEeY+im750d49JsVA4P6j6UglL3mvbT1 JuEL6Vo/xf3YStA6cHbz6v+zXKm8Qvg== X-Received: by 2002:a5d:6410:0:b0:306:3a28:f950 with SMTP id z16-20020a5d6410000000b003063a28f950mr3061894wru.7.1683097735919; Wed, 03 May 2023 00:08:55 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7NT02o8A0M+QHO4dvjy7p83osecccZDAuDP/z0mhC15FeHFbBpxcPnSHMrIpl83AzT6cM6xQ== X-Received: by 2002:a5d:6410:0:b0:306:3a28:f950 with SMTP id z16-20020a5d6410000000b003063a28f950mr3061838wru.7.1683097735470; Wed, 03 May 2023 00:08:55 -0700 (PDT) Received: from ?IPV6:2003:cb:c711:6a00:9109:6424:1804:a441? (p200300cbc7116a00910964241804a441.dip0.t-ipconnect.de. [2003:cb:c711:6a00:9109:6424:1804:a441]) by smtp.gmail.com with ESMTPSA id v2-20020a1cf702000000b003f32f013c3csm962580wmh.6.2023.05.03.00.08.52 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 03 May 2023 00:08:54 -0700 (PDT) Message-ID: <1b34e9a4-83c0-2f44-1457-dd8800b9287a@redhat.com> Date: Wed, 3 May 2023 09:08:52 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 To: Matthew Rosato , Lorenzo Stoakes , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton Cc: Jason Gunthorpe , Jens Axboe , Matthew Wilcox , Dennis Dalessandro , Leon Romanovsky , Christian Benvenuti , Nelson Escobar , Bernard Metzler , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Bjorn Topel , Magnus Karlsson , Maciej Fijalkowski , Jonathan Lemon , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Christian Brauner , Richard Cochran , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , John Fastabend , linux-fsdevel@vger.kernel.org, linux-perf-users@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, Oleg Nesterov , Jason Gunthorpe , John Hubbard , Jan Kara , "Kirill A . Shutemov" , Pavel Begunkov , Mika Penttila , Dave Chinner , Theodore Ts'o , Peter Xu , "Paul E . McKenney" , Christian Borntraeger References: <20d078c5-4ee6-18dc-d3a5-d76b6a68f64e@linux.ibm.com> Content-Language: en-US From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH v8 0/3] mm/gup: disallow GUP writing to file-backed mappings by default In-Reply-To: <20d078c5-4ee6-18dc-d3a5-d76b6a68f64e@linux.ibm.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-perf-users@vger.kernel.org On 03.05.23 02:31, Matthew Rosato wrote: > On 5/2/23 6:51 PM, Lorenzo Stoakes wrote: >> Writing to file-backed mappings which require folio dirty tracking using >> GUP is a fundamentally broken operation, as kernel write access to GUP >> mappings do not adhere to the semantics expected by a file system. >> >> A GUP caller uses the direct mapping to access the folio, which does not >> cause write notify to trigger, nor does it enforce that the caller marks >> the folio dirty. >> >> The problem arises when, after an initial write to the folio, writeback >> results in the folio being cleaned and then the caller, via the GUP >> interface, writes to the folio again. >> >> As a result of the use of this secondary, direct, mapping to the folio no >> write notify will occur, and if the caller does mark the folio dirty, this >> will be done so unexpectedly. >> >> For example, consider the following scenario:- >> >> 1. A folio is written to via GUP which write-faults the memory, notifying >> the file system and dirtying the folio. >> 2. Later, writeback is triggered, resulting in the folio being cleaned and >> the PTE being marked read-only. >> 3. The GUP caller writes to the folio, as it is mapped read/write via the >> direct mapping. >> 4. The GUP caller, now done with the page, unpins it and sets it dirty >> (though it does not have to). >> >> This change updates both the PUP FOLL_LONGTERM slow and fast APIs. As >> pin_user_pages_fast_only() does not exist, we can rely on a slightly >> imperfect whitelisting in the PUP-fast case and fall back to the slow case >> should this fail. >> >> v8: >> - Fixed typo writeable -> writable. >> - Fixed bug in writable_file_mapping_allowed() - must check combination of >> FOLL_PIN AND FOLL_LONGTERM not either/or. >> - Updated vma_needs_dirty_tracking() to include write/shared to account for >> MAP_PRIVATE mappings. >> - Move to open-coding the checks in folio_pin_allowed() so we can >> READ_ONCE() the mapping and avoid unexpected compiler loads. Rename to >> account for fact we now check flags here. >> - Disallow mapping == NULL or mapping & PAGE_MAPPING_FLAGS other than >> anon. Defer to slow path. >> - Perform GUP-fast check _after_ the lowest page table level is confirmed to >> be stable. >> - Updated comments and commit message for final patch as per Jason's >> suggestions. > > Tested again on s390 using QEMU with a memory backend file (on ext4) and vfio-pci -- This time both vfio_pin_pages_remote (which will call pin_user_pages_remote(flags | FOLL_LONGTERM)) and the pin_user_pages_fast(FOLL_WRITE | FOLL_LONGTERM) in kvm_s390_pci_aif_enable are being allowed (e.g. returning positive pin count) At least it's consistent now ;) And it might be working as expected ... In v7: * pin_user_pages_fast() succeeded * vfio_pin_pages_remote() failed But also in v7: * GUP-fast allows pinning (anonymous) pages in MAP_PRIVATE file mappings * Ordinary GUP allows pinning pages in MAP_PRIVATE file mappings In v8: * pin_user_pages_fast() succeeds * vfio_pin_pages_remote() succeeds But also in v8: * GUP-fast allows pinning (anonymous) pages in MAP_PRIVATE file mappings * Ordinary GUP allows pinning pages in MAP_PRIVATE file mappings I have to speculate, but ... could it be that you are using a private mapping? In QEMU, unfortunately, the default for memory-backend-file is "share=off" (private) ... for memory-backend-memfd it is "share=on" (shared). The default is stupid ... If you invoke QEMU manually, can you specify "share=on" for the memory-backend-file? I thought libvirt would always default to "share=on" for file mappings (everything else doesn't make much sense) ... but you might have to specify in addition to -- Thanks, David / dhildenb