From patchwork Fri May 19 12:02:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baoquan He X-Patchwork-Id: 13248261 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 50854C77B7F for ; Fri, 19 May 2023 12:02:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E57A5900005; Fri, 19 May 2023 08:02:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E0647900003; Fri, 19 May 2023 08:02:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CF53D900005; Fri, 19 May 2023 08:02:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id BCE06900003 for ; Fri, 19 May 2023 08:02:53 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 704DB809F6 for ; Fri, 19 May 2023 12:02:53 +0000 (UTC) X-FDA: 80806868226.18.3C15B67 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf23.hostedemail.com (Postfix) with ESMTP id DD9D81401A9 for ; Fri, 19 May 2023 12:02:19 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="gJ/E6MLu"; spf=pass (imf23.hostedemail.com: domain of bhe@redhat.com designates 170.10.133.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=1684497740; 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=cwIaLMT16E5qj8r3voiOM19WTdKFjWSp1/HW3YYU9XQ=; b=VPgUaqc+qRMu4b/j2u3t/nUF+uLRHCiknQFQFyLIAuuNgQy13Lsw4RK5Xh/hfW29gUaMXC uFPOeuWn1452AJvxdfrHE4m12kHxhp0w5mH1J3KOUin79EYEewnmShzGFAaDB2dxQv0fvQ b+XC2dcEX26bYT2G2r8uhya1xbitZV0= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="gJ/E6MLu"; spf=pass (imf23.hostedemail.com: domain of bhe@redhat.com designates 170.10.133.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=1684497740; a=rsa-sha256; cv=none; b=TZpYZ7MRKHyxgbPLKQGPdFAp5SfcqRpyBzr6MUAcQu/efS0DuPJch32bXG9epx9CtKnz13 tm0U8Ivu1jggUnFuy4oEBdranJtuZMzAc95DynCsy2+q0HcjxHezfbW4wbzx0vER2HDpA+ uNseM1Z0Xx7Nbo7O+1fv5XI8ya3kRyU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684497738; 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=cwIaLMT16E5qj8r3voiOM19WTdKFjWSp1/HW3YYU9XQ=; b=gJ/E6MLuVu2EmXNG7Jfnt/BZmOSXrtS0gRK3fzrA54aArVII2QpT4L5fvQjmyN3yF4ci9d 2GfiQt+zo9nnJ435JNMIw+kZ8E3OpTL+uovFHiaUrp4tijkkmxiUVJwPfE+0ykzVkEarut W5wBjNwSzjwRzkTmRWE8jLS051g4BpY= 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-618-C3i_wYVyMvq9E-ledI_xKw-1; Fri, 19 May 2023 08:02:15 -0400 X-MC-Unique: C3i_wYVyMvq9E-ledI_xKw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D2DC985A5BF; Fri, 19 May 2023 12:02:14 +0000 (UTC) Received: from localhost (ovpn-12-79.pek2.redhat.com [10.72.12.79]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 05C7840CFD46; Fri, 19 May 2023 12:02:13 +0000 (UTC) Date: Fri, 19 May 2023 20:02:10 +0800 From: Baoquan He To: Thomas Gleixner Cc: "Russell King (Oracle)" , Andrew Morton , linux-mm@kvack.org, Christoph Hellwig , Uladzislau Rezki , Lorenzo Stoakes , Peter Zijlstra , John Ogness , linux-arm-kernel@lists.infradead.org, Mark Rutland , Marc Zyngier , x86@kernel.org Subject: [RFC PATCH 2/3] mm/vmalloc.c: Only flush VM_FLUSH_RESET_PERMS area immediately Message-ID: References: <87zg658fla.ffs@tglx> <87r0rg93z5.ffs@tglx> <87ilcs8zab.ffs@tglx> <87fs7w8z6y.ffs@tglx> <874joc8x7d.ffs@tglx> <87r0rg73wp.ffs@tglx> <87edng6qu8.ffs@tglx> MIME-Version: 1.0 In-Reply-To: <87edng6qu8.ffs@tglx> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Rspamd-Queue-Id: DD9D81401A9 X-Rspam-User: X-Stat-Signature: dhbpizcw6ounrkr6jkqt3xdu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dom0Qqu/qYb6BP+tlNt3SqIIN6GVpOEdcebSY14KAG/lJnKaI3sIcrMMiLTK9oJaDeo2fA54HRo2cwmE1DfdcI2MpNSMNzV9rrCQz0lgzZ0H5qQQwPUbLSpE42T5f0Nsi0yaBfi+eqOEO50uoZzLTpFmL85wM9BtmvvFRItW1TjQNdafbV2VV5zMABCOADhGYcZkLLwJDXrOXBIqzcbI8/Wi1es1KI+TaY= 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: When freeing vmalloc range mapping, only unmapping the page tables is done, TLB flush is lazily deferred to a late stage until lazy_max_pages() is met or vmalloc() can't find available virtual memory region. However, to free VM_FLUSH_RESET_PERMS memory of vmalloc, TLB flushing need be done immediately before freeing pages, and the direct map needs resetting permissions and TLB flushing. Please see commit 868b104d7379 ("mm/vmalloc: Add flag for freeing of special permsissions") for more details. In the current code, when freeing VM_FLUSH_RESET_PERMS memory, lazy purge is also done to try to save a TLB flush later. When doing that, it merges the direct map range with the percpu vbq dirty range and all purge ranges by calculating flush range of [min:max]. That will add the huge gap between direct map range and vmalloc range into the final TLB flush range. So here, only flush VM_FLUSH_RESET_PERMS area immediately, and leave the lazy flush to the normal points, e.g when allocating a new vmap_area, or lazy_max_pages() is met. Signed-off-by: Baoquan He --- mm/vmalloc.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 31e8d9e93650..87134dd8abc3 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -2690,9 +2690,10 @@ static inline void set_area_direct_map(const struct vm_struct *area, */ static void vm_reset_perms(struct vm_struct *area) { - unsigned long start = ULONG_MAX, end = 0; + unsigned long start = ULONG_MAX, end = 0, pages = 0; unsigned int page_order = vm_area_page_order(area); - int flush_dmap = 0; + struct list_head local_flush_list; + struct vmap_area alias_va, va; int i; /* @@ -2708,17 +2709,33 @@ static void vm_reset_perms(struct vm_struct *area) page_size = PAGE_SIZE << page_order; start = min(addr, start); end = max(addr + page_size, end); - flush_dmap = 1; } } + va.va_start = (unsigned long)area->addr; + va.va_end = (unsigned long)(area->addr + area->size); /* * Set direct map to something invalid so that it won't be cached if * there are any accesses after the TLB flush, then flush the TLB and * reset the direct map permissions to the default. */ set_area_direct_map(area, set_direct_map_invalid_noflush); - _vm_unmap_aliases(start, end, flush_dmap); + if (IS_ENABLED(CONFIG_HAVE_FLUSH_TLB_KERNEL_VAS)) { + if (end > start) { + pages = (end - start) >> PAGE_SHIFT; + alias_va.va_start = (unsigned long)start; + alias_va.va_end = (unsigned long)end; + list_add(&alias_va.list, &local_flush_list); + } + + pages += area->size >> PAGE_SHIFT; + list_add(&va.list, &local_flush_list); + + flush_tlb_kernel_vas(&local_flush_list, pages); + } else { + flush_tlb_kernel_range(start, end); + flush_tlb_kernel_range(va.va_start, va.va_end); + } set_area_direct_map(area, set_direct_map_default_noflush); }