Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2155350pxj; Sun, 13 Jun 2021 10:23:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxo5qvgWcT9/v5oWsPCvi0MoaWhKPOFxVw9MeLRS7yDSMTKJ4s20/FKc9Pp5wU/nYhlKYh+ X-Received: by 2002:a17:906:28d5:: with SMTP id p21mr11849574ejd.358.1623604984592; Sun, 13 Jun 2021 10:23:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623604984; cv=none; d=google.com; s=arc-20160816; b=h1mNOIi3G/AXDkGCTdZ+5ilU4aqyezsararMcaU4oirDf8nOmVeZilEpQ3ThNtxz+A MbKMs73hC6i3AZlfU2EIe/DuHIQrilFWoQ5wZV6v8X99voCT8poRJVZQTgJ9/xh17TZA n0kbQx6RG5AXk5ZAAS9V+dzZ6kpJwlTvVC7LB+YIakmR2qS8rGFtjf8dslOTgKYrXBhQ RY6CxjElpiE9U2DLcFjX9qYo1eSDBZRXeyfLPlvaAnyeoAKR5Y5G1t7D6Oa7SxP12h7g N4VIIqMDAf/ixtqAzwghXO9jbBEpIJPkpgVrv6lxI75dlP7MAgkoyBjVv4VdlHO9YLxZ nvNg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=9YrEcNG/N2YFUqtgoD+2CSLrFOJntdnn7MCq89XsJdc=; b=lZkFgK17qGyYKu72uNhl2jv6QVmXXpowkxwNagqw4Ac7t/19IbbMAprOq2l5VVumHO yOZwPU8oWYOZw5mSQCFR3wZeyK1r0IQJpp6U+UjOWeSXtQOrQLjUTOSKr/NvOI/K69ys fSxA5XeJvmG1NV/tBjybGU1HrB6og1V/qlqtoc2nvX/8B0RAAvGZuCBvEQzBh0mdjbkB j9wXAY8T/Xe3f9PMOQUyEFAjawe5uwvEzt1SV0WTLt9PIfIR9FfmT/mS5T0OL/lhg4J4 cihvVH7OkvhEF+vvtscCKfq8hC7QyDnBIwlwAWUngiGGRI384Jx3EYzQ5JGQeUIMWkVn pHMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@mail.ustc.edu.cn header.s=dkim header.b=oZ8DSkyl; 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=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mail.ustc.edu.cn Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bx25si8795294ejc.431.2021.06.13.10.22.13; Sun, 13 Jun 2021 10:23:04 -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=@mail.ustc.edu.cn header.s=dkim header.b=oZ8DSkyl; 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=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mail.ustc.edu.cn Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231969AbhFMROC (ORCPT + 99 others); Sun, 13 Jun 2021 13:14:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33598 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231902AbhFMRN7 (ORCPT ); Sun, 13 Jun 2021 13:13:59 -0400 Received: from ustc.edu.cn (email6.ustc.edu.cn [IPv6:2001:da8:d800::8]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 1747DC061574; Sun, 13 Jun 2021 10:11:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mail.ustc.edu.cn; s=dkim; h=Received:Date:From:To:Cc:Subject: Message-ID:In-Reply-To:References:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=9YrEcNG/N2YFUqtgoD+2CSLrFOJntdnn7M Cq89XsJdc=; b=oZ8DSkyl3+yzFHd/bMM1IVyZR4lHE4DiiDHKsVLWkCHYILS2SF xqU3nsWL92FjLm8qwLrgL7LPMQWfHnVLRWkWOM/qBCEYJtlOiAm75Yseriobp9hY u220xym9G2f7sidk/jOrCjv269pdfSloeqBcTIit4McfRTumFBZkhXhCI= Received: from xhacker (unknown [101.86.20.15]) by newmailweb.ustc.edu.cn (Coremail) with SMTP id LkAmygCHj1s2PMZgI9DTAA--.35616S2; Mon, 14 Jun 2021 01:11:19 +0800 (CST) Date: Mon, 14 Jun 2021 01:05:46 +0800 From: Jisheng Zhang To: Andreas Schwab Cc: Paul Walmsley , Palmer Dabbelt , Albert Ou , Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , =?UTF-8?B?QmrDtnJuIFTDtnBlbA==?= , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Luke Nelson , Xi Wang , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, netdev@vger.kernel.org, bpf@vger.kernel.org Subject: Re: [PATCH 7/9] riscv: bpf: Avoid breaking W^X Message-ID: <20210614010546.7a0d5584@xhacker> In-Reply-To: <87bl8cqrpv.fsf@igel.home> References: <20210330022144.150edc6e@xhacker> <20210330022521.2a904a8c@xhacker> <87o8ccqypw.fsf@igel.home> <20210612002334.6af72545@xhacker> <87bl8cqrpv.fsf@igel.home> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-CM-TRANSID: LkAmygCHj1s2PMZgI9DTAA--.35616S2 X-Coremail-Antispam: 1UD129KBjvJXoWxAw1fKF4UZF1fWF17XFW3Awb_yoW5XF4fpr 1UCFWfKryvqr1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, 11 Jun 2021 18:41:16 +0200 Andreas Schwab wrote: > On Jun 12 2021, Jisheng Zhang wrote: > > > I reproduced an kernel panic with the defconfig on qemu, but I'm not sure whether > > this is the issue you saw, I will check. > > > > 0.161959] futex hash table entries: 512 (order: 3, 32768 bytes, linear) > > [ 0.167028] pinctrl core: initialized pinctrl subsystem > > [ 0.190727] Unable to handle kernel paging request at virtual address ffffffff81651bd8 > > [ 0.191361] Oops [#1] > > [ 0.191509] Modules linked in: > > [ 0.191814] CPU: 0 PID: 1 Comm: swapper/0 Not tainted 5.13.0-rc5-default+ #3 > > [ 0.192179] Hardware name: riscv-virtio,qemu (DT) > > [ 0.192492] epc : __memset+0xc4/0xfc > > [ 0.192712] ra : skb_flow_dissector_init+0x22/0x86 > > Yes, that's the same. > > Andreas. > I think I found the root cause: commit 2bfc6cd81bd ("move kernel mapping outside of linear mapping") moves BPF JIT region after the kernel: #define BPF_JIT_REGION_START PFN_ALIGN((unsigned long)&_end) The &_end is unlikely aligned with PMD SIZE, so the front bpf jit region sits with kernel .data section in one PMD. But kenrel is mapped in PMD SIZE, so when bpf_jit_binary_lock_ro() is called to make the first bpf jit prog ROX, we will make part of kernel .data section RO too, so when we write, for example memset the .data section, MMU will trigger store page fault. To fix the issue, we need to make the bpf jit region PMD size aligned by either patch BPF_JIT_REGION_START to align on PMD size rather than PAGE SIZE, or something as below patch to move the BPF region before modules region: diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 9469f464e71a..997b894edbc2 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -31,8 +31,8 @@ #define BPF_JIT_REGION_SIZE (SZ_128M) #ifdef CONFIG_64BIT /* KASLR should leave at least 128MB for BPF after the kernel */ -#define BPF_JIT_REGION_START PFN_ALIGN((unsigned long)&_end) -#define BPF_JIT_REGION_END (BPF_JIT_REGION_START + BPF_JIT_REGION_SIZE) +#define BPF_JIT_REGION_START (BPF_JIT_REGION_END - BPF_JIT_REGION_SIZE) +#define BPF_JIT_REGION_END (MODULES_VADDR) #else #define BPF_JIT_REGION_START (PAGE_OFFSET - BPF_JIT_REGION_SIZE) #define BPF_JIT_REGION_END (VMALLOC_END) @@ -40,8 +40,8 @@ /* Modules always live before the kernel */ #ifdef CONFIG_64BIT -#define MODULES_VADDR (PFN_ALIGN((unsigned long)&_end) - SZ_2G) #define MODULES_END (PFN_ALIGN((unsigned long)&_start)) +#define MODULES_VADDR (MODULES_END - SZ_128M) #endif can you please try it? Per my test, the issue is fixed. Thanks