From patchwork Mon Dec 16 19:24:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13910248 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B5566E7717F for ; Mon, 16 Dec 2024 19:25:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A8A1B6B00C4; Mon, 16 Dec 2024 14:24:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A3B146B00C5; Mon, 16 Dec 2024 14:24:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8B5376B00C6; Mon, 16 Dec 2024 14:24:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 68CF16B00C4 for ; Mon, 16 Dec 2024 14:24:57 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 25330C09E8 for ; Mon, 16 Dec 2024 19:24:57 +0000 (UTC) X-FDA: 82901798700.07.A6A30B1 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) by imf10.hostedemail.com (Postfix) with ESMTP id 36865C0010 for ; Mon, 16 Dec 2024 19:24:41 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=OROA+Nrh; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf10.hostedemail.com: domain of 3hX5gZwYKCEY02zmvjowwotm.kwutqv25-uus3iks.wzo@flex--surenb.bounces.google.com designates 209.85.210.202 as permitted sender) smtp.mailfrom=3hX5gZwYKCEY02zmvjowwotm.kwutqv25-uus3iks.wzo@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734377081; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=kODX23DYVFRUazWZEBIanW7JFjEXStT07MxX5tftr1k=; b=AZX+QrUKUE6tnrA1/UcvjFI/Y6nXsdBeb4f8G3AnA1TGPBSA23UgNJoMepKQN2G1DAvvdl 4Im65epxTZeFb3/8Fe5p5eBGmjzgpU1xX+iRdUyT6aoyysrrnHY6Gj2yUC4Gvr+mxu/c7l uD9iQAQDuylE4NVjwMwmTlKICNgE1P4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734377081; a=rsa-sha256; cv=none; b=4+iYv11iLTHY0axXEXgwkrnFiw2FP8X3jM1E1DKfF4MEzWAzaRHVieBvI+xG/dQOflx3d7 PhzCawLgMmbLx3gW2MZhg75uHkQx3yhL/ldw8ORZFkw5HBtrKhF8w72nnaknX5QECNWWCM O06gyKmFykvu2VcJIJTF0L2bKYV++DI= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=OROA+Nrh; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf10.hostedemail.com: domain of 3hX5gZwYKCEY02zmvjowwotm.kwutqv25-uus3iks.wzo@flex--surenb.bounces.google.com designates 209.85.210.202 as permitted sender) smtp.mailfrom=3hX5gZwYKCEY02zmvjowwotm.kwutqv25-uus3iks.wzo@flex--surenb.bounces.google.com Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-7289afa200aso5921859b3a.0 for ; Mon, 16 Dec 2024 11:24:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734377094; x=1734981894; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=kODX23DYVFRUazWZEBIanW7JFjEXStT07MxX5tftr1k=; b=OROA+Nrhah0ZCRy3lL03hQDhX4HHfbIaURtFj7DUVpzu0uf/McITe3QOs3WfJXMBSG KVWxGHTlG+RnAQjhW8jkjo7lFTFUZFlt7MxGDH1EuecMuuWuTEWtCsL0vu5v07lLVfcK 1Se376A/Hml2QabjSLEgxH3rh2A6G8Qhh+aqKTZw8MUqezgiELPqEB4atBAgDN5JXEjz 83cCZZlH4IlxpEuFns05gMit+qsBVK37HKDp/KhKd4aEqy4O3ml9maAxlQ/iGA/j5ZS+ KlUvUhcY5JDj7PKw6aOYnf1TzDBnCCMsZC7nEv/mXJm3vKmHc7OQjUTx2SLVzXHf46q+ TCeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734377094; x=1734981894; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=kODX23DYVFRUazWZEBIanW7JFjEXStT07MxX5tftr1k=; b=eao4zYZfZ2vuFL5tdWqpw5+TypGP+HY9mqnz9JioMj2JLp2/6Gl4ACkpXFhb+8jA3X WUkXD/sTsjNXJmMeHV3r7QljfNCuvEwWF7HbtnUQ3/dfDV5vxc7Eh7+JNozMzYxOOsWV p5Nme3WHd1xN/yrlBRGKy8RbSUI3NpBCHaa+ffXR7ii/+GMtVct1YQwB00LXILGlsK4l xVLSgG3lRgIGEwESwm5HS/lSMvMM5FV/vBwNOAiuvAcFJfc55VxG0y4472lWiOFDM7gB F/eSOA89kc2U/gk6GbAopaPeg4uY7fg84ZAeIpb+xS+cAqAiO6GHppQ6jy9wlZX/GI3L QuTw== X-Forwarded-Encrypted: i=1; AJvYcCUN7MdFqmwoRsEmIPygizhjgSCrZWOKz49quhLsikmw37inj8zM7PXaf1gH7TPEnBQxvuT5jhwr0g==@kvack.org X-Gm-Message-State: AOJu0Yy9wnqXbPwHJiAPuCOkmAno8Ho+g/iatk45prrjntySNBT64L2h BeiTzmibH68qeAheq/A3YH05U7TO0M0XhUFV+CKLHXhnbSzzSJqcMs65bHvT46rIBsiVI3hj0np XKA== X-Google-Smtp-Source: AGHT+IFs6nvWjzM1OoknSdPIN0ghPnyqI6YPL5avZzhKMYnwXQrh2jvp6FsUbYnz6o+KIo68RndWt2pA8Qg= X-Received: from pfjg1.prod.google.com ([2002:a05:6a00:b81:b0:725:8ee5:e458]) (user=surenb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2995:b0:725:eacf:cfda with SMTP id d2e1a72fcca58-7290c25ae78mr18948341b3a.17.1734377093879; Mon, 16 Dec 2024 11:24:53 -0800 (PST) Date: Mon, 16 Dec 2024 11:24:18 -0800 In-Reply-To: <20241216192419.2970941-1-surenb@google.com> Mime-Version: 1.0 References: <20241216192419.2970941-1-surenb@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241216192419.2970941-16-surenb@google.com> Subject: [PATCH v6 15/16] mm: make vma cache SLAB_TYPESAFE_BY_RCU From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: peterz@infradead.org, willy@infradead.org, liam.howlett@oracle.com, lorenzo.stoakes@oracle.com, mhocko@suse.com, vbabka@suse.cz, hannes@cmpxchg.org, mjguzik@gmail.com, oliver.sang@intel.com, mgorman@techsingularity.net, david@redhat.com, peterx@redhat.com, oleg@redhat.com, dave@stgolabs.net, paulmck@kernel.org, brauner@kernel.org, dhowells@redhat.com, hdanton@sina.com, hughd@google.com, lokeshgidra@google.com, minchan@google.com, jannh@google.com, shakeel.butt@linux.dev, souravpanda@google.com, pasha.tatashin@soleen.com, klarasmodin@gmail.com, corbet@lwn.net, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@android.com, surenb@google.com X-Stat-Signature: 38aa64w5b5pob78ciogobw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teX7 NVQHVNtKccbW3TMPsGFGDPNZw80C+yFV1R9aCLE3qf5XvlKtxTNoDJ5cx3zICc+Vl9l1zD7VpF+yNOcOele40h/BEHxD8hNjS5cRff3MMPfvJB04s3jjaqDAbS+PRtSH7FoaDTNKMCBH4OI96QvbclRe/wSNaRztIXcQHS+G8JPiIBfPjwMvyf0nV24XPMiamWpcE2GuTb9HM3FZosFbOzp0eN3ShdyrCc4lEOF/vRnxADIC5Iyd6DHayTmf/Ceu230BC3ASK4E1basMWzeF2Pqwv/uwfbIzRJH7U5ERCv+7J9bsXEdH+ZhekGIILo9h288wfJ4Eu/HVhJtH+qhIP9MtS/TT8Os3IKhriHZoCK/DQZCFvXr/pVep7/hHHH24FhRruF9hhDPh9lH/usThPxpNTnVJSwaouJLIy9/Jv5U/peT7jdQuh1+dPnFbTyZZxgatmKablLNNmjSWqkNpqAUK+Rh/fJE2cAgmr8b5VW3V175ltoNn4BZAXR8u5ltVriqzlJgE617X7mtJi5YTy+8l3PbUVxhVgEDrybK9B2x71ErX60KOLJOfdb0PoV/flbCyQs1mtRAxniDWJ0WUyn/iGoQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: To enable SLAB_TYPESAFE_BY_RCU for vma cache we need to ensure that object reuse before RCU grace period is over will be detected by lock_vma_under_rcu(). Current checks are sufficient as long as vma is detached before it is freed. Implement this guarantee by calling vma_ensure_detached() before vma is freed and make vm_area_cachep SLAB_TYPESAFE_BY_RCU. This will facilitate vm_area_struct reuse and will minimize the number of call_rcu() calls. Signed-off-by: Suren Baghdasaryan --- include/linux/mm.h | 2 -- include/linux/mm_types.h | 10 +++++++--- include/linux/slab.h | 6 ------ kernel/fork.c | 34 ++++++++++---------------------- mm/mmap.c | 8 +++++++- mm/vma.c | 15 +++----------- mm/vma.h | 2 +- tools/testing/vma/vma_internal.h | 7 +------ 8 files changed, 29 insertions(+), 55 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index bfd01ae07660..da773302af70 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -258,8 +258,6 @@ void setup_initial_init_mm(void *start_code, void *end_code, struct vm_area_struct *vm_area_alloc(struct mm_struct *); struct vm_area_struct *vm_area_dup(struct vm_area_struct *); void vm_area_free(struct vm_area_struct *); -/* Use only if VMA has no other users */ -void __vm_area_free(struct vm_area_struct *vma); #ifndef CONFIG_MMU extern struct rb_root nommu_region_tree; diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 803f718c007c..a720f7383dd8 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -544,6 +544,12 @@ static inline void *folio_get_private(struct folio *folio) typedef unsigned long vm_flags_t; +/* + * freeptr_t represents a SLUB freelist pointer, which might be encoded + * and not dereferenceable if CONFIG_SLAB_FREELIST_HARDENED is enabled. + */ +typedef struct { unsigned long v; } freeptr_t; + /* * A region containing a mapping of a non-memory backed file under NOMMU * conditions. These are held in a global tree and are pinned by the VMAs that @@ -658,9 +664,7 @@ struct vm_area_struct { unsigned long vm_start; unsigned long vm_end; }; -#ifdef CONFIG_PER_VMA_LOCK - struct rcu_head vm_rcu; /* Used for deferred freeing. */ -#endif + freeptr_t vm_freeptr; /* Pointer used by SLAB_TYPESAFE_BY_RCU */ }; /* diff --git a/include/linux/slab.h b/include/linux/slab.h index 10a971c2bde3..681b685b6c4e 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -234,12 +234,6 @@ enum _slab_flag_bits { #define SLAB_NO_OBJ_EXT __SLAB_FLAG_UNUSED #endif -/* - * freeptr_t represents a SLUB freelist pointer, which might be encoded - * and not dereferenceable if CONFIG_SLAB_FREELIST_HARDENED is enabled. - */ -typedef struct { unsigned long v; } freeptr_t; - /* * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests. * diff --git a/kernel/fork.c b/kernel/fork.c index f1ddfc7b3b48..7affb9245f64 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -471,36 +471,16 @@ struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig) return new; } -void __vm_area_free(struct vm_area_struct *vma) +void vm_area_free(struct vm_area_struct *vma) { #ifdef CONFIG_PER_VMA_LOCK - /* The vma should be detached while being destroyed. */ - VM_BUG_ON_VMA(!is_vma_detached(vma), vma); + vma_ensure_detached(vma); #endif vma_numab_state_free(vma); free_anon_vma_name(vma); kmem_cache_free(vm_area_cachep, vma); } -#ifdef CONFIG_PER_VMA_LOCK -static void vm_area_free_rcu_cb(struct rcu_head *head) -{ - struct vm_area_struct *vma = container_of(head, struct vm_area_struct, - vm_rcu); - - __vm_area_free(vma); -} -#endif - -void vm_area_free(struct vm_area_struct *vma) -{ -#ifdef CONFIG_PER_VMA_LOCK - call_rcu(&vma->vm_rcu, vm_area_free_rcu_cb); -#else - __vm_area_free(vma); -#endif -} - static void account_kernel_stack(struct task_struct *tsk, int account) { if (IS_ENABLED(CONFIG_VMAP_STACK)) { @@ -3147,6 +3127,11 @@ void __init mm_cache_init(void) void __init proc_caches_init(void) { + struct kmem_cache_args args = { + .use_freeptr_offset = true, + .freeptr_offset = offsetof(struct vm_area_struct, vm_freeptr), + }; + sighand_cachep = kmem_cache_create("sighand_cache", sizeof(struct sighand_struct), 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_TYPESAFE_BY_RCU| @@ -3163,8 +3148,9 @@ void __init proc_caches_init(void) sizeof(struct fs_struct), 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, NULL); - vm_area_cachep = KMEM_CACHE(vm_area_struct, - SLAB_HWCACHE_ALIGN|SLAB_NO_MERGE|SLAB_PANIC| + vm_area_cachep = kmem_cache_create("vm_area_struct", + sizeof(struct vm_area_struct), &args, + SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_TYPESAFE_BY_RCU| SLAB_ACCOUNT); mmap_init(); nsproxy_cache_init(); diff --git a/mm/mmap.c b/mm/mmap.c index df9154b15ef9..c848f6d645e9 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1284,7 +1284,13 @@ void exit_mmap(struct mm_struct *mm) do { if (vma->vm_flags & VM_ACCOUNT) nr_accounted += vma_pages(vma); - remove_vma(vma, /* unreachable = */ true); +#ifdef CONFIG_PER_VMA_LOCK + if (!is_vma_detached(vma)) { + vma_start_write(vma); + vma_mark_detached(vma); + } +#endif + remove_vma(vma); count++; cond_resched(); vma = vma_next(&vmi); diff --git a/mm/vma.c b/mm/vma.c index 0436a7d21e01..1b46b92b2d4d 100644 --- a/mm/vma.c +++ b/mm/vma.c @@ -406,23 +406,14 @@ static bool can_vma_merge_right(struct vma_merge_struct *vmg, /* * Close a vm structure and free it. */ -void remove_vma(struct vm_area_struct *vma, bool unreachable) +void remove_vma(struct vm_area_struct *vma) { might_sleep(); vma_close(vma); if (vma->vm_file) fput(vma->vm_file); mpol_put(vma_policy(vma)); - if (unreachable) { -#ifdef CONFIG_PER_VMA_LOCK - if (!is_vma_detached(vma)) { - vma_start_write(vma); - vma_mark_detached(vma); - } -#endif - __vm_area_free(vma); - } else - vm_area_free(vma); + vm_area_free(vma); } /* @@ -1206,7 +1197,7 @@ static void vms_complete_munmap_vmas(struct vma_munmap_struct *vms, /* Remove and clean up vmas */ mas_set(mas_detach, 0); mas_for_each(mas_detach, vma, ULONG_MAX) - remove_vma(vma, /* unreachable = */ false); + remove_vma(vma); vm_unacct_memory(vms->nr_accounted); validate_mm(mm); diff --git a/mm/vma.h b/mm/vma.h index 24636a2b0acf..3e6c14a748c2 100644 --- a/mm/vma.h +++ b/mm/vma.h @@ -170,7 +170,7 @@ int do_vmi_munmap(struct vma_iterator *vmi, struct mm_struct *mm, unsigned long start, size_t len, struct list_head *uf, bool unlock); -void remove_vma(struct vm_area_struct *vma, bool unreachable); +void remove_vma(struct vm_area_struct *vma); void unmap_region(struct ma_state *mas, struct vm_area_struct *vma, struct vm_area_struct *prev, struct vm_area_struct *next); diff --git a/tools/testing/vma/vma_internal.h b/tools/testing/vma/vma_internal.h index ac0a59906fea..3342cad87ece 100644 --- a/tools/testing/vma/vma_internal.h +++ b/tools/testing/vma/vma_internal.h @@ -700,14 +700,9 @@ static inline void mpol_put(struct mempolicy *) { } -static inline void __vm_area_free(struct vm_area_struct *vma) -{ - free(vma); -} - static inline void vm_area_free(struct vm_area_struct *vma) { - __vm_area_free(vma); + free(vma); } static inline void lru_add_drain(void)