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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A9F8BC77B73 for ; Tue, 2 May 2023 15:15:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 499676B007D; Tue, 2 May 2023 11:15:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4540F6B007E; Tue, 2 May 2023 11:15:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3372F6B0080; Tue, 2 May 2023 11:15:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by kanga.kvack.org (Postfix) with ESMTP id 157EB6B007D for ; Tue, 2 May 2023 11:15:10 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1683040509; 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=JyJ9o/uZ5Fkt+a7cyQGvMDE4+1gb71QWVsxfUUVV2BONmSgoc7vWSqNv77v9yZXsLAyApH +Buvy0TjUWrvFI3V+lMOt9/R4NfFCShzj7S31juEpOCyg0LAkq3Blpruw+Hy5EVudbO6YE 8xiFdYtmMUoC2qDF9g9S0TEX51I6Nz4= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-196-Rjci4WxAOtKgKUk5rWDhkg-1; Tue, 02 May 2023 11:04:08 -0400 X-MC-Unique: Rjci4WxAOtKgKUk5rWDhkg-1 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-3f33f8ffa05so12724495e9.1 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=lSF7qc0/7g7OwRlCIgRqj1ugxEFQeg+Jo5u5QLETBL+VXLYO9gh8l5QOsepRCqPVbH aLldZVuhOTiz1ok6HCbTCGvbS1w+vrWrf+kg5tDC927RYNOEIarp9/HxDxIl5jO1C9Lp Z4jcuiT35GoJaNvbAmnGNqpFGWwfDdJhMKpygXsWM6gHCH+U7rt7q0GWqTIsVS53/qg/ 5JPPnShMOjPNAZUlIQSnPGuNDE7ss4o9DEL+yn4P7i3MIPKVwqzDadSvpMTi+Dx0vLcW NHsFMTur8HmJ0Y6Kufba00Q23h+HwfVRV34j0qz0muf2Syl/AmYY08FIuLkLz7P1+my5 d5qw== X-Gm-Message-State: AC+VfDx7qICM3XRn33BoeXzFFTUNfrKF9BAbCwEeyEC0sJo98bFmsXEj P2IlHrrJ5Hv8W8v5c64Rq3LLuZld5S8BgVXdr60fBm8g54fI182LrHDU+lgSjwxwAMm5Zg4yOmV HWfILR4uJFRk= X-Received: by 2002:a05:600c:2155:b0:3f1:733b:8aba with SMTP id v21-20020a05600c215500b003f1733b8abamr12322987wml.35.1683039846147; Tue, 02 May 2023 08:04:06 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7fR4SIsKy+pjVJlia2+ofkHkmOsp+3kyFCIl6F1FN9R8Gs5f1OzLPjrCnv9OT8frSEFeaEqg== 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 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: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit 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 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