linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jinyang He <hejinyang@loongson.cn>
To: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
Cc: Huacai Chen <chenhuacai@kernel.org>,
	Jiaxun Yang <jiaxun.yang@flygoat.com>,
	linux-mips@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] MIPS: relocatable: Provide kaslr_offset() to get the kernel offset
Date: Tue, 2 Feb 2021 13:55:25 +0800	[thread overview]
Message-ID: <e813890a-a091-b831-98f1-725e28d81742@loongson.cn> (raw)
In-Reply-To: <20210201125028.GA8621@alpha.franken.de>

On 02/01/2021 08:50 PM, Thomas Bogendoerfer wrote:

> On Wed, Jan 27, 2021 at 12:12:25PM +0800, Jinyang He wrote:
>> Use kimage_vaddr to indicate kernel start address. Provide kaslr_offset()
>> to get the kernel offset when KASLR is enabled. Error may occur before
>> update_kimage_vaddr(), so put it at the end of the offset branch.
>>
>> Fixes: a307a4ce9ecd ("MIPS: Loongson64: Add KASLR support")
>> Reported-by: kernel test robot <lkp@intel.com>
>> Signed-off-by: Jinyang He <hejinyang@loongson.cn>
>> ---
>>   arch/mips/include/asm/page.h |  6 ++++++
>>   arch/mips/kernel/relocate.c  | 12 ++++++++++++
>>   arch/mips/kernel/setup.c     |  3 +++
>>   3 files changed, 21 insertions(+)
>>
>> diff --git a/arch/mips/include/asm/page.h b/arch/mips/include/asm/page.h
>> index 6a77bc4..9429520 100644
>> --- a/arch/mips/include/asm/page.h
>> +++ b/arch/mips/include/asm/page.h
>> @@ -255,6 +255,12 @@ extern bool __virt_addr_valid(const volatile void *kaddr);
>>   
>>   #define VM_DATA_DEFAULT_FLAGS	VM_DATA_FLAGS_TSK_EXEC
>>   
>> +extern unsigned long kimage_vaddr;
>> +static inline unsigned long kaslr_offset(void)
>> +{
>> +	return kimage_vaddr - VMLINUX_LOAD_ADDRESS;
>> +}
> this breaks for 32bit kernels:
>
> <command-line>:0:22: error: large integer implicitly truncated to unsigned type [-Werror=overflow]
> /local/tbogendoerfer/korg/linux/arch/mips/kernel/setup.c:87:41: note: in expansion of macro ‘VMLINUX_LOAD_ADDRESS’
>   unsigned long kimage_vaddr __initdata = VMLINUX_LOAD_ADDRESS;
>                                           ^~~~~~~~~~~~~~~~~~~~
> cc1: all warnings being treated as errors
>

Sorry for not considering the 32bits kernel. Maybe a variable is enough.

Like this,

diff --git a/arch/mips/include/asm/page.h b/arch/mips/include/asm/page.h
index 6a77bc4a6eec..74082e35d57c 100644
--- a/arch/mips/include/asm/page.h
+++ b/arch/mips/include/asm/page.h
@@ -255,6 +255,12 @@ extern bool __virt_addr_valid(const volatile void 
*kaddr);

  #define VM_DATA_DEFAULT_FLAGS    VM_DATA_FLAGS_TSK_EXEC

+extern unsigned long __kaslr_offset;
+static inline unsigned long kaslr_offset(void)
+{
+    return __kaslr_offset;
+}
+
  #include <asm-generic/memory_model.h>
  #include <asm-generic/getorder.h>

diff --git a/arch/mips/kernel/relocate.c b/arch/mips/kernel/relocate.c
index c643c816cbe0..95abb9c82f00 100644
--- a/arch/mips/kernel/relocate.c
+++ b/arch/mips/kernel/relocate.c
@@ -300,6 +300,13 @@ static inline int __init relocation_addr_valid(void 
*loc_new)
      return 1;
  }

+static inline void __init update_kaslr_offset(unsigned long *addr, long 
offset)
+{
+    unsigned long *new_addr = (unsigned long *)RELOCATED(addr);
+
+    *new_addr = (unsigned long)offset;
+}
+
  #if defined(CONFIG_USE_OF)
  void __weak *plat_get_fdt(void)
  {
@@ -410,6 +417,9 @@ void *__init relocate_kernel(void)

          /* Return the new kernel's entry point */
          kernel_entry = RELOCATED(start_kernel);
+
+        /* Error may occur before, so keep it at last */
+        update_kaslr_offset(&__kaslr_offset, offset);
      }
  out:
      return kernel_entry;
diff --git a/arch/mips/kernel/setup.c b/arch/mips/kernel/setup.c
index 7e1f8e277437..cde234326738 100644
--- a/arch/mips/kernel/setup.c
+++ b/arch/mips/kernel/setup.c
@@ -84,6 +84,9 @@ static struct resource code_resource = { .name = 
"Kernel code", };
  static struct resource data_resource = { .name = "Kernel data", };
  static struct resource bss_resource = { .name = "Kernel bss", };

+unsigned long __kaslr_offset __initdata = 0;
+EXPORT_SYMBOL(__kaslr_offset);
+
  static void *detect_magic __initdata = detect_memory_region;

  #ifdef CONFIG_MIPS_AUTO_PFN_OFFSET

I'll send v2 later if no other comment.

Thanks,
Jinyang


      reply	other threads:[~2021-02-02  5:56 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-27  4:12 [PATCH] MIPS: relocatable: Provide kaslr_offset() to get the kernel offset Jinyang He
2021-02-01 12:50 ` Thomas Bogendoerfer
2021-02-02  5:55   ` Jinyang He [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e813890a-a091-b831-98f1-725e28d81742@loongson.cn \
    --to=hejinyang@loongson.cn \
    --cc=chenhuacai@kernel.org \
    --cc=jiaxun.yang@flygoat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mips@vger.kernel.org \
    --cc=tsbogend@alpha.franken.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).