Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp1175751ioo; Sun, 22 May 2022 06:09:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwu93nfy76VFbrgRAx0WFr+Brxaq+Qogu8GLPVqwzTlrAHFkb43ZDaqwFeL2TeWG97ulu7U X-Received: by 2002:a05:6402:2925:b0:42a:c5b4:aff8 with SMTP id ee37-20020a056402292500b0042ac5b4aff8mr19534699edb.392.1653224965222; Sun, 22 May 2022 06:09:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653224965; cv=none; d=google.com; s=arc-20160816; b=uSmnCVDW8lH3hXyUV8p5EHWZWjeBMCScE/q2ZVzgvhWsKMM0Ra/6CK+d6ajwRKLiQo CVCTQKftogzQ7HizGKaV7rBCIiYEqQHpxlg3HEZKL+r+Vm9nHuRh5MJENst2KCGQ+gRG KyGKB1UndgkZ7Rrh4AXzbbixJvkv73qQ1OaSbKgOCGd9xJbZvxsUvhrnbqsf5Zys6dnV uOg2ZyWpa9Hlg7nZDRLVbVSYyXwAe05zW1gI1aSaiM0AstiTBIiErkbcby3lF5t2dYE8 XuPUGKij/Ht19qVOl2JA0nBswBrv6MwbN6D/4HwB9hV1rtjTuHk7WSum90O+XqR7yTo/ 0lAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=ikfQc/ZOG7EJokBAFYccHiaAA1f4v5zShuvB/Kv0HFo=; b=aqUu6Z2ilIJIaMlvLcv8Nbd89gmrTUXbmMa27L+NU1s0WznQVtga7J1+Iiy8e7EBoa FtCXz0tPKuJJMHkJhtCSV+EKX1OH+COMB22R/GBlaRH6+cTBYsryRyUKmMf8eRVt290E cw+wmxj1yK1avSh30evZvTvqQnnXOzUL+lrWM2aEF3Wv6ynOdVvikaexAkDP7c3PCDmB aZ1go0cTsFwM0F7yzMxJBorF/4l12qee5aqVdYIk0aSwS+nTuybmQ5ZFvCQrjgxddaZm vtvpDuypJ3fPOohQZOvcHp2d3LYU6HsNm6nAysiT19YgMmN8vo58CQL8+XHpd3+ceMJe 0XwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=S8c1LgyS; 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 e1-20020a1709062d4100b006f3ad555460si11581962eji.652.2022.05.22.06.08.58; Sun, 22 May 2022 06:09:25 -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=S8c1LgyS; 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 S234658AbiEUEWi (ORCPT + 99 others); Sat, 21 May 2022 00:22:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58050 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230143AbiEUEWf (ORCPT ); Sat, 21 May 2022 00:22:35 -0400 Received: from mail-pg1-x535.google.com (mail-pg1-x535.google.com [IPv6:2607:f8b0:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E95E419591A for ; Fri, 20 May 2022 21:22:34 -0700 (PDT) Received: by mail-pg1-x535.google.com with SMTP id x12so9258342pgj.7 for ; Fri, 20 May 2022 21:22:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to; bh=ikfQc/ZOG7EJokBAFYccHiaAA1f4v5zShuvB/Kv0HFo=; b=S8c1LgySUO1SkmryhkzPJ3RvfXMRba+mg9/9xI+hvcBVKrUQPLH0gTxcsz3RvmzOnF y9jwVOkN/aw7fivKVtZbStb4mmKayCfLzWRtRtdPh8hwcbU+9RsBbGpxB+qlFE8pxDNa NwY+1oDT0JXMwIOwUDk3yOJ9e/nTEjM7E6dYJUQ9SQ1GV6lUklSRidG7NudQrGcopR95 Xc5rez12atmEBrQ1JxDZNFIBOHNDHc0zirX+s9pRTZKxA79r3lkaz7luO2UV42cNTOLv TIslk8+TY0UGEJOLY3ZFl6fGB7dexSWc9qPbzu3OVTqAM+vgney4Rf78y4GNZhZ30UU/ SvZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=ikfQc/ZOG7EJokBAFYccHiaAA1f4v5zShuvB/Kv0HFo=; b=usXm7MTHde3Bf1HP9WzSIzBjHLX0zPDgsCqDgE03qEDCZfVoqzOcAOERjvzWN9Ev2D 9po5FexJah452t3n2df5KqkKbLllQ/HF83ilpgD/pqpKV1WcW3tudepsG6WWNrt4LYfR BdM+hpkN35p7mADCAheYQ2VFHopLLky7pi9n9VGTNKHuDrpGF5PaN7Jt5Z64d/cqJUyC 2sxdYPckCC7VWhulxwv3HJYtIVVPcNpm6nORMLyT/coP+hJGo287Xg8/UBjrOipoirQ1 b2acTpm32+bAvaqd+cQN5hRsQG4UdRodfoRH1jFBYduRgmbeoysNjHfqiVnD7QWDsxNd xXKg== X-Gm-Message-State: AOAM533/JZ4iS/os/M5/gOytndOrg6T+mYx79TAWSnq7VbTQu7+A7ZI8 6ZGq3eKfVu4B7mMum7lNKIE= X-Received: by 2002:a05:6a00:1254:b0:517:c9ef:bdaf with SMTP id u20-20020a056a00125400b00517c9efbdafmr13401028pfi.14.1653106954379; Fri, 20 May 2022 21:22:34 -0700 (PDT) Received: from strix-laptop (2001-b011-20e0-15d4-84b3-8c62-a0b8-199e.dynamic-ip6.hinet.net. [2001:b011:20e0:15d4:84b3:8c62:a0b8:199e]) by smtp.gmail.com with ESMTPSA id jb11-20020a17090b408b00b001dcf9fe5cddsm2651420pjb.38.2022.05.20.21.22.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 May 2022 21:22:33 -0700 (PDT) Date: Sat, 21 May 2022 12:22:25 +0800 From: Chih-En Lin To: Christophe Leroy Cc: Andrew Morton , "linux-mm@kvack.org" , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Christian Brauner , "Matthew Wilcox (Oracle)" , Vlastimil Babka , William Kucharski , John Hubbard , Yunsheng Lin , Arnd Bergmann , Suren Baghdasaryan , Colin Cross , Feng Tang , "Eric W. Biederman" , Mike Rapoport , Geert Uytterhoeven , Anshuman Khandual , "Aneesh Kumar K.V" , Daniel Axtens , Jonathan Marek , Pasha Tatashin , Peter Xu , Andrea Arcangeli , Thomas Gleixner , Andy Lutomirski , Sebastian Andrzej Siewior , Fenghua Yu , David Hildenbrand , "linux-kernel@vger.kernel.org" , Kaiyang Zhao , Huichun Feng , Jim Huang Subject: Re: [RFC PATCH 5/6] mm, pgtable: Add the reference counter for COW PTE Message-ID: <20220521042225.GB1508515@strix-laptop> References: <20220519183127.3909598-1-shiyn.lin@gmail.com> <20220519183127.3909598-6-shiyn.lin@gmail.com> <3ac4823b-8f44-c2d8-e550-7d3abf49a87e@csgroup.eu> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <3ac4823b-8f44-c2d8-e550-7d3abf49a87e@csgroup.eu> 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,T_SCC_BODY_TEXT_LINE 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 On Fri, May 20, 2022 at 02:30:29PM +0000, Christophe Leroy wrote: > > > Le 19/05/2022 ? 20:31, Chih-En Lin a ?crit?: > > Add the reference counter cow_pgtable_refcount to maintain the number > > of process references to COW PTE. Before decreasing the reference > > count, it will check whether the counter is one or not for reusing > > COW PTE when the counter is one. > > > > Signed-off-by: Chih-En Lin > > --- > > include/linux/mm.h | 1 + > > include/linux/mm_types.h | 1 + > > include/linux/pgtable.h | 27 +++++++++++++++++++++++++++ > > mm/memory.c | 1 + > > 4 files changed, 30 insertions(+) > > > > diff --git a/include/linux/mm.h b/include/linux/mm.h > > index 221926a3d818..e48bb3fbc33c 100644 > > --- a/include/linux/mm.h > > +++ b/include/linux/mm.h > > @@ -2329,6 +2329,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; > > + atomic_set(&page->cow_pgtable_refcount, 1); > > return true; > > } > > > > diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h > > index 5dcbd7f6c361..984d81e47d53 100644 > > --- a/include/linux/mm_types.h > > +++ b/include/linux/mm_types.h > > @@ -221,6 +221,7 @@ struct page { > > #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS > > int _last_cpupid; > > #endif > > + atomic_t cow_pgtable_refcount; /* COW page table */ > > pmd_t *cow_pte_owner; /* cow pte: pmd */ > > } _struct_page_alignment; > > > > diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h > > index faca57af332e..33c01fec7b92 100644 > > --- a/include/linux/pgtable.h > > +++ b/include/linux/pgtable.h > > @@ -604,6 +604,33 @@ static inline bool cow_pte_owner_is_same(pmd_t *pmd, pmd_t *owner) > > true : false; > > } > > > > +extern void cow_pte_fallback(struct vm_area_struct *vma, pmd_t *pmd, > > + unsigned long addr); > > 'extern' keyword is pointless for fonction prototypes. No new ones > should added. I see. It totally does not need the extern to the non-static function. > > + > > +static inline int pmd_get_pte(pmd_t *pmd) > > +{ > > + return atomic_inc_return(&pmd_page(*pmd)->cow_pgtable_refcount); > > +} > > + > > +/* If the COW PTE page->cow_pgtable_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. > > + */ > > +static inline int pmd_put_pte(struct vm_area_struct *vma, pmd_t *pmd, > > + unsigned long addr) > > +{ > > + if (!atomic_add_unless(&pmd_page(*pmd)->cow_pgtable_refcount, -1, 1)) { > > + cow_pte_fallback(vma, pmd, addr); > > + return 1; > > + } > > + return 0; > > I would do something more flat by reverting the test: > > { > if (atomic_add_unless(&pmd_page(*pmd)->cow_pgtable_refcount, -1, 1)) > return 0; > > cow_pte_fallback(vma, pmd, addr); > return 1; > } > Thanks!