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 E8EB4C433FE for ; Thu, 29 Sep 2022 13:39:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5A4138D0002; Thu, 29 Sep 2022 09:39:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 52B9E8D0001; Thu, 29 Sep 2022 09:39:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3570C8D0002; Thu, 29 Sep 2022 09:39:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 1ECD08D0001 for ; Thu, 29 Sep 2022 09:39:03 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id E3EAB120111 for ; Thu, 29 Sep 2022 13:39:02 +0000 (UTC) X-FDA: 79965228924.27.5D92B53 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) by imf25.hostedemail.com (Postfix) with ESMTP id 77721A000C for ; Thu, 29 Sep 2022 13:39:02 +0000 (UTC) Received: by mail-pf1-f174.google.com with SMTP id g130so889158pfb.8 for ; Thu, 29 Sep 2022 06:39:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date; bh=g3H7Fcpyw8fyayYdLffV0RX5soiF4EbtKkJrM7w/NLU=; b=m3GLnqQdyjwLMEin21Lj7q5pk3V5oolReoHaMp2tZ+bf/VZ2oWLbMLWQB8BjLcplSr A4y5yLMQuhZKV/GNIx36RWKVNjffaPLza7CDxOQ218pbya9mGURP+fhCv4a50IZlDiqA f57iV9VLkG1cpkmmaqFjiTXstGMTC5q8oZtinhqIeLsWApbLgrGPHhhMnCkm0VlQu6s7 XYodXt1DvXfuKe52a3/LqKBqQAinGpoloXFKW0VdpUvEvnuekHiEol7/um8djQV4xFHR 7dnukJk7xjNWbGxBzJq+rFrkj0tjOOq3Jbevtw2O/lD7ihUuWsP/oysFySddbYS9KyX+ FPDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date; bh=g3H7Fcpyw8fyayYdLffV0RX5soiF4EbtKkJrM7w/NLU=; b=PwpqsuNU3KUB+Hpssf1JGf7M4n7jLkdhuNbPhP7J07Wy7asBhuuTxJFjtwlsO/LSzI P3Z/xlp1u48+pgrRueomi+orSEIVnFLquYFMDgCKxwMJunUreT/k7VYw/302Rbk8e5E0 yfMnYgVzbVIj5BDMmGthRY/v3iOz3BW8wJrp0gtZ8VNo7OhzgTg8W/C0QZj7f+r28B03 tjpex38ccjf77ixr673i1jguWCxLxoOCjHyjfbmBfSNIA46Ipru8yJ2lsEr40CoOszEU 6mUrepad0SNcCwlfNtA8iQH13y6emByqug0JFewB+PI3gTrU+4KslvHEiwrVXjz/Y4c4 WfYA== X-Gm-Message-State: ACrzQf2TBusZDN837UCy3O/WKCBUc8XK0Rd/vdo3WN2O9McdBLGSwugN Y2IA9JEmRMcD1w5ZDngv3DY= X-Google-Smtp-Source: AMsMyM47X/JMtaA6HAVtDJgIftP1m1YbO9TT8Bgi74p9v1UlSWtNvp4o7o4kFmds/6oxEX73Z+VoFA== X-Received: by 2002:a63:8a43:0:b0:440:34f2:c3c4 with SMTP id y64-20020a638a43000000b0044034f2c3c4mr3024905pgd.408.1664458741170; Thu, 29 Sep 2022 06:39:01 -0700 (PDT) Received: from strix-laptop (2001-b011-20e0-1b9a-f5f9-665b-0715-9cc1.dynamic-ip6.hinet.net. [2001:b011:20e0:1b9a:f5f9:665b:715:9cc1]) by smtp.gmail.com with ESMTPSA id 205-20020a6214d6000000b0055b05b90480sm833306pfu.66.2022.09.29.06.38.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Sep 2022 06:39:00 -0700 (PDT) Date: Thu, 29 Sep 2022 21:38:53 +0800 From: Chih-En Lin To: David Hildenbrand Cc: Nadav Amit , Andrew Morton , Qi Zheng , Matthew Wilcox , Christophe Leroy , "linux-kernel@vger.kernel.org" , "linux-mm@kvack.org" , Luis Chamberlain , Kees Cook , Iurii Zaikin , Vlastimil Babka , William Kucharski , "Kirill A . Shutemov" , Peter Xu , Suren Baghdasaryan , Arnd Bergmann , Tong Tiangen , Pasha Tatashin , Li kunyu , Anshuman Khandual , Minchan Kim , Yang Shi , Song Liu , Miaohe Lin , Thomas Gleixner , Sebastian Andrzej Siewior , Andy Lutomirski , Fenghua Yu , Dinglan Peng , Pedro Fonseca , Jim Huang , Huichun Feng Subject: Re: [RFC PATCH v2 9/9] mm: Introduce Copy-On-Write PTE table Message-ID: References: <20220927162957.270460-1-shiyn.lin@gmail.com> <20220927162957.270460-10-shiyn.lin@gmail.com> <3D21021E-490F-4FE0-9C75-BB3A46A66A26@vmware.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1664458742; a=rsa-sha256; cv=none; b=uKfhzCJ1WQ1aOHI1txF4F0DptaLwRZUdtq9JvTQELxSrjxt/g+fusRwfaDGAS6n5zSCHPh yzlsKVb8kLi1U5K+2k0FfrXtUD7HaEHlMQRbKy783/95GaCDpgHaHXY2X1D9KWDjMc7JtC lUq0viHO4wMF+7JzjUqvs5lx7tnAEk8= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=m3GLnqQd; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf25.hostedemail.com: domain of shiyn.lin@gmail.com designates 209.85.210.174 as permitted sender) smtp.mailfrom=shiyn.lin@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1664458742; h=from:from:sender: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:dkim-signature; bh=g3H7Fcpyw8fyayYdLffV0RX5soiF4EbtKkJrM7w/NLU=; b=BCRFlp0mHrDWDztuIji+exOvpWkU1qCGyZxdlMoLG4HBe+heUOqxTYTdH83r+zvON0tG1u 1frPI2CW9V52KxI5etAfbtlXZnFDtpYt/NBZJA43ftHtnnD4nnH195LQDm1A8fyVUc3AvK GT4zkT7+lj/mmb8cLWCCl7jNRyEgvH0= X-Stat-Signature: th5z3jarog6jrki8jitbrtcp98kz7sza X-Rspamd-Queue-Id: 77721A000C Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=m3GLnqQd; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf25.hostedemail.com: domain of shiyn.lin@gmail.com designates 209.85.210.174 as permitted sender) smtp.mailfrom=shiyn.lin@gmail.com X-Rspamd-Server: rspam02 X-Rspam-User: X-HE-Tag: 1664458742-510269 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: Sorry for replying late. On Wed, Sep 28, 2022 at 04:03:19PM +0200, David Hildenbrand wrote: > On 27.09.22 21:53, Chih-En Lin wrote: > > On Tue, Sep 27, 2022 at 06:38:05PM +0000, Nadav Amit wrote: > > > I only skimmed the patches that you sent. The last couple of patches seem a > > > bit rough and dirty, so I am sorry to say that I skipped them (too many > > > “TODO” and “XXX” for my taste). > > > > > > I am sure other will have better feedback than me. I understand there is a > > > tradeoff and that this mechanism is mostly for high performance > > > snapshotting/forking. It would be beneficial to see whether this mechanism > > > can somehow be combined with existing ones (mshare?). > > > > Still thanks for your feedback. :) > > I'm looking at the PTE refcount and mshare patches. And, maybe it can > > combine with them in the future. > > > > > The code itself can be improved. I found the reasoning about synchronization > > > and TLB flushes and synchronizations to be lacking, and the code to seem > > > potentially incorrect. Better comments would help, even if the code is > > > correct. > > > > > > There are additional general questions. For instance, when sharing a > > > page-table, do you properly update the refcount/mapcount of the mapped > > > pages? And are there any possible interactions with THP? > > > > Since access to those mapped pages will cost a lot of time, and this > > will make fork() even have more overhead. It will not update the > > refcount/mapcount of the mapped pages. > > Oh no. > > So we'd have pages logically mapped into two processes (two page table > structures), but the refcount/mapcount/PageAnonExclusive would not reflect > that? > > Honestly, I don't think it is upstream material in that hacky form. No, we > don't need more COW CVEs or more COW over-complications that destabilize the > whole system. > I know setting the write protection is not enough to prove the security safe since the previous COW CVEs are related to it. And, if skipping the accounting to reduce the overhead of fork() is not suitable for upstream , we can change it. But, I think COW to the table can still be an upstream material. Recently the patches, like refcount for the empty user PTE page table pages and mshare for the pages shared between the processes require more PTE entries, showing that the modern system uses a lot of memory for the page table (especially the PTE table). So, I think the method, COW to the table, might reduce the memory usage for the side of the multiple users PTE page table. > IMHO, a relaxed form that focuses on only the memory consumption reduction > could *possibly* be accepted upstream if it's not too invasive or complex. > During fork(), we'd do exactly what we used to do to PTEs (increment > mapcount, refcount, trying to clear PageAnonExclusive, map the page R/O, > duplicate swap entries; all while holding the page table lock), however, > sharing the prepared page table with the child process using COW after we > prepared it. > > Any (most once we want to *optimize* rmap handling) modification attempts > require breaking COW -- copying the page table for the faulting process. But > at that point, the PTEs are already write-protected and properly accounted > (refcount/mapcount/PageAnonExclusive). > > Doing it that way might not require any questionable GUP hacks and swapping, > MMU notifiers etc. "might just work as expected" because the accounting > remains unchanged" -- we simply de-duplicate the page table itself we'd have > after fork and any modification attempts simply replace the mapped copy. Agree. However for GUP hacks, if we want to do the COW to page table, we still need the hacks in this patch (using the COW_PTE_OWN_EXCLUSIVE flag to check whether the PTE table is available or not before we do the COW to the table). Otherwise, it will be more complicated since it might need to handle situations like while preparing the COW work, it just figuring out that it needs to duplicate the whole table and roll back (recover the state and copy it to new table). Hopefully, I'm not wrong here. > But devil is in the detail (page table lock, TLB flushing). Sure, it might be an overhead in the page fault and needs to be handled carefully. ;) > "will make fork() even have more overhead" is not a good excuse for such > complexity/hacks -- sure, it will make your benchmark results look better in > comparison ;) ;);) I think that, even if we do the accounting with the COW page table, it still has a little bit improve. > -- > Thanks, > > David / dhildenb > Thanks, Chih-En Lin