Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp1607062rdb; Tue, 20 Feb 2024 01:04:38 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVBMOJLz6OpTKn+o38+mPEjIhR5UY+2famn5aluIwaD5r/qDfLxfCPp3DKqfWpkTO8TmE0nQ9FfOiNSeTQeNFOoLB6DKEojz9+XOXoBqA== X-Google-Smtp-Source: AGHT+IFSiIZ3afux/B8JPFWP3yA7UIVwG3HsgRpNkthN/5/AsVEixTYu9XGrcFn2P5JU0PuGCelq X-Received: by 2002:a05:622a:1991:b0:42c:39b2:a7c5 with SMTP id u17-20020a05622a199100b0042c39b2a7c5mr22377137qtc.26.1708419877875; Tue, 20 Feb 2024 01:04:37 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708419877; cv=pass; d=google.com; s=arc-20160816; b=bc1Gf4VcAUZ6dah1KP2UdZ1GL4y/UBzIsFW+WdC1zx/a6jv/xLy46loyMog1gQiots ooAM+03mJrazKOtlOUigkt1ww3QmK/RwZGxwzBPOov1yUP5M1BqDLw2xEe+buaRo6o9X fpOjtCfwlHPJ10602PkAa+l8FyyAq/9G1YSIOMO2JsT/hrCZnuj/dbvJD+YhGP6j4yoA byzg9/j5Bh6Z25xIm9y/hNjYewfkYyv5+Oy3zS0dacMQxIG3yIQ6Ch7D6EP+JKsjvMQg 9BuRLTpI8urJ63oiiOrhd/iGhFxz7hvllEZdZHOIGvL0aD+lQ+WyXAnGnBScuYOJELm4 jnZw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=JV7XRgkyn32uFBYga8rvYebRxax991AzZuQhaMoyqkk=; fh=pLJoBgmNyqObkuh28seqSKIcM0vRdHLanxakiUll5Wc=; b=EI9ogw3HN08vxF3Ip45Pa5aW5R3kCfV6GbvQje8VlJWaQ6NiD6dlN3EbP5Rvlr1GOp ZauZR8RS6o0Ag/Uxz3WrS8zh/ZNR2uV1NSpkOzwqDp08F5eb/IoguY1+LXsHksfdfWtd gG+2IHuHN/g0Rn+8kdphg7McMW2CE/UCVxmZLC/WC77huGt1zl+/y6WwbunML0GU6YBv iNynkQAqA0G1GPLbZ4iocLrQhqQpmm4k2bhR5Z3qh/s3K0qhPoZoZMrnPwrHsFImC9P3 wGRtY14OgXZw9lmL7u3/zQiLFwvP4weKh5j1Sg4We3TXzXKvtYxfwYw9N8YGXoR9Nxij nmCQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=I4Vm+Tpu; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-72639-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72639-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id d11-20020ac847cb000000b0042e03118c67si5655425qtr.535.2024.02.20.01.04.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 01:04:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-72639-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=I4Vm+Tpu; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-72639-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72639-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 98BC61C23D5C for ; Tue, 20 Feb 2024 09:04:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AE98D62148; Tue, 20 Feb 2024 09:02:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="I4Vm+Tpu" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E6E055FDC3 for ; Tue, 20 Feb 2024 09:02:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708419762; cv=none; b=G0rmops1DjT6Xko5u6plANOmOytYIgRs1UXrKESEOSUcsE5/3HAi5uSCNcCPUSVGfNklbcbco/1XxHDe/6ogKFKsQN2yGfI2UETEDRJsB6mUDkvsdMu14hXCxXTT0DoHVL73NPYp7Qb9zRmjGe5OAA97aqMKEsVjew/uSsO7uaQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708419762; c=relaxed/simple; bh=mYhlp/eMEhKS03KF61dRGQDnPpkxK3pDWAEVI1m8zMM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PpLUz8EKW6RC8uxQLg1WhlBMKTyJphKvs1WUD7mX0wXr8ZP+sMRhLdJGzmO0uA+a7xmMwc6XmNa1Sak5R1iPF19zx8zNQnmeEzhZgWGZQXJpr4eNG9lbtoBUbEwgkUQFFwSB2VOIQMeyQVsClbPlnL+Cp7SL0mPe+BNUhHwf6dI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=I4Vm+Tpu; arc=none smtp.client-ip=192.198.163.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708419761; x=1739955761; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mYhlp/eMEhKS03KF61dRGQDnPpkxK3pDWAEVI1m8zMM=; b=I4Vm+TpuuRKIwgrVwWdPMbRRJkNdUf2tYPQmY+lm+aYxl6VdBZub0wkN UaG6dsCIXtkIpWVNPIx9CnLQCHF/uwzGKtLR4fqFI1amUikeCM7Y5VO/+ OAvpfuY2Ivf/ULK3esGW6aG/7Fe9dcjPNkwm0QgakGvt76B749b63vAH4 oNJP2y5f0ospts/LSeNr+Z/wB2oa5V7CsWU6FNvbVRCl74urw2QVUAYqx W4TS4IV59SP2x+R5Di8E9UU936yMidM1w2yfWQv5JTcjciXQ+FtW+q7An hABhNoEqwpWEUmYGyo6ZrL0US1O1VDGUMcx4mmENWitpVfngcuXELwohU g==; X-IronPort-AV: E=McAfee;i="6600,9927,10989"; a="2391932" X-IronPort-AV: E=Sophos;i="6.06,172,1705392000"; d="scan'208";a="2391932" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Feb 2024 01:02:31 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10989"; a="913039557" X-IronPort-AV: E=Sophos;i="6.06,172,1705392000"; d="scan'208";a="913039557" Received: from linux-pnp-server-09.sh.intel.com ([10.239.176.190]) by fmsmga002.fm.intel.com with ESMTP; 20 Feb 2024 01:02:27 -0800 From: rulinhuang To: urezki@gmail.com Cc: akpm@linux-foundation.org, colin.king@intel.com, hch@infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, lstoakes@gmail.com, rulin.huang@intel.com, tim.c.chen@intel.com, zhiguo.zhou@intel.com, wangyang.guo@intel.com, tianyou.li@intel.com Subject: [PATCH v2] mm/vmalloc: lock contention optimization under multi-threading Date: Tue, 20 Feb 2024 04:05:21 -0500 Message-ID: <20240220090521.3316345-1-rulin.huang@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240209115147.261510-1-rulin.huang@intel.com> References: <20240209115147.261510-1-rulin.huang@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- V1 -> V2: Avoided the partial initialization issue of vm and separated insert_vmap_area() from alloc_vmap_area() --- mm/vmalloc.c | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index d12a17fc0c171..768e45f2ed946 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); base-commit: de927f6c0b07d9e698416c5b287c521b07694cac -- 2.43.0