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 74655C77B75 for ; Tue, 23 May 2023 14:03:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1996E900005; Tue, 23 May 2023 10:03:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 12317900002; Tue, 23 May 2023 10:03:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F0664900005; Tue, 23 May 2023 10:03:31 -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 DB551900002 for ; Tue, 23 May 2023 10:03:31 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id E654F1404C3 for ; Tue, 23 May 2023 14:03:30 +0000 (UTC) X-FDA: 80821687380.09.3A32769 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by imf21.hostedemail.com (Postfix) with ESMTP id E83A11C02FC for ; Tue, 23 May 2023 14:02:18 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=linutronix.de header.s=2020 header.b=E8msUzrt; dkim=pass header.d=linutronix.de header.s=2020e header.b=2ZkOvG8P; dmarc=pass (policy=none) header.from=linutronix.de; spf=pass (imf21.hostedemail.com: domain of tglx@linutronix.de designates 193.142.43.55 as permitted sender) smtp.mailfrom=tglx@linutronix.de ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684850540; a=rsa-sha256; cv=none; b=gjjtsdA8zHLKpIxZ0Vu/IsBGNRYWJYZHF3sWIleyM3y0mazk8byczaII2NfiGQKmjSIMeL 1ok1pBX9fuyTGXf4uOo3bFoDqu6a5gl6PiJThaxoTeQOK4M3b43ryuyuEy1GBHn9/VzDe8 C2MtAaOmoc4M1vdTo5auqnskIhren4s= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=linutronix.de header.s=2020 header.b=E8msUzrt; dkim=pass header.d=linutronix.de header.s=2020e header.b=2ZkOvG8P; dmarc=pass (policy=none) header.from=linutronix.de; spf=pass (imf21.hostedemail.com: domain of tglx@linutronix.de designates 193.142.43.55 as permitted sender) smtp.mailfrom=tglx@linutronix.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684850540; 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: references:references:dkim-signature; bh=W0htcsfLUpxLaMjJlRhsnhJ2QB5K+K/5FLFN+owMbd8=; b=U/C6nZeWMi7m0pzHPUznJLZ9GtJybkJCNh/X/TDsf5A4lfUPAe4KAu1sL665R1AYJgLSnQ 4hFa3pUn5kHS4B7bgaQqCyDqzISCCEo9hZA+033WgbAeVT3e+O7jIZgKPql0OCA53HjdOU mjZ0DU+ocd7vQC7jq9etYVJC4tGJ1Xs= Message-ID: <20230523140002.852175941@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1684850537; 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: references:references; bh=W0htcsfLUpxLaMjJlRhsnhJ2QB5K+K/5FLFN+owMbd8=; b=E8msUzrtsiWtA2B5JJElvZ9RMci1JDZlT3dpqp7cFBl++PlTBFaUK7oAi3CmEOhk0GJLia Qpktjc9Lq2CMpsxEjhl2zM1c5dsyGr31VoQqRSGzSoueYsDJYr7zMj9ho6ZtJSaTsI6+P/ bu4GuutGZC0QDWZDxITmNZieXOlSrEEO36AeKmFcqP10Ml21zXS20vH7I4vzIIoYG+aA1D QfbHVXMrxAAXvtjG3ZbTrdLepc5XGWSYMtwWtL8TyuwkB8LBTfhfKd5C2hEZfvPOuDWKWC Q7R9E8Z/P3/gI+NjUdoeSAa66DOb0rT4+GT5dvOtz4kMqhUbgVhHwt2vD5WZqA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1684850537; 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: references:references; bh=W0htcsfLUpxLaMjJlRhsnhJ2QB5K+K/5FLFN+owMbd8=; b=2ZkOvG8PhJJhu+JdOqY+10Ly9n1nLuRhAsQTa+gmieHFEKo+gcYkT3As2ODkg2iowsRABX GxdoteIlF/RD9CCw== From: Thomas Gleixner To: linux-mm@kvack.org Cc: Andrew Morton , Christoph Hellwig , Uladzislau Rezki , Lorenzo Stoakes , Peter Zijlstra , Baoquan He Subject: [patch 6/6] mm/vmalloc: Dont purge usable blocks unnecessarily References: <20230523135902.517032811@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Date: Tue, 23 May 2023 16:02:17 +0200 (CEST) X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: E83A11C02FC X-Stat-Signature: acpdsw8wgcn1aofp6cissfza8hyfq395 X-HE-Tag: 1684850538-921657 X-HE-Meta: U2FsdGVkX19eBfs7DBfXWoNW5Qz7MsQpCOqT0GyRI+l29ZsTHzmImwrVnD+nkztpcrUzewJDUW2Z9mzvZ3XLjAmd1C5raGRcfY5QDxfFHHAD5kd/Hc8YlrthWBGFDyr+WX51g3+jgXWiLWc9RIJXAxpT22hF7I8JpFNMyTZFm1qoLMjk1bUVcGiMEGLlwWR4Vy9pTMs3sKmTDWSnEse0IrC73322y3w+bYQpyWWot4u6j/oOGpNluKill+IoVOwsnlTvhibIwDpH43p2RedesY0kJ2TsdfCC+0Mlb3hVWJfxD6wXvdLSV6cWdcqulSjDyiYFDhSTOl8f0Ho2TgfgXUGSiDYGXKKG7I2NJJYfZ9oDHn+vEEgtzFwqshUCa7s/Vdro+WlosAMIHww17KNwaxf+Jfn+GQ4+Q9+0qMLbxxO8Z1njAWC1lUQlS1aIVhedGNLOIL/WHE3Kb440PeUrI/SNLiIOQ7sj/Q2+un4suiorHu8CAxFQaqFbv/vrzFAqfdDAAdGLkfLrZ+KXJD6mBroKShGX1SogQRTtWeU4lDU7GvOivTlxl9xINMWAsaiuWidhmhRWJrJQjBID8gBT5PsaQyEowD8kgp74YUXasg3pICZ9fcjieYXtzpDfa4m1hke4Xdql8RF0Ptz28nNyMMhm3aLj8OZkMXYSB4Ynp01GmAwW3WhqT7ZYMo2C/+mxNtf2R+Q2u9h2vtyMBh/3jEHGL9A0RKCV31A6kjtnkkitgsw15YosyRF5JJ6ongoRTJbN42BKWJNcinwA52HVxtdFIiGVcDNhnOKjy4lNBD57ONVr/LkQ7Y57l6VhaiDZbm/AM/OuGRMu/iSTgUaTz1RpMB4nOwSR1OMpV0sYcCOEXlqqmBCsp0o1fFzNnvbm/HOgFwnV3tPJFlmYMZ7zg9hkgx/sc2rR2ibG/+/PsHo3NNKN2hhV1tJP5musGKMvrdPM8K60AVpx3nBUEdS yUl/VP+9 EXbu11VnPbaP9Alc+3lZ/5Cz6M4pkzC8LBDBeKD8g71qC/WBGtUiq+Kk4Pp0VxvkjQbe5LhLJoYIyckQcdIq1MciJsr6hZb26K3Yf 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: Purging fragmented blocks is done unconditionally in several contexts: 1) From drain_vmap_area_work(), when the number of lazy to be freed vmap_areas reached the threshold 2) Reclaiming vmalloc address space from pcpu_get_vm_areas() 3) _unmap_aliases() #1 There is no reason to zap fragmented vmap blocks unconditionally, simply because reclaiming all lazy areas drains at least 32MB * fls(num_online_cpus()) per invocation which is plenty. #2 Reclaiming when running out of space or due to memory pressure makes a lot of sense #3 _unmap_aliases() requires to touch everything because the caller has no clue which vmap_area used a particular page last and the vmap_area lost that information too. Except for the vfree + VM_FLUSH_RESET_PERMS case, which removes the vmap area first and then cares about the flush. That in turn requires a full walk of _all_ vmap areas including the one which was just added to the purge list. But as this has to be flushed anyway this is an opportunity to combine outstanding TLB flushes and do the housekeeping of purging freed areas, but like #1 there is no real good reason to zap usable vmap blocks unconditionally. Add a @force_purge argument to the relevant functions and if not true only purge fragmented blocks which have less than 1/4 of their capacity left. Signed-off-by: Thomas Gleixner --- mm/vmalloc.c | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -791,7 +791,7 @@ get_subtree_max_size(struct rb_node *nod RB_DECLARE_CALLBACKS_MAX(static, free_vmap_area_rb_augment_cb, struct vmap_area, rb_node, unsigned long, subtree_max_size, va_size) -static void purge_vmap_area_lazy(void); +static void purge_vmap_area_lazy(bool force_purge); static BLOCKING_NOTIFIER_HEAD(vmap_notify_list); static void drain_vmap_area_work(struct work_struct *work); static DECLARE_WORK(drain_vmap_work, drain_vmap_area_work); @@ -1649,7 +1649,7 @@ static struct vmap_area *alloc_vmap_area overflow: if (!purged) { - purge_vmap_area_lazy(); + purge_vmap_area_lazy(true); purged = 1; goto retry; } @@ -1717,7 +1717,7 @@ static atomic_long_t vmap_lazy_nr = ATOM static DEFINE_MUTEX(vmap_purge_lock); /* for per-CPU blocks */ -static void purge_fragmented_blocks_allcpus(void); +static void purge_fragmented_blocks_allcpus(bool force_purge); /* * Purges all lazily-freed vmap areas. @@ -1787,10 +1787,10 @@ static bool __purge_vmap_area_lazy(unsig /* * Kick off a purge of the outstanding lazy areas. */ -static void purge_vmap_area_lazy(void) +static void purge_vmap_area_lazy(bool force_purge) { mutex_lock(&vmap_purge_lock); - purge_fragmented_blocks_allcpus(); + purge_fragmented_blocks_allcpus(force_purge); __purge_vmap_area_lazy(ULONG_MAX, 0); mutex_unlock(&vmap_purge_lock); } @@ -1908,6 +1908,12 @@ static struct vmap_area *find_unlink_vma #define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE) +/* + * Purge threshold to prevent overeager purging of fragmented blocks for + * regular operations: Purge if vb->free is less than 1/4 of the capacity. + */ +#define VMAP_PURGE_THRESHOLD (VMAP_BBMAP_BITS / 4) + #define VMAP_RAM 0x1 /* indicates vm_map_ram area*/ #define VMAP_BLOCK 0x2 /* mark out the vmap_block sub-type*/ #define VMAP_FLAGS_MASK 0x3 @@ -2087,12 +2093,16 @@ static void free_vmap_block(struct vmap_ } static bool purge_fragmented_block(struct vmap_block *vb, struct vmap_block_queue *vbq, - struct list_head *purge_list) + struct list_head *purge_list, bool force_purge) { if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS)) return false; - /* prevent further allocs after releasing lock */ + /* Don't overeagerly purge usable blocks unless requested */ + if (!force_purge && vb->free < VMAP_PURGE_THRESHOLD) + return false; + + /* prevent further allocs after releasing lock */ WRITE_ONCE(vb->free, 0); /* prevent purging it again */ WRITE_ONCE(vb->dirty, VMAP_BBMAP_BITS); @@ -2115,7 +2125,7 @@ static void free_purged_blocks(struct li } } -static void purge_fragmented_blocks(int cpu) +static void purge_fragmented_blocks(int cpu, bool force_purge) { LIST_HEAD(purge); struct vmap_block *vb; @@ -2130,19 +2140,19 @@ static void purge_fragmented_blocks(int continue; spin_lock(&vb->lock); - purge_fragmented_block(vb, vbq, &purge); + purge_fragmented_block(vb, vbq, &purge, force_purge); spin_unlock(&vb->lock); } rcu_read_unlock(); free_purged_blocks(&purge); } -static void purge_fragmented_blocks_allcpus(void) +static void purge_fragmented_blocks_allcpus(bool force_purge) { int cpu; for_each_possible_cpu(cpu) - purge_fragmented_blocks(cpu); + purge_fragmented_blocks(cpu, force_purge); } static void *vb_alloc(unsigned long size, gfp_t gfp_mask) @@ -4173,7 +4183,7 @@ struct vm_struct **pcpu_get_vm_areas(con overflow: spin_unlock(&free_vmap_area_lock); if (!purged) { - purge_vmap_area_lazy(); + purge_vmap_area_lazy(true); purged = true; /* Before "retry", check if we recover. */