Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp4031103ybv; Sun, 16 Feb 2020 11:58:01 -0800 (PST) X-Google-Smtp-Source: APXvYqxWwkvTU830oBELiwbqBDoyF3kqKzO0Mjfw1PGH+tvxN9CUb1BSAG87vIflh5Ql2FvFlg6t X-Received: by 2002:a05:6808:b13:: with SMTP id s19mr7681421oij.119.1581883081598; Sun, 16 Feb 2020 11:58:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581883081; cv=none; d=google.com; s=arc-20160816; b=n7Ep0hxpxvzGOhrXO1BVlhjkVJUpQlJRYhDWtigCqLpHUKdVlCssZQ6fXEXZMipweV q7j4k95tP2UbUjuw7RR5TERgRyPp1FMP1xUgSPZCX+547lJWLhFD/AYRn6iOe8RATnVQ izWtbzABDEgihzyuSbVAm5Bl7zsr4nIBnDZp0QfmeVv7ZsJfpkIZcRsVRXDaj/XvzgGO 66dAdXEC8w4laeBshz15g2qtH5OO7SuVoH1ksDQHJH6l1wDZ/+/jODGfxrMTIYClLDg4 RHWKEHeTK6HcJ4spFDzbLxKLVY302wGKRcdejkBqqpq+V9nx5uU4ocWv30h1hLDHYsi6 Bfaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=+XQyQuYytrO5gDTwaDpFBdZzyIFaPAu6G5Sk9BfjJuA=; b=dMh9stFvVIL41bCZxgY7gaC2Mi+1E26g0aa0NPaoFMIRD+e6utmYsMV/gWQ7lNfG/b JSpvPG0RRpWWI9IaCU3OnusTdBz83Dcy0BX2+qAiXByes8MyWu6xq2bRrOJuPGfrWbOg Wl9LYZcZ+f3McDqpgq5il2Xht4JtAZ9gTiC+Ud84awQE3zITFLy80y4RNmB1s93dt/uS t8FKIELHaC4/fGFn2mzJk8vZ0StHQMM2kiDGf5S5DkpGQzbC1L6hhFgOPBe629cADxKL ThjJ0L9ruhY9QbMHC3KwJgC99MWbyVOdt/S7aroGDsl0fZxTV7jThRz2DtcilnlTdMkA bJ/w== 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 k1si5942802oic.245.2020.02.16.11.57.49; Sun, 16 Feb 2020 11:58:01 -0800 (PST) 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 S1726962AbgBPT4Q (ORCPT + 99 others); Sun, 16 Feb 2020 14:56:16 -0500 Received: from relay6-d.mail.gandi.net ([217.70.183.198]:54405 "EHLO relay6-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726036AbgBPT4P (ORCPT ); Sun, 16 Feb 2020 14:56:15 -0500 X-Originating-IP: 79.86.19.127 Received: from [192.168.0.12] (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay6-d.mail.gandi.net (Postfix) with ESMTPSA id 40287C0005; Sun, 16 Feb 2020 19:56:12 +0000 (UTC) Subject: Re: [PATCH v2 3/3] riscv: Fix crash when flushing executable ioremap regions To: Jan Kiszka , Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-riscv@lists.infradead.org Cc: linux-kernel@vger.kernel.org References: <8a555b0b0934f0ba134de92f6cf9db8b1744316c.1581767384.git.jan.kiszka@web.de> From: Alex Ghiti Message-ID: <441527ef-1fd4-ed98-8381-8902c4e05fc5@ghiti.fr> Date: Sun, 16 Feb 2020 14:56:10 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2/16/20 11:05 AM, Jan Kiszka wrote: > On 16.02.20 15:41, Alex Ghiti wrote: >> Hi Jan, >> >> On 2/15/20 6:49 AM, Jan Kiszka wrote: >>> From: Jan Kiszka >>> >>> Those are not backed by page structs, and pte_page is returning an >>> invalid pointer. >>> >>> Signed-off-by: Jan Kiszka >>> =2D-- >>> ? arch/riscv/mm/cacheflush.c | 3 ++- >>> ? 1 file changed, 2 insertions(+), 1 deletion(-) >>> >>> diff --git a/arch/riscv/mm/cacheflush.c b/arch/riscv/mm/cacheflush.c >>> index 8930ab7278e6..9ee2c1a387cc 100644 >>> =2D-- a/arch/riscv/mm/cacheflush.c >>> +++ b/arch/riscv/mm/cacheflush.c >>> @@ -84,7 +84,8 @@ void flush_icache_pte(pte_t pte) >>> ? { >>> ????? struct page *page =3D pte_page(pte); >>> >>> -??? if (!test_and_set_bit(PG_dcache_clean, &page->flags)) >>> +??? if (!pfn_valid(pte_pfn(pte)) || >>> +??????? !test_and_set_bit(PG_dcache_clean, &page->flags)) >>> ????????? flush_icache_all(); >>> ? } >>> ? #endif /* CONFIG_MMU */ >>> =2D- >>> 2.16.4 >>> >>> >> >> When did you encounter such a situation ? i.e. executable code that is >> not backed by struct page ? >> >> Riscv uses the generic implementation of ioremap and the way >> _PAGE_IOREMAP is defined does not allow to map executable memory region >> using ioremap, so I'm interested to understand how we end up in >> flush_icache_pte for an executable region not backed by any struct page. > > You can create executable mappings of memory that Linux does not > initially consider as RAM via ioremap_prot or ioremap_page_range. We are > using that in Jailhouse to load the hypervisor code into reserved memory > that is ioremapped for the purpose. Works fine on x86, arm and arm64. > > Jan Ok thanks, I had missed this API. Regarding your patch, I find it weird to do anything if the pfn is invalid, we could have garbage in pte pointing to an invalid region for example (I admit that the effect of flushing the icache would not be catastrophic in that situation). I'm not saying I will come with a better solution but I'll take a deeper look tomorrow. Alex