From patchwork Tue Nov 24 09:52:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11927817 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 97C04C56201 for ; Tue, 24 Nov 2020 09:58:40 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 034A920679 for ; Tue, 24 Nov 2020 09:58:39 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="XDYX4dXd" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 034A920679 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 7ED256B0093; Tue, 24 Nov 2020 04:58:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 774FC6B0096; Tue, 24 Nov 2020 04:58:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 617CE6B0098; Tue, 24 Nov 2020 04:58:39 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0062.hostedemail.com [216.40.44.62]) by kanga.kvack.org (Postfix) with ESMTP id 45CC16B0093 for ; Tue, 24 Nov 2020 04:58:39 -0500 (EST) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 0A7F68249980 for ; Tue, 24 Nov 2020 09:58:39 +0000 (UTC) X-FDA: 77518862358.24.honey54_13154292736d Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin24.hostedemail.com (Postfix) with ESMTP id E14571A4A5 for ; Tue, 24 Nov 2020 09:58:38 +0000 (UTC) X-HE-Tag: honey54_13154292736d X-Filterd-Recvd-Size: 9094 Received: from mail-pg1-f195.google.com (mail-pg1-f195.google.com [209.85.215.195]) by imf42.hostedemail.com (Postfix) with ESMTP for ; Tue, 24 Nov 2020 09:58:38 +0000 (UTC) Received: by mail-pg1-f195.google.com with SMTP id w4so16976043pgg.13 for ; Tue, 24 Nov 2020 01:58:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ZFwyQlsscT2RBnxHWArfuTVJVgRwj0xOpsgIKb91Ge0=; b=XDYX4dXdNWF3Fzn1dtGBYeifCfGEgb75fiQL65NrdLEisMD4Yjgdvk+N3BLItujm2a 5JfBNhqze5ZmKNgmb89doGH/AVnWBc3oYcTwUB6v9/vBFfJ+eGIGaXWtcCtPNocKonY2 kf0Pe5l2R4v/U84HUg8jxCoLkeTAsG9C5UtfLckgc3/SHCik32CyJ9ErPu/2B2AeXp4B 37/TibIqva5tDvTnGJQ/Z26Box1IlLx/ZpK1cfdoD4kyV4mfV+Y2JmlmdIqDhize0RU1 lABC0IrCkGG5KdoT+6mSWOtT9BbVzcXez3Iupgxa9K82Kav6b5abq0J0W1KWZHEjaDTA JPHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ZFwyQlsscT2RBnxHWArfuTVJVgRwj0xOpsgIKb91Ge0=; b=caQeurMfNA72fP+ToSyM7CuuzEGcf88wfoypvdI8x42uV2T1ogycICBXPtmC7aW/vO ir6PLWhpgyKrY2OIsA/62j91bS4d4BYWTey4l97+KIy0A8xVRo82TF15C/VEn9EvA68v TPvBHpGEGd7QQQ8Vd4CeRqUVb5uLp+e+Pf61C5no3++sgP6aw7GT5/V3Xtd34fAKlaCH 8Dg1k/bUa7Z0fY/9EZtRioLzk5AmyUQCmbpMzf11bQWvG82eIvip5kxw+8m5+Rqs80du Zig2SdF0KasyHbWvXJvYmWpBy3iYeQ24kDQuvKrTql8aiRrIHar++Py/iCiM5GXjiZMo hWYA== X-Gm-Message-State: AOAM530X3qObMnP8Pnowk8mMAlWvwu+I+kGP9C2i8OXqLOeMIbnrrXcF 0Kj9tJdANrQnMOiYmAoG6OVIEA== X-Google-Smtp-Source: ABdhPJxo20c2KFbIHjZgWLrl89GNL9t8kpfDAKmq+mis09TlkPg6pRW/DaEm5NbaUDzC6/ruk91INQ== X-Received: by 2002:a17:90b:3781:: with SMTP id mz1mr3959844pjb.229.1606211917751; Tue, 24 Nov 2020 01:58:37 -0800 (PST) Received: from localhost.localdomain ([103.136.220.120]) by smtp.gmail.com with ESMTPSA id t20sm2424562pjg.25.2020.11.24.01.58.27 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 24 Nov 2020 01:58:37 -0800 (PST) From: Muchun Song To: corbet@lwn.net, mike.kravetz@oracle.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, paulmck@kernel.org, mchehab+huawei@kernel.org, pawan.kumar.gupta@linux.intel.com, rdunlap@infradead.org, oneukum@suse.com, anshuman.khandual@arm.com, jroedel@suse.de, almasrymina@google.com, rientjes@google.com, willy@infradead.org, osalvador@suse.de, mhocko@suse.com, song.bao.hua@hisilicon.com Cc: duanxiongchun@bytedance.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Muchun Song Subject: [PATCH v6 11/16] mm/hugetlb: Introduce remap_huge_page_pmd_vmemmap helper Date: Tue, 24 Nov 2020 17:52:54 +0800 Message-Id: <20201124095259.58755-12-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20201124095259.58755-1-songmuchun@bytedance.com> References: <20201124095259.58755-1-songmuchun@bytedance.com> MIME-Version: 1.0 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: The __free_huge_page_pmd_vmemmap and __remap_huge_page_pmd_vmemmap are almost the same code. So introduce remap_free_huge_page_pmd_vmemmap helper to simplify the code. Signed-off-by: Muchun Song --- mm/hugetlb_vmemmap.c | 87 +++++++++++++++++++++------------------------------- 1 file changed, 35 insertions(+), 52 deletions(-) diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index d6a1b06c1322..509ca451e232 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -127,6 +127,10 @@ (__boundary - 1 < (end) - 1) ? __boundary : (end); \ }) +typedef void (*vmemmap_pte_remap_func_t)(struct page *reuse, pte_t *ptep, + unsigned long start, unsigned long end, + void *priv); + static inline unsigned int vmemmap_pages_per_hpage(struct hstate *h) { return free_vmemmap_pages_per_hpage(h) + RESERVE_VMEMMAP_NR; @@ -162,21 +166,42 @@ static pmd_t *vmemmap_to_pmd(unsigned long page) return pmd_offset(pud, page); } +static void remap_huge_page_pmd_vmemmap(pmd_t *pmd, unsigned long start, + unsigned long end, + vmemmap_pte_remap_func_t fn, void *priv) +{ + unsigned long next, addr = start; + struct page *reuse = NULL; + + do { + pte_t *ptep; + + ptep = pte_offset_kernel(pmd, addr); + if (!reuse) + reuse = pte_page(ptep[TAIL_PAGE_REUSE]); + + next = vmemmap_hpage_addr_end(addr, end); + fn(reuse, ptep, addr, next, priv); + } while (pmd++, addr = next, addr != end); + + flush_tlb_kernel_range(start, end); +} + static void __remap_huge_page_pte_vmemmap(struct page *reuse, pte_t *ptep, unsigned long start, - unsigned long end, - struct list_head *remap_pages) + unsigned long end, void *priv) { pgprot_t pgprot = PAGE_KERNEL; void *from = page_to_virt(reuse); unsigned long addr; + struct list_head *pages = priv; for (addr = start; addr < end; addr += PAGE_SIZE) { void *to; struct page *page; pte_t entry, old = *ptep; - page = list_first_entry(remap_pages, struct page, lru); + page = list_first_entry(pages, struct page, lru); list_del(&page->lru); to = page_to_virt(page); copy_page(to, from); @@ -196,28 +221,6 @@ static void __remap_huge_page_pte_vmemmap(struct page *reuse, pte_t *ptep, } } -static void __remap_huge_page_pmd_vmemmap(pmd_t *pmd, unsigned long start, - unsigned long end, - struct list_head *vmemmap_pages) -{ - unsigned long next, addr = start; - struct page *reuse = NULL; - - do { - pte_t *ptep; - - ptep = pte_offset_kernel(pmd, addr); - if (!reuse) - reuse = pte_page(ptep[TAIL_PAGE_REUSE]); - - next = vmemmap_hpage_addr_end(addr, end); - __remap_huge_page_pte_vmemmap(reuse, ptep, addr, next, - vmemmap_pages); - } while (pmd++, addr = next, addr != end); - - flush_tlb_kernel_range(start, end); -} - static inline void alloc_vmemmap_pages(struct hstate *h, struct list_head *list) { unsigned int nr = free_vmemmap_pages_per_hpage(h); @@ -258,7 +261,8 @@ void alloc_huge_page_vmemmap(struct hstate *h, struct page *head) start = vmemmap_addr + RESERVE_VMEMMAP_SIZE; end = vmemmap_addr + vmemmap_pages_size_per_hpage(h); - __remap_huge_page_pmd_vmemmap(pmd, start, end, &map_pages); + remap_huge_page_pmd_vmemmap(pmd, start, end, + __remap_huge_page_pte_vmemmap, &map_pages); } static inline void free_vmemmap_page_list(struct list_head *list) @@ -273,13 +277,13 @@ static inline void free_vmemmap_page_list(struct list_head *list) static void __free_huge_page_pte_vmemmap(struct page *reuse, pte_t *ptep, unsigned long start, - unsigned long end, - struct list_head *free_pages) + unsigned long end, void *priv) { /* Make the tail pages are mapped read-only. */ pgprot_t pgprot = PAGE_KERNEL_RO; pte_t entry = mk_pte(reuse, pgprot); unsigned long addr; + struct list_head *pages = priv; for (addr = start; addr < end; addr += PAGE_SIZE, ptep++) { struct page *page; @@ -287,34 +291,12 @@ static void __free_huge_page_pte_vmemmap(struct page *reuse, pte_t *ptep, VM_WARN_ON(!pte_present(old)); page = pte_page(old); - list_add(&page->lru, free_pages); + list_add(&page->lru, pages); set_pte_at(&init_mm, addr, ptep, entry); } } -static void __free_huge_page_pmd_vmemmap(pmd_t *pmd, unsigned long start, - unsigned long end, - struct list_head *vmemmap_pages) -{ - unsigned long next, addr = start; - struct page *reuse = NULL; - - do { - pte_t *ptep; - - ptep = pte_offset_kernel(pmd, addr); - if (!reuse) - reuse = pte_page(ptep[TAIL_PAGE_REUSE]); - - next = vmemmap_hpage_addr_end(addr, end); - __free_huge_page_pte_vmemmap(reuse, ptep, addr, next, - vmemmap_pages); - } while (pmd++, addr = next, addr != end); - - flush_tlb_kernel_range(start, end); -} - void free_huge_page_vmemmap(struct hstate *h, struct page *head) { pmd_t *pmd; @@ -330,7 +312,8 @@ void free_huge_page_vmemmap(struct hstate *h, struct page *head) start = vmemmap_addr + RESERVE_VMEMMAP_SIZE; end = vmemmap_addr + vmemmap_pages_size_per_hpage(h); - __free_huge_page_pmd_vmemmap(pmd, start, end, &free_pages); + remap_huge_page_pmd_vmemmap(pmd, start, end, + __free_huge_page_pte_vmemmap, &free_pages); free_vmemmap_page_list(&free_pages); }