From patchwork Fri Jan 13 03:19:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baoquan He X-Patchwork-Id: 13099676 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 DAF3EC54EBD for ; Fri, 13 Jan 2023 03:19:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 769CD8E0001; Thu, 12 Jan 2023 22:19:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 719C6900003; Thu, 12 Jan 2023 22:19:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5E1F98E0005; Thu, 12 Jan 2023 22:19:56 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 501D08E0001 for ; Thu, 12 Jan 2023 22:19:56 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 2F899A01BA for ; Fri, 13 Jan 2023 03:19:56 +0000 (UTC) X-FDA: 80348321592.04.B2854BC Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf01.hostedemail.com (Postfix) with ESMTP id 9B7394000F for ; Fri, 13 Jan 2023 03:19:54 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=BLRan1YX; spf=pass (imf01.hostedemail.com: domain of bhe@redhat.com designates 170.10.129.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=1673579994; 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=11FXsd5OGrkSG+2jGllxahOR0gT94+BYaKre9aO1JhQ=; b=h8G2pJu0kWabOzFU0cOdWX6HMNVOqTj6LUpEbuBYUUh+JAI2yb9dzhTVwSO0gHHsLhFFaM kf09Ep93sjs5GmRaf0qWJpHY9RpTkAbUaWiyPEoSGPWpNrHHJeTAcIWKN5lNmXcFqeeLnA jw+oZPcsUmFpsp13AEapp9AttQCHS7c= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=BLRan1YX; spf=pass (imf01.hostedemail.com: domain of bhe@redhat.com designates 170.10.129.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=1673579994; a=rsa-sha256; cv=none; b=nH1sTLas7KbYJVOxMq50jGTD9Zt87BGhN8ROvU6dr952vdQ0LfOH6/1Yq9ojZTujdcyhQ0 rB3FxaHDhHNkQ7JkAwWaGEP6OzGRmrK41dZMa8DIn0OuVBD5RLDZfI2eeSL0Nf545S36v4 1ujvTCCc2vcXoyqSiM2y54ZDlmG8C3M= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1673579993; 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=11FXsd5OGrkSG+2jGllxahOR0gT94+BYaKre9aO1JhQ=; b=BLRan1YX2Dnnv10mMVONGXAn+TSc2+pJNSPzOu2Q8J1PrxUXrIRuxpA2iRrj3v4dnfOB8l ini6qr9whCFiQkHwQeBeooEB4BspuSSFWsI5p8i2QK5NxJg2zCyv4c2Zr4t1wezzt2sZV3 0bxH0Tki7Qz/4z/SqZrEgi64YnjS5Oo= 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-215-NpIfbxH0N0Se2tE09W6OBg-1; Thu, 12 Jan 2023 22:19:49 -0500 X-MC-Unique: NpIfbxH0N0Se2tE09W6OBg-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 12D3F85C064; Fri, 13 Jan 2023 03:19:49 +0000 (UTC) Received: from fedora.redhat.com (ovpn-12-229.pek2.redhat.com [10.72.12.229]) by smtp.corp.redhat.com (Postfix) with ESMTP id C67C74078903; Fri, 13 Jan 2023 03:19:44 +0000 (UTC) From: Baoquan He To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, urezki@gmail.com, lstoakes@gmail.com, stephen.s.brennan@oracle.com, willy@infradead.org, akpm@linux-foundation.org, hch@infradead.org, Baoquan He Subject: [PATCH v3 3/7] mm/vmalloc.c: allow vread() to read out vm_map_ram areas Date: Fri, 13 Jan 2023 11:19:17 +0800 Message-Id: <20230113031921.64716-4-bhe@redhat.com> In-Reply-To: <20230113031921.64716-1-bhe@redhat.com> References: <20230113031921.64716-1-bhe@redhat.com> MIME-Version: 1.0 Content-type: text/plain X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: b6xrygofmz863yg4kz1sa6sij3io5re1 X-Rspamd-Queue-Id: 9B7394000F X-HE-Tag: 1673579994-877488 X-HE-Meta: U2FsdGVkX1/F19cQNiQExFC48dhw2hAo5t074J8Nupw99XaVzKFTy1NpjErelmwS5z3noVssrQ0oQfnJ5YMJKj0HuP07655jxQoghHCkXmgueGOXIBD86oa7Lje5/TaMyIYm2gVwHrP1qayZkpZXUf2V533cQPDNcyyGCawGz9z+roanEZPkPXm95ix2OQOBXKHrFpwLLZSObUbcxfAu7DuDduvbMPOLttrimwVkZDUmW9qQnDrSJdL58duVQf+Jii6EQo4R65SSZtVWj0CuQhEiz40449Zc/RW15FxUctiIV9/0RjKBRMl+pJ2JU3YexPStufuXQz/0eWZLgnNnPdNtJ0aMc7d8t3zJsWk0TZpNLOEMXzWpLqfa+3PecnGO0Zf2V6u3U8uDxCu1ekLge/xtuqUj1WLHnlfI+FjI5wkor3MAHJUnFRQvNblPTBSuU+YemG6iNLunqBwzi002bEZPQi1kXqvVEeRDN/wxOFTZHaFE73KvpEWj6JiSwyDvOhCFpCDfdWkX8cA1tYWB0dlUQq0ZZfiNO23945B8fjgRvnTTyIvarV1c4AbuGMfHOyxCpNQ0wmtP8oJR/vjA6qBhslhaQiZW8lmRFThdje0uWBXmLHoqebWl6lMwKvo/dMmDuCQyHwNNoAQO/8IwGMGYNGUgIFfxGjv4NriLf8XR6dybzIhxHUa7LaOvtIYhvVO41UFlrDGVtkGIdgrJO4ceUyC905hCdQbFIC7ewtosFP2wfDXdO87NfCmdok4N9RXZVJVyQA/milHKhAqpId7+5KRDQDHp0Vp38UE7+pSs++M/FN0F05fNvAb90ESGR4NSOYkyFu3l9Y3CpgyOoFSbCQ21zirV2H724FS8o4RG+C0jKUZdu0jMrMlGfWbxHWY6pUgzHnB/Z8o2jqexwmocQwnR5H/lE3ykRhNrJAXEBnLl5yzCysOXLdpDwEOm0VvLYKhlkXrUS/Xe4Cd oIw== 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. Signed-off-by: Baoquan He --- mm/vmalloc.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 73 insertions(+), 7 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index ab4825050b5c..13875bc41e27 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -3544,6 +3544,65 @@ 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)); + + 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); + while (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 @@ -3574,7 +3633,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 +3654,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 = vm ? get_vm_area_size(vm) : va_size(va); + + if (addr >= vaddr + size) continue; while (addr < vaddr) { if (count == 0) @@ -3610,10 +3673,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);