Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp85819ybt; Thu, 18 Jun 2020 19:04:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxectVZA5xXgY4LCl0YGVG1WyyRE34u5u1D/FlrDCfEWwCTxS8Xc0Gh9GaGm9g7a1RhLlr2 X-Received: by 2002:a17:906:344c:: with SMTP id d12mr1442696ejb.130.1592532299619; Thu, 18 Jun 2020 19:04:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592532299; cv=none; d=google.com; s=arc-20160816; b=JQWeyWJU5i3UF4T99Cdt0Wq4XGokuTV4EIN1hrbMABhT2xDhXIZu70f+JFfeYR5Bmb kiy0oaR4a/fB7FwPa/4w8mw/IlxXfX+TpkoWVKPsmZB7gytPYZfrGa7cMgioO0xiBnMk Zi6SdMFOgShNQfGm/ifcevdkSVIRdjSlN9uuVhDP6Ue29jQk4/o2Bhi4o4/pgcw/EGLy IqoS0zMYcCY5p0IXvSf8G2bag+EcmY7OfJeQd/nRnF+8845WuYQRIy+W/9Faykvs/XD+ RpNNJWafCigCy8T4S4LbnoNHuEgygOEhOPdpkSJhcqHfN8RJNE4HIkUHyQvKlDMb1DmT gGXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=hSUmoRRwgG54n+dXHVLG7g+3QD6ietfWE7Sc1nJWilA=; b=WZwRjh7regfYxWcNjvzpLmiHSNqxVDmKGSlNseiFBSAMDWn9qbrj2uSMjKBi7hFPWm oHI8GKxwMNjsIM0kQhBizvsXa1sJ+Jrm1xhibrK5JL1HNn0QQwXEhcAOW/M1QL3imiyM dExC/E0aOtL1BePPxgkAcZ1hTznSBe1vOj4V7VOmsVICy31gnu4y0aloW+chG7+jfcY+ jJl91sfX0gW8WXEnZJkWXktPdOVDD+SI0nuEbuGQLQzGDA9X76JRVTPxlgIVrTLWtoGO +NtxusKh0XL3eGfPG/l/jvACdlT5sxe4WNdTy7ecktfG4PzwJj7KHD8jWoNwJD692uiH Vo9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@atishpatra.org header.s=google header.b=VF36gC7r; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k4si2810916ejg.386.2020.06.18.19.04.37; Thu, 18 Jun 2020 19:04:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@atishpatra.org header.s=google header.b=VF36gC7r; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729469AbgFSArp (ORCPT + 99 others); Thu, 18 Jun 2020 20:47:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37548 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727109AbgFSAro (ORCPT ); Thu, 18 Jun 2020 20:47:44 -0400 Received: from mail-wm1-x341.google.com (mail-wm1-x341.google.com [IPv6:2a00:1450:4864:20::341]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0B026C06174E for ; Thu, 18 Jun 2020 17:47:43 -0700 (PDT) Received: by mail-wm1-x341.google.com with SMTP id a6so3531657wmm.5 for ; Thu, 18 Jun 2020 17:47:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=atishpatra.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=hSUmoRRwgG54n+dXHVLG7g+3QD6ietfWE7Sc1nJWilA=; b=VF36gC7rzlmmpFllwB7XmfOD3rj0Qjlb+6Xqyr5EC9avDZVM6t/m01GDWsT8he5R64 xFbfR7DIW3ZUy0oClJNFpyePtaFDzMgvExc7mI8KuuOZye7wYH+3sPiYR1KGDagpuGR4 99fm8PVCja6oeGgtKfTD5MFQwx5qKklJ3CJw4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=hSUmoRRwgG54n+dXHVLG7g+3QD6ietfWE7Sc1nJWilA=; b=bcGM/63ZuZ8JZZixj0QkyazONm2jsnpP0h1vKNw1sTMIRJIi/2eJAyRORaNMKME78v 5wYiwrrxEXFS7INA+ZL1fonoJJCaY1N1Z0u3+EBAfrwoCBD34w9VlG8KgfQhFS9CTTm9 FZbSJvdwrbswasLsvneQZpof372SNMRrsGO26wMd87qWYLS58Is0wDIfbx+rBf5rKh4Q f9BHDTp2D5OA6q3biBFunktZ5LbHdt56YSpygebXzxaeTPrCnyQ3DTr4UfmrUkQON6XW 4kDMJAWO/7vgXbWqRbpad+OdBqaXNwdpSgSUD/g/vMBIqumVOBiL4i6BCyQIXAjXM3xC +Ncg== X-Gm-Message-State: AOAM531NCfVaTqxCP0X9ZHmlHtRnmqHSSVbzDFDxxAc0IcKqdIGdedXs P8qtP4rw5yHga0xvd64r1V4nj8IGDOsR3V0L1E6k X-Received: by 2002:a1c:2e0e:: with SMTP id u14mr925037wmu.55.1592527661718; Thu, 18 Jun 2020 17:47:41 -0700 (PDT) MIME-Version: 1.0 References: <20200603153608.30056-1-alex@ghiti.fr> <20200603153608.30056-3-alex@ghiti.fr> In-Reply-To: <20200603153608.30056-3-alex@ghiti.fr> From: Atish Patra Date: Thu, 18 Jun 2020 17:47:30 -0700 Message-ID: Subject: Re: [PATCH 2/2] riscv: Use PUD/PGDIR entries for linear mapping when possible To: Alexandre Ghiti Cc: Paul Walmsley , Palmer Dabbelt , Anup Patel , Atish Patra , linux-riscv , "linux-kernel@vger.kernel.org List" Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jun 3, 2020 at 8:38 AM Alexandre Ghiti wrote: > > Improve best_map_size so that PUD or PGDIR entries are used for linear > mapping when possible as it allows better TLB utilization. > > Signed-off-by: Alexandre Ghiti > --- > arch/riscv/mm/init.c | 45 +++++++++++++++++++++++++++++++++----------- > 1 file changed, 34 insertions(+), 11 deletions(-) > > diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c > index 9a5c97e091c1..d275f9f834cf 100644 > --- a/arch/riscv/mm/init.c > +++ b/arch/riscv/mm/init.c > @@ -424,13 +424,29 @@ static void __init create_pgd_mapping(pgd_t *pgdp, > create_pgd_next_mapping(nextp, va, pa, sz, prot); > } > > -static uintptr_t __init best_map_size(phys_addr_t base, phys_addr_t size) > +static bool is_map_size_ok(uintptr_t map_size, phys_addr_t base, > + uintptr_t base_virt, phys_addr_t size) > { > - /* Upgrade to PMD_SIZE mappings whenever possible */ > - if ((base & (PMD_SIZE - 1)) || (size & (PMD_SIZE - 1))) > - return PAGE_SIZE; > + return !((base & (map_size - 1)) || (base_virt & (map_size - 1)) || > + (size < map_size)); > +} > + > +static uintptr_t __init best_map_size(phys_addr_t base, uintptr_t base_virt, > + phys_addr_t size) > +{ > +#ifndef __PAGETABLE_PMD_FOLDED > + if (is_map_size_ok(PGDIR_SIZE, base, base_virt, size)) > + return PGDIR_SIZE; > + > + if (pgtable_l4_enabled) > + if (is_map_size_ok(PUD_SIZE, base, base_virt, size)) > + return PUD_SIZE; > +#endif > + > + if (is_map_size_ok(PMD_SIZE, base, base_virt, size)) > + return PMD_SIZE; > > - return PMD_SIZE; > + return PAGE_SIZE; > } > > /* > @@ -576,7 +592,7 @@ void create_kernel_page_table(pgd_t *pgdir, uintptr_t map_size) > asmlinkage void __init setup_vm(uintptr_t dtb_pa) > { > uintptr_t va, end_va; > - uintptr_t map_size = best_map_size(load_pa, MAX_EARLY_MAPPING_SIZE); > + uintptr_t map_size; > > load_pa = (uintptr_t)(&_start); > load_sz = (uintptr_t)(&_end) - load_pa; > @@ -587,6 +603,7 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa) > > kernel_virt_addr = KERNEL_VIRT_ADDR; > > + map_size = best_map_size(load_pa, PAGE_OFFSET, MAX_EARLY_MAPPING_SIZE); > va_pa_offset = PAGE_OFFSET - load_pa; > va_kernel_pa_offset = kernel_virt_addr - load_pa; > pfn_base = PFN_DOWN(load_pa); > @@ -700,6 +717,8 @@ static void __init setup_vm_final(void) > > /* Map all memory banks */ > for_each_memblock(memory, reg) { > + uintptr_t remaining_size; > + > start = reg->base; > end = start + reg->size; > > @@ -707,15 +726,19 @@ static void __init setup_vm_final(void) > break; > if (memblock_is_nomap(reg)) > continue; > - if (start <= __pa(PAGE_OFFSET) && > - __pa(PAGE_OFFSET) < end) > - start = __pa(PAGE_OFFSET); > > - map_size = best_map_size(start, end - start); > - for (pa = start; pa < end; pa += map_size) { > + pa = start; > + remaining_size = reg->size; > + > + while (remaining_size) { > va = (uintptr_t)__va(pa); > + map_size = best_map_size(pa, va, remaining_size); > + > create_pgd_mapping(swapper_pg_dir, va, pa, > map_size, PAGE_KERNEL); > + > + pa += map_size; > + remaining_size -= map_size; > } > } > This may not work in the RV32 with 2G memory and if the map_size is determined to be a page size for the last memblock. Both pa & remaining_size will overflow and the loop will try to map memory from zero again. > -- > 2.20.1 > > -- Regards, Atish