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 0C1ECC433EF for ; Thu, 20 Jan 2022 15:40:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 828766B00A3; Thu, 20 Jan 2022 10:40:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7D7606B00A5; Thu, 20 Jan 2022 10:40:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6774D6B00A6; Thu, 20 Jan 2022 10:40:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0189.hostedemail.com [216.40.44.189]) by kanga.kvack.org (Postfix) with ESMTP id 543F96B00A3 for ; Thu, 20 Jan 2022 10:40:00 -0500 (EST) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 1805B181CA087 for ; Thu, 20 Jan 2022 15:40:00 +0000 (UTC) X-FDA: 79051076160.12.BC8843E Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf10.hostedemail.com (Postfix) with ESMTP id 891F4C0062 for ; Thu, 20 Jan 2022 15:39:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1642693199; 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=8iqaG7Lc+VchV66bNoQHn2xE3BtOzlPVk9h6F+EPx9w=; b=dQSiwUL2P6q/3Ncc0i6N7HLqF54uIu45kemOqtU96wSg7+1u8H0Ys3G9BdepXweUG9IjiZ TmGy50od8x36Ku4TUeClY9oTzp0wRdJEU3pieaUpvaAc7JlSz2t6ZkUyjet9F9uApnS+3w w1G2Ug2om+LTDpO1F0kQk6nbYvq+oe0= Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-452-lqkIHribNM2-d6N4btuE1A-1; Thu, 20 Jan 2022 10:39:57 -0500 X-MC-Unique: lqkIHribNM2-d6N4btuE1A-1 Received: by mail-ed1-f70.google.com with SMTP id ee53-20020a056402293500b004022f34edcbso6253770edb.11 for ; Thu, 20 Jan 2022 07:39:57 -0800 (PST) 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=8iqaG7Lc+VchV66bNoQHn2xE3BtOzlPVk9h6F+EPx9w=; b=dBgVdR5wA8mlljggEv/D099Yp8SREPOOyHU9OiTrhQjp6MvA9euD0V45XGEXUjuyww xdDGW1lXIxItSmd1OmqPL2JtYdqcGdOAxFV0IZBB92H8T/tMIOn90r8u0ArQrBCbqLKP ycOYEiaXFAiYLan/g4J5fYiwUZfmE8yPS80dhCoMTjhnAS868o7OX+1BuGHBt01CQeZP 8obWlZLEwyfopuqYDPNuGhXlMx7z55IDN5TSJ4eFGqR+kL0i2oqbqT1LMR0JTOmzWyy5 rF/sykNS/ej4wxgCynibTF9tV7NnV2cExclFJoDnZFnyNn2qiE3NbL2YFVnKmSdDcgL1 i/WQ== X-Gm-Message-State: AOAM531Q3YIQN6Zndmpf/VHncai5x5t4wzFVep4M/rvoUC4vxPJosLDa UwIir7qsA4hjM5E9lHxQwC9OKCyTTPrAhisls3MqPbQ8PjhsaBh2ToycOnonJo7jJl6J1vnMlVL /ZX/86G6W6B4= X-Received: by 2002:a17:906:4453:: with SMTP id i19mr28809529ejp.305.1642693196528; Thu, 20 Jan 2022 07:39:56 -0800 (PST) X-Google-Smtp-Source: ABdhPJzha5k25rT2Htr7L0NG19u/lndf6crtI76bzYEx2SL35kc/iOccj2GEZbXytDaFkqL8JgLq+w== X-Received: by 2002:a17:906:4453:: with SMTP id i19mr28809509ejp.305.1642693196269; Thu, 20 Jan 2022 07:39:56 -0800 (PST) Received: from ?IPV6:2003:cb:c70e:5800:eeb:dae2:b1c0:f5d1? (p200300cbc70e58000eebdae2b1c0f5d1.dip0.t-ipconnect.de. [2003:cb:c70e:5800:eeb:dae2:b1c0:f5d1]) by smtp.gmail.com with ESMTPSA id k4sm1011334edo.55.2022.01.20.07.39.55 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 20 Jan 2022 07:39:55 -0800 (PST) Message-ID: <759f9bc8-0b10-7f0f-28a6-f292bed9053f@redhat.com> Date: Thu, 20 Jan 2022 16:39:55 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.4.0 Subject: Re: [PATCH] mm: reuse the unshared swapcache page in do_wp_page To: Matthew Wilcox Cc: "zhangliang (AG)" , Andrew Morton , Linux-MM , Linux Kernel Mailing List , wangzhigang17@huawei.com, Linus Torvalds References: <172ccfbb-7e24-db21-7d84-8c8d8c3805fd@redhat.com> <9cd7eee2-91fd-ddb8-e47d-e8585e5baa05@redhat.com> <747ff31c-6c9e-df6c-f14d-c43aa1c77b4a@redhat.com> From: David Hildenbrand Organization: Red Hat In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 891F4C0062 X-Stat-Signature: tqkigco8zzp39rtemdttfmpc7f45yk8f Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=dQSiwUL2; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf10.hostedemail.com: domain of david@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=david@redhat.com X-Rspamd-Server: rspam08 X-HE-Tag: 1642693199-324709 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 20.01.22 16:36, Matthew Wilcox wrote: > On Thu, Jan 20, 2022 at 04:26:22PM +0100, David Hildenbrand wrote: >> On 20.01.22 15:39, Matthew Wilcox wrote: >>> On Thu, Jan 20, 2022 at 03:15:37PM +0100, David Hildenbrand wrote: >>>> On 17.01.22 14:31, zhangliang (AG) wrote: >>>>> Sure, I will do that :) >>>> >>>> I'm polishing up / testing the patches and might send something out for discussion shortly. >>>> Just a note that on my branch was a version with a wrong condition that should have been fixed now. >>>> >>>> I am still thinking about PTE mapped THP. For these, we'll always >>>> have page_count() > 1, essentially corresponding to the number of still-mapped sub-pages. >>>> >>>> So if we end up with a R/O mapped part of a THP, we'll always have to COW and cannot reuse ever, >>>> although it's really just a single process mapping the THP via PTEs. >>>> >>>> One approach would be to scan the currently locked page table for entries mapping >>>> this same page. If page_count() corresponds to that value, we know that only we are >>>> mapping the THP and there are no additional references. That would be a special case >>>> if we find an anon THP in do_wp_page(). Hm. >>> >>> You're starting to optimise for some pretty weird cases at that point. >> >> So your claim is that read-only, PTE mapped pages are weird? How do you >> come to that conclusion? > > Because normally anon THP pages are PMD mapped. That's rather > the point of anon THPs. For example unless we are talking about *drumroll* COW handling. > >> If we adjust the THP reuse logic to split on additional references >> (page_count() == 1) -- similarly as suggested by Linus to fix the CVE -- >> we're going to end up with exactly that more frequently. > > I don't understand. Are we talking past each other? As I understand > the situation we're talking about here, a process has created a THP, > done something to cause it to be partially mapped (or mapped in a > misaligned way) in its own address space, then forked, and we're > trying to figure out if it's safe to reuse it? I say that situation is > rare enough that it's OK to always allocate an order-0 page and > copy into it. Yes, we are talking past each other and no, I am talking about fully mapped THP, just mapped via PTEs. Please refer to our THP COW logic: do_huge_pmd_wp_page() > >>> Anon THP is always going to start out aligned (and can be moved by >>> mremap()). Arguably it should be broken up if it's moved so it can be >>> reformed into aligned THPs by khugepaged. >> >> Can you elaborate, I'm missing the point where something gets moved. I >> don't care about mremap() at all here. >> >> >> 1. You have a read-only, PTE mapped THP >> 2. Write fault on the THP >> 3. We PTE-map the THP because we run into a false positive in our COW >> logic to handle COW on PTE >> 4. Write fault on the PTE >> 5. We always have to COW each and every sub-page and can never reuse, >> because page_count() > 1 >> >> That's essentially what reuse_swap_page() tried to handle before. >> Eventually optimizing for this is certainly the next step, but I'd like >> to document which effect the removal of reuse_swap_page() will have to THP. > > I'm talking about step 0. How do we get a read-only, PTE-mapped THP? > Through mremap() or perhaps through an mprotect()/mmap()/munmap() that > failed to split the THP. do_huge_pmd_wp_page() -- Thanks, David / dhildenb