Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp155829pxb; Wed, 8 Sep 2021 20:26:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxNRRNxdl9AuU2NqmH4OX3MW9uKqLrqEfSS5jyivjsQw3S9+ygLDrmJvFi8euggExkggO8G X-Received: by 2002:a05:6e02:dc2:: with SMTP id l2mr738026ilj.76.1631158011897; Wed, 08 Sep 2021 20:26:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631158011; cv=none; d=google.com; s=arc-20160816; b=VxHlL6Ef4kJzk5EeIE/4QVKOwwIKXaKpVTWibW/o0RsvczLmdV7uEIY/2HuItEZdDz mY3P02eBvEZwNzcQAh3BT1kbJ9hHbI8cwXSTGqu60RrC4592KrUabkAf4knXKnV/Vir3 NVcCTqU6QJnm6y8fifKVV8gQAHsHlsqUgugvC93F1rRjNAmQ2RZqJdBNdR7f5OI9xFoA Y0jcgpgQjbcGm0jMO1dI4pdT9z73ujDGTkvXQSyoj27xPFX4zRtvsfy8gQfSbdnMQeJJ yqYql6sxBlgE/ji4sCMz2gM2Vuj4TdWEWbc1tQvOwcy1Ve7Rxrzd1xHDUS4g6U45OHax m2YA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:from:references:cc:to :subject; bh=NFkvwG7DDZ/1BD7SdJbb5WFg6p/rELcvcjnN1s5hg+8=; b=Qb8LZQOGea54ZT8j06Fkva+7gUI+z/sFPfLAV2K0PDSMPenqE2e4HM+s40Jx4jFRSP PGpS+XpeRp0h3u3iMgxwuMNLaIbEzjcnTZsjtq5tPWQCD+0zGKK8SQJC3AvZw0NhHmBd 0AdpjiuhruBFv0459zEeTfSa1ttRe5fKpQ37VycSsrztqU6gZT1BaRDQeDnkwHiNuP41 GnR222g3e6GH+z+jKW1mDiGWEmfbD2Wmm/pQYJzalCi3WouJ0Jhp2dX1nbTOItIjsePt 4f5MtIGbQ039Nc79A83ZVFHiTe5TYUIvW4aLPh++WZTprnHo+oCpmjIfddb0QnESZ0Fc FigQ== ARC-Authentication-Results: i=1; mx.google.com; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n14si550700ilm.165.2021.09.08.20.26.40; Wed, 08 Sep 2021 20:26:51 -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; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349998AbhIIDYn (ORCPT + 99 others); Wed, 8 Sep 2021 23:24:43 -0400 Received: from szxga03-in.huawei.com ([45.249.212.189]:15310 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237357AbhIIDYn (ORCPT ); Wed, 8 Sep 2021 23:24:43 -0400 Received: from dggemv711-chm.china.huawei.com (unknown [172.30.72.54]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4H4kpt1lzlz8stp; Thu, 9 Sep 2021 11:23:02 +0800 (CST) Received: from kwepemm600003.china.huawei.com (7.193.23.202) by dggemv711-chm.china.huawei.com (10.1.198.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.8; Thu, 9 Sep 2021 11:23:32 +0800 Received: from [10.174.179.79] (10.174.179.79) by kwepemm600003.china.huawei.com (7.193.23.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.8; Thu, 9 Sep 2021 11:23:31 +0800 Subject: Re: [PATCH v2 -next] riscv: mm: remove redundant trampoline PGD for 64bit To: Alex Ghiti , Palmer Dabbelt CC: Paul Walmsley , , "Anup Patel" , , , Atish Patra , , , , , , References: <150d9366-c042-3119-d86f-163b579d91b4@huawei.com> <13c4d471-dc3b-32d8-6535-36d3862592d4@ghiti.fr> From: Nanyong Sun Message-ID: <678f7a40-988a-ce60-0b7a-2a70fe28ecd1@huawei.com> Date: Thu, 9 Sep 2021 11:23:30 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: <13c4d471-dc3b-32d8-6535-36d3862592d4@ghiti.fr> Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 8bit X-Originating-IP: [10.174.179.79] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To kwepemm600003.china.huawei.com (7.193.23.202) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2021/9/8 16:56, Alex Ghiti wrote: > Hi Nanyong, > > Le 8/09/2021 à 08:42, Nanyong Sun a écrit : >> >> On 2021/8/14 6:08, Palmer Dabbelt wrote: >>> On Mon, 02 Aug 2021 05:43:02 PDT (-0700), alex@ghiti.fr wrote: >>>> Hi Nanyong, >>>> >>>> Le 28/07/2021 à 13:55, Alex Ghiti a écrit : >>>>> >>>>> >>>>> Le 28/07/2021 à 04:49, Nanyong Sun a écrit : >>>>>> Remove redundant trampoline PGD for 64bit and add more comment >>>>>> for why 32bit systems need trampoline PGD. >>>>>> >>>>>> >>>>>> +load_kernel_pgd: >>>>>> +        /* >>>>>> +         * Switch to kernel page tables.  A full fence is necessary >>>>>> in order to >>>>>> +         * avoid using the trampoline translations, which are only >>>>>> correct for >>>>>> +         * the first superpage.  Fetching the fence is guarnteed >>>>>> to work >>>>>> +         * because that first superpage is translated the same way. >>>>>> +         */ >>>>>> +        csrw CSR_SATP, a2 >>>>>> +        sfence.vma >>>>>> + >>>>>> +load_done: >>>>>>       /* Set trap vector to spin forever to help debug */ >>>>>>       la a0, .Lsecondary_park >>>>>>       csrw CSR_TVEC, a0 >>>> >>>> >>>> I suppose stvec was set this way to catch any problem with >>>> early_pg_dir, >>>> you moved that and then this defeats this original purpose. >>> >> Hi Alex, >> >>      I don't think so, before set early_pg_dir to satp, it's the >> physical address world, we must set stvec as >> >> the first place in virtual address world we want jump to. And I don't >> think ".Lsecondary_park " can catch >> >> problem of bad early_pg_dir, if the basic page table is wrong, CPU >> also can not go to the virtual address stored in stvec correctly. > > But I think then that it loops forever at the stvec address which > allows to know where the boot failed. If satp had a problem, then cpu can not fetch instruction where stvec pointing to, as what palmer said: if you end up in a position where the processer is unable to commit an instruction you also lose the ability to do anything meaningful with the debugger, thus essentially locking up the system. > >> >> More, in the original code, before set trampoline_pg_dir, what if the >> trampoline_pg_dir had a problem? > > You're right but this debug 'feature' was not installed, I guess > somebody had a hard time at some point with the early page table and > not the trampoline :) > > Anyway, I was just pointing that you 'broke' the current way things > work and unless this is for an explicit good reason, that should not > happen. > The design logic is: at the first time cpu convert to virtual address world from physical world, actually stvec is a real "trampoline",  it can not be set as a pointer to spin trap, it should be set to the first place in virtual world where we wanna go. After that, then we set stvec as a spin trap to catch any problem in later running. So, for 64bit system, if we want to delete  trampoline_pg_dir, the design principle is not broken here. For 32bit system, I really need change back. >> >>> Essentially. >>> >>> The specific issue is that the JTAG debug spec is defined (or at >>> least was when I was using it, it's been years since I've needed to >>> do that) in terms of committed instructions.  Thus if you end up in >>> a position where the processer is unable to commit an instruction >>> you also lose the ability to do anything meaningful with the >>> debugger, thus essentially locking up the system. >>> >>> The most common way to end up in a situation where the processor is >>> unable to commit an instruction is to have a fault with an invalid >>> trap vector: maybe dangling from M-mode, the last boot, reset, >>> whatever.  Then as soon as you take a trap the system locks up.  Any >>> trap before we have a working trap handler is a bug, but it's way >>> harder to debug things when the debugger doesn't function. >>> >>> There is of course no way to fundamentally prevent these sort of >>> no-commitable-instruction situations, but I got into the habbit of >>> just setting up a trivial trap entry point ASAP -- it probably took >>> a dozen rounds of trying to debug the debugger only to realize it >>> was per spec to hang, but that idiom eventually crept into pretty >>> much everything. >>> >>> Not sure if the debug spec is still written this way (or if >>> debuggers respect it), as I haven't had to use one in a while. >>> >>>> >>>> >>>>>> >>>>>> diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c >>>>>> index ac48742fa6fc..306fcb2334fa 100644 >>>>>> --- a/arch/riscv/mm/init.c >>>>>> +++ b/arch/riscv/mm/init.c >>>>>> @@ -219,13 +219,17 @@ unsigned long pfn_base __ro_after_init; >>>>>>   EXPORT_SYMBOL(pfn_base); >>>>>>   pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss; >>>>>> +#ifndef CONFIG_64BIT >>>>>>   pgd_t trampoline_pg_dir[PTRS_PER_PGD] __page_aligned_bss; >>>>>> +#endif /* CONFIG_64BIT */ >>>> >>>> >>>> As stated in Documentation/process/coding-style.rst, it is better >>>> to use >>>> __maybe_unused rather than #ifdefs. >>>> >>>> >> I'm afraid that __maybe_unused can not save one page memory here. > > What do you mean? > I mean trampoline_pg_dir cost 4096 bytes here and __maybe_unused only tell compiler don't raise a warning, but it still cost memory.