From patchwork Sat Dec 17 01:54:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baoquan He X-Patchwork-Id: 13075748 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 6893CC4332F for ; Sat, 17 Dec 2022 01:55:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0683A8E0006; Fri, 16 Dec 2022 20:55:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 018E48E0001; Fri, 16 Dec 2022 20:55:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E23B08E0006; Fri, 16 Dec 2022 20:55:03 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id D335E8E0001 for ; Fri, 16 Dec 2022 20:55:03 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id A6B42140363 for ; Sat, 17 Dec 2022 01:55:03 +0000 (UTC) X-FDA: 80250130086.01.4ED1185 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf08.hostedemail.com (Postfix) with ESMTP id 22FEA160004 for ; Sat, 17 Dec 2022 01:55:01 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=hnCjrTbF; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf08.hostedemail.com: domain of bhe@redhat.com designates 170.10.129.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=1671242102; 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=8uJ0J4mR/6OkkIc2w45IdSAQBabtBwURgx8VBlnBo3s=; b=VmwNOQoaryMsa0g4xhIf5V4qdXU/CTft5NzqIiGVg6TKa3oqEw0YhQ4hE82vabtceJ8aVf CzApusHqUocLuhP4SW6ojJbsjWvaTdFJY9IZwQQJBqWWaPPdIUhvQ6AXtLJUbAqBk6fka7 svhvE+s4OXQg1Hx7fhMKANTpHfBGIG0= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=hnCjrTbF; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf08.hostedemail.com: domain of bhe@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=bhe@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1671242102; a=rsa-sha256; cv=none; b=lpl1Ysf1szi9f0VOmBljnXG/P7i1GUwbn/zvYiZTSJFpaNWLZ89lkvtjn7ADhGOQS19i39 auXKYuromglhOpf8Cf0oKobQXaHo7Wju+DNOiqlz30AxGV4YtvbkLIKcHnnU3zr0R0Gzak VCYZbvFHR6NNVTaPGeSNqijUqISNlYQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1671242101; 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=8uJ0J4mR/6OkkIc2w45IdSAQBabtBwURgx8VBlnBo3s=; b=hnCjrTbFmiD4tEV0IZKNoucGCs+6z9X18+KRcQSgE0+PSlPIinzOU5nbjrmYsX1+nupS9Z EYa35iRJRA6qPpGPz4VLD1Y7VE1/Yw0hMzIvGLkauPyHzJwNvXT6j4P55IZOXzX5oPK+G7 5MZhqjmHthmy2iRIsDGGvvYSHwR9NdA= 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-317-0DN93yIENQmxRONjjR8X0Q-1; Fri, 16 Dec 2022 20:54:58 -0500 X-MC-Unique: 0DN93yIENQmxRONjjR8X0Q-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 9225B18A63EB; Sat, 17 Dec 2022 01:54:57 +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 BF1BD400F5A; Sat, 17 Dec 2022 01:54:53 +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 3/7] mm/vmalloc.c: allow vread() to read out vm_map_ram areas Date: Sat, 17 Dec 2022 09:54:31 +0800 Message-Id: <20221217015435.73889-4-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-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 22FEA160004 X-Stat-Signature: xfqwkaamwp4ytydu471wg3mi4spwcftj X-HE-Tag: 1671242101-660355 X-HE-Meta: U2FsdGVkX19lE34conavqE0Nt9mAzy9a8J3h1VdDH4O4qHYiobZGod8MEPrK1VcFebFhbPY9VGM8VJBWeHdt+8ervqGlB51BvgjyoyUlBL/ZowBeMjYcmcHayoTO1OmGBm8cKS0lq4rOuFgVvI0dLPHpzYqHJYLc/N/yHIv6dBZTckUfbypN43hJMu0Hhw+C+Uql5maziLunhh4dyqVcvAJrOPg0PhKkqaB2FWj8ZXQAqNcIXGpQp12Tj0EU4FxjEe+hS6UGYbj2VdXh0kIB4E0oN3IAMtaOSQ1CntEAVBqKCoa0zdfDoL1Jarxu2iktyO85qq4LHvAIkGrbbttrkjiXnz3On+kbfxm3Fx6KnXFean4YOKd4ZHz8FaMNhLYpsKrkErJfJJxUwGtYseCD+L5F1oWFWUI0ZBciPkYFJxDUQvX4Lej45UdXoMNMkjVOiUCTsFrDLpYjQt8p0BvVPyzvHRzPcBsabIJHFAlzbQmtzdf7KuaxcgMLLOwRYlnbreqfKnsZ1fYmwCLuqYh9f90+DSAzspNIGwSFmqBWl07omaNu8yfBPYGQcSIklc/y9WrKCkTYLL+6LfG6/fARupGSv67/JWVuluXfGbQg5KmZdME0I47+LTa8Q8bke1FStUzJg6EbLw0L/OX9CDDftPbKtR/N/6amdiFI8OBGpRR9FthL9ev2dzkjVvnPITqTvwQPzcBRlFnDCW1LWxPaw0vQ1uM+5/GSNBKtnqN4cofqDTkGUA0tU8xDu6XzLXHlFRuYq/IuSnWMH+MjtlRLTSpBD1Z8Ohqf1ScgdlN8X/pN6WwAg/geWEcRlQqDvq6qQ9+7lc5vUBfJRKtp1uCk5gmt4SQIUO+c3gOB1JHXIn3YSVjji5qLuD9U9LAxuJB/OP2PRpFKPhyCdd2qtrbo1SBcy2fjVrsJTznGaY5NhMH7Z70qar2IPkeKbwMQRBPXUoOuWVPQrnIbwsuQ5tq ebg== 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: Currently, vread can read out vmalloc areas which is associated with a vm_struct. While this doesn't work for areas created by vm_map_ram() interface because it doesn't have an associated vm_struct. Then in vread(), these areas will be skipped. Here, add a new function vb_vread() to read out areas managed by vmap_block specifically. Then recognize vm_map_ram areas via vmap->flags and handle them respectively. Signed-off-by: Baoquan He --- mm/vmalloc.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 7 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 190f29bbaaa7..6612914459cf 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -3515,6 +3515,51 @@ static int aligned_vread(char *buf, char *addr, unsigned long count) return copied; } +static void vb_vread(char *buf, char *addr, int count) +{ + char *start; + struct vmap_block *vb; + unsigned long offset; + unsigned int rs, re, n; + + vb = xa_load(&vmap_blocks, addr_to_vb_idx((unsigned long)addr)); + + spin_lock(&vb->lock); + if (bitmap_empty(vb->used_map, VMAP_BBMAP_BITS)) { + spin_unlock(&vb->lock); + memset(buf, 0, count); + return; + } + for_each_set_bitrange(rs, re, vb->used_map, VMAP_BBMAP_BITS) { + if (!count) + break; + start = vmap_block_vaddr(vb->va->va_start, rs); + if (addr < start) { + if (count == 0) + break; + *buf = '\0'; + buf++; + addr++; + count--; + } + /*it could start reading from the middle of used region*/ + offset = offset_in_page(addr); + n = (re - rs + 1) << PAGE_SHIFT - offset; + if (n > count) + n = count; + aligned_vread(buf, start+offset, n); + + buf += n; + addr += n; + count -= n; + } + spin_unlock(&vb->lock); + + /* zero-fill the left dirty or free regions */ + if (count) + memset(buf, 0, count); +} + /** * vread() - read vmalloc area in a safe way. * @buf: buffer for reading data @@ -3545,7 +3590,7 @@ long vread(char *buf, char *addr, unsigned long count) struct vm_struct *vm; char *vaddr, *buf_start = buf; unsigned long buflen = count; - unsigned long n; + unsigned long n, size, flags; addr = kasan_reset_tag(addr); @@ -3566,12 +3611,16 @@ long vread(char *buf, char *addr, unsigned long count) if (!count) break; - if (!va->vm) + vm = va->vm; + flags = va->flags & VMAP_FLAGS_MASK; + + if (!vm && !flags) continue; - vm = va->vm; - vaddr = (char *) vm->addr; - if (addr >= vaddr + get_vm_area_size(vm)) + vaddr = (char *) va->va_start; + size = flags ? va_size(va) : get_vm_area_size(vm); + + if (addr >= vaddr + size) continue; while (addr < vaddr) { if (count == 0) @@ -3581,10 +3630,13 @@ long vread(char *buf, char *addr, unsigned long count) addr++; count--; } - n = vaddr + get_vm_area_size(vm) - addr; + n = vaddr + size - addr; if (n > count) n = count; - if (!(vm->flags & VM_IOREMAP)) + + if ((flags & (VMAP_RAM|VMAP_BLOCK)) == (VMAP_RAM|VMAP_BLOCK)) + vb_vread(buf, addr, n); + else if ((flags & VMAP_RAM) || !(vm->flags & VM_IOREMAP)) aligned_vread(buf, addr, n); else /* IOREMAP area is treated as memory hole */ memset(buf, 0, n);