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: 34160 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp1304316wrn; Fri, 16 Dec 2022 17:57:15 -0800 (PST) X-Google-Smtp-Source: AA0mqf5IhkgftIA9zXZgCX977pWka75tuCcyJGHtYalbOPc/Xn30senrtTREptmbZujpQ5vR3GD9 X-Received: by 2002:a17:902:e30b:b0:189:dc5e:8889 with SMTP id q11-20020a170902e30b00b00189dc5e8889mr30486567plc.43.1671242235164; Fri, 16 Dec 2022 17:57:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671242235; cv=none; d=google.com; s=arc-20160816; b=ECzi5TYMoRP6jZC9uoMZ8tUhw78lcIMoK/D5ZpkdeDsGxBn7Zae3DW+6CwEfhNmq4i dxIsPhVaWVuHdmZp3A4TuTisoE7S92txRhNHPwRapFwzuNSvCTky/pNWCyU0L99ILkZ1 IHQHkZuEkMkdXEndrajiKhaK0Hdf+z7ZYFnBVWp14jAGRw+Vvvvb6j+2vvPEUImuKymh t4kntaj9SqkD8mt1G8436wkjlHLdOHAINRuz0BoA5VFjatvBIAUb7a1j3IEA3yhss9nX OgYLD8pOeURndzFzL23t0gikUeYROBPls+vHfEnVteOvgD/mzax4+al/fgMUHTektsmO qsvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=NFxowZ19NpzMI8sTYkYa3ikwqerJc6k8ne6d4Xda4xk=; b=q6go48B766XiptpssHsszSdCttd2XU0DB3lnj94kZftHK9HHuraxtb69dCcVObzAqY IIJT4TuLcC2RM61W6ucp3iL6Hmt3nfp/FIf1NRF9vEnG2Cb73cwmWx/HL+CgizWdb7Gm YIYaXZ/EkDfuAdAK0wzR5CpozZhR18TpMS51TS1pxiGdizstFd6cuGHBL9ZwnYYb64BQ YCEHbWUW3y8ESSPHFVItFUt6Rt9OsQkQc/pa8eeaIaIybG+O7zOpVizX7AEo3Etd1JK/ mrAs8IqRTw15BvVY9ufmHUZW5huh8aoknUNKwvx6Ce66pig29nzcTIHCKlLtgEVxDswO ewOw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=WNxqPtGb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m9-20020a170902db0900b00186b1459953si4681969plx.382.2022.12.16.17.57.02; Fri, 16 Dec 2022 17:57:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=WNxqPtGb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230160AbiLQB4F (ORCPT + 99 others); Fri, 16 Dec 2022 20:56:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230014AbiLQBzw (ORCPT ); Fri, 16 Dec 2022 20:55:52 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 85A0A4A590 for ; Fri, 16 Dec 2022 17:54:57 -0800 (PST) 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-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752424497841027369?= X-GMAIL-MSGID: =?utf-8?q?1752424497841027369?= 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;