Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp779357pxb; Fri, 22 Apr 2022 10:59:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx7xkbOCr8aW0y0NROn83oJ6w5QhwXnWA2RZ2Kv5/EVy1rM7QD0xMk6qsAQkHnO4Da0TU7d X-Received: by 2002:a17:903:1251:b0:156:9d8e:1077 with SMTP id u17-20020a170903125100b001569d8e1077mr5624225plh.116.1650650373885; Fri, 22 Apr 2022 10:59:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650650373; cv=none; d=google.com; s=arc-20160816; b=PJW2mZbrG9lNGUgCQ1X4DPdrJ9flrklHrh+ieT4iKxTvPCdq/60wC/xmPA3pNZo0/c zMb12F9EoVurleRh7EVvTFC3UPvFlL+gILMPdPu8z7g9dkaPnx9vqOD90OnSdBuSQrg/ rLIYPnZCezYeJeb5VmeGBUH9oqIuohutzuXnNPjzL3lCvJoSaxcL7pGCiVbOU2jCXsCN Ti+E4RpeUYS8VmnYjO1KSykCYMsg9Oi5lmrHTp5+CL1AXe+XtoIL5s0o55Ce7hRbW50O lW5VgnDqUznXIWUoSdZnUy+gvyuUOhJX58ZObvNI163kyxmwwADFj/hxI5rjI+Z0eH4z j16g== 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=YUXfwOQompnJdIwVAr+ky90k3csgxSJYtFYxvhspJD8=; b=oDv/qaEcVgeVxa2Q33zbLpoBwrwHgrt+kaeIaRZNtzXFJJ43Ojx9BbSLa+z5Rkn2M0 yEB7Qiigx/ZDGPRa0uED2mOE2HZZY+I7GYEBLVqBm8mzx6IN+Ibsh0GaAmmiEqHLWvDa vMPe1eXa7yULSoDUIbLt1ftK3r22hQOGbZVdkUgf4igBsD9+9kfzhcy5YgBqWb9cUcmB DW5bTwuoL+NZ5sfZYSWsNdRjjHVFLPnHqqndP+aSKNtdO5E8u/xAii52MeSYLcwGhQ4r EAyP+K1fyFGi3Nw0BIk7DORcotucg1MWDAQqSLIXTMjjhxSRHwioZuxD6PqnnctDElNv yq1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=jBl1gmg6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id j13-20020a170903024d00b00155f7af1822si9384901plh.484.2022.04.22.10.59.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Apr 2022 10:59:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=jBl1gmg6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D845710FF36; Fri, 22 Apr 2022 10:41:02 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1380986AbiDTRQf (ORCPT + 99 others); Wed, 20 Apr 2022 13:16:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33524 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236702AbiDTRQe (ORCPT ); Wed, 20 Apr 2022 13:16:34 -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 ESMTP id AAFBF457A9 for ; Wed, 20 Apr 2022 10:13:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1650474826; 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=YUXfwOQompnJdIwVAr+ky90k3csgxSJYtFYxvhspJD8=; b=jBl1gmg6wPwLvVy+n5LEz1cBblL+6eeTmypLE3g+NlFdcUvl/JOa1EPCaFqBe0yCLCzuZq ay8nC4fqegacmTvzywG8D1tgPny6+mzYNpOamAhkR4xvjPSskD4zORFWurr4tjLQEVwzc+ GWb8sH1K84KTed4ewb07KESHBQ5YDJk= 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.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-417-s8hxY59HP1KhFjzehQSw4w-1; Wed, 20 Apr 2022 13:13:45 -0400 X-MC-Unique: s8hxY59HP1KhFjzehQSw4w-1 Received: by mail-wm1-f70.google.com with SMTP id t2-20020a7bc3c2000000b003528fe59cb9so1222107wmj.5 for ; Wed, 20 Apr 2022 10:13:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:organization:in-reply-to :content-transfer-encoding; bh=YUXfwOQompnJdIwVAr+ky90k3csgxSJYtFYxvhspJD8=; b=S6iF1PotjeaRCsk7846OhoojX0yBG9BdKc5WLA5FGTbeQxhfhTMEcGlISLz6/2s+Xb NVfko7g79mD+jebwf8zRW3t6z9mB2c2BpWaPSDHijelsMKyQQ+adIrZCISOfch1Lt5A9 egdTVLJA37tbYqt+4JNmQQxMOZD8E3qrIwtFK4SG/t4f9dWsC8fA6upo41uSd/o2qSPG ZqDyyZ6t4Z2M6Z2JemEWKnrIgeag7E1smISNFadfNyajkc3OEUhfrzEaTnKqKTcdfn0u pP+KnpMSBu8FvLSZYPyC3qNtoxO4ebUnyZ5iPgoul7uXg3ZqLtFJm+2nnLB4CkPU2UlQ G5Sg== X-Gm-Message-State: AOAM531QW0phYFRp5relCAWD4l8plGOvX5wt7Y+ujJnBTsQe2c9yEXVh fcOZNy66o7nC647HEkz9k1ymSDjx9zRnxVhe8/H8/Km3zJ6u1so75XgPokD/XJp54mRp4hXyg7A nyWMOWp7vJv0xZmBeR9U0WNQE X-Received: by 2002:a1c:21c5:0:b0:38e:b464:6a39 with SMTP id h188-20020a1c21c5000000b0038eb4646a39mr4624133wmh.186.1650474824361; Wed, 20 Apr 2022 10:13:44 -0700 (PDT) X-Received: by 2002:a1c:21c5:0:b0:38e:b464:6a39 with SMTP id h188-20020a1c21c5000000b0038eb4646a39mr4624108wmh.186.1650474824060; Wed, 20 Apr 2022 10:13:44 -0700 (PDT) Received: from ?IPV6:2003:cb:c702:3d00:23e4:4c84:67a5:3ccf? (p200300cbc7023d0023e44c8467a53ccf.dip0.t-ipconnect.de. [2003:cb:c702:3d00:23e4:4c84:67a5:3ccf]) by smtp.gmail.com with ESMTPSA id q16-20020adff950000000b00205aa05fa03sm313602wrr.58.2022.04.20.10.13.41 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 20 Apr 2022 10:13:43 -0700 (PDT) Message-ID: Date: Wed, 20 Apr 2022 19:13:40 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.6.2 Subject: Re: [PATCH v2 1/8] mm/swap: remember PG_anon_exclusive via a swp pte bit Content-Language: en-US To: Vlastimil Babka , linux-kernel@vger.kernel.org Cc: Andrew Morton , Hugh Dickins , Linus Torvalds , David Rientjes , Shakeel Butt , John Hubbard , Jason Gunthorpe , Mike Kravetz , Mike Rapoport , Yang Shi , "Kirill A . Shutemov" , Matthew Wilcox , Jann Horn , Michal Hocko , Nadav Amit , Rik van Riel , Roman Gushchin , Andrea Arcangeli , Peter Xu , Donald Dutile , Christoph Hellwig , Oleg Nesterov , Jan Kara , Liang Zhang , Pedro Gomes , Oded Gabbay , Catalin Marinas , Will Deacon , Michael Ellerman , Benjamin Herrenschmidt , Paul Mackerras , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Gerald Schaefer , linux-mm@kvack.org, x86@kernel.org, linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org References: <20220329164329.208407-1-david@redhat.com> <20220329164329.208407-2-david@redhat.com> From: David Hildenbrand Organization: Red Hat In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,RDNS_NONE,SPF_HELO_NONE 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 20.04.22 19:10, Vlastimil Babka wrote: > On 3/29/22 18:43, David Hildenbrand wrote: >> Currently, we clear PG_anon_exclusive in try_to_unmap() and forget about >> it. We do this, to keep fork() logic on swap entries easy and efficient: >> for example, if we wouldn't clear it when unmapping, we'd have to lookup >> the page in the swapcache for each and every swap entry during fork() and >> clear PG_anon_exclusive if set. >> >> Instead, we want to store that information directly in the swap pte, >> protected by the page table lock, similarly to how we handle >> SWP_MIGRATION_READ_EXCLUSIVE for migration entries. However, for actual >> swap entries, we don't want to mess with the swap type (e.g., still one >> bit) because it overcomplicates swap code. >> >> In try_to_unmap(), we already reject to unmap in case the page might be >> pinned, because we must not lose PG_anon_exclusive on pinned pages ever. >> Checking if there are other unexpected references reliably *before* >> completely unmapping a page is unfortunately not really possible: THP >> heavily overcomplicate the situation. Once fully unmapped it's easier -- >> we, for example, make sure that there are no unexpected references >> *after* unmapping a page before starting writeback on that page. >> >> So, we currently might end up unmapping a page and clearing >> PG_anon_exclusive if that page has additional references, for example, >> due to a FOLL_GET. >> >> do_swap_page() has to re-determine if a page is exclusive, which will >> easily fail if there are other references on a page, most prominently >> GUP references via FOLL_GET. This can currently result in memory >> corruptions when taking a FOLL_GET | FOLL_WRITE reference on a page even >> when fork() is never involved: try_to_unmap() will succeed, and when >> refaulting the page, it cannot be marked exclusive and will get replaced >> by a copy in the page tables on the next write access, resulting in writes >> via the GUP reference to the page being lost. >> >> In an ideal world, everybody that uses GUP and wants to modify page >> content, such as O_DIRECT, would properly use FOLL_PIN. However, that >> conversion will take a while. It's easier to fix what used to work in the >> past (FOLL_GET | FOLL_WRITE) remembering PG_anon_exclusive. In addition, >> by remembering PG_anon_exclusive we can further reduce unnecessary COW >> in some cases, so it's the natural thing to do. >> >> So let's transfer the PG_anon_exclusive information to the swap pte and >> store it via an architecture-dependant pte bit; use that information when >> restoring the swap pte in do_swap_page() and unuse_pte(). During fork(), we >> simply have to clear the pte bit and are done. >> >> Of course, there is one corner case to handle: swap backends that don't >> support concurrent page modifications while the page is under writeback. >> Special case these, and drop the exclusive marker. Add a comment why that >> is just fine (also, reuse_swap_page() would have done the same in the >> past). >> >> In the future, we'll hopefully have all architectures support >> __HAVE_ARCH_PTE_SWP_EXCLUSIVE, such that we can get rid of the empty >> stubs and the define completely. Then, we can also convert >> SWP_MIGRATION_READ_EXCLUSIVE. For architectures it's fairly easy to >> support: either simply use a yet unused pte bit that can be used for swap >> entries, steal one from the arch type bits if they exceed 5, or steal one >> from the offset bits. >> >> Note: R/O FOLL_GET references were never really reliable, especially >> when taking one on a shared page and then writing to the page (e.g., GUP >> after fork()). FOLL_GET, including R/W references, were never really >> reliable once fork was involved (e.g., GUP before fork(), >> GUP during fork()). KSM steps back in case it stumbles over unexpected >> references and is, therefore, fine. >> >> Signed-off-by: David Hildenbrand > > With the fixup as reportedy by Miaohe Lin > > Acked-by: Vlastimil Babka > > (sent a separate mm-commits mail to inquire about the fix going missing from > mmotm) > > https://lore.kernel.org/mm-commits/c3195d8a-2931-0749-973a-1d04e4baec94@suse.cz/T/#m4e98ccae6f747e11f45e4d0726427ba2fef740eb Yes I saw that, thanks for catching that! -- Thanks, David / dhildenb