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 7E868C4332F for ; Thu, 22 Dec 2022 11:38:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B0960900003; Thu, 22 Dec 2022 06:38:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AB9F1900002; Thu, 22 Dec 2022 06:38:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 95992900003; Thu, 22 Dec 2022 06:38:32 -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 80F12900002 for ; Thu, 22 Dec 2022 06:38:32 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 1F29A1C4C24 for ; Thu, 22 Dec 2022 11:38:32 +0000 (UTC) X-FDA: 80269744464.04.1ABA672 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf05.hostedemail.com (Postfix) with ESMTP id 9347B100006 for ; Thu, 22 Dec 2022 11:38:29 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=hxNiAkB9; spf=pass (imf05.hostedemail.com: domain of bhe@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=bhe@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1671709109; 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=D2c/4EKQG1CyLBHCaqgEyAQsuYjRgCOdz9VhRLCzbvE=; b=hDHs112b1R9VotEICbUJfg6v44Jb9kI4rR9wtE+qsWrCKlpi1hc3kI6ZJWMl/YmP8SWQ+V 3QccngMivGfHcnyekAhtAOuA4rFh8dfh2DfplGjfeF+zNuXNIIGpskQQB6V+CUnX/7SIGT ThsMi/0ous6kQs9usXs+a9nofUmrsDc= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=hxNiAkB9; spf=pass (imf05.hostedemail.com: domain of bhe@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=bhe@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1671709109; a=rsa-sha256; cv=none; b=pSGhk5EmYFxt73gMg4U7HQj/edHDCPnQmMRiB3627KWGRjzd6DrIMxHg66ihGhdk85ocQu k0OTsvG6E0ft67I6fs08GuprC3rUsywqii3KLug74U9d9Njzay2cShP7ZV7UDsPoc8rW/+ MHbV7Qg5iyJPGenqsPSXaDKAvxd1nVg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1671709109; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=D2c/4EKQG1CyLBHCaqgEyAQsuYjRgCOdz9VhRLCzbvE=; b=hxNiAkB9wgS77Nb4zc+NMDdbRsSqw87DEdPrDI80blYF2cLiMkEQcfbREGd8CMkmASjxyw 2xW+p3L/+zyZ7l+jusR/faijMq323iH9U5qCrY9cKNvEHDjqkId8yS/H/juyezL29TyCiV Rqop6fPkdolUky0GaQPfnH3VwUMG9dw= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-427-zksao7wdMiaz9HNIn_nrIQ-1; Thu, 22 Dec 2022 06:38:22 -0500 X-MC-Unique: zksao7wdMiaz9HNIn_nrIQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E186A101A521; Thu, 22 Dec 2022 11:38:21 +0000 (UTC) Received: from localhost (ovpn-12-50.pek2.redhat.com [10.72.12.50]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 154C8112132C; Thu, 22 Dec 2022 11:38:20 +0000 (UTC) Date: Thu, 22 Dec 2022 19:38:14 +0800 From: Baoquan He To: "Uladzislau Rezki (Sony)" Cc: Andrew Morton , linux-mm@kvack.org, LKML , Lorenzo Stoakes , 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-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 9347B100006 X-Rspam-User: X-Stat-Signature: g69ukqnx13gypbz3cmpwfdfn4knrp9wq X-HE-Tag: 1671709109-78069 X-HE-Meta: U2FsdGVkX19hgwT4eB1NIfEl6EoT1JBFa4PYrXcs0wgXbAYMuA1pxJVM7Ay/alegVfPqEevqiI3MacMEZtYJN6IuYBmPWdjVb3a7aNZT9CCjDzGexwN1E2XMP4LZAS2WTVE+4cnfcv4IYOhaCLWQHr4as6R1IQrdfaAfePzEPKF1+EPil+GmDPPB+TBJ98FP75lYLXvlar9b5KikLADmq2z2mIspkSlqbzKycnoDSV4/KZi0DsRcX6/yaX+Mw4UrCgtji3zx3fqD727P4dl+DXNkNpJvpLnhDJ4OZ3XwM2sCkXkr/lAPIyRcj40T0wziHcPYMPPkGe/866gqrtc3v8J7diVmHegdDKXHl4LbzmKjUBpj4z8mWHCvfy9tmHhuif7c9XsiS/xg0/8ula4qfc+UOGc1HYzPZjMcZVzxjGvvwF5E1zkwcCsJvEbJXUqv1IXY38u8mT9kpcqZzLemdEiutGvwKH6C0y0Z9RhGS1S/j1T5dkCGBfaUjl07XqOG9ek+lV9wwiXQXvIvRyncm/c+qcyEuX/qRluyUJ4JxiBYE4WHtMwYqq1PElUTBd/Ogv+pW4GOR0hfux/rXX9RQLrNsybD53DLmdW1Tf1f329Hc7AoeGHc7MSAVqaR65i1O8MOFEMUI9l6igWW0lFaYOZ2sCYuCRYPrwRnV1xVCQrGCjmuMkLKJwgQ3QTYmOtWzg7wXynQ0SWEkSoGcD7MmPY1MPrAKQaIPMXB/ogPkHtL4ymkuvBwr0aOfbECxSnG+Keej9HvUSBiGVTsyivEKPGaqetHL2w0k8wdEwwxxWDScjH9oSs5jMMRHkPxm96PBnujSz/1EfvEYYT9GfArGwJfwIowhw2c5jliJxDtx2PKJOPYNbkxRR5+Dr2PBVMybk+iOmykAPb68AUz3ZiHFdgi/Qk4OGdtTC9tQ0LgT7Siz44znLgj5ySLnCIFR3+lvD3+lZoE3Z9QA2P+Oeo M6A== 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: On 12/21/22 at 06:44pm, 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. > > 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. > > 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. I like this patch. This takes off the va->vm clearning too. Finally I don't need to worry about the va->flags clearing during unmapping when reading out vmap_block areas. > > 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. > > 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); > + } > > 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); > } > > 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) > { > + 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; > + 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 >