Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp7136233rwr; Tue, 2 May 2023 09:57:29 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6/6dkWE5qKAVZaL/vqUBnu1yZTBy4N//XCaYrWtytqAQJTZkBBgP+sX5GvE3dsUVfN8v0H X-Received: by 2002:a17:903:2444:b0:1aa:f818:7a23 with SMTP id l4-20020a170903244400b001aaf8187a23mr8365807pls.27.1683046649498; Tue, 02 May 2023 09:57:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683046649; cv=none; d=google.com; s=arc-20160816; b=DzqLm6UHl8P19kRZgvA6Opjs7GEVsM1sJrAgdxuFUOzwMUZmCXNqCTdD7Uv/dbktRn GzS/GqtmzQ1K3hdyVxtI7d0POm5Z+sDlaccq8mWe0vNHEVZgs4476pfn/SWLapR77JhY LMfavrvmUOLJ8QQCo3+zSO1RIKu7MQMJXDXlJOri35qPdWVlia40w0yiL4he1WOpwufY xzIh691V6h+dIHlxshR5zAF1P4epFuU9cdK5bWY9nYkr8iVPf0Qc7xXgw05rruJRuaep JFJGQ0zXuIqKelPS+i1sypnJHPkxJcF1f+1F9SB53NdmwPvuJzBbNAJiStPkb/V2nIMW 7iTQ== 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 :organization:from:references:cc:to:content-language:subject :user-agent:mime-version:date:message-id:dkim-signature; bh=dpO0j0fuAlZuSPU9fXnXLLOnBHWx4/eBeZNb665FeGo=; b=SQpXBNlE5HDeNLzFI2kuR0h1cAWRsu0+2jwJPKKlJ7+IXiC+k7zIVsc0YGfhFmj69y KN/J/VgLwzIlZ+kZXm1X7r8ZNJ0D1CElDkMj1XNpfuf7guuw+7TvD9PL78Pj4M83agh4 PK5iEGc40Ghk3eaAzaU7sTGB8lxRJmOFyHkgeyHI2TnGQVW03XYmoD8GwQR6wIVDFDdU 5kTKky9/X3RmyUy6vTKbRlytZ/SHaa9AsZCwS+JOK8JhpA41oScIqCNrhCTzn23GPTsD bYfQjzcq5WbHz3Iq+og3RkTahO9hffeAqQ1YHiCZF3YLGwFT+vBQRca2oZ1KhMPM3Ivm AuHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=LpwUvUKT; 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-20020a170903048d00b001a92875cda9si26063529plb.288.2023.05.02.09.57.15; Tue, 02 May 2023 09:57:29 -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=LpwUvUKT; 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 S233992AbjEBQnk (ORCPT + 99 others); Tue, 2 May 2023 12:43:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234064AbjEBQni (ORCPT ); Tue, 2 May 2023 12:43:38 -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 8CA4A1FD8 for ; Tue, 2 May 2023 09:42:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1683045769; 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=dpO0j0fuAlZuSPU9fXnXLLOnBHWx4/eBeZNb665FeGo=; b=LpwUvUKTn6dkHShQyY7CiX9tkiBkYksUj0U+DwRg27ewzArZLSv2N+tapdZKPGHI662Hi9 roT0K2ZmStl8UmprQ/zM7MLD2LmzrTeJBDPhx2bG9yw5ZOgTgQ9Q222Ms1v9EMatMUDZXn I9JzbGjVfPQBNcZeyuVt5EzApPrhUfo= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-608-ejK0MScSMeCCJxQNTM3lUQ-1; Tue, 02 May 2023 12:42:48 -0400 X-MC-Unique: ejK0MScSMeCCJxQNTM3lUQ-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-3f3157128b4so110163705e9.0 for ; Tue, 02 May 2023 09:42:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683045767; x=1685637767; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=dpO0j0fuAlZuSPU9fXnXLLOnBHWx4/eBeZNb665FeGo=; b=ZBtrAeB7BxPb2CCZMg7E+XXNkrzF9zTbtRJrsl7intRJX3F5rMJ+KIsU9EqREn9D3i QHAE8gL6qh1i/pZwNdGOlkcXB1HgTml5pJHpyO0kkz3opRb/gSVhrCFf4Xv0Rjk2hCCr At+xANKbzFUXJhIw12Bg6PSNv+XKiaggU64/780HVEGAyo4tHk9DNMZxhg6m8Sql/uPu ocns/Ia/t5A8DXZtQObJNUKcSIACarFc37dh4xwVR4HCwM/aKpnR2ECEW9euO0qbdinA xlxC3qYAG3juMDKjQNs6eTbd3oMklWlgElyiODay2fIgmfnGWwHtqzV8e4BMzHiCMuz3 a5Hw== X-Gm-Message-State: AC+VfDwoHw6DyblNXNU5skSuJEqdjTchzEat+JpRlFNY54tZnX/jL6Bx 0zIdCnDU2M1V9Er5D6hghLFb1XAQDTFXg5a+/zrOjniET2fI4KMnuC/SEidjC2tAGqq1rhWsQNC 0SkbSVUJ8pt/Wf8ye0vKKgLuI X-Received: by 2002:a5d:68c5:0:b0:306:2c20:c4fb with SMTP id p5-20020a5d68c5000000b003062c20c4fbmr5554512wrw.2.1683045767273; Tue, 02 May 2023 09:42:47 -0700 (PDT) X-Received: by 2002:a5d:68c5:0:b0:306:2c20:c4fb with SMTP id p5-20020a5d68c5000000b003062c20c4fbmr5554450wrw.2.1683045766894; Tue, 02 May 2023 09:42:46 -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 u12-20020adfdd4c000000b0030635735a57sm2817098wrm.60.2023.05.02.09.42.43 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 02 May 2023 09:42:45 -0700 (PDT) Message-ID: Date: Tue, 2 May 2023 18:42:42 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 Subject: Re: [PATCH v7 2/3] mm/gup: disallow FOLL_LONGTERM GUP-nonfast writing to file-backed mappings 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 , Matthew Rosato , "Paul E . McKenney" , Christian Borntraeger References: From: David Hildenbrand Organization: Red Hat 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 18:34, 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 | 43 ++++++++++++++++++++++++++++++++++++++++++- > 1 file changed, 42 insertions(+), 1 deletion(-) > > diff --git a/mm/gup.c b/mm/gup.c > index ff689c88a357..6e209ca10967 100644 > --- a/mm/gup.c > +++ b/mm/gup.c > @@ -959,16 +959,53 @@ 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. A long term > + * pin is the most egregious case so this is the case we disallow. > + */ > + if (!(gup_flags & (FOLL_PIN | FOLL_LONGTERM))) > + return true; If you really want to keep FOLL_PIN here ... this has to be if ((gup_flags & (FOLL_PIN | FOLL_LONGTERM)) != (FOLL_PIN | FOLL_LONGTERM)) or two separate checks. Otherwise you'd also proceed if only FOLL_PIN is set. Unless my tired eyes betrayed me. -- Thanks, David / dhildenb