From patchwork Sat Dec 17 01:54:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baoquan He X-Patchwork-Id: 13075747 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 D92E2C4332F for ; Sat, 17 Dec 2022 01:55:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6AB0B8E0005; Fri, 16 Dec 2022 20:55:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 65B1F8E0001; Fri, 16 Dec 2022 20:55:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 522898E0005; Fri, 16 Dec 2022 20:55:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 4340E8E0001 for ; Fri, 16 Dec 2022 20:55:00 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 14E13C01C2 for ; Sat, 17 Dec 2022 01:55:00 +0000 (UTC) X-FDA: 80250129960.27.45DA0B6 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf12.hostedemail.com (Postfix) with ESMTP id 6533C40005 for ; Sat, 17 Dec 2022 01:54:57 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=WNxqPtGb; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf12.hostedemail.com: domain of bhe@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=bhe@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1671242097; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=NFxowZ19NpzMI8sTYkYa3ikwqerJc6k8ne6d4Xda4xk=; b=z1rmwts4KgktKfy6FbNG1ccsGVvq2X2TrC/lyuGZ9NH8QAvSVFM0ZWw+k4i/HTMcdHeOEW MSvgbx1a56h6L14BCYLLZD2brVfbXQOubaF+HrVIXJy33jH7AqItdf5ErIHkHhLXNhuxN0 tdxJ7bNu7vWRYUEFBLAwb/0Zpm011Rc= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=WNxqPtGb; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf12.hostedemail.com: domain of bhe@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=bhe@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1671242097; a=rsa-sha256; cv=none; b=YpReWNOdTjk8AEv6OHSJGp/mYzkp20KD3VtyH0m2MkPZ4MEZrzdHQdsFs/SanaoaacKHDT MX17M2Ec8wdunL2OlN8iQVYB8MjcgUUB9pF+wk89Tq88PMyAY//Pn3UJMOFTtQfp0wjzA9 P17cZ5AvyZq1UTRG4TZqLvqSr0O4z3o= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1671242096; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=NFxowZ19NpzMI8sTYkYa3ikwqerJc6k8ne6d4Xda4xk=; b=WNxqPtGbgUGrlv4K+MczMXNVUCgcZtjiCx5pPebEMzj5YsXFO1XQkLeTu9dCoCRZIhia4X /ApSZdCQ4hobNLrEOXci3eta9g7f4N/R7enTU6y0aC9c5AFWAs4ZzcoqciMUIsDOc6szXw mPFs9zz4VyvcJmGrwOcS7LlqItMtNiE= 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-180-hokThCgxMV65_JphGCiUqg-1; Fri, 16 Dec 2022 20:54:53 -0500 X-MC-Unique: hokThCgxMV65_JphGCiUqg-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0170785A588; Sat, 17 Dec 2022 01:54:53 +0000 (UTC) Received: from MiWiFi-R3L-srv.redhat.com (ovpn-12-34.pek2.redhat.com [10.72.12.34]) by smtp.corp.redhat.com (Postfix) with ESMTP id D6A58400F5B; Sat, 17 Dec 2022 01:54:48 +0000 (UTC) From: Baoquan He To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, urezki@gmail.com, stephen.s.brennan@oracle.com, willy@infradead.org, akpm@linux-foundation.org, hch@infradead.org, Baoquan He Subject: [PATCH v2 2/7] mm/vmalloc.c: add flags to mark vm_map_ram area Date: Sat, 17 Dec 2022 09:54:30 +0800 Message-Id: <20221217015435.73889-3-bhe@redhat.com> In-Reply-To: <20221217015435.73889-1-bhe@redhat.com> References: <20221217015435.73889-1-bhe@redhat.com> MIME-Version: 1.0 Content-type: text/plain X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Rspamd-Queue-Id: 6533C40005 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: ikmqzbmqc51ds7ga3kqc9f1j77wkcjkm X-HE-Tag: 1671242097-96738 X-HE-Meta: U2FsdGVkX1/4d5o64kwmfmBTZofglB6n5is3Y4Q/CRwc7U8tcNJNxfpyEFiOMBgYtp6c3Y3jSLqR8VnQBSliyNx4setWpjf7OgzVLJwnedb/AFPKOOnfAB/7/5AMmFX9eGF+DDsYiXlfpt1cLVLtCfR41dp6eQOV0DkGFzqyEuaSE7nV0wckZnstDBx7sKzTvdgMQCPedxBB48abWaFtCTcW7cXkHyCO9uENgJwBz/nlaE8NnxcoC9rsD0Fu2GIWAuw3cibNyl0eF/JKV/aPN1tUl6LrMTJ0sDTDYzQHl5pOFQFsoRlKgLKJvhjLqPqp45BYTOYNOdupPd4HK54oz2Z4XBcGF5uASPjP5RDugWLmPA4iYj6Mhlu9IbulawGXKU2Z6ghuQcVWFh8Km9Aggz1WvRj2hotFm02riRjLvRUsGot1zTRyVmjKLhYuq7YQBWq8Vze4O4pT4OZ7CsYF1t7lnHNCJ6IHjyCyneLtA19hFjVTS/PkGLnnZnGWMzgmTZZiRKBrlplXoBVo+VkZDJekOge7k8minlc96f2S/4xhUZGFjFrOL53M1LmHESSDjSUpt9/wBbgS6qR8vp9TErP/Ancl7J+WNg2fB7mP40/p3uC9R0egOao2cpOr5M5hTi0gPic8QtsShqJoaKv/8/hyYNUnpt7XqheZD3erluMI4oPAI0HpVMkriRaV7MUx76asGmta06UMIig5WdFeQfQOqtIoFZqI7d9TfInecbLrtGXytcioF4bLfDw4oFmpikgxtf2ApRWeD+HV2ZgV1Fe9yKS0K+osyCf+rs8/qUkBEIsnPNWGQnQZi7a8nd9IpUjU2ESdxeuJC6erxc1F+RXvX+gYDM2WHcKyVgwud67kseig5AszLzqG9SIDTAdHKhbndfUQmC7Ig/+qKyAbZKSeElFsg7Jku7lseBT1fOSDG1oOnWNng4qMi4KAhRpZZNjwukrR35xYSTPmRbq 8NA== 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: Through vmalloc API, a virtual kernel area is reserved for physical address mapping. And vmap_area is used to track them, while vm_struct is allocated to associate with the vmap_area to store more information and passed out. However, area reserved via vm_map_ram() is an exception. It doesn't have vm_struct to associate with vmap_area. And we can't recognize the vmap_area with '->vm == NULL' as a vm_map_ram() area because the normal freeing path will set va->vm = NULL before unmapping, please see function remove_vm_area(). Meanwhile, there are two types of vm_map_ram area. One is the whole vmap_area being reserved and mapped at one time; the other is the whole vmap_area with VMAP_BLOCK_SIZE size being reserved, while mapped into split regions with smaller size several times via vb_alloc(). To mark the area reserved through vm_map_ram(), add flags field into struct vmap_area. Bit 0 indicates whether it's a vm_map_ram area, while bit 1 indicates whether it's a vmap_block type of vm_map_ram area. This is a preparatoin for later use. Signed-off-by: Baoquan He --- include/linux/vmalloc.h | 1 + mm/vmalloc.c | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h index 096d48aa3437..69250efa03d1 100644 --- a/include/linux/vmalloc.h +++ b/include/linux/vmalloc.h @@ -76,6 +76,7 @@ struct vmap_area { unsigned long subtree_max_size; /* in "free" tree */ struct vm_struct *vm; /* in "busy" tree */ }; + unsigned long flags; /* mark type of vm_map_ram area */ }; /* archs that select HAVE_ARCH_HUGE_VMAP should override one or more of these */ diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 5d3fd3e6fe09..190f29bbaaa7 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -1586,7 +1586,8 @@ preload_this_cpu_lock(spinlock_t *lock, gfp_t gfp_mask, int node) static struct vmap_area *alloc_vmap_area(unsigned long size, unsigned long align, unsigned long vstart, unsigned long vend, - int node, gfp_t gfp_mask) + int node, gfp_t gfp_mask, + unsigned long va_flags) { struct vmap_area *va; unsigned long freed; @@ -1630,6 +1631,7 @@ static struct vmap_area *alloc_vmap_area(unsigned long size, va->va_start = addr; va->va_end = addr + size; va->vm = NULL; + va->flags = va_flags; spin_lock(&vmap_area_lock); insert_vmap_area(va, &vmap_area_root, &vmap_area_list); @@ -1887,6 +1889,10 @@ struct vmap_area *find_vmap_area(unsigned long addr) #define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE) +#define VMAP_RAM 0x1 +#define VMAP_BLOCK 0x2 +#define VMAP_FLAGS_MASK 0x3 + struct vmap_block_queue { spinlock_t lock; struct list_head free; @@ -1962,7 +1968,8 @@ static void *new_vmap_block(unsigned int order, gfp_t gfp_mask) va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE, VMALLOC_START, VMALLOC_END, - node, gfp_mask); + node, gfp_mask, + VMAP_RAM|VMAP_BLOCK); if (IS_ERR(va)) { kfree(vb); return ERR_CAST(va); @@ -2229,8 +2236,12 @@ void vm_unmap_ram(const void *mem, unsigned int count) return; } - va = find_vmap_area(addr); + spin_lock(&vmap_area_lock); + va = __find_vmap_area((unsigned long)addr, &vmap_area_root); BUG_ON(!va); + if (va) + va->flags &= ~VMAP_RAM; + spin_unlock(&vmap_area_lock); debug_check_no_locks_freed((void *)va->va_start, (va->va_end - va->va_start)); free_unmap_vmap_area(va); @@ -2265,7 +2276,8 @@ void *vm_map_ram(struct page **pages, unsigned int count, int node) } else { struct vmap_area *va; va = alloc_vmap_area(size, PAGE_SIZE, - VMALLOC_START, VMALLOC_END, node, GFP_KERNEL); + VMALLOC_START, VMALLOC_END, + node, GFP_KERNEL, VMAP_RAM); if (IS_ERR(va)) return NULL; @@ -2505,7 +2517,7 @@ static struct vm_struct *__get_vm_area_node(unsigned long size, if (!(flags & VM_NO_GUARD)) size += PAGE_SIZE; - va = alloc_vmap_area(size, align, start, end, node, gfp_mask); + va = alloc_vmap_area(size, align, start, end, node, gfp_mask, 0); if (IS_ERR(va)) { kfree(area); return NULL;