Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp251737ybl; Fri, 9 Aug 2019 05:37:25 -0700 (PDT) X-Google-Smtp-Source: APXvYqwcsVVKpQpYzBwjNvmj2chbvNcmDM45whMU9PKHXAFn4+oq8aluO445CduKKWKQH/B4lBcp X-Received: by 2002:a63:f401:: with SMTP id g1mr17952357pgi.314.1565354244974; Fri, 09 Aug 2019 05:37:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565354244; cv=none; d=google.com; s=arc-20160816; b=QyM1hzqpzl1ljg2sM8kdb3eeJSIo1WuLQ4Eiisast5dVpeCCoU8W7jrc3djT3OPU2r dgD4hIO64oCrxj/IxChc5m1iTOzRg0OsvhFaGcT1AcPGDzReDr2Eg2j0GpFiCaU8HL8I yzG1OprnZb4ID53FYRYfXUXDjP38IFgJ9z57+iqYquiZ0FJ8WHYxWNrD1P3v56uvQGNC UygIsflacibhrQuSIAOnSNjzFZKKarYPIhHn5Gl/A1KdXXEFEpSqFF6JGGt8ro/klb05 Se4ewirAaBu9HivEJMtjmOWgJ+SGQf6i47yGjVLhsdkC4dETOqZIEtD3b0mVwfOhPF4I R4vw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from; bh=lrTZlB8CU2bT4EmJHppauzFngRhBBlw2xPlNd2GJDd4=; b=nMHTb8FUEfcxw6m8ffCW8hHmgLb1MaGTgZNWU39ZWuFnmTYj03d3L8WGy/uMSeItLg fups74GyyA3+Y8XIQTxqrNJg+bwBHKFv1bpAGyCbHA2j5MbyO1SqhHTLJe+CLxvgAeMI 6Tx1hgloMyka90eyi704jWL1NOXerT+TipDnxnaUHTZ5exVgCAzbeGpRyJ8S0aPPOoDC S/pjRf2Ko2KksZeyERXUr7P55viI9PbXGo96H42Rc32W0MbKvzCa0Od1C5t4wcgHpwq9 gMW7cCfk+yl151SxvLi3quNxgFbEnAlfdDVOUXQkXDV/AduMuBdXEjg+jQ7qDlLovybq FYuA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y22si6858791pll.36.2019.08.09.05.37.09; Fri, 09 Aug 2019 05:37:24 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2406558AbfHIMgT (ORCPT + 99 others); Fri, 9 Aug 2019 08:36:19 -0400 Received: from bilbo.ozlabs.org ([203.11.71.1]:48057 "EHLO ozlabs.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726255AbfHIMgS (ORCPT ); Fri, 9 Aug 2019 08:36:18 -0400 Received: from authenticated.ozlabs.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mail.ozlabs.org (Postfix) with ESMTPSA id 464l8w0FPbz9sBF; Fri, 9 Aug 2019 22:36:15 +1000 (AEST) From: Michael Ellerman To: Chuhong Yuan Cc: Benjamin Herrenschmidt , Paul Mackerras , linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, Chuhong Yuan Subject: Re: [PATCH] powerpc/mm: Use refcount_t for refcount In-Reply-To: <20190808071808.6531-1-hslester96@gmail.com> References: <20190808071808.6531-1-hslester96@gmail.com> Date: Fri, 09 Aug 2019 22:36:15 +1000 Message-ID: <87y302segw.fsf@concordia.ellerman.id.au> MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Chuhong Yuan writes: > Reference counters are preferred to use refcount_t instead of > atomic_t. > This is because the implementation of refcount_t can prevent > overflows and detect possible use-after-free. > So convert atomic_t ref counters to refcount_t. > > Signed-off-by: Chuhong Yuan Thanks. We don't have a fast implementation of refcount_t, so I'm worried this could cause a measurable performance regression. Did you benchmark it at all? cheers > diff --git a/arch/powerpc/mm/book3s64/mmu_context.c b/arch/powerpc/mm/book3s64/mmu_context.c > index 2d0cb5ba9a47..f836fd5a6abc 100644 > --- a/arch/powerpc/mm/book3s64/mmu_context.c > +++ b/arch/powerpc/mm/book3s64/mmu_context.c > @@ -231,7 +231,7 @@ static void pmd_frag_destroy(void *pmd_frag) > /* drop all the pending references */ > count = ((unsigned long)pmd_frag & ~PAGE_MASK) >> PMD_FRAG_SIZE_SHIFT; > /* We allow PTE_FRAG_NR fragments from a PTE page */ > - if (atomic_sub_and_test(PMD_FRAG_NR - count, &page->pt_frag_refcount)) { > + if (refcount_sub_and_test(PMD_FRAG_NR - count, &page->pt_frag_refcount)) { > pgtable_pmd_page_dtor(page); > __free_page(page); > } > diff --git a/arch/powerpc/mm/book3s64/pgtable.c b/arch/powerpc/mm/book3s64/pgtable.c > index 7d0e0d0d22c4..40056896ce4e 100644 > --- a/arch/powerpc/mm/book3s64/pgtable.c > +++ b/arch/powerpc/mm/book3s64/pgtable.c > @@ -277,7 +277,7 @@ static pmd_t *__alloc_for_pmdcache(struct mm_struct *mm) > return NULL; > } > > - atomic_set(&page->pt_frag_refcount, 1); > + refcount_set(&page->pt_frag_refcount, 1); > > ret = page_address(page); > /* > @@ -294,7 +294,7 @@ static pmd_t *__alloc_for_pmdcache(struct mm_struct *mm) > * count. > */ > if (likely(!mm->context.pmd_frag)) { > - atomic_set(&page->pt_frag_refcount, PMD_FRAG_NR); > + refcount_set(&page->pt_frag_refcount, PMD_FRAG_NR); > mm->context.pmd_frag = ret + PMD_FRAG_SIZE; > } > spin_unlock(&mm->page_table_lock); > @@ -317,8 +317,7 @@ void pmd_fragment_free(unsigned long *pmd) > { > struct page *page = virt_to_page(pmd); > > - BUG_ON(atomic_read(&page->pt_frag_refcount) <= 0); > - if (atomic_dec_and_test(&page->pt_frag_refcount)) { > + if (refcount_dec_and_test(&page->pt_frag_refcount)) { > pgtable_pmd_page_dtor(page); > __free_page(page); > } > diff --git a/arch/powerpc/mm/pgtable-frag.c b/arch/powerpc/mm/pgtable-frag.c > index a7b05214760c..4ef8231b677f 100644 > --- a/arch/powerpc/mm/pgtable-frag.c > +++ b/arch/powerpc/mm/pgtable-frag.c > @@ -24,7 +24,7 @@ void pte_frag_destroy(void *pte_frag) > /* drop all the pending references */ > count = ((unsigned long)pte_frag & ~PAGE_MASK) >> PTE_FRAG_SIZE_SHIFT; > /* We allow PTE_FRAG_NR fragments from a PTE page */ > - if (atomic_sub_and_test(PTE_FRAG_NR - count, &page->pt_frag_refcount)) { > + if (refcount_sub_and_test(PTE_FRAG_NR - count, &page->pt_frag_refcount)) { > pgtable_page_dtor(page); > __free_page(page); > } > @@ -71,7 +71,7 @@ static pte_t *__alloc_for_ptecache(struct mm_struct *mm, int kernel) > return NULL; > } > > - atomic_set(&page->pt_frag_refcount, 1); > + refcount_set(&page->pt_frag_refcount, 1); > > ret = page_address(page); > /* > @@ -87,7 +87,7 @@ static pte_t *__alloc_for_ptecache(struct mm_struct *mm, int kernel) > * count. > */ > if (likely(!pte_frag_get(&mm->context))) { > - atomic_set(&page->pt_frag_refcount, PTE_FRAG_NR); > + refcount_set(&page->pt_frag_refcount, PTE_FRAG_NR); > pte_frag_set(&mm->context, ret + PTE_FRAG_SIZE); > } > spin_unlock(&mm->page_table_lock); > @@ -110,8 +110,7 @@ void pte_fragment_free(unsigned long *table, int kernel) > { > struct page *page = virt_to_page(table); > > - BUG_ON(atomic_read(&page->pt_frag_refcount) <= 0); > - if (atomic_dec_and_test(&page->pt_frag_refcount)) { > + if (refcount_dec_and_test(&page->pt_frag_refcount)) { > if (!kernel) > pgtable_page_dtor(page); > __free_page(page); > diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h > index 3a37a89eb7a7..7fe23a3faf95 100644 > --- a/include/linux/mm_types.h > +++ b/include/linux/mm_types.h > @@ -14,6 +14,7 @@ > #include > #include > #include > +#include > > #include > > @@ -147,7 +148,7 @@ struct page { > unsigned long _pt_pad_2; /* mapping */ > union { > struct mm_struct *pt_mm; /* x86 pgds only */ > - atomic_t pt_frag_refcount; /* powerpc */ > + refcount_t pt_frag_refcount; /* powerpc */ > }; > #if ALLOC_SPLIT_PTLOCKS > spinlock_t *ptl; > -- > 2.20.1