From patchwork Mon Feb 6 08:40:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baoquan He X-Patchwork-Id: 13129480 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 DC6C8C61DA4 for ; Mon, 6 Feb 2023 08:40:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 774D16B0075; Mon, 6 Feb 2023 03:40:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6FD5A6B0078; Mon, 6 Feb 2023 03:40:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 577286B007B; Mon, 6 Feb 2023 03:40:56 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 44D726B0075 for ; Mon, 6 Feb 2023 03:40:56 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 1555D1A044C for ; Mon, 6 Feb 2023 08:40:56 +0000 (UTC) X-FDA: 80436221712.21.0587A66 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf17.hostedemail.com (Postfix) with ESMTP id 4E0264001E for ; Mon, 6 Feb 2023 08:40:54 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Wza+ghHr; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf17.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=1675672854; 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=qDthkt1yrsdVPAFblgfFdo4HNws2a1WpfRKMjlx3T68=; b=YrC6eyk0dhOzi+BduOvRwQthtzlURIRe7ZOTiXgNLB0HrLidiDxF9hDN+AOiZeqIjjFzeU QVarKWAQczHf94kDQNUd5W8xmhO5QfVCKi04g1h0s2BRiOfFfdyZsHu2s6i9MZLzPEs5Z6 0nxlMJLk1pZqSIm8UZYCNgcOEZusmYQ= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Wza+ghHr; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf17.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=1675672854; a=rsa-sha256; cv=none; b=qSgQodLSB4kG+wgu9AwqSJ3dmn+wL32sYChG+MKNAjgG3vaItc7aFOnvsFtwZgmG5rfUHj exgjH5j8bPMCTGZiovu+uHOyBXJh7X/lA6ngXgeUWbnsCdaY4iWkpB+OYhJKZI1tzTq2Gm T97LqmLVICFvGX/D4jeyWmD+PopHRWg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675672853; 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=qDthkt1yrsdVPAFblgfFdo4HNws2a1WpfRKMjlx3T68=; b=Wza+ghHrUUAPL77B6S7ekG/Bs4SQET6zBoV9ksNqbqhVYZeQbTqlYddLI8b6kxD67tl3qx juYDxW2SIeavfYtunI2xPtTp040wTy+4H6FPKanNr+p2r+/hHNTkgFQJo0lnXPt2K+rjkh 9/nCVVqG/wnJesAXWn9KEdlwF5Qm5BI= 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-620-GfzowxhZMZyVQncuaXs6Yw-1; Mon, 06 Feb 2023 03:40:42 -0500 X-MC-Unique: GfzowxhZMZyVQncuaXs6Yw-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2F7C5858F0E; Mon, 6 Feb 2023 08:40:42 +0000 (UTC) Received: from MiWiFi-R3L-srv.redhat.com (ovpn-12-71.pek2.redhat.com [10.72.12.71]) by smtp.corp.redhat.com (Postfix) with ESMTP id 14BF52166B2A; Mon, 6 Feb 2023 08:40:37 +0000 (UTC) From: Baoquan He To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, akpm@linux-foundation.org, stephen.s.brennan@oracle.com, urezki@gmail.com, lstoakes@gmail.com, error27@gmail.com, Baoquan He Subject: [PATCH v5 3/7] mm/vmalloc.c: allow vread() to read out vm_map_ram areas Date: Mon, 6 Feb 2023 16:40:16 +0800 Message-Id: <20230206084020.174506-4-bhe@redhat.com> In-Reply-To: <20230206084020.174506-1-bhe@redhat.com> References: <20230206084020.174506-1-bhe@redhat.com> MIME-Version: 1.0 Content-type: text/plain X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Rspamd-Queue-Id: 4E0264001E X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: 8iihu1zs3j9zhmywihqtitii81xexm75 X-HE-Tag: 1675672854-363883 X-HE-Meta: U2FsdGVkX192Rtpzzq7rYZuf91jxnRwnsuArivolt5tSC4m1ha8r6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Z13ctOfH6GUoHj7tdHZw8aL2s9vVkg5OiLeW3YIDuWGj/GvYCkY0H9mTvGBDEnkO5EsMq5MWVuuEexKNdsLidVJeqTSGePuuaAzxVGh3JyiZ71xVJQjXnLtVQSCwsKV78MzUiVgRADzETccINgT 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 are all skipped. Here, add a new function vmap_ram_vread() to read out vm_map_ram areas. The area created with vmap_ram_vread() interface directly can be handled like the other normal vmap areas with aligned_vread(). While areas which will be further subdivided and managed with vmap_block need carefully read out page-aligned small regions and zero fill holes. Reported-by: Stephen Brennan Signed-off-by: Baoquan He Reviewed-by: Lorenzo Stoakes Tested-by: Stephen Brennan --- mm/vmalloc.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 81 insertions(+), 7 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index ab4825050b5c..4bb78ebd70f6 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -3544,6 +3544,68 @@ static int aligned_vread(char *buf, char *addr, unsigned long count) return copied; } +static void vmap_ram_vread(char *buf, char *addr, int count, unsigned long flags) +{ + char *start; + struct vmap_block *vb; + unsigned long offset; + unsigned int rs, re, n; + + /* + * If it's area created by vm_map_ram() interface directly, but + * not further subdividing and delegating management to vmap_block, + * handle it here. + */ + if (!(flags & VMAP_BLOCK)) { + aligned_vread(buf, addr, count); + return; + } + + /* + * Area is split into regions and tracked with vmap_block, read out + * each region and zero fill the hole between regions. + */ + vb = xa_load(&vmap_blocks, addr_to_vb_idx((unsigned long)addr)); + if (!vb) + goto finished; + + spin_lock(&vb->lock); + if (bitmap_empty(vb->used_map, VMAP_BBMAP_BITS)) { + spin_unlock(&vb->lock); + goto finished; + } + for_each_set_bitrange(rs, re, vb->used_map, VMAP_BBMAP_BITS) { + if (!count) + break; + start = vmap_block_vaddr(vb->va->va_start, rs); + while (addr < start) { + if (count == 0) + goto unlock; + *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; + } +unlock: + spin_unlock(&vb->lock); + +finished: + /* 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 @@ -3574,7 +3636,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); @@ -3595,12 +3657,21 @@ long vread(char *buf, char *addr, unsigned long count) if (!count) break; - if (!va->vm) + vm = va->vm; + flags = va->flags & VMAP_FLAGS_MASK; + /* + * VMAP_BLOCK indicates a sub-type of vm_map_ram area, need + * be set together with VMAP_RAM. + */ + WARN_ON(flags == VMAP_BLOCK); + + 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 = vm ? get_vm_area_size(vm) : va_size(va); + + if (addr >= vaddr + size) continue; while (addr < vaddr) { if (count == 0) @@ -3610,10 +3681,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_ram_vread(buf, addr, n, flags); + else if (!(vm->flags & VM_IOREMAP)) aligned_vread(buf, addr, n); else /* IOREMAP area is treated as memory hole */ memset(buf, 0, n);