From mboxrd@z Thu Jan 1 00:00:00 1970 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 04690C4332F for ; Wed, 21 Dec 2022 19:11:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 197468E0002; Wed, 21 Dec 2022 14:11:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 147948E0001; Wed, 21 Dec 2022 14:11:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F29C38E0002; Wed, 21 Dec 2022 14:11:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id E03BA8E0001 for ; Wed, 21 Dec 2022 14:11:48 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 9D63E4086C for ; Wed, 21 Dec 2022 19:11:48 +0000 (UTC) X-FDA: 80267257896.13.7A71D85 Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) by imf25.hostedemail.com (Postfix) with ESMTP id E544FA000D for ; Wed, 21 Dec 2022 19:11:45 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b="A/RS840o"; spf=pass (imf25.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.48 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1671649906; a=rsa-sha256; cv=none; b=ZVPm0KFVtm/M/hQhKUbqFzDOOh2W2CFHykjuP3/79MtlcM/9MI0qk5gDHK6C75pIJ33BjB EO7XJpxFpd4lsXa8FqUjDOanbcjDpEJfNrLaKOtAJRv61bnyd+vFnRxnOZbGWR983q6947 z1Jwp73PQKVoX3alDDsd8KHCUnbhR2M= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b="A/RS840o"; spf=pass (imf25.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.48 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1671649906; 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=JbYuuxURZ+HULop4cvQjD1UbE1sTZhJCtl/bY00O3aI=; b=a3Ok0iuQwt+xRoZ4Exy4lHa101g+kMgoHcgQTUkqj2tmwNcUCh4+S/+M/O4Opaq+SSvJgn 7MxzrOKwrnk7Ro7VZaUYzCZLPIYw7MUW7N8swPk7f8zhkAo1Eye1q2M2RaI4zT+ge54J+N 1NgZ8574+okb+Bf2tB7QWx63qZj4u4s= Received: by mail-wm1-f48.google.com with SMTP id i187-20020a1c3bc4000000b003d1e906ca23so2273555wma.3 for ; Wed, 21 Dec 2022 11:11:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=JbYuuxURZ+HULop4cvQjD1UbE1sTZhJCtl/bY00O3aI=; b=A/RS840oCo/HZ9cxbMliBu6RhaOi9m1RWH/Qky05PoNcHhidv4Xd+qg09V7VqgGdt0 KTD7LAaRdpOu4y9GbtZoHACH6vRLl6d86yWO9PIdm+ZVLTcs01HjNdQwMo4mJX5WVjB2 TN3fkt2iprul9zqkgT9u0zZ8nhOY+O+/S1sCpFa30nLNCPxZ6Jzm3f9smrf5hTEkRds7 0TVS/i2GLdYAHXZmycv7uOKlXbm89pzQvMZZ35IU6PmZx3fpKCeQLX0bvB71fIuuDfbh 5ZK7mskzbu7vAs37ZFVCS7V+9tZd9d8ALRTSSumlgQLyebSfoLf+sIqoidEDhXx3oo9g q4/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=JbYuuxURZ+HULop4cvQjD1UbE1sTZhJCtl/bY00O3aI=; b=4Q2bEg2v0gK6XPdKqKeGCBfCx/tcs2UFXEP1+Cpruktf2J1ePTOnBJTNx2kiO2RNwQ MeSz625s6CHt7wqHx52cTWe843FOHLeLtqO7mjzgWi0Nqfx8CmiPTllUnalNO7vG+IIB 1xhizY1iefdYUEvH7As3BHP5WBc61v6RV7pUu48Hf8m926AhXd7Fl/wYX7Wgy0h1qFCL 7o7PRiVKqJAUcsaa5xOJEN7I/DVN/iSGvHv8dx7z73IFaxLifFbb2QW14v56bISoPOYf c8E4rFNou10xjzoKCiX3in+9fo5K9HofcCEXn7ZmggRZcZ16mi16xhuwFV9Z4SCGlgpp 9LWA== X-Gm-Message-State: AFqh2kppbnLO8Pkc4MmAYNPvrH7vZGq2gdHitO5Y+tgKtCxKgzY9i8KT cFqRu4drybfcqv7lkIzMSMI= X-Google-Smtp-Source: AMrXdXsIMx+E0i6YC6gy8hrmfbrNNhLohFajh26CEV8oZb/iCssXF/Sg8lXsGo1/itTYGZDk6gb26w== X-Received: by 2002:a05:600c:1509:b0:3d2:17a5:17fe with SMTP id b9-20020a05600c150900b003d217a517femr2540935wmg.18.1671649904309; Wed, 21 Dec 2022 11:11:44 -0800 (PST) Received: from localhost ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.gmail.com with ESMTPSA id bu13-20020a056000078d00b002422816aa25sm18648497wrb.108.2022.12.21.11.11.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Dec 2022 11:11:43 -0800 (PST) Date: Wed, 21 Dec 2022 19:11:42 +0000 From: Lorenzo Stoakes To: "Uladzislau Rezki (Sony)" Cc: Andrew Morton , linux-mm@kvack.org, LKML , Baoquan He , Christoph Hellwig , Matthew Wilcox , Nicholas Piggin , Oleksiy Avramchenko , Roman Gushchin Subject: Re: [PATCH v2 1/3] mm: vmalloc: Avoid of calling __find_vmap_area() twise in __vunmap() Message-ID: References: <20221221174454.1085130-1-urezki@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221221174454.1085130-1-urezki@gmail.com> X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: E544FA000D X-Stat-Signature: roif4ugxch74f9t9pwkp6esfqsxz9thh X-HE-Tag: 1671649905-382802 X-HE-Meta: U2FsdGVkX19bQD6zjmTw+Yo69QMtRlU+vKpTRV5vF6fRMbodNFENB9eH4c8V0qUjZ6IMMHzss3qwOHSU+Ea/BbTEcNLUQAElczh77dh8r+LurX8YfEY+gyZM3JJk3KlWJF0gBMtMgfBuw8qNNj7GTs5e/s02nk2GY/5RlR9p4CtvroWqV8jwGDFz0jnXvhVBh20onGbPnt/TRMsKXNPrbhz7BOGiZZQrgE/VVHVN7gSBFaqMPFFn0NDTEmx+FtQjwAcOhFpgM8IK3QE4KzwHGghr0LR1WcJgTx8VAsLurB3htrRaa6gn0IhH0LEGu7uig6SFbBhUqxdDknDGrMDxTM5eam2lNOG8uN7TKmVA5+gWbrAd86ap/gwk5Zrsdi1dMDXKXgMaU/+2r6yzn3Dh2DhATYPXO8JiTMXVplH7DmUnF5GxvbnwM5pxhrpGIpmkDje0pq89W9OmHLD1uDe8/ILn290ygssByJ6a/UowIVz/8dD+S87kXXOYBDm/uYErBkm2qNHwBz12aAUU4lGTLtQ7zAoqtfPIoBYy35tDykUzJyjiLQ4wd7ah0OOQP+2zDs+h9yF+BPsaMtmXlxobhPy9ct1PpNE7VefHciLxNu8UFFqI5/3hpY6CbNVsSZIy72UAXoYQ2ivou9ZggISDT36LlqSoGQ63GEeI9VJ7ua2vx+hUngy+S3c8QNB/laHlB+x6BxCvLRzgwxEfrE3WiiELiBjR81j90HLaBKeU1rcXsjLteAy/ZdSpq+Hm8RFtCMsU1iH+Qg9cV2O4tIiHcgfGQuEiJ/iNouiL8eDGyk8pa5HKE2kfCj+VFdorJqFm6J3AkyzoU5+jtvisuKwgWQEVdBchKSjZSbuaX2Mk41KJLxxnTAq+BoBoQLX2suZeIaGjPfb6l+0duGHjzjcdKDyceoZiSaDpLJ+KvywCAYVNI7uw07jOZsplWCKRDfW2jBEGxl9M57Jxa80lQbv l+uJtbyQ Ivo+jXJ7R8dyo5qWuwSORylD7cCYoSMvPCw/Nv7OaALBMBzjjWnJZoxwpRQd5nVb71wJtWCOR+ydvzZSKEARallkPWTcIxeN12jRMw64IUQ6I+8JOAG3S8ZDNq150OrPkSo1AzrApJ4BSOCU= 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: Some pedantic grammar/spelling stuff:- (I know it can be a little annoying to get grammatical suggestions so I do hope that it isn't too irritating!) For the Subject line:- 'mm: vmalloc: Avoid of calling __find_vmap_area() twise in __vunmap()' -> 'mm: vmalloc: Avoid calling __find_vmap_area() twice in __vunmap()' On Wed, Dec 21, 2022 at 06:44:52PM +0100, Uladzislau Rezki (Sony) wrote: > Currently __vunmap() path calls __find_vmap_area() two times. One on > entry to check that area exists, second time inside remove_vm_area() > function that also performs a new search of VA. Perhaps slightly tweak to:- "Currently the __vunmap() path calls __find_vmap_area() twice. Once on entry to check that the area exists, then inside the remove_vm_area() function which also performs a new search for the VA." > > In order to improvie it from a performance point of view we split > remove_vm_area() into two new parts: > - find_unlink_vmap_area() that does a search and unlink from tree; > - __remove_vm_area() that does a removing but without searching. 'that does a removing but without searching' reads better I think as 'that removes without searching'. > > In this case there is no any functional change for remove_vm_area() > whereas vm_remove_mappings(), where a second search happens, switches > to the __remove_vm_area() variant where already detached VA is passed > as a parameter, so there is no need to find it again. > 'where already detached VA' -> 'where the already detached VA' as a minor nit here! > Performance wise, i use test_vmalloc.sh with 32 threads doing alloc > free on a 64-CPUs-x86_64-box: > > perf without this patch: > - 31.41% 0.50% vmalloc_test/10 [kernel.vmlinux] [k] __vunmap > - 30.92% __vunmap > - 17.67% _raw_spin_lock > native_queued_spin_lock_slowpath > - 12.33% remove_vm_area > - 11.79% free_vmap_area_noflush > - 11.18% _raw_spin_lock > native_queued_spin_lock_slowpath > 0.76% free_unref_page > > perf with this patch: > - 11.35% 0.13% vmalloc_test/14 [kernel.vmlinux] [k] __vunmap > - 11.23% __vunmap > - 8.28% find_unlink_vmap_area > - 7.95% _raw_spin_lock > 7.44% native_queued_spin_lock_slowpath > - 1.93% free_vmap_area_noflush > - 0.56% _raw_spin_lock > 0.53% native_queued_spin_lock_slowpath > 0.60% __vunmap_range_noflush > > __vunmap() consumes around ~20% less CPU cycles on this test. Very nice, amazing work! > > Reported-by: Roman Gushchin > Signed-off-by: Uladzislau Rezki (Sony) > --- > mm/vmalloc.c | 66 +++++++++++++++++++++++++++++++++------------------- > 1 file changed, 42 insertions(+), 24 deletions(-) > > diff --git a/mm/vmalloc.c b/mm/vmalloc.c > index 9e30f0b39203..28030d2441f1 100644 > --- a/mm/vmalloc.c > +++ b/mm/vmalloc.c > @@ -1825,9 +1825,11 @@ static void free_vmap_area_noflush(struct vmap_area *va) > unsigned long va_start = va->va_start; > unsigned long nr_lazy; > > - spin_lock(&vmap_area_lock); > - unlink_va(va, &vmap_area_root); > - spin_unlock(&vmap_area_lock); > + if (!list_empty(&va->list)) { > + spin_lock(&vmap_area_lock); > + unlink_va(va, &vmap_area_root); > + spin_unlock(&vmap_area_lock); > + } Do we want to do the same in free_vmap_area()? > > nr_lazy = atomic_long_add_return((va->va_end - va->va_start) >> > PAGE_SHIFT, &vmap_lazy_nr); > @@ -1871,6 +1873,19 @@ struct vmap_area *find_vmap_area(unsigned long addr) > return va; > } > > +static struct vmap_area *find_unlink_vmap_area(unsigned long addr) > +{ > + struct vmap_area *va; > + > + spin_lock(&vmap_area_lock); > + va = __find_vmap_area(addr, &vmap_area_root); > + if (va) > + unlink_va(va, &vmap_area_root); > + spin_unlock(&vmap_area_lock); > + > + return va; > +} > + > /*** Per cpu kva allocator ***/ > > /* > @@ -2591,6 +2606,20 @@ struct vm_struct *find_vm_area(const void *addr) > return va->vm; > } > > +static struct vm_struct *__remove_vm_area(struct vmap_area *va) > +{ > + struct vm_struct *vm; > + > + if (!va || !va->vm) > + return NULL; > + > + vm = va->vm; > + kasan_free_module_shadow(vm); > + free_unmap_vmap_area(va); > + > + return vm; > +} > + > /** > * remove_vm_area - find and remove a continuous kernel virtual area > * @addr: base address > @@ -2607,22 +2636,8 @@ struct vm_struct *remove_vm_area(const void *addr) > > might_sleep(); > > - spin_lock(&vmap_area_lock); > - va = __find_vmap_area((unsigned long)addr, &vmap_area_root); > - if (va && va->vm) { > - struct vm_struct *vm = va->vm; > - > - va->vm = NULL; > - spin_unlock(&vmap_area_lock); > - > - kasan_free_module_shadow(vm); > - free_unmap_vmap_area(va); > - > - return vm; > - } > - > - spin_unlock(&vmap_area_lock); > - return NULL; > + va = find_unlink_vmap_area((unsigned long) addr); > + return __remove_vm_area(va); > } Really nice separation of concerns and cleanup. > > static inline void set_area_direct_map(const struct vm_struct *area, > @@ -2637,15 +2652,16 @@ static inline void set_area_direct_map(const struct vm_struct *area, > } > > /* Handle removing and resetting vm mappings related to the vm_struct. */ > -static void vm_remove_mappings(struct vm_struct *area, int deallocate_pages) > +static void vm_remove_mappings(struct vmap_area *va, int deallocate_pages) Perhaps rename this to va_remove_mappings() or vmap_area_remove_mappings() since it is now explicitly accepting a vmap_area rather than vm_struct? > { > + struct vm_struct *area = va->vm; > unsigned long start = ULONG_MAX, end = 0; > unsigned int page_order = vm_area_page_order(area); > int flush_reset = area->flags & VM_FLUSH_RESET_PERMS; > int flush_dmap = 0; > int i; > > - remove_vm_area(area->addr); > + __remove_vm_area(va); > > /* If this is not VM_FLUSH_RESET_PERMS memory, no need for the below. */ > if (!flush_reset) > @@ -2690,6 +2706,7 @@ static void vm_remove_mappings(struct vm_struct *area, int deallocate_pages) > static void __vunmap(const void *addr, int deallocate_pages) > { > struct vm_struct *area; Feels like it's getting a bit confusing with 'va' representing vmap_area and 'area' which represents... vm_struct (this file has a bunch of naming inconsistencies like this actually), perhaps rename this to 'vm'? > + struct vmap_area *va; > > if (!addr) > return; > @@ -2698,19 +2715,20 @@ static void __vunmap(const void *addr, int deallocate_pages) > addr)) > return; > > - area = find_vm_area(addr); > - if (unlikely(!area)) { > + va = find_unlink_vmap_area((unsigned long)addr); > + if (unlikely(!va)) { > WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n", > addr); > return; > } > > + area = va->vm; > debug_check_no_locks_freed(area->addr, get_vm_area_size(area)); > debug_check_no_obj_freed(area->addr, get_vm_area_size(area)); > > kasan_poison_vmalloc(area->addr, get_vm_area_size(area)); > > - vm_remove_mappings(area, deallocate_pages); > + vm_remove_mappings(va, deallocate_pages); > > if (deallocate_pages) { > int i; > -- > 2.30.2 > Other than some pendatic points about grammar/naming this looks really good!