[PATCH 03/22] mm: introduce MMF_KERNEL flag and set it for init_mm
Christophe Leroy (CS GROUP)
chleroy at kernel.org
Mon Aug 3 06:59:35 PDT 2026
Le 14/07/2026 à 16:03, Kevin Brodsky a écrit :
> mm code often needs to know whether some mm represents a kernel or
> user address space. This is currently done by comparing the mm
> pointer with &init_mm; besides not being particularly elegant, this
> ignores the fact that other mm's (e.g. efi_mm) may also represent
> parts of the kernel address space.
>
> Introduce a new mm flag MMF_KERNEL and set it for init_mm.
> Subsequent patches will use this flag to replace comparisons with
> &init_mm. No functional change is introduced for now.
Did you consider performance impact ? This test is usually done in quite
critical memory handling functions.
init_mm is known at link time. Before your patch 08/22 there is just a
comparison of mm (r3) with a constant (loaded in r10):
c0014048 <assert_pte_locked>:
c0014048: 3d 40 c1 09 lis r10,-16119
c001404c: 39 4a 03 98 addi r10,r10,920
c0014050: 7c 03 50 00 cmpw r3,r10
c0014054: 4d 82 00 20 beqlr
...
After patch 08/22 we have, it first checks that mm is not 0, then it
loads the word located at mm+528 then AND it with 0x1. This load might
be costly.
c0014048 <assert_pte_locked>:
c0014048: 2c 03 00 00 cmpwi r3,0
c001404c: 7c 85 23 78 mr r5,r4
c0014050: 41 82 00 10 beq c0014060 <assert_pte_locked+0x18>
c0014054: 81 23 02 10 lwz r9,528(r3)
c0014058: 71 29 00 01 andi. r9,r9,1
c001405c: 4c 82 00 20 bnelr
...
Christophe
>
> Assisted-by: Codex:GPT-5.5
> Signed-off-by: Kevin Brodsky <kevin.brodsky at arm.com>
> ---
> include/linux/mm_types.h | 10 ++++++++++
> mm/init-mm.c | 1 +
> 2 files changed, 11 insertions(+)
>
> diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
> index a2e0dc5892ff..7838ea3aac00 100644
> --- a/include/linux/mm_types.h
> +++ b/include/linux/mm_types.h
> @@ -1414,6 +1414,8 @@ struct mm_struct {
> char flexible_array[] __aligned(__alignof__(unsigned long));
> };
>
> +#define MM_FLAGS_INIT(flags) { .__mm_flags = { BITMAP_FROM_U64(flags) } }
> +
> static inline bool mm_flags_test(int flag, const struct mm_struct *mm)
> {
> return test_bit(flag, ACCESS_PRIVATE(&mm->flags, __mm_flags));
> @@ -2001,6 +2003,9 @@ enum {
> #define MMF_TOPDOWN 31 /* mm searches top down by default */
> #define MMF_TOPDOWN_MASK BIT(MMF_TOPDOWN)
>
> +#define MMF_KERNEL 32 /* mm belongs to the kernel */
> +#define MMF_KERNEL_MASK BIT_ULL(MMF_KERNEL)
> +
> #define MMF_INIT_LEGACY_MASK (MMF_DUMP_FILTER_MASK |\
> MMF_DISABLE_THP_MASK | MMF_HAS_MDWE_MASK |\
> MMF_VM_MERGE_ANY_MASK | MMF_TOPDOWN_MASK)
> @@ -2008,6 +2013,11 @@ enum {
> /* Legacy flags must fit within 32 bits. */
> static_assert((u64)MMF_INIT_LEGACY_MASK <= (u64)UINT_MAX);
>
> +static inline bool mm_is_kernel(const struct mm_struct *mm)
> +{
> + return mm && mm_flags_test(MMF_KERNEL, mm);
> +}
> +
> /*
> * Initialise legacy flags according to masks, propagating selected flags on
> * fork. Further flag manipulation can be performed by the caller.
> diff --git a/mm/init-mm.c b/mm/init-mm.c
> index 3e792aad7626..93773269bf87 100644
> --- a/mm/init-mm.c
> +++ b/mm/init-mm.c
> @@ -34,6 +34,7 @@ struct mm_struct init_mm = {
> .pgd = swapper_pg_dir,
> .mm_users = ATOMIC_INIT(2),
> .mm_count = ATOMIC_INIT(1),
> + .flags = MM_FLAGS_INIT(MMF_KERNEL_MASK),
> .write_protect_seq = SEQCNT_ZERO(init_mm.write_protect_seq),
> MMAP_LOCK_INITIALIZER(init_mm)
> .page_table_lock = __SPIN_LOCK_UNLOCKED(init_mm.page_table_lock),
>
More information about the linux-um
mailing list