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 87951C7619A for ; Mon, 27 Mar 2023 07:36:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0D0196B0071; Mon, 27 Mar 2023 03:36:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 080586B0072; Mon, 27 Mar 2023 03:36:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E8A40900002; Mon, 27 Mar 2023 03:36:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id DA1BF6B0071 for ; Mon, 27 Mar 2023 03:36:36 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 90B64160433 for ; Mon, 27 Mar 2023 07:36:36 +0000 (UTC) X-FDA: 80613870792.28.74919E8 Received: from mail-lf1-f44.google.com (mail-lf1-f44.google.com [209.85.167.44]) by imf27.hostedemail.com (Postfix) with ESMTP id 7BE3F40007 for ; Mon, 27 Mar 2023 07:36:34 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=k9RZeIZm; spf=pass (imf27.hostedemail.com: domain of urezki@gmail.com designates 209.85.167.44 as permitted sender) smtp.mailfrom=urezki@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=1679902594; 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=318LEVgLs5GfKsnkbXBEtpfsZyH32RaLTzfh7r4/RNc=; b=ivlg+V972wJVQklijHIf49oJ5Ti/gkvyqsWnUsGfAnsVzXadhNN39fS7pj3DzYmOTz7WTA btFI5gES7EEF/wP0kyrfe3N36m8cihGXSARfLVVtoh3Rx+E9kirAZiQK058RbhyKxXM50Y +DWrO2VkMoDNn11ccj+m0gJQpJoIV1o= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=k9RZeIZm; spf=pass (imf27.hostedemail.com: domain of urezki@gmail.com designates 209.85.167.44 as permitted sender) smtp.mailfrom=urezki@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679902594; a=rsa-sha256; cv=none; b=xBbokcsxUWv3W5Yh0V/4kuaqgPJVe7ZHg/ptqm7yfXTZkRpbn29wzaeq/HVJvoLxffba9t /FRgZzvtMeM4behYlbmvXhY8WJjDLFBRVPY9PeK2nh7mxQD0wCPWKWHlGQY/jYypelFzOk hCJ7HFpkROFSydQYFxe2FSynGjSAk+E= Received: by mail-lf1-f44.google.com with SMTP id y20so10091567lfj.2 for ; Mon, 27 Mar 2023 00:36:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679902593; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:date:from:from:to:cc:subject:date:message-id:reply-to; bh=318LEVgLs5GfKsnkbXBEtpfsZyH32RaLTzfh7r4/RNc=; b=k9RZeIZm6jX+wZAPakN7BrSHARjyDQ8sTq75Lb0m8HhmOvFKrmfLVk8pBm97+4h5VO djUkbqeci8MwKkfXoXbqt544fwD45Fita/BajeCokf+7AX+B2/AYBp7FQhfHp+I7NNeb FqXi9yVgnUnv2y6BUDhk7JT+x7WtNpCQzgKhejJfTBnLpBbfwGKcmLhtD3bGBfUi+QKE fddjl7bzPFkHm+2lmj7OncwWwg4Ui1OfOtSCOk5b6rYymOQ8FzpReaLGE85hJqe1NnVG xf78IDDnpiqrOXBvD9b6gb5r/Qc8Cgpnb7xw3lj50MiQEdopV6absmPdKfpFzrXKltRl nAhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679902593; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:date:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=318LEVgLs5GfKsnkbXBEtpfsZyH32RaLTzfh7r4/RNc=; b=c88yMxjDvHVjXJIOLZAGoCbvPuvPMt1E+d2pQK2rZTuLBkoA5cQL7lYA3n5dlELiDQ o7zHOhZiyk8ZKtEClAJKmW0cMaa9siRk2FE+IOMSjUkgkt+EVTfI9+sX6Rj0pNVVouKx Drl6AcZeUaJY2i/crNEQsaFGt73Se0aWTmxTclw2JV8KyYZufyddGZwpXb/mQ2t5KVlI 913BXmwqBC8tJsqRPUqL6bNpEwMhOGoOMMpzRprZ48lmZrTp/luth7V8Cp7q0vNlGMeJ tsJgaoLAnyrkxsayw7LW93jJDLfNfOvWnpALxF1yneQAY8iEpDDJKgtKFFwlqrqViLF8 e5Fg== X-Gm-Message-State: AAQBX9clRUhRJ4mB2cvrYQslwXMsFhEyZwzIrHXy9rJJYESHObPXJ5ds hMasJz+bPg7IcP0DdfZ7NY8= X-Google-Smtp-Source: AKy350aUFHBQkeJQFArJroOFt5AEe0B6M3bNqBWvmTIuXvld60QcaDR1S5Y0oVnWqa7UgVXqh1VKTg== X-Received: by 2002:ac2:4434:0:b0:4eb:dd2:f3d2 with SMTP id w20-20020ac24434000000b004eb0dd2f3d2mr1026818lfl.43.1679902592632; Mon, 27 Mar 2023 00:36:32 -0700 (PDT) Received: from pc636 (host-90-233-209-50.mobileonline.telia.com. [90.233.209.50]) by smtp.gmail.com with ESMTPSA id x19-20020a19f613000000b004eb0e8e414bsm453118lfe.47.2023.03.27.00.36.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Mar 2023 00:36:32 -0700 (PDT) From: Uladzislau Rezki X-Google-Original-From: Uladzislau Rezki Date: Mon, 27 Mar 2023 09:36:29 +0200 To: Lorenzo Stoakes Cc: "Uladzislau Rezki (Sony)" , Andrew Morton , linux-mm@kvack.org, LKML , Baoquan He , Christoph Hellwig , Matthew Wilcox , Dave Chinner , Oleksiy Avramchenko Subject: Re: [PATCH 1/1] mm: vmalloc: Remove a global vmap_blocks xarray Message-ID: References: <20230323192111.1501308-1-urezki@gmail.com> <9ef9690d-cd79-4024-b7f1-eae2916cf5a2@lucifer.local> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <9ef9690d-cd79-4024-b7f1-eae2916cf5a2@lucifer.local> X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 7BE3F40007 X-Stat-Signature: girqi87i7rsb7z46jfthk7couux6pboh X-HE-Tag: 1679902594-752878 X-HE-Meta: U2FsdGVkX18gSnUXvGvm59RdbSyyoDlc7X1D2xjBEEE+NqHnNbUkSAN7hk0ifDqgF8o4iGpbQ4AiKNF34iPST2hpSFMKlVpCv8V7pB8r9k66N76Ax99iZoG2rgRiy3RAV6fAkyC2yNhKMpqfV52fgxqRtNYCMuAvGiPUOr/Y9U08XgUtNr0x82nOtVua2bdsDe+qB3KrAzBbsvymWxacaxJd0TZucAuPj2J/gn4SGXwFd9mUf8nHcgMgPYRRyzl8JM/n9DvQB17KY47hID4MQ7X62JdARNzkHo/IXkXXFXffJ5Xfxxqd4y62Xyo8hYY3MRy9qnq5AP6i7ht2MLeN7Yplszrvk7g5EyniEz9dmICPFWMSGZwttKv1w2XuBY58SgTq4UYqOtvlXX3DwHv6rPMZsopsO6tylNo41BQjxwRJyU8cV/aFSf/N5WBmQ3ZBcA1g65SItTatqk0wWnN2a4UremjXAOd21tKwPwQCERm9LhoK64g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 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Thu, Mar 23, 2023 at 09:46:00PM +0000, Lorenzo Stoakes wrote: > On Thu, Mar 23, 2023 at 08:21:11PM +0100, Uladzislau Rezki (Sony) wrote: > > A global vmap_blocks-xarray array can be contented under > > heavy usage of the vm_map_ram()/vm_unmap_ram() APIs. The > > lock_stat shows that a "vmap_blocks.xa_lock" lock is a > > second in a top-list when it comes to contentions: > > > > > > ---------------------------------------- > > class name con-bounces contentions ... > > ---------------------------------------- > > vmap_area_lock: 2554079 2554276 ... > > -------------- > > vmap_area_lock 1297948 [<00000000dd41cbaa>] alloc_vmap_area+0x1c7/0x910 > > vmap_area_lock 1256330 [<000000009d927bf3>] free_vmap_block+0x4a/0xe0 > > vmap_area_lock 1 [<00000000c95c05a7>] find_vm_area+0x16/0x70 > > -------------- > > vmap_area_lock 1738590 [<00000000dd41cbaa>] alloc_vmap_area+0x1c7/0x910 > > vmap_area_lock 815688 [<000000009d927bf3>] free_vmap_block+0x4a/0xe0 > > vmap_area_lock 1 [<00000000c1d619d7>] __get_vm_area_node+0xd2/0x170 > > > > vmap_blocks.xa_lock: 862689 862698 ... > > ------------------- > > vmap_blocks.xa_lock 378418 [<00000000625a5626>] vm_map_ram+0x359/0x4a0 > > vmap_blocks.xa_lock 484280 [<00000000caa2ef03>] xa_erase+0xe/0x30 > > ------------------- > > vmap_blocks.xa_lock 576226 [<00000000caa2ef03>] xa_erase+0xe/0x30 > > vmap_blocks.xa_lock 286472 [<00000000625a5626>] vm_map_ram+0x359/0x4a0 > > ... > > > > > > that is a result of running vm_map_ram()/vm_unmap_ram() in > > a loop. The test creates 64(on 64 CPUs system) threads and > > each one maps/unmaps 1 page. > > > > After this change the "xa_lock" can be considered as a noise > > in the same test condition: > > > > > > ... > > &xa->xa_lock#1: 10333 10394 ... > > -------------- > > &xa->xa_lock#1 5349 [<00000000bbbc9751>] xa_erase+0xe/0x30 > > &xa->xa_lock#1 5045 [<0000000018def45d>] vm_map_ram+0x3a4/0x4f0 > > -------------- > > &xa->xa_lock#1 7326 [<0000000018def45d>] vm_map_ram+0x3a4/0x4f0 > > &xa->xa_lock#1 3068 [<00000000bbbc9751>] xa_erase+0xe/0x30 > > ... > > > > > > Nice! Really good to see contention reduced, but in addition I'm a huge fan > of us removing the global state in vmalloc and this is a good start. > > I've noticed a small perf regression after 3 runs of ./test_vmalloc.sh > performance from an average of 119356136169 cycles to 120404645782 or +0.9% > but this doesn't seem especially egregious. > We are lack of extra vm_map_ram()/vm_unmap_ram() tests in the test_vmalloc.sh. It would be good to add them to the test-suite. > > This patch does not fix vmap_area_lock/free_vmap_area_lock and > > purge_vmap_area_lock bottle-necks, it is rather a separate rework. > > > > Signed-off-by: Uladzislau Rezki (Sony) > > --- > > mm/vmalloc.c | 50 ++++++++++++++++++++++++++------------------------ > > 1 file changed, 26 insertions(+), 24 deletions(-) > > > > diff --git a/mm/vmalloc.c b/mm/vmalloc.c > > index 978194dc2bb8..13b5342bed9a 100644 > > --- a/mm/vmalloc.c > > +++ b/mm/vmalloc.c > > @@ -1911,6 +1911,7 @@ static struct vmap_area *find_unlink_vmap_area(unsigned long addr) > > struct vmap_block_queue { > > spinlock_t lock; > > struct list_head free; > > + struct xarray vmap_blocks; > > }; > > > > struct vmap_block { > > @@ -1927,25 +1928,18 @@ struct vmap_block { > > /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */ > > static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue); > > > > -/* > > - * XArray of vmap blocks, indexed by address, to quickly find a vmap block > > - * in the free path. Could get rid of this if we change the API to return a > > - * "cookie" from alloc, to be passed to free. But no big deal yet. > > - */ > > Doesn't this comment still apply? Or is the idea of returning the "cookie" > not really viable? > Since a vmap_block_queue is a per-cpu thing, though it is not fully serialized in terms of per-cpu classical meaning, IMHO, it is not a big issue. If we return a cookie then, indeed, we do not need to find a vmap_block and performance wise it should be better. For how much, i do not know, it requires data. From the other hand an API has to be changed accordingly. But i can leave the comment! > > -static DEFINE_XARRAY(vmap_blocks); > > - > > -/* > > - * We should probably have a fallback mechanism to allocate virtual memory > > - * out of partially filled vmap blocks. However vmap block sizing should be > > - * fairly reasonable according to the vmalloc size, so it shouldn't be a > > - * big problem. > > - */ > > Again, is this comment no longer relevant? > Looks like yes :) But i am not sure i understand correctly what author meant. It looks like this: void *vm_map_ram(struct page **pages, unsigned int count, int node) { unsigned long size = (unsigned long)count << PAGE_SHIFT; unsigned long addr; void *mem; if (likely(count <= VMAP_MAX_ALLOC)) { mem = vb_alloc(size, GFP_KERNEL); if (IS_ERR(mem)) return NULL; ... instead of returning NULL, go directly with a fall-back, that is: struct vmap_area *va; va = alloc_vmap_area(size, PAGE_SIZE, VMALLOC_START, VMALLOC_END, node, GFP_KERNEL, VMAP_RAM); if (IS_ERR(va)) return NULL; addr = va->va_start; mem = (void *)addr; > > +static struct vmap_block_queue * > > +addr_to_vbq(unsigned long addr) > > +{ > > + int cpu = (addr / VMAP_BLOCK_SIZE) % num_possible_cpus(); > > + return &per_cpu(vmap_block_queue, cpu); > > +} > > Andrew's already commented on this, so I won't dwell but it does seem odd > to subdivide by number of possible CPUs rather than just use the actual > CPU. I guess your response to his question will also answer mine :) > I will upload a v2 where i try to explain in detail as much as i can, after that we can see if there are extra comments or questions and discuss if so. > > > > -static unsigned long addr_to_vb_idx(unsigned long addr) > > +static unsigned long > > +addr_to_vb_va_start(unsigned long addr) > > { > > - addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1); > > - addr /= VMAP_BLOCK_SIZE; > > - return addr; > > + /* A start address of block an address belongs to. */ > > A nit, but might be worth referring to the assert in vmap_block_vaddr(), as > this comment seems a bit redundant otherwise as it is implied by the code > it comments. > OK. I can remove that comment. > > + return rounddown(addr, VMAP_BLOCK_SIZE); > > } > > > > static void *vmap_block_vaddr(unsigned long va_start, unsigned long pages_off) > > @@ -1953,7 +1947,7 @@ static void *vmap_block_vaddr(unsigned long va_start, unsigned long pages_off) > > unsigned long addr; > > > > addr = va_start + (pages_off << PAGE_SHIFT); > > - BUG_ON(addr_to_vb_idx(addr) != addr_to_vb_idx(va_start)); > > + BUG_ON(addr_to_vb_va_start(addr) != addr_to_vb_va_start(va_start)); > > Maybe nitty, but perhaps better to WARN_ON() here to avoid BUG_ON proliferation? > Indeed, it is better to go with WARN_ON() or even WARN_ON_ONCE(). > And can't this be the below? > > WARN_ON(addr_to_vb_va_start(addr) != va_start); > Yep, it can be. Thanks for it! > > return (void *)addr; > > } > > > > @@ -1970,7 +1964,6 @@ static void *new_vmap_block(unsigned int order, gfp_t gfp_mask) > > struct vmap_block_queue *vbq; > > struct vmap_block *vb; > > struct vmap_area *va; > > - unsigned long vb_idx; > > int node, err; > > void *vaddr; > > > > @@ -2003,8 +1996,8 @@ static void *new_vmap_block(unsigned int order, gfp_t gfp_mask) > > bitmap_set(vb->used_map, 0, (1UL << order)); > > INIT_LIST_HEAD(&vb->free_list); > > > > - vb_idx = addr_to_vb_idx(va->va_start); > > - err = xa_insert(&vmap_blocks, vb_idx, vb, gfp_mask); > > + vbq = addr_to_vbq(va->va_start); > > + err = xa_insert(&vbq->vmap_blocks, va->va_start, vb, gfp_mask); > > This seems actually like a nice subtle improvement in that we are now > indexing always on va_start explicitly and will always load using > addr_to_vb_va_start(). > Yep, because we already have an index, it is a va->va_start. > > > if (err) { > > kfree(vb); > > free_vmap_area(va); > > @@ -2021,9 +2014,11 @@ static void *new_vmap_block(unsigned int order, gfp_t gfp_mask) > > > > static void free_vmap_block(struct vmap_block *vb) > > { > > + struct vmap_block_queue *vbq; > > struct vmap_block *tmp; > > > > - tmp = xa_erase(&vmap_blocks, addr_to_vb_idx(vb->va->va_start)); > > + vbq = addr_to_vbq(vb->va->va_start); > > + tmp = xa_erase(&vbq->vmap_blocks, vb->va->va_start); > > BUG_ON(tmp != vb); > > > > spin_lock(&vmap_area_lock); > > @@ -2135,6 +2130,7 @@ static void vb_free(unsigned long addr, unsigned long size) > > unsigned long offset; > > unsigned int order; > > struct vmap_block *vb; > > + struct vmap_block_queue *vbq; > > > > BUG_ON(offset_in_page(size)); > > BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC); > > @@ -2143,7 +2139,10 @@ static void vb_free(unsigned long addr, unsigned long size) > > > > order = get_order(size); > > offset = (addr & (VMAP_BLOCK_SIZE - 1)) >> PAGE_SHIFT; > > - vb = xa_load(&vmap_blocks, addr_to_vb_idx(addr)); > > + > > + vbq = addr_to_vbq(addr); > > + vb = xa_load(&vbq->vmap_blocks, addr_to_vb_va_start(addr)); > > + > > spin_lock(&vb->lock); > > bitmap_clear(vb->used_map, offset, (1UL << order)); > > spin_unlock(&vb->lock); > > @@ -3486,6 +3485,7 @@ static void vmap_ram_vread(char *buf, char *addr, int count, unsigned long flags > > { > > char *start; > > struct vmap_block *vb; > > + struct vmap_block_queue *vbq; > > unsigned long offset; > > unsigned int rs, re, n; > > > > @@ -3503,7 +3503,8 @@ static void vmap_ram_vread(char *buf, char *addr, int count, unsigned long flags > > * Area is split into regions and tracked with vmap_block, read out > > * each region and zero fill the hole between regions. > > */ > > - vb = xa_load(&vmap_blocks, addr_to_vb_idx((unsigned long)addr)); > > + vbq = addr_to_vbq((unsigned long) addr); > > + vb = xa_load(&vbq->vmap_blocks, addr_to_vb_va_start((unsigned long) addr)); > > if (!vb) > > goto finished; > > > > @@ -4272,6 +4273,7 @@ void __init vmalloc_init(void) > > p = &per_cpu(vfree_deferred, i); > > init_llist_head(&p->list); > > INIT_WORK(&p->wq, delayed_vfree_work); > > + xa_init(&vbq->vmap_blocks); > > } > > > > /* Import existing vmlist entries. */ > > -- > > 2.30.2 > >