Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp6992671rwr; Tue, 2 May 2023 08:08:13 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7u6gRrfv7c2KHUcoboLUvK1Ptn4fmHj3gbKybZcHaPP1tj5wmJ1Boz5fgD1YW+PLD9dwG/ X-Received: by 2002:a17:903:644:b0:1ab:d16:3c8a with SMTP id kh4-20020a170903064400b001ab0d163c8amr1876425plb.6.1683040093009; Tue, 02 May 2023 08:08:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683040092; cv=none; d=google.com; s=arc-20160816; b=mxp5MSag3UE8ZhUyCSyBxc3HmHh8MSf01hI8v2csHn2XjQCjAC/+2iWQt9yUjbGnyK cpV3Liy3hou4ADGtSryuHFS/cIsEbwRObX61wR+86TdpaoD8NHjYsPJ2jP3lkhFApYGu /LQmnoZdnJim6rxA2nfxsBThe4c87PgJUOFRjQVhG1i6/OkaXLLVfSl0SnDZJlPExahU rFwHayRUM7XJj1fEUti08s/glgVFawruZQA/C/MeioiflBwoQkHWfyp1mIbC1+n9eWxC 3Mc6pkUkrKgGuC+B6ZajzfRb7OaG5RU/kkVRQC/HtVzSHlRSejxauDuSojpPuEaw67QC I7lQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:subject :organization:from:references:cc:to:content-language:user-agent :mime-version:date:message-id:dkim-signature; bh=X3VqBpjJe45pWSOeflD2EoRODS2Pv6RFfC5l84cign8=; b=mjYgRv845RZkT+n/k98dnyBQ2Vdk10AzYVjp9Br3wyuffYJiepPFeRAOAZL1sCE0q0 NHKgxTl9xaThdbrTmRn2+vwgFOJRCqpj7SFszsnpunxf4QO8YMnmo1FCYGyYpsiA4PDM BE//z19gUx4tOsYAZ6CwlZIQufHrWHN7QyElEOT2F/S30DuOqELz8CbetE26+sP3AhA9 L3z+G3IFec1fN0pasIWXijG5U32lJmUt6As1NUbpTjog6wfJAuuv8LQZ4IU2Okk3OM3P UMlsLztDBt+GaNjFuB0gTI2DykKATRIeOhrCCBrMvDwUGI9EaNnd0BfgWriFhVw0N2Rw Vs7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=MnER1ioS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id jj13-20020a170903048d00b001a04202f782si27005621plb.122.2023.05.02.08.07.42; Tue, 02 May 2023 08:08:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=MnER1ioS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S234511AbjEBPFF (ORCPT + 99 others); Tue, 2 May 2023 11:05:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58432 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234520AbjEBPFB (ORCPT ); Tue, 2 May 2023 11:05:01 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B1AB42118 for ; Tue, 2 May 2023 08:04:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1683039853; 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=X3VqBpjJe45pWSOeflD2EoRODS2Pv6RFfC5l84cign8=; b=MnER1ioSYXIHAgCzU+eAf+NYuhPfi2dZ096SfHcJ8RtH2cv8XsYymrKEm3rU/Z20npQDIl srEZT2/J/YK4LTLeMZQ9w/yYg6/cXooLJlYas7RTPWO1tcTmSRhG8mEdW+sFSvd5tCdag+ 9RifO+phxWNfeHI0QwVGfA0/JCQ7P9g= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-220-Sfo5T286Mxu0FuPU7ro_SQ-1; Tue, 02 May 2023 11:04:10 -0400 X-MC-Unique: Sfo5T286Mxu0FuPU7ro_SQ-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-3f171d201afso23944645e9.0 for ; Tue, 02 May 2023 08:04:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683039846; x=1685631846; h=content-transfer-encoding:in-reply-to:subject:organization:from :references:cc:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=X3VqBpjJe45pWSOeflD2EoRODS2Pv6RFfC5l84cign8=; b=GSBUQnGdrFkR37JiaRBEmOYD0Ndv05oHQeaslddybdI+ZHpFVhYUVWiRfooSm6Jgpi +Qu38mkZe1lN3xVyEjjcnPiTzkJ7+169+BYYquYEXoiir0puxvWhBwt9UBfCAXM3pKur utAfJ7CyNXd4Y24KNCEJoU0el0gUO1NvcM4ULaMda3FAdP+0SEun8CQrd3d3L+/IWBFN mQc59MHC8u3DRXoKvy138pHEwOxnhSnBa+5HbLM2+De/gfrcoethA3roXIs0UhrdjUy0 auJLmpA8+/HWU0TutlLAcaPxtlg/wkE2Tp+zVSI2vXsJUHsHzpUG3YTii/Xxj3/Lt50o VLbw== X-Gm-Message-State: AC+VfDx/Ls2sZ0GdamB1qIQrjQNryssGEC0zgdmpKpLrOX7BxLJK4JBO lL1sTTTpUuD0CkohGeM1ukzwI8dOE7SLsHwAJahKNOrHORVQzX+j7KTa19rLMjYr29bmF7sYOUD useRx1S7lrDZU8Xfv5cXegUI/4wxUpeOj X-Received: by 2002:a05:600c:2155:b0:3f1:733b:8aba with SMTP id v21-20020a05600c215500b003f1733b8abamr12322986wml.35.1683039846147; Tue, 02 May 2023 08:04:06 -0700 (PDT) X-Received: by 2002:a05:600c:2155:b0:3f1:733b:8aba with SMTP id v21-20020a05600c215500b003f1733b8abamr12322948wml.35.1683039845716; Tue, 02 May 2023 08:04:05 -0700 (PDT) Received: from ?IPV6:2003:cb:c700:2400:6b79:2aa:9602:7016? (p200300cbc70024006b7902aa96027016.dip0.t-ipconnect.de. [2003:cb:c700:2400:6b79:2aa:9602:7016]) by smtp.gmail.com with ESMTPSA id u5-20020a7bc045000000b003f32c9ea20fsm11140277wmc.11.2023.05.02.08.04.03 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 02 May 2023 08:04:05 -0700 (PDT) Message-ID: <1d82794a-4c12-cdc3-a868-f013bf9fe46f@redhat.com> Date: Tue, 2 May 2023 17:04:02 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 Content-Language: en-US To: 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 References: From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH v6 2/3] mm/gup: disallow FOLL_LONGTERM GUP-nonfast writing to file-backed mappings In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 02.05.23 01:11, 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 results in both data being written to a folio without writenotify, and > the folio being dirtied unexpectedly (if the caller decides to do so). > > This issue was first reported by Jan Kara [1] in 2018, where the problem > resulted in file system crashes. > > This is only relevant when the mappings are file-backed and the underlying > file system requires folio dirty tracking. File systems which do not, such > as shmem or hugetlb, are not at risk and therefore can be written to > without issue. > > Unfortunately this limitation of GUP has been present for some time and > requires future rework of the GUP API in order to provide correct write > access to such mappings. > > However, for the time being we introduce this check to prevent the most > egregious case of this occurring, use of the FOLL_LONGTERM pin. > > These mappings are considerably more likely to be written to after > folios are cleaned and thus simply must not be permitted to do so. > > This patch changes only the slow-path GUP functions, a following patch > adapts the GUP-fast path along similar lines. > > [1]:https://lore.kernel.org/linux-mm/20180103100430.GE4911@quack2.suse.cz/ > > Suggested-by: Jason Gunthorpe > Signed-off-by: Lorenzo Stoakes > Reviewed-by: John Hubbard > Reviewed-by: Mika Penttilä > Reviewed-by: Jan Kara > Reviewed-by: Jason Gunthorpe > --- > mm/gup.c | 41 ++++++++++++++++++++++++++++++++++++++++- > 1 file changed, 40 insertions(+), 1 deletion(-) > > diff --git a/mm/gup.c b/mm/gup.c > index ff689c88a357..0f09dec0906c 100644 > --- a/mm/gup.c > +++ b/mm/gup.c > @@ -959,16 +959,51 @@ static int faultin_page(struct vm_area_struct *vma, > return 0; > } > > +/* > + * 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. > + * > + * 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 results in both data being written to a folio without writenotify, and > + * the folio being dirtied unexpectedly (if the caller decides to do so). > + */ > +static bool writeable_file_mapping_allowed(struct vm_area_struct *vma, > + unsigned long gup_flags) > +{ > + /* If we aren't pinning then no problematic write can occur. */ > + if (!(gup_flags & (FOLL_GET | FOLL_PIN))) > + return true; I think we should really not look at FOLL_GET here. Just check for FOLL_PIN (as said, even FOLL_LONGTERM would be sufficient, but I understand the reasoning to keep it, although I would drop it :P ). It also better matches your comment regarding pinning ... See the comment in is_valid_gup_args() regarding "LONGTERM can only be specified when pinning". (well, there we also check that FOLL_PIN has to be set ... ;) ) > + > + /* We limit this check to the most egregious case - a long term pin. */ > + if (!(gup_flags & FOLL_LONGTERM)) > + return true; > + > + /* If the VMA requires dirty tracking then GUP will be problematic. */ > + return vma_needs_dirty_tracking(vma); ... should that be "!vma_needs_dirty_tracking(vma)" ? If the fs needs dirty tracking, it should be disallowed. Maybe that explains why it's still working for Matthew in his s390x test. ... or I am too tired and messed up :) -- Thanks, David / dhildenb