From patchwork Wed Feb 21 03:29:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Rulin" X-Patchwork-Id: 13564889 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 6ECE3C48BC4 for ; Wed, 21 Feb 2024 03:26:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B609D6B0080; Tue, 20 Feb 2024 22:26:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B10FC6B0082; Tue, 20 Feb 2024 22:26:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9FFE66B0085; Tue, 20 Feb 2024 22:26:37 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 907386B0080 for ; Tue, 20 Feb 2024 22:26:37 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 3BEF51408E7 for ; Wed, 21 Feb 2024 03:26:37 +0000 (UTC) X-FDA: 81814373634.21.84F3B71 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.16]) by imf21.hostedemail.com (Postfix) with ESMTP id 088F41C0008 for ; Wed, 21 Feb 2024 03:26:34 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=iiGvafdM; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf21.hostedemail.com: domain of rulin.huang@intel.com designates 192.198.163.16 as permitted sender) smtp.mailfrom=rulin.huang@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708485995; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=nAtI5N1VzKrI30glzR5e9SQd6rjl3thUoVYCCUDTYl0=; b=COIfu07Gf1ZXmO4j5Gkh6GCA1PNUk3O7puRVL5K7taIUygUfYAv3fERS0uEkDlQ7Nm/AFo R0fD4hX8VS0L6CY0zYyt4MCeH1rH62DY6P/PFl0kWpZrqz2JJUg78PJl0O/6wWoJ5tvMCk n2DG0ahewK4ozlNIcYPp8SD/+OxFAJk= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=iiGvafdM; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf21.hostedemail.com: domain of rulin.huang@intel.com designates 192.198.163.16 as permitted sender) smtp.mailfrom=rulin.huang@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708485995; a=rsa-sha256; cv=none; b=O4iDFpWThzPj709WrO3PZPWun0JP6Jb3hxWABRYi9ssG7Cgg5SW7mHTI3u0lKJCPp5+Qme E9Pxh95Jkkk7cvKG7Vi8xmOwB64xjq0z0AoN4mi5HJaKVAE3Y/sWmxzxYZNjyWVqxPwTzx VmOCve1HO950pLIG4JyIa6+ApgOyuCs= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708485995; x=1740021995; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=DRLcudGaxUx+pa5ipV8atxZFzPwCvpE7YqFV5CEEaes=; b=iiGvafdMB0cdSSB7qltxws00WUSJKzs/p/goKDPc4nDm4wFGBcRDAbXh pEwO//R2eEiZ7UoNr30C1eaBO+FRRZ4RbcJbYxVgvpehwaP+XQSZsJ1mb rSKH9OZwPO1DS8r6WODtLwybyBVouZRo6GwUVPRAGWaWS2hUinldYrC5s uy2jyYIt17nOp8UIwvpUJ5gOtkSaJ0WshKESySjpPs1cF9Yzhlm/vefdX BSNjvrqcraNR1gzVPQPWHJ5CDGGcSy0q+JQkmj+0q1g8AH87BqNv+V68t SE13z1Z6Q4zEa7Kogw+B9bUIpQF9kJoYL+UJ6Ja4pFlrQNKYdKSQlJ2Ys g==; X-IronPort-AV: E=McAfee;i="6600,9927,10990"; a="3104521" X-IronPort-AV: E=Sophos;i="6.06,174,1705392000"; d="scan'208";a="3104521" Received: from orviesa009.jf.intel.com ([10.64.159.149]) by fmvoesa110.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Feb 2024 19:26:32 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,174,1705392000"; d="scan'208";a="4935110" Received: from linux-pnp-server-09.sh.intel.com ([10.239.176.190]) by orviesa009.jf.intel.com with ESMTP; 20 Feb 2024 19:26:29 -0800 From: rulinhuang To: akpm@linux-foundation.org Cc: colin.king@intel.com, hch@infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, lstoakes@gmail.com, rulin.huang@intel.com, tianyou.li@intel.com, tim.c.chen@intel.com, urezki@gmail.com, wangyang.guo@intel.com, zhiguo.zhou@intel.com Subject: [PATCH v3] mm/vmalloc: lock contention optimization under multi-threading Date: Tue, 20 Feb 2024 22:29:05 -0500 Message-ID: <20240221032905.11392-1-rulin.huang@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240207033059.1565623-1-rulin.huang@intel.com> References: <20240207033059.1565623-1-rulin.huang@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 088F41C0008 X-Stat-Signature: ickdzahn7zbxrh6y69rhdnignxzmc6t4 X-HE-Tag: 1708485994-583128 X-HE-Meta: U2FsdGVkX1/BhH/8KaI31X1KbxXapzRUaxKInR64cP3B901HFCF4+HdOD04Udd+PW9UH4/aCjTFqUN9lxsxYNazosdxalt7fTDHrm259ZgyCCuYukJvh6jOPTBH99lNkuoC7S+Qro490a62ZODbZH1EGxRzGwWnl2x2otN0W5wwKaCIAlB/f3oQ/yji9i/zSdiOv2N8Xoln+/Rqk9MihRF3/l1MAP1PQhRZIgSPilM+3OiuypFZNNvs+jhaMtdEbm1AazIXESTqzGQiSwwaYly7RRT9qd49e0sUS2S2AjisalrkSsOnAD6KVc6Qj/VPS5hB2R02841Y13HgaSH0qneCMtYlBxMwyytlS+OcxqLgJsdEAMo1FM7PNzD/gl8P6wHlWmLe1tIUwuxjw/qr5WIdD82Y1/15UVcQf3OYA79VsAOKe9nBVRieRhXly12LyKzHjCVDOq/6aPq3KtcKFzRmsTobUiyIvaoWeWI1udUPfJigU9DB8zOOeF2hUyaXCGdqf0TM6ptHuLqZCz6gtvD3NBPT5bBJJFu+r4BWemWx2Aizs1tntkKrlPExLTexuyYBoKY4nR31zgAt7GQKfQtfaFNTuinHlygrFxC3tLy/QFYaTwhXUDJsYFqrYTPG2YDCRry8dzgz351+L1SX/0QxEi/54IHGRtQVYIAOGxkZ9U4HAYsQLpkp/vRGEZtu61J3Aw7g3qVQJAqKgDaqjidZmcud/1jkXinnJ10C8++wQHWfu3vtmFYu8HTm23uakb35u7XxZLJRT4MDJkP6ZDTQ/wo0sqUBHuqjgxnG9v6ccfkSW84pdciAX/Nw1RqoeSkViYy1CCYjIxr+At4gz81qqhbJ1+MLIx8idps/5vQT2NLBea3iXU+KHb6zvu9Mo 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: When allocating a new memory area where the mapping address range is known, it is observed that the vmap_area lock is acquired twice. The first acquisition occurs in the alloc_vmap_area() function when inserting the vm area into the vm mapping red-black tree. The second acquisition occurs in the setup_vmalloc_vm() function when updating the properties of the vm, such as flags and address, etc. Combine these two operations together in alloc_vmap_area(), which improves scalability when the vmap_area lock is contended. By doing so, the need to acquire the lock twice can also be eliminated. With the above change, tested on intel icelake platform(160 vcpu, kernel v6.7), a 6% performance improvement and a 7% reduction in overall spinlock hotspot are gained on stress-ng/pthread(https://github.com/ColinIanKing/stress-ng), which is the stress test of thread creations. Reviewed-by: Chen Tim C Reviewed-by: King Colin Signed-off-by: rulinhuang Signed-off-by: Baoquan He --- V1 -> V2: Avoided the partial initialization issue of vm and separated insert_vmap_area() from alloc_vmap_area() V2 -> V3: Rebased on 6.8-rc5 --- mm/vmalloc.c | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) base-commit: b401b621758e46812da61fa58a67c3fd8d91de0d diff --git a/mm/vmalloc.c b/mm/vmalloc.c index d12a17fc0c17..768e45f2ed94 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -1630,17 +1630,18 @@ static struct vmap_area *alloc_vmap_area(unsigned long size, va->vm = NULL; va->flags = va_flags; - spin_lock(&vmap_area_lock); - insert_vmap_area(va, &vmap_area_root, &vmap_area_list); - spin_unlock(&vmap_area_lock); - BUG_ON(!IS_ALIGNED(va->va_start, align)); BUG_ON(va->va_start < vstart); BUG_ON(va->va_end > vend); ret = kasan_populate_vmalloc(addr, size); if (ret) { - free_vmap_area(va); + /* + * Insert/Merge it back to the free tree/list. + */ + spin_lock(&free_vmap_area_lock); + merge_or_add_vmap_area_augment(va, &free_vmap_area_root, &free_vmap_area_list); + spin_unlock(&free_vmap_area_lock); return ERR_PTR(ret); } @@ -1669,6 +1670,13 @@ static struct vmap_area *alloc_vmap_area(unsigned long size, return ERR_PTR(-EBUSY); } +static inline void insert_vmap_area_with_lock(struct vmap_area *va) +{ + spin_lock(&vmap_area_lock); + insert_vmap_area(va, &vmap_area_root, &vmap_area_list); + spin_unlock(&vmap_area_lock); +} + int register_vmap_purge_notifier(struct notifier_block *nb) { return blocking_notifier_chain_register(&vmap_notify_list, nb); @@ -2045,6 +2053,8 @@ static void *new_vmap_block(unsigned int order, gfp_t gfp_mask) return ERR_CAST(va); } + insert_vmap_area_with_lock(va); + vaddr = vmap_block_vaddr(va->va_start, 0); spin_lock_init(&vb->lock); vb->va = va; @@ -2398,6 +2408,8 @@ void *vm_map_ram(struct page **pages, unsigned int count, int node) if (IS_ERR(va)) return NULL; + insert_vmap_area_with_lock(va); + addr = va->va_start; mem = (void *)addr; } @@ -2538,7 +2550,7 @@ static void vmap_init_free_space(void) } } -static inline void setup_vmalloc_vm_locked(struct vm_struct *vm, +static inline void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va, unsigned long flags, const void *caller) { vm->flags = flags; @@ -2548,14 +2560,6 @@ static inline void setup_vmalloc_vm_locked(struct vm_struct *vm, va->vm = vm; } -static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va, - unsigned long flags, const void *caller) -{ - spin_lock(&vmap_area_lock); - setup_vmalloc_vm_locked(vm, va, flags, caller); - spin_unlock(&vmap_area_lock); -} - static void clear_vm_uninitialized_flag(struct vm_struct *vm) { /* @@ -2600,6 +2604,8 @@ static struct vm_struct *__get_vm_area_node(unsigned long size, setup_vmalloc_vm(area, va, flags, caller); + insert_vmap_area_with_lock(va); + /* * Mark pages for non-VM_ALLOC mappings as accessible. Do it now as a * best-effort approach, as they can be mapped outside of vmalloc code. @@ -4166,7 +4172,7 @@ struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets, for (area = 0; area < nr_vms; area++) { insert_vmap_area(vas[area], &vmap_area_root, &vmap_area_list); - setup_vmalloc_vm_locked(vms[area], vas[area], VM_ALLOC, + setup_vmalloc_vm(vms[area], vas[area], VM_ALLOC, pcpu_get_vm_areas); } spin_unlock(&vmap_area_lock);