Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp5547268pxb; Wed, 26 Jan 2022 14:42:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJyHfLbNM+mrep5ea/qKgpFG2sKfW9w4HQNwEbWNhvyZ8NBemAEPUWNT/FePi82rx+h+lEbe X-Received: by 2002:a17:907:7e9f:: with SMTP id qb31mr746277ejc.468.1643236957871; Wed, 26 Jan 2022 14:42:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643236957; cv=none; d=google.com; s=arc-20160816; b=AkmSK+0GShOiv/9gq88lL95uf4Qug0Mryt8gNx+6MUOO1Ni3MJD7L3BsrW9FAkngFm g5O/d4knFt5kKACcWgmarJv2F0Jh4YMwFy52lMDtD4U30AfWEmgAidRVjxJIxg8J50FQ DLCtCUnMcyqZl3Nb0iOliVuh5I7hSn6Kehpb8Qx84vPcyoT6GBjzRTXFNi127GyTTesC CahVlhUXUgfhDv3GFCtV924RvmfM7Yelf2WEmb/bsQ1LNQ3FlgT2w/kVGvYiF9Unh9oo QXCBQXQZIAjNQXsUVeB7Ro7b0eQQPhzoNEX03/r65phnWdGc4XuPENegsbDovxRbtM7z Liow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=wHSZGcqASjvkDHfsWnxtZXfgdyFpTFjH+EicTP6DUw0=; b=Srl8aJd+nM1wLcYYltYbS60kytn6FpuIFkzRTJEpnecWqZaaHRTPSZOG6bjEaebk8/ 3kH8xKOjyl1wfgFNiFHIykKPUffDxkkrvLn0tyK2eiMuYPFWdWyBCxS4j2qhMCUokC6N ePLB8OzZ6270bpMrU4zpZ8+fgramQjIcyvTd54+sGHdFYrysIG9TMwCSG5xcj/VdUbkm 8XxZYJSp9ga7qUnJvI6hU/sWUVpB58Pe96O+prji6A6Vwnjuc+JfFmV/2ordYj8OIHR9 lDySxx8+zS6QPnCPwWRNfM/DNJNEonYL5UNcRS6VHsOO6coalLuLg7GAOAjbhVnpSU0S 1WCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=A1glERp8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ga2si299094ejc.987.2022.01.26.14.42.12; Wed, 26 Jan 2022 14:42:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=A1glERp8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230316AbiAZUgS (ORCPT + 99 others); Wed, 26 Jan 2022 15:36:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230181AbiAZUgR (ORCPT ); Wed, 26 Jan 2022 15:36:17 -0500 Received: from mail-ed1-x52f.google.com (mail-ed1-x52f.google.com [IPv6:2a00:1450:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0DD2FC06161C for ; Wed, 26 Jan 2022 12:36:17 -0800 (PST) Received: by mail-ed1-x52f.google.com with SMTP id c24so821819edy.4 for ; Wed, 26 Jan 2022 12:36:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=wHSZGcqASjvkDHfsWnxtZXfgdyFpTFjH+EicTP6DUw0=; b=A1glERp8hCYBWE0s6Te6kySPMXGfZTwbtiiz/dCOcPbUse+bVmCxGmO4uUTeZd8knA H2yxCCBdKS3zQpXpvS/87lgCI+WV/PNj8cBYIwQwrkgBHtMOd5M148yu51TuuHBzZ/Vg VIFu5s+0RUbBhbQDZHU4DwQe9XC9QU6NnKugmw2mV+R+Fnl+9Q+668FpCFjnGilMYEAz kPl384LKwJmW/sVXQDzzJftBLxl30IEzGCYMsPCHQv02Cql+xoXrQ3KMtroJu79sqUGt 8kS96HN66s/g/Dl3IgrFaMw2GoKwflqumvPjALvV54g2jcpLLVPH1F+Jj7razSbPCqkQ 80Rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=wHSZGcqASjvkDHfsWnxtZXfgdyFpTFjH+EicTP6DUw0=; b=ZTd9XHoUpD3UNvM5hqkjbn+VVKF5342Vs8SeJfklnGrXy/jiBIif5b8qmi6Tjz3APJ CC/kkZXjIREdTFCPUpq/z8BHuGfYFgvgL/1ukm44M9B0i7Nxr40D0H1EivWATL4VjGIV r6Apyi9ctU+UwbqkSc5HLNE+cwJ7PtVmprju4H5w9ceUobywaRarA4dgh2VDcoc8Omp5 zkX3W+B1QZKoMIYP5No/d62RsdWEVB5c9VH9NwQsPI+OJL1IXVZiYv3xq6Yuez+sI5y9 2S95MAlCUHUC3rAvf0lV6q3xw/r+PB33b5i1mUfwaNy8mWcUe3g6NwZtxlkK9R9OAQ4D gMuQ== X-Gm-Message-State: AOAM533lXbQDfVwOkEYgq6EAOVsC5xxNomyvl9+e+hNs/5D2DdORqyHy tZ9/IqszIGoDBWYUxcoJXwUukT7MB01NIJCf7H8= X-Received: by 2002:aa7:df16:: with SMTP id c22mr658273edy.177.1643229375413; Wed, 26 Jan 2022 12:36:15 -0800 (PST) MIME-Version: 1.0 References: <20220126095557.32392-1-david@redhat.com> <20220126095557.32392-6-david@redhat.com> In-Reply-To: <20220126095557.32392-6-david@redhat.com> From: Yang Shi Date: Wed, 26 Jan 2022 12:36:03 -0800 Message-ID: Subject: Re: [PATCH RFC v2 5/9] mm/huge_memory: streamline COW logic in do_huge_pmd_wp_page() To: David Hildenbrand Cc: Linux Kernel Mailing List , Andrew Morton , Hugh Dickins , Linus Torvalds , David Rientjes , Shakeel Butt , John Hubbard , Jason Gunthorpe , Mike Kravetz , Mike Rapoport , "Kirill A . Shutemov" , Matthew Wilcox , Vlastimil Babka , 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 , Linux MM Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jan 26, 2022 at 2:00 AM David Hildenbrand wrote: > > We currently have a different COW logic for anon THP than we have for > ordinary anon pages in do_wp_page(): the effect is that the issue reported > in CVE-2020-29374 is currently still possible for anon THP: an unintended > information leak from the parent to the child. > > Let's apply the same logic (page_count() == 1), with similar > optimizations to remove additional references first as we really want to > avoid PTE-mapping the THP and copying individual pages best we can. > > If we end up with a page that has page_count() != 1, we'll have to PTE-map > the THP and fallback to do_wp_page(), which will always copy the page. > > Note that KSM does not apply to THP. > > I. Interaction with the swapcache and writeback > > While a THP is in the swapcache, the swapcache holds one reference on each > subpage of the THP. So with PageSwapCache() set, we expect as many > additional references as we have subpages. If we manage to remove the > THP from the swapcache, all these references will be gone. > > Usually, a THP is not split when entered into the swapcache and stays a > compound page. However, try_to_unmap() will PTE-map the THP and use PTE > swap entries. There are no PMD swap entries for that purpose, consequently, > we always only swapin subpages into PTEs. > > Removing a page from the swapcache can fail either when there are remaining > swap entries (in which case COW is the right thing to do) or if the page is > currently under writeback. > > Having a locked, R/O PMD-mapped THP that is in the swapcache seems to be > possible only in corner cases, for example, if try_to_unmap() failed > after adding the page to the swapcache. However, it's comparatively easy to > handle. > > As we have to fully unmap a THP before starting writeback, and swapin is > always done on the PTE level, we shouldn't find a R/O PMD-mapped THP in the > swapcache that is under writeback. This should at least leave writeback > out of the picture. > > II. Interaction with GUP references > > Having a R/O PMD-mapped THP with GUP references (i.e., R/O references) > will result in PTE-mapping the THP on a write fault. Similar to ordinary > anon pages, do_wp_page() will have to copy sub-pages and result in a > disconnect between the GUP references and the pages actually mapped into > the page tables. To improve the situation in the future, we'll need > additional handling to mark anonymous pages as definitely exclusive to a > single process, only allow GUP pins on exclusive anon pages, and > disallow sharing of exclusive anon pages with GUP pins e.g., during > fork(). > > III. Interaction with references from LRU pagevecs > > Similar to ordinary anon pages, we can have LRU pagevecs referencing our > THP. Reliably removing such references requires draining LRU pagevecs on > all CPUs -- lru_add_drain_all() -- a possibly expensive operation that can > sleep. For now, similar do do_wp_page(), let's conditionally drain the > local LRU pagevecs only if we detect !PageLRU(). > > IV. Interaction with speculative/temporary references > > Similar to ordinary anon pages, other speculative/temporary references on > the THP, for example, from the pagecache or page migration code, will > disallow exclusive reuse of the page. We'll have to PTE-map the THP. > > Signed-off-by: David Hildenbrand > --- > mm/huge_memory.c | 19 +++++++++++++++---- > 1 file changed, 15 insertions(+), 4 deletions(-) > > diff --git a/mm/huge_memory.c b/mm/huge_memory.c > index 406a3c28c026..b6ba88a98266 100644 > --- a/mm/huge_memory.c > +++ b/mm/huge_memory.c > @@ -1286,6 +1286,7 @@ vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf) > struct page *page; > unsigned long haddr = vmf->address & HPAGE_PMD_MASK; > pmd_t orig_pmd = vmf->orig_pmd; > + int swapcache_refs = 0; > > vmf->ptl = pmd_lockptr(vma->vm_mm, vmf->pmd); > VM_BUG_ON_VMA(!vma->anon_vma, vma); > @@ -1303,7 +1304,6 @@ vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf) > page = pmd_page(orig_pmd); > VM_BUG_ON_PAGE(!PageHead(page), page); > > - /* Lock page for reuse_swap_page() */ > if (!trylock_page(page)) { > get_page(page); > spin_unlock(vmf->ptl); > @@ -1319,10 +1319,20 @@ vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf) > } > > /* > - * We can only reuse the page if nobody else maps the huge page or it's > - * part. > + * See do_wp_page(): we can only map the page writable if there are > + * no additional references. > */ > - if (reuse_swap_page(page)) { > + if (PageSwapCache(page)) > + swapcache_refs = thp_nr_pages(page); > + if (page_count(page) > 1 + swapcache_refs + !PageLRU(page)) > + goto unlock_fallback; > + if (!PageLRU(page)) > + lru_add_drain(); IMHO, draining lru doesn't help out too much for THP since THP will be drained to LRU immediately once it is added into pagevec. > + if (page_count(page) > 1 + swapcache_refs) > + goto unlock_fallback; > + if (swapcache_refs) > + try_to_free_swap(page); > + if (page_count(page) == 1) { > pmd_t entry; > entry = pmd_mkyoung(orig_pmd); > entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); > @@ -1333,6 +1343,7 @@ vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf) > return VM_FAULT_WRITE; > } > > +unlock_fallback: > unlock_page(page); > spin_unlock(vmf->ptl); > fallback: > -- > 2.34.1 >