From patchwork Mon Mar 27 17:01:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Uladzislau Rezki X-Patchwork-Id: 13189675 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 883ADC6FD1D for ; Mon, 27 Mar 2023 17:01:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 14FB8900003; Mon, 27 Mar 2023 13:01:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0FF62900002; Mon, 27 Mar 2023 13:01:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F0987900003; Mon, 27 Mar 2023 13:01:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id E1D0C900002 for ; Mon, 27 Mar 2023 13:01:31 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id A94981A06EC for ; Mon, 27 Mar 2023 17:01:31 +0000 (UTC) X-FDA: 80615294382.22.B0D238F Received: from mail-ed1-f53.google.com (mail-ed1-f53.google.com [209.85.208.53]) by imf29.hostedemail.com (Postfix) with ESMTP id A711E120034 for ; Mon, 27 Mar 2023 17:01:29 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=VuwcNy6R; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf29.hostedemail.com: domain of urezki@gmail.com designates 209.85.208.53 as permitted sender) smtp.mailfrom=urezki@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1679936489; 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:references:dkim-signature; bh=MiwTgU7gCi7Y1Efu5iqCpK4GW+Ibe2u/4CDr6wU0ciU=; b=Fkn37PAjIGp3Zn1cbEkLNIv4CzhucXC0Dul8FxShuac3/OkE0J6vTmz5iL3LnUnnOlmQ9k yW9+szvPVERmmox3a/g6Ulg1fApNFeV1XC2tSQ1fhkc/wAg3yY/XfMraJMKlQgxtIG4PKB 4jnxqEtAE0moqIDntY0QS8YuGNY0VNg= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=VuwcNy6R; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf29.hostedemail.com: domain of urezki@gmail.com designates 209.85.208.53 as permitted sender) smtp.mailfrom=urezki@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679936489; a=rsa-sha256; cv=none; b=a+jRMfdrCx6+T8Q3la2mQs0fF01m4v9amDCHqbjEYqxldWzojYTJrsTApLG9t9jIl5FJkU YfgiMwuyR9AG5Mr5HixRoEoFrvbBFRYw/J6R32mivFIB6pu+vi01UyBCVYz3ioNc22HHgD 8EnEgW6omBdun3jF5+xMEB530UpprV0= Received: by mail-ed1-f53.google.com with SMTP id er18so27754654edb.9 for ; Mon, 27 Mar 2023 10:01:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679936488; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=MiwTgU7gCi7Y1Efu5iqCpK4GW+Ibe2u/4CDr6wU0ciU=; b=VuwcNy6RW81yA7xf/j2OkbcH2aJCR/IAPvX6ggUPJyMiRJOM3xgphsThTHMf3iIWsk qADyQMlC7N17pNN2HHtsL4sNmyhlr/NUlU3gfAaXaHdNbHsd3MCdirAT7CZyBIhXLnji rFHFUxKWDyR5+1AhUnW3m32hwt2egzgc3MyFr1Va4p+DaYpXcBD1GSb9Kc2gdDptu3g2 qaKlY4AHOuXzQ3n7lViZv97v6u8YplWfF98/CgL8b7+exOQLmyVwB3SwkhG+uomPgYnX kFq2JmFvC9T9CmDuDz0G2k341sJinKa0gqoFeR4r0pM6rqLFiUvrZtQSG90xSt70YBOf Fzvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679936488; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=MiwTgU7gCi7Y1Efu5iqCpK4GW+Ibe2u/4CDr6wU0ciU=; b=MyroUnQimdsX5ovoh0fImWeRukUQrzQ7XE6iY6upsBOrzONkOWgEc/wUErH6ARDLXW A6PABj8+WgdgfjFiS8+m8g4HC8gnp+fZ9tUWPtte/APo1lPVKnB1NSW9PgydmySELMPB ktvsc+szZ4CMEBa/VMeemKFjuAV2CjaWt2XL+gHcLX51i3dRxv1BTeQ7RXvfUS1WOW/c tbuMqdAE778DBuE1lu9IJfHHrV5bkreaU7cCpEYrZH+ltmKL6TuPmTZvzkhpPbBBjZ2r v2rr9SHQNqQsAK9uUlYwC4i2XIcfhJxQyzwQVwJ2PbNvvauFN7AWI/nW292/0Gb2Sk5x Ahow== X-Gm-Message-State: AAQBX9eK31iNhrnZS336/Zhfqn+9Fh0oxUho+ZOqVAMU2EOsDpdJO2a/ Rhx6ofzCBvLA/q8b+CE218A= X-Google-Smtp-Source: AKy350blcU31zArFdsw3R4rpxiKy5JwQp/0Wh2XL2VeKxnjpCY4W9K1IjkEdzo3FsQlP0SswPG9PDA== X-Received: by 2002:aa7:c54f:0:b0:4fa:4bc4:a911 with SMTP id s15-20020aa7c54f000000b004fa4bc4a911mr12898927edr.13.1679936488024; Mon, 27 Mar 2023 10:01:28 -0700 (PDT) Received: from pc638.lan ([155.137.26.201]) by smtp.gmail.com with ESMTPSA id t27-20020a50ab5b000000b004c0c5864cc5sm14936026edc.25.2023.03.27.10.01.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Mar 2023 10:01:27 -0700 (PDT) From: "Uladzislau Rezki (Sony)" To: Andrew Morton Cc: linux-mm@kvack.org, LKML , Baoquan He , Lorenzo Stoakes , Christoph Hellwig , Matthew Wilcox , Dave Chinner , Uladzislau Rezki , Oleksiy Avramchenko Subject: [PATCH v3 1/2] mm: vmalloc: Remove a global vmap_blocks xarray Date: Mon, 27 Mar 2023 19:01:25 +0200 Message-Id: <20230327170126.406044-1-urezki@gmail.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: A711E120034 X-Stat-Signature: 64czz8bk6c6adgyxfwy31mmo9eo6xmek X-HE-Tag: 1679936489-164895 X-HE-Meta: U2FsdGVkX1+8Vn7wjk7+k26cbkyAimhbKxHg+jZXoPKFrC4pqIbzKPEgg9VrapDNGYQYLOCUWVosaVka+e1FRhHoRDnhdlPjtQOOdwG+1aFbpIL6i0TP+WpezA2ACPJ2BfL4ERU2v1Z/Io8luSw948Yg1EknsK+xL14Uwg7TTtJIGIjIs2+Sq8BlRjU2ZFV/3DCOqyJTe5M107UQ/YB0FAjHLhh7LKYo3LyhbZXv5Y4hEJTCTCF6eqVC2r4ahVvEf+apcPXgXUCkMvsDmDe+F3vOcfdYPb99v+hfESe4BuNRIBAPk3pbXt0uUF6Av8bSKlclCmDBxhMtb3V5/g5wOPKCWN/qbbxBvDS3GlfDixkTNdt4/RLdeNG80AzudLwT0M2Mt8lvo0MDoVz0ivG5WJ0wBAQqwelrwDmEvKe/tVqa1d9QSvDduELwYfanA2U8+Ugzis5caWPpHBtk0OsNF2y4U2sDNNKN/5el/OOtCHTGJZfi/IZzjwqUkb5gsgrseOsY8MzOoHYWV4pcZ6hg6gIWtv3CV8i46JDE/oGG4T/ZC2zwgz4mIzBEV0eqM5PWVsbGG6ltIVTTqg4o+6C6tBe4TbqoJRJlSNy3lAKwf3XJOz+0lUmWIivmt3S5WIdx4aSoz4wrgnquhqsgEK9sxSMiIuzFQgwFaRuYaG34BEKT91I8CLfQNwstXc9h1E6/g7054t1UpstfY6COUYlJKDhLFZO3C4Qyb8LTfxMwGml17ko0UOYxrd8iO/e4pNLRey54wwEuXe3wDBnkCkFxbykvLmO48llrI+3FcK25G4lVETR6gtv8Oiz83I5EfQStO4Gk5kgpUP5NsPz/ep0uoMHA9vsibn9VdLwhWNLigqRNnWoYnV8v3wn17UcKOPL4i9IwxPBedEqadWCANqUGPf/CcGlC7wvNClK6g6zg2QlhZBend0BjTrLJ/+ieWSDaM4b5jjvicbRfyaCoIED 3aod+p9J lUxszvbXfKptz8h/36zOXgiac3yegMjS9v9JcQsnSV/lwLBzLPb6PD4H9KMjqawa6ObNfEfBUac0LVBDkcBHLZ4JqFw09AKqe8LmPr5xA9XdyFxwE/cisGzjJ4mhnzqAIbX4YPlGoJm/u4lAtZ0BAJ/lByG1RNhqz6Wtofwgm7dczmr0xxWjtnNZ7HQdi07eoCVkzMKhS2M7v6s5mOssG2tmwcXph4pp0a3K78JNZtxqKYuB4T/CIKgYag5S7ofRvBn9bbMMr2yfimW5QxDP9/PO+rMYVbnCAKEzpI+cFh0gsSUxocnt4Bejvmk4+M/+y3TxHCDH9sb9ge12zzCx2CDXRTXshFyUNY3KUEzh2qa77xfWomRr4PF8L3Qp754tMVAvUAGK2k+YFrylou6E77fiU7UwPjUwOhDFY7AadZf5IdB1+Sh9q5UO6itffo4eZO8/EX++1y3smbLe/Jv90Sb6ha7piWzTu7L+xFLOjD0Got4gGbPcVuqfA1dywJd1WuST1 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: 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 ... 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. v1 - v2: - Add more comments(Andrew Morton req.) - Switch to WARN_ON_ONCE(Lorenzo Stoakes req.) v2 -> v3: - Fix a kernel-doc complain(Matthew Wilcox) Signed-off-by: Uladzislau Rezki (Sony) --- mm/vmalloc.c | 85 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 64 insertions(+), 21 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 978194dc2bb8..821256ecf81c 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -1908,9 +1908,22 @@ static struct vmap_area *find_unlink_vmap_area(unsigned long addr) #define VMAP_BLOCK 0x2 /* mark out the vmap_block sub-type*/ #define VMAP_FLAGS_MASK 0x3 +/* + * 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. + */ struct vmap_block_queue { spinlock_t lock; struct list_head free; + + /* + * An xarray requires an extra memory dynamically to + * be allocated. If it is an issue, we can use rb-tree + * instead. + */ + struct xarray vmap_blocks; }; struct vmap_block { @@ -1928,24 +1941,46 @@ struct vmap_block { 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. + * In order to fast access to any "vmap_block" associated with a + * specific address, we store them into a per-cpu xarray. A hash + * function is addr_to_vbq() whereas a key is a vb->va->va_start + * value. + * + * Please note, a vmap_block_queue, which is a per-cpu, is not + * serialized by a raw_smp_processor_id() current CPU, instead + * it is chosen based on a CPU-index it belongs to, i.e. it is + * a hash-table. + * + * An example: + * + * CPU_1 CPU_2 CPU_0 + * | | | + * V V V + * 0 10 20 30 40 50 60 + * |------|------|------|------|------|------|... + * CPU0 CPU1 CPU2 CPU0 CPU1 CPU2 + * + * - CPU_1 invokes vm_unmap_ram(6), 6 belongs to CPU0 zone, thus + * it access: CPU0/INDEX0 -> vmap_blocks -> xa_lock; + * + * - CPU_2 invokes vm_unmap_ram(11), 11 belongs to CPU1 zone, thus + * it access: CPU1/INDEX1 -> vmap_blocks -> xa_lock; + * + * - CPU_0 invokes vm_unmap_ram(20), 20 belongs to CPU2 zone, thus + * it access: CPU2/INDEX2 -> vmap_blocks -> xa_lock. */ -static DEFINE_XARRAY(vmap_blocks); +static struct vmap_block_queue * +addr_to_vbq(unsigned long addr) +{ + int index = (addr / VMAP_BLOCK_SIZE) % num_possible_cpus(); -/* - * 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. - */ + return &per_cpu(vmap_block_queue, index); +} -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; + return rounddown(addr, VMAP_BLOCK_SIZE); } static void *vmap_block_vaddr(unsigned long va_start, unsigned long pages_off) @@ -1953,7 +1988,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)); + WARN_ON_ONCE(addr_to_vb_va_start(addr) != va_start); return (void *)addr; } @@ -1970,7 +2005,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 +2037,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); if (err) { kfree(vb); free_vmap_area(va); @@ -2021,9 +2055,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 +2171,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 +2180,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 +3526,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 +3544,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 +4314,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. */