Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp1198917rwb; Tue, 27 Sep 2022 09:39:05 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7VI7V4z5obeZPshaS+RKar/IddEKJmQLtS3ao49GuVCS05TeZGfSSVkJSx9xnEiwAUHRww X-Received: by 2002:a17:902:f7ca:b0:178:9c90:b010 with SMTP id h10-20020a170902f7ca00b001789c90b010mr27553830plw.149.1664296745327; Tue, 27 Sep 2022 09:39:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664296745; cv=none; d=google.com; s=arc-20160816; b=Wg0cVTOb8r4Jgf8ejCc8rkVprDg4du7Vz+MrRNZ+Z9pjHds6p1/ssFYeADmTBPbV24 NbWdlxsnVx7SH2Za4sCiq4Sxp29DuRayim9pIjjBe4rjZMSIua00ownZp/A9LmSeLY0r NI8kDA8zXkbUXVrCXy0dahhtww5SAuH9DR4TVuZ/tpNkYxgfalsoPWgPLUyCixlp5qLe LviTbEKQhliStvPY4qIe86AkbLlDirBO49ig69ec4f66EMCwuVY5AN9y7eY4T84YPexa L4Y7EfFBLBvVQmbJOLGmf6nDINFDOSwEg9aY4vj+Es+qD86QXR4cQM5xNOJfaeo0Ple2 /Agw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=XuRjx7DiMyFErohU6l5P8iRkgXDYC8Q+F8gmrtJ+ilo=; b=bLNJa8j6b52OC+9qVVcNYWxDkR36tg6kfTX96clU/mPY7l+By72po30QaK6OWcwgfJ gpQtBN4xgfkRaoCisF/svGl4LqZZns0C5EoFlsp3lvQsj2A6NL2t/TLJXP3O9tPju+Gs AZOpWU4IKsMxktDkmCk36DVc8hRRkBYs/CMt2F0OkeW91noebbWGN4y0ZK40fnDG4ojo VGN6b90gRaP5kNOZ1JkYrk++dWpQjvRLvKpYMk4oDHz4U9W4XvG47+oXbug6zbyWitC+ vZw4BZ/3C6CPzz2B3WrrdWEGhuIjvDIlzwooC+qnrQ/7GRGL/Yne16gXYmkJkNoZxhM2 XlwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=BUZqiRpZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p9-20020a637409000000b0041b86985526si2420988pgc.189.2022.09.27.09.38.53; Tue, 27 Sep 2022 09:39:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=BUZqiRpZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S231860AbiI0Q2t (ORCPT + 99 others); Tue, 27 Sep 2022 12:28:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232940AbiI0Q2X (ORCPT ); Tue, 27 Sep 2022 12:28:23 -0400 Received: from mail-pf1-x432.google.com (mail-pf1-x432.google.com [IPv6:2607:f8b0:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 490D41D66E6 for ; Tue, 27 Sep 2022 09:28:04 -0700 (PDT) Received: by mail-pf1-x432.google.com with SMTP id d82so10121440pfd.10 for ; Tue, 27 Sep 2022 09:28:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=XuRjx7DiMyFErohU6l5P8iRkgXDYC8Q+F8gmrtJ+ilo=; b=BUZqiRpZc+y9cuKG56oBy+Cfa11WNSqYjMXnosAmPhlk/lSKqYHn4hBSpj/jIf58w5 I9qqOV/xm+s71mwq9zjCCSYffCPx9a50VMkrfSsqXCK+mN/b4e6hEOlKOolaJUTLxIqC VwRC3b9iS7HmqV77y7pGxVVKSrzAOhQgEcl/7dch2nR+pKj1CxC+vlFOZChPdDepcxnv UFDQMokXHNYITZHeIoOXNkO4NIDhzmsx66zSvT78H/d0eIndhhGEjIwAVklKSYQZiD/V 6H7xjsp1lbsiNpMH3hkT8bqlX2SYf2GrGg3f8YRsP/r9wEive/ykWyXiTUmWSniup8Hj W8cQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=XuRjx7DiMyFErohU6l5P8iRkgXDYC8Q+F8gmrtJ+ilo=; b=wTTcOwfVJsgAbqv4d1S3rJKGtyiHZrV7ip4PkOU0BGXImBJZt0xuxM8QbL+iB9ueyu 44RMxPrgUs6YGxERZAAUgYPdyUloTnVKr3jHlBncm+5r8BX8yuKC2cX3J9MAF54aTXSf DFgWrL/FDgdYs5ycPGJA9Tai+VogjH8blDF2qvLUGy1Q5LSEjnuRoAZC6Y/BueQ5QCbx HDgNHKeJ+kRYdcxXxEVrIrqE6JDWz7ZSLkxxf+VNUYZhzbvl2kHN2CYyCJRmoEgatgIM 7UGJFiSO5gnCNdRZz0eCJL+XfXFPG7tAfsLEXiRY6NQiNZaEOoch9njLdEiEy9X4W596 IoRw== X-Gm-Message-State: ACrzQf3AR/ADlO0oVVY9YToVxjpKRyPbwrvN2UPpqIZpwYeSTb12ysu7 UDrvTqavJxdfQqlNuc9vGfc= X-Received: by 2002:a05:6a00:1253:b0:546:3d50:3284 with SMTP id u19-20020a056a00125300b005463d503284mr29961879pfi.72.1664296083652; Tue, 27 Sep 2022 09:28:03 -0700 (PDT) Received: from archlinux.localdomain ([140.121.198.213]) by smtp.googlemail.com with ESMTPSA id 9-20020a17090a0f0900b001f333fab3d6sm8602360pjy.18.2022.09.27.09.27.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Sep 2022 09:28:02 -0700 (PDT) From: Chih-En Lin To: Andrew Morton , Qi Zheng , David Hildenbrand , Matthew Wilcox , Christophe Leroy Cc: 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 , Nadav Amit , 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 , Chih-En Lin Subject: [RFC PATCH v2 5/9] mm, pgtable: Add a refcount to PTE table Date: Wed, 28 Sep 2022 00:29:53 +0800 Message-Id: <20220927162957.270460-6-shiyn.lin@gmail.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220927162957.270460-1-shiyn.lin@gmail.com> References: <20220927162957.270460-1-shiyn.lin@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham 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 Reuse the _refcount in struct page for the page table to maintain the number of process references to COWed PTE table. Before decreasing the refcount, it will check whether refcount is one or not for reusing shared PTE table. Signed-off-by: Chih-En Lin --- include/linux/mm.h | 1 + include/linux/pgtable.h | 28 ++++++++++++++++++++++++++++ mm/memory.c | 1 + 3 files changed, 30 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index 965523dcca3b8..bfe6a8c7ab9ed 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2290,6 +2290,7 @@ static inline bool pgtable_pte_page_ctor(struct page *page) __SetPageTable(page); inc_lruvec_page_state(page, NR_PAGETABLE); page->cow_pte_owner = NULL; + set_page_count(page, 1); return true; } diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index 25c1e5c42fdf3..8b497d7d800ed 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -9,6 +9,7 @@ #ifdef CONFIG_MMU #include +#include #include #include #include @@ -628,6 +629,33 @@ static inline bool cow_pte_owner_is_same(pmd_t *pmd, pmd_t *owner) return smp_load_acquire(&pmd_page(*pmd)->cow_pte_owner) == owner; } +static inline int pmd_get_pte(pmd_t *pmd) +{ + return page_ref_inc_return(pmd_page(*pmd)); +} + +/* + * If the COW PTE refcount is 1, instead of decreasing the counter, + * clear write protection of the corresponding PMD entry and reset + * the COW PTE owner to reuse the table. + * But if the reuse parameter is false, do not thing. This help us + * to handle the situation that PTE table we already handled. + */ +static inline int pmd_put_pte(struct vm_area_struct *vma, pmd_t *pmd, + unsigned long addr, bool reuse) +{ + if (!page_ref_add_unless(pmd_page(*pmd), -1, 1) && reuse) { + cow_pte_fallback(vma, pmd, addr); + return 1; + } + return 0; +} + +static inline int cow_pte_count(pmd_t *pmd) +{ + return page_count(pmd_page(*pmd)); +} + #ifndef pte_access_permitted #define pte_access_permitted(pte, write) \ (pte_present(pte) && (!(write) || pte_write(pte))) diff --git a/mm/memory.c b/mm/memory.c index d29f84801f3cd..3e66e229f4169 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -2875,6 +2875,7 @@ void cow_pte_fallback(struct vm_area_struct *vma, pmd_t *pmd, pmd_t new; VM_WARN_ON(pmd_write(*pmd)); + VM_WARN_ON(cow_pte_count(pmd) != 1); start = addr & PMD_MASK; end = (addr + PMD_SIZE) & PMD_MASK; -- 2.37.3