From: Charlie Jenkins <charlie@rivosinc.com>
To: Yangyu Chen <cyy@cyyself.name>
Cc: linux-riscv@lists.infradead.org,
Paul Walmsley <paul.walmsley@sifive.com>,
Palmer Dabbelt <palmer@dabbelt.com>,
Albert Ou <aou@eecs.berkeley.edu>, Guo Ren <guoren@kernel.org>,
Andy Chiu <andy.chiu@sifive.com>,
Conor Dooley <conor.dooley@microchip.com>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/3] RISC-V: mm: fix mmap behavior in sv48 address space
Date: Fri, 19 Jan 2024 17:50:36 -0800 [thread overview]
Message-ID: <Zasm7D/bQH9XGHvf@ghost> (raw)
In-Reply-To: <tencent_871D1B705162CE42D21BD1F86E0447B44709@qq.com>
On Mon, Jan 15, 2024 at 03:58:30AM +0800, Yangyu Chen wrote:
> A commit add2cc6b6515 ("RISC-V: mm: Restrict address space for
> sv39,sv48,sv57") from patch[1] restricts regular mmap return address in the
> sv48 space if the address hint is not above the sv48 userspace address.
> However, this commit treats the address wrong which only use sv48 if the
> hint address is above sv48 user address space. Actually, it should use sv48
> if the address is above sv39 user address space. Moreover, the original
It is not valid to use any address in sv48 in this case. It is designed
to provide an address that is in sv39 to guarantee that the application
is able to handle any address that is returned by mmap. If, for example,
the application asks for the address 1<<42, it expects that all of the
bits above 42 are not used. The address provided by mmap must not
contain those bits, so it uses the sv39 address space.
- Charlie
> patch code looks very complex in logic, we can simplify it with min marco.
>
> [1]. https://lore.kernel.org/r/20230809232218.849726-2-charlie@rivosinc.com
>
> Signed-off-by: Yangyu Chen <cyy@cyyself.name>
> ---
> arch/riscv/include/asm/processor.h | 39 ++++++------------------------
> 1 file changed, 8 insertions(+), 31 deletions(-)
>
> diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h
> index e1944ff0757a..7ead6a3e1f12 100644
> --- a/arch/riscv/include/asm/processor.h
> +++ b/arch/riscv/include/asm/processor.h
> @@ -9,6 +9,7 @@
> #include <linux/const.h>
> #include <linux/cache.h>
> #include <linux/prctl.h>
> +#include <linux/minmax.h>
>
> #include <vdso/processor.h>
>
> @@ -18,37 +19,13 @@
> #define DEFAULT_MAP_WINDOW (UL(1) << (MMAP_VA_BITS - 1))
> #define STACK_TOP_MAX TASK_SIZE
>
> -#define arch_get_mmap_end(addr, len, flags) \
> -({ \
> - unsigned long mmap_end; \
> - typeof(addr) _addr = (addr); \
> - if ((_addr) == 0 || (IS_ENABLED(CONFIG_COMPAT) && is_compat_task())) \
> - mmap_end = STACK_TOP_MAX; \
> - else if ((_addr) >= VA_USER_SV57) \
> - mmap_end = STACK_TOP_MAX; \
> - else if ((((_addr) >= VA_USER_SV48)) && (VA_BITS >= VA_BITS_SV48)) \
> - mmap_end = VA_USER_SV48; \
> - else \
> - mmap_end = VA_USER_SV39; \
> - mmap_end; \
> -})
> -
> -#define arch_get_mmap_base(addr, base) \
> -({ \
> - unsigned long mmap_base; \
> - typeof(addr) _addr = (addr); \
> - typeof(base) _base = (base); \
> - unsigned long rnd_gap = DEFAULT_MAP_WINDOW - (_base); \
> - if ((_addr) == 0 || (IS_ENABLED(CONFIG_COMPAT) && is_compat_task())) \
> - mmap_base = (_base); \
> - else if (((_addr) >= VA_USER_SV57) && (VA_BITS >= VA_BITS_SV57)) \
> - mmap_base = VA_USER_SV57 - rnd_gap; \
> - else if ((((_addr) >= VA_USER_SV48)) && (VA_BITS >= VA_BITS_SV48)) \
> - mmap_base = VA_USER_SV48 - rnd_gap; \
> - else \
> - mmap_base = VA_USER_SV39 - rnd_gap; \
> - mmap_base; \
> -})
> +#define arch_get_mmap_end(addr, len, flags) \
> + ((addr) >= DEFAULT_MAP_WINDOW ? STACK_TOP_MAX :\
> + min(DEFAULT_MAP_WINDOW, STACK_TOP_MAX))
> +
> +#define arch_get_mmap_base(addr, base) \
> + ((addr) >= DEFAULT_MAP_WINDOW ? base :\
> + min(base, DEFAULT_MAP_WINDOW))
>
> #else
> #define DEFAULT_MAP_WINDOW TASK_SIZE
> --
> 2.43.0
>
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
next prev parent reply other threads:[~2024-01-20 1:52 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-14 19:55 [PATCH 0/3] RISC-V: mm: correct mmap behavior in sv48 address space Yangyu Chen
2024-01-14 19:58 ` [PATCH 1/3] RISC-V: mm: fix " Yangyu Chen
2024-01-20 1:50 ` Charlie Jenkins [this message]
2024-01-14 19:58 ` [PATCH 2/3] RISC-V: mm: only test mmap without hint Yangyu Chen
2024-01-14 19:58 ` [PATCH 3/3] Documentation: riscv: correct sv57 kernel behavior Yangyu Chen
2024-01-19 16:42 ` [PATCH 0/3] RISC-V: mm: correct mmap behavior in sv48 address space Yangyu Chen
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=Zasm7D/bQH9XGHvf@ghost \
--to=charlie@rivosinc.com \
--cc=andy.chiu@sifive.com \
--cc=aou@eecs.berkeley.edu \
--cc=conor.dooley@microchip.com \
--cc=cyy@cyyself.name \
--cc=guoren@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=palmer@dabbelt.com \
--cc=paul.walmsley@sifive.com \
/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).