From patchwork Tue Aug 29 08:11:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Uladzislau Rezki X-Patchwork-Id: 13368662 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 590D6C6FA8F for ; Tue, 29 Aug 2023 08:12:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D4FBD28003C; Tue, 29 Aug 2023 04:11:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CFF15280037; Tue, 29 Aug 2023 04:11:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B509028003C; Tue, 29 Aug 2023 04:11:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 9E446280037 for ; Tue, 29 Aug 2023 04:11:51 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 8154E160666 for ; Tue, 29 Aug 2023 08:11:51 +0000 (UTC) X-FDA: 81176423622.09.DA7FEEA Received: from mail-lf1-f51.google.com (mail-lf1-f51.google.com [209.85.167.51]) by imf28.hostedemail.com (Postfix) with ESMTP id 8755EC0021 for ; Tue, 29 Aug 2023 08:11:49 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b="R8KeIq/3"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf28.hostedemail.com: domain of urezki@gmail.com designates 209.85.167.51 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=1693296709; 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:in-reply-to:references:references:dkim-signature; bh=zH+nVcQCC5XPUzS1kyDL4qDeiDz/PyOeL+vfo5mJUXo=; b=1zpddUmIYUEAfK79oFwmwHL00e9fzF88MuM+Obm95KZQrDMbdvRRwW9B+SGuxNWzhFa3r5 umxK8Lwu+r7PykCj2LEGDS9Cnb1sK0vZlhNSMrcFHaDeqq+vO2+bL0VwUxpsRTP4mIe+1F dWuPivw+o8pzag0qASJCI4dVzbl6WHc= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b="R8KeIq/3"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf28.hostedemail.com: domain of urezki@gmail.com designates 209.85.167.51 as permitted sender) smtp.mailfrom=urezki@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1693296709; a=rsa-sha256; cv=none; b=4XmCmvZziVxsCvDYIAjHw/lr1razvHkJwuy4KpAM+BSlXhhhkDzKHKosCH9knMxDOa5osz vYT6DZGYLmCO6az6Jfe+63gjflvK9/sF7v42hP18NbRL9HYJobfzy45Et2rs81ZS6Kj97z yUvKVMwUmZCd4Nv2pEHf4nXP7/RFhDs= Received: by mail-lf1-f51.google.com with SMTP id 2adb3069b0e04-500b0f06136so4255741e87.0 for ; Tue, 29 Aug 2023 01:11:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1693296708; x=1693901508; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=zH+nVcQCC5XPUzS1kyDL4qDeiDz/PyOeL+vfo5mJUXo=; b=R8KeIq/3gtO3iTgd0IM5MatHKA1srv2GfCeW89vuYPHGX1HrOm/YGVm8W/cQQusDa+ k4bPIBJsDvaqObULuZhov2b43smtAmAGE3mE/O87ZlEQVF1j/x+eK7mRjKby8fzRKPZI IR/xvnboQVc9ZPuO9gmR6JyvftTpEkcxXUztdZKbYKEp2zcqAr8mLmQ95qbB0hx232ja 9uhNWbJTEO2HHCO2QyalVugYHNtvpuxPxCUy7tZtxEWn7u7kcohg9BOsznKV8ysYNJ/r e+D6evKP257A/+P9M2pdRttK1CsHqFt3XnOrHhp5Utqib5+jDwPlUL1gxQY0akAHcqLo VLFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693296708; x=1693901508; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zH+nVcQCC5XPUzS1kyDL4qDeiDz/PyOeL+vfo5mJUXo=; b=UrZ+kZIMPZ2HKbYu01H/pdnNEPf3uuCIgYZfUwPKqSTzVCGGZGwnQ5O063B1uIJbIh nk8O0qwQldBix1pDU2+EXNcUBeOzNrHg/1A6WrnrMLnyY5XgqZZuhGePZ38tLv3WgQmu EsHBQiI1iy415KOx63j1oNGgc+JYZzIkeC5Yk2uRv+cnPd27n+etWkrpe0cYnJr0KEnn CFz1A71dJDBdqDU0F+88iozx07dAw4qH4pvXvhPYRRa9mGKNoblqgmfsgVvYlbclZxjT ne1Vr90Eld8rB2PMc9zY7wFqiUCnVCItQjqUE6zsjWpvmslgx6oGLfhX4S3amXjChnKe ndag== X-Gm-Message-State: AOJu0YxptGTuNlgkYBKfwg5uHxJGjF0G1t3Z/3ieNVS9KtkATGjmuBEF FSi8+3t80pM/pMegPkwEBmUGskN3/02xug== X-Google-Smtp-Source: AGHT+IEdFI1NxFW8VIakie/KzaEK0YUPq/uRCFagJRaw5Bk5Hwv7EB3kWT6TkchlvGIBEwP/Xo/3aw== X-Received: by 2002:a05:6512:128c:b0:4fa:f96c:745f with SMTP id u12-20020a056512128c00b004faf96c745fmr25351346lfs.38.1693296707770; Tue, 29 Aug 2023 01:11:47 -0700 (PDT) Received: from pc638.lan ([155.137.26.201]) by smtp.gmail.com with ESMTPSA id f25-20020a19ae19000000b004fbad341442sm1868026lfc.97.2023.08.29.01.11.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Aug 2023 01:11:47 -0700 (PDT) From: "Uladzislau Rezki (Sony)" To: linux-mm@kvack.org, Andrew Morton Cc: LKML , Baoquan He , Lorenzo Stoakes , Christoph Hellwig , Matthew Wilcox , "Liam R . Howlett" , Dave Chinner , "Paul E . McKenney" , Joel Fernandes , Uladzislau Rezki , Oleksiy Avramchenko Subject: [PATCH v2 5/9] mm: vmalloc: Remove global purge_vmap_area_root rb-tree Date: Tue, 29 Aug 2023 10:11:38 +0200 Message-Id: <20230829081142.3619-6-urezki@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230829081142.3619-1-urezki@gmail.com> References: <20230829081142.3619-1-urezki@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: tkuw433kf19xdrx8h6hym35r8r5qcbr4 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 8755EC0021 X-HE-Tag: 1693296709-672019 X-HE-Meta: U2FsdGVkX180HsB1pwzc7tCXRQ6B9BQg8rOOgIr2bNPl/X5cRaIaGrQRlk0Y5E7+xGrc6HqpLjhoF0T8c1D0O4vthVwq8fZp8+yuLtrfqcdugF1HfDUpmcxYEEBY8Y+Dqznlo/FNqENmJGSzktrFcu8Z6DnCSoMtrl/20JO+2NmpJi2F2eCgq6BW7K4S/QkM0n0WbEZxHGb9Y5M01R5qGpk2WXEHcU6ZJYOAhQN175EyJklfHmNw/qOHrlv3jSQDada81t04IbD9X2rSwlhKo3wnWz1wT9PQljqm1VWiyEUcQt4GFj0ZN21MrgmYIimuqBbt9rhegMRi37f9HtwdRFelxfncrJtVwmDr+qJ6NyXFAw0vEk9mLFkk6gw6QcD6QQ9XPtg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oVNNOmtgZJExZeWoqp72F7h4V7o= 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: Similar to busy VA, lazily-freed area is stored to a node it belongs to. Such approach does not require any global locking primitive, instead an access becomes scalable what mitigates a contention. This patch removes a global purge-lock, global purge-tree and global purge list. Signed-off-by: Uladzislau Rezki (Sony) Reviewed-by: Baoquan He --- mm/vmalloc.c | 135 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 82 insertions(+), 53 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index ae0368c314ff..5a8a9c1370b6 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -733,10 +733,6 @@ static DEFINE_SPINLOCK(free_vmap_area_lock); LIST_HEAD(vmap_area_list); static bool vmap_initialized __read_mostly; -static struct rb_root purge_vmap_area_root = RB_ROOT; -static LIST_HEAD(purge_vmap_area_list); -static DEFINE_SPINLOCK(purge_vmap_area_lock); - /* * This kmem_cache is used for vmap_area objects. Instead of * allocating from slab we reuse an object from this cache to @@ -784,6 +780,12 @@ struct rb_list { struct vmap_node { /* Bookkeeping data of this node. */ struct rb_list busy; + struct rb_list lazy; + + /* + * Ready-to-free areas. + */ + struct list_head purge_list; }; static struct vmap_node *nodes, snode; @@ -1768,40 +1770,22 @@ static DEFINE_MUTEX(vmap_purge_lock); /* for per-CPU blocks */ static void purge_fragmented_blocks_allcpus(void); +static cpumask_t purge_nodes; /* * Purges all lazily-freed vmap areas. */ -static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end) +static unsigned long +purge_vmap_node(struct vmap_node *vn) { - unsigned long resched_threshold; - unsigned int num_purged_areas = 0; - struct list_head local_purge_list; + unsigned long num_purged_areas = 0; struct vmap_area *va, *n_va; - lockdep_assert_held(&vmap_purge_lock); - - spin_lock(&purge_vmap_area_lock); - purge_vmap_area_root = RB_ROOT; - list_replace_init(&purge_vmap_area_list, &local_purge_list); - spin_unlock(&purge_vmap_area_lock); - - if (unlikely(list_empty(&local_purge_list))) - goto out; - - start = min(start, - list_first_entry(&local_purge_list, - struct vmap_area, list)->va_start); - - end = max(end, - list_last_entry(&local_purge_list, - struct vmap_area, list)->va_end); - - flush_tlb_kernel_range(start, end); - resched_threshold = lazy_max_pages() << 1; + if (list_empty(&vn->purge_list)) + return 0; spin_lock(&free_vmap_area_lock); - list_for_each_entry_safe(va, n_va, &local_purge_list, list) { + list_for_each_entry_safe(va, n_va, &vn->purge_list, list) { unsigned long nr = (va->va_end - va->va_start) >> PAGE_SHIFT; unsigned long orig_start = va->va_start; unsigned long orig_end = va->va_end; @@ -1823,13 +1807,55 @@ static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end) atomic_long_sub(nr, &vmap_lazy_nr); num_purged_areas++; - - if (atomic_long_read(&vmap_lazy_nr) < resched_threshold) - cond_resched_lock(&free_vmap_area_lock); } spin_unlock(&free_vmap_area_lock); -out: + return num_purged_areas; +} + +/* + * Purges all lazily-freed vmap areas. + */ +static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end) +{ + unsigned long num_purged_areas = 0; + struct vmap_node *vn; + int i; + + lockdep_assert_held(&vmap_purge_lock); + purge_nodes = CPU_MASK_NONE; + + for (i = 0; i < nr_nodes; i++) { + vn = &nodes[i]; + + INIT_LIST_HEAD(&vn->purge_list); + + if (RB_EMPTY_ROOT(&vn->lazy.root)) + continue; + + spin_lock(&vn->lazy.lock); + WRITE_ONCE(vn->lazy.root.rb_node, NULL); + list_replace_init(&vn->lazy.head, &vn->purge_list); + spin_unlock(&vn->lazy.lock); + + start = min(start, list_first_entry(&vn->purge_list, + struct vmap_area, list)->va_start); + + end = max(end, list_last_entry(&vn->purge_list, + struct vmap_area, list)->va_end); + + cpumask_set_cpu(i, &purge_nodes); + } + + if (cpumask_weight(&purge_nodes) > 0) { + flush_tlb_kernel_range(start, end); + + for_each_cpu(i, &purge_nodes) { + vn = &nodes[i]; + num_purged_areas += purge_vmap_node(vn); + } + } + trace_purge_vmap_area_lazy(start, end, num_purged_areas); return num_purged_areas > 0; } @@ -1848,16 +1874,9 @@ static void reclaim_and_purge_vmap_areas(void) static void drain_vmap_area_work(struct work_struct *work) { - unsigned long nr_lazy; - - do { - mutex_lock(&vmap_purge_lock); - __purge_vmap_area_lazy(ULONG_MAX, 0); - mutex_unlock(&vmap_purge_lock); - - /* Recheck if further work is required. */ - nr_lazy = atomic_long_read(&vmap_lazy_nr); - } while (nr_lazy > lazy_max_pages()); + mutex_lock(&vmap_purge_lock); + __purge_vmap_area_lazy(ULONG_MAX, 0); + mutex_unlock(&vmap_purge_lock); } /* @@ -1867,6 +1886,7 @@ static void drain_vmap_area_work(struct work_struct *work) */ static void free_vmap_area_noflush(struct vmap_area *va) { + struct vmap_node *vn = addr_to_node(va->va_start); unsigned long nr_lazy_max = lazy_max_pages(); unsigned long va_start = va->va_start; unsigned long nr_lazy; @@ -1880,10 +1900,9 @@ static void free_vmap_area_noflush(struct vmap_area *va) /* * Merge or place it to the purge tree/list. */ - spin_lock(&purge_vmap_area_lock); - merge_or_add_vmap_area(va, - &purge_vmap_area_root, &purge_vmap_area_list); - spin_unlock(&purge_vmap_area_lock); + spin_lock(&vn->lazy.lock); + merge_or_add_vmap_area(va, &vn->lazy.root, &vn->lazy.head); + spin_unlock(&vn->lazy.lock); trace_free_vmap_area_noflush(va_start, nr_lazy, nr_lazy_max); @@ -4390,15 +4409,21 @@ static void show_numa_info(struct seq_file *m, struct vm_struct *v) static void show_purge_info(struct seq_file *m) { + struct vmap_node *vn; struct vmap_area *va; + int i; - spin_lock(&purge_vmap_area_lock); - list_for_each_entry(va, &purge_vmap_area_list, list) { - seq_printf(m, "0x%pK-0x%pK %7ld unpurged vm_area\n", - (void *)va->va_start, (void *)va->va_end, - va->va_end - va->va_start); + for (i = 0; i < nr_nodes; i++) { + vn = &nodes[i]; + + spin_lock(&vn->lazy.lock); + list_for_each_entry(va, &vn->lazy.head, list) { + seq_printf(m, "0x%pK-0x%pK %7ld unpurged vm_area\n", + (void *)va->va_start, (void *)va->va_end, + va->va_end - va->va_start); + } + spin_unlock(&vn->lazy.lock); } - spin_unlock(&purge_vmap_area_lock); } static int s_show(struct seq_file *m, void *p) @@ -4545,6 +4570,10 @@ static void vmap_init_nodes(void) vn->busy.root = RB_ROOT; INIT_LIST_HEAD(&vn->busy.head); spin_lock_init(&vn->busy.lock); + + vn->lazy.root = RB_ROOT; + INIT_LIST_HEAD(&vn->lazy.head); + spin_lock_init(&vn->lazy.lock); } }