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 624A0C433EF for ; Thu, 20 Jan 2022 15:37:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C49D26B009F; Thu, 20 Jan 2022 10:37:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BD1F86B00A1; Thu, 20 Jan 2022 10:37:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A4BF36B00A2; Thu, 20 Jan 2022 10:37:06 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0218.hostedemail.com [216.40.44.218]) by kanga.kvack.org (Postfix) with ESMTP id 9066C6B009F for ; Thu, 20 Jan 2022 10:37:06 -0500 (EST) Received: from smtpin17.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 40134844FE for ; Thu, 20 Jan 2022 15:37:06 +0000 (UTC) X-FDA: 79051068852.17.623553E Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf08.hostedemail.com (Postfix) with ESMTP id 60780160044 for ; Thu, 20 Jan 2022 15:37:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=SU7pSbDiQ/H6zVLiOL36DNrrKEIKg1kB7apuo0AXxvE=; b=jawJn9LY5JlIVJ9d092vGriYv1 yOO0ukaUjEiKupPaLTChb45dzdhS1u8sBeDaz4xjuuUu67w1k34w8/q7OMT7BPiv301o/elB0pi0B LHnSGOHBonBm/tSZS6AN8Zaeo5JJK47awfBd/BE63dOF1f6fY7aROwIahbftXOMewSetA+UVN9Brv XR40Q/pAcUpOBklqOjFBQjPjqmlmTp1qWfTXQNRYZKTnWDYadzo+DTKU3390gNhmS8+2Rb0dicoiD WcgZJulDFVCB/jio9ET/rMe061WlCzzv/nHngjWNCJDnkErbv5/8MnJiH7K6hBUZCRVISoU9SRUU1 bJcgDvng==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1nAZUd-00ENXg-EK; Thu, 20 Jan 2022 15:36:55 +0000 Date: Thu, 20 Jan 2022 15:36:55 +0000 From: Matthew Wilcox To: David Hildenbrand Cc: "zhangliang (AG)" , Andrew Morton , Linux-MM , Linux Kernel Mailing List , wangzhigang17@huawei.com, Linus Torvalds Subject: Re: [PATCH] mm: reuse the unshared swapcache page in do_wp_page Message-ID: References: <172ccfbb-7e24-db21-7d84-8c8d8c3805fd@redhat.com> <9cd7eee2-91fd-ddb8-e47d-e8585e5baa05@redhat.com> <747ff31c-6c9e-df6c-f14d-c43aa1c77b4a@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 60780160044 X-Stat-Signature: w5dogyc9ak8iwqiahznw5ia9kuohnhb6 Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=jawJn9LY; dmarc=none; spf=none (imf08.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org X-HE-Tag: 1642693024-825281 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 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. > 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. > > 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.