From patchwork Mon Feb 6 08:40:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baoquan He X-Patchwork-Id: 13129476 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 49BA1C61DA4 for ; Mon, 6 Feb 2023 08:40:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A5ADC6B0072; Mon, 6 Feb 2023 03:40:34 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A0AD96B0073; Mon, 6 Feb 2023 03:40:34 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8D2856B0074; Mon, 6 Feb 2023 03:40:34 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 7F90A6B0072 for ; Mon, 6 Feb 2023 03:40:34 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 478D4A0B88 for ; Mon, 6 Feb 2023 08:40:34 +0000 (UTC) X-FDA: 80436220788.14.F4BDF9A Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf14.hostedemail.com (Postfix) with ESMTP id 6092D100008 for ; Mon, 6 Feb 2023 08:40:32 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="VJmSAV/J"; spf=pass (imf14.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=1675672832; 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: references:dkim-signature; bh=eDVaZJdMGL4kVKzSO0PRgS/vEsAWvBE6o/r2igpI1bc=; b=ITT+ux+OKCsReJL4ru2dXEd/n+yS5ky4uGAGcM3EnKqbFf0t0gA468g/q6i01haPk5TLsN iU9iTCpNKLvB8d4RbTnsj+xmYEHAYtEEklsiJBNN1w+Qv/tg1FIStekmFphfrB0qY5MhLb 0xyMWzMiJ8W/6C5vJO09mWdBVaFZjT4= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="VJmSAV/J"; spf=pass (imf14.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=1675672832; a=rsa-sha256; cv=none; b=h6cccfUwLl+1PVxuvcOTtj39AoBF8hCpFpLQdKjf0J3/wQ+ljxK1Ztpzs8CRsPKNApB3Z5 pQZ++KqDD4Iox/Bi5fYTzucQRMYih7qh7PmzK4i1up6yhQd+1qYUTHqPVKPIDRk1EGy4WK cr8KUTM+/bHS2BSg29RmOQqJEhyG9pQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675672831; 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; bh=eDVaZJdMGL4kVKzSO0PRgS/vEsAWvBE6o/r2igpI1bc=; b=VJmSAV/J+qELJ6bb5i2P2ScbIxcwiBpFea7CuWeMKnZqk3DtJy/ZrqpF7Fpd+a6HZlJiQO 6UNT4giYe2dw5sg1gg3eBpAknimk/EmNJlEQrNsvol/ZGqfxB9a1P9nNYaUUkGe8dA5rs9 Ideen8sd9VykaBKKgI8UmAkbIjRFhnc= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-659-6hb2469dMb-FDzgH3OfGLw-1; Mon, 06 Feb 2023 03:40:28 -0500 X-MC-Unique: 6hb2469dMb-FDzgH3OfGLw-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 B8B342A59567; Mon, 6 Feb 2023 08:40:27 +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 9D5E02166B2A; Mon, 6 Feb 2023 08:40:23 +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 0/7] mm/vmalloc.c: allow vread() to read out vm_map_ram areas Date: Mon, 6 Feb 2023 16:40:13 +0800 Message-Id: <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-Stat-Signature: gunogzy3coxk5yaw3zmmzn6dfrr1wka3 X-Rspam-User: X-Rspamd-Queue-Id: 6092D100008 X-Rspamd-Server: rspam06 X-HE-Tag: 1675672832-830570 X-HE-Meta: U2FsdGVkX1+kGh0Hqd6nUk/0tQv8v9Q2MgFQ4rUgIiIT2CMDnb2EjdqawIo7AEdyrOrSV+lGqV2JGRwdffpn9kmrnlkxYc8S4ACPDq8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p5UMRmIZNch4FcTUOi0nY9DDG4efGSXFzZeDaESaugxjRJCndSw7KUF4ALVZS5/11LJusk4JmP9l/YKKdRHY9bIBjTVR7hjCDdleFVWec9RBebm5tDFXuFUA= 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: Problem: *** Stephen reported vread() will skip vm_map_ram areas when reading out /proc/kcore with drgn utility. Please see below link to get more details. /proc/kcore reads 0's for vmap_block https://lore.kernel.org/all/87ilk6gos2.fsf@oracle.com/T/#u Root cause: *** The normal vmalloc API uses struct vmap_area to manage the virtual kernel area allocated, and associate a vm_struct to store more information and pass out. However, area reserved through vm_map_ram() interface doesn't allocate vm_struct to associate with. So the current code in vread() will skip the vm_map_ram area through 'if (!va->vm)' conditional checking. Solution: *** To mark the area reserved through vm_map_ram() interface, add field 'flags' into struct vmap_area. Bit 0 indicates this is vm_map_ram area created through vm_map_ram() interface, bit 1 marks out the type of vm_map_ram area which makes use of vmap_block to manage split regions via vb_alloc/free(). And also add bitmap field 'used_map' into struct vmap_block to mark those further subdivided regions being used to differentiate with dirty and free regions in vmap_block. With the help of above vmap_area->flags and vmap_block->used_map, we can recognize and handle vm_map_ram areas successfully. All these are done in patch 1~3. Meanwhile, do some improvement on areas related to vm_map_ram areas in patch 4, 5. And also change area flag from VM_ALLOC to VM_IOREMAP in patch 6, 7 because this will show them as 'ioremap' in /proc/vmallocinfo, and exclude them from /proc/kcore. Testing *** - I did basic testing on kvm guest, and run below commands to access kcore file to do statistics: makedumpfile --mem-usage /proc/kcore - Stephen helped to test v4 and confirm the problem he spotted and reported has been resolved with the v4 patchset. Changelog *** v4->v5: - Add a 'unlock' label and goto jumping in while loop of vmap_ram_vread() function, this improve the codes execution. This is done in patch 3. - Add Stephen's Reported-by and Tested-by tags in patch 3. v3->v4: - Fix typo in pach 2 catched by Lorenzo. - Add WARN_ON(flags == VMAP_BLOCK) in vread() to address Dan's concern that VMAP_BLOCK could be set alone in vmap->flags. - Add checking on the returned value from xa_load() in vmap_ram_vread(), Uladzislau commented on the risk of this place. - Fix a bug in s_show() which is changed in patch 5. The old change will cause 'va->vm is NULL but the VMAP_RAM flag is not set' case wrongly handled. Please see below link for details. - https://lore.kernel.org/all/Y8aAmuUY9OxrYlLm@kili/T/#u - Add Uladzislau and Lorenzo's Reviewed-by. v2->v3: - Benefited from find_unlink_vmap_area() introduced by Uladzislau, do not need to worry about the va->vm and va->flags reset during freeing. - Change to identify vm_map_area with VMAP_RAM in vmap->flags in vread(). - Rename the old vb_vread() to vmap_ram_vread(). - Handle two kinds of vm_map_area area reading in vmap_ram_vread() respectively. - Fix bug of the while loop code block in vmap_block reading, found by Lorenzo. - Improve conditional check related to vm_map_ram area, suggested by Lorenzo. v1->v2: - Change alloc_vmap_area() to pass in va_flags so that we can pass and set vmap_area->flags for vm_map_ram area. With this, no extra action need be added to acquire vmap_area_lock when doing the vmap_area->flags setting. Uladzislau reviewed v1 and pointed out the issue. - Improve vb_vread() to cover the case where reading is started from a dirty or free region. RFC->v1: - Add a new field flags in vmap_area to mark vm_map_ram area. It cold be risky reusing the vm union in vmap_area in RFC. I will consider reusing the union in vmap_area to save memory later. Now just take the simpler way to let's focus on resolving the main problem. - Add patch 4~7 for optimization. Baoquan He (7): mm/vmalloc.c: add used_map into vmap_block to track space of vmap_block mm/vmalloc.c: add flags to mark vm_map_ram area mm/vmalloc.c: allow vread() to read out vm_map_ram areas mm/vmalloc: explicitly identify vm_map_ram area when shown in /proc/vmcoreinfo mm/vmalloc: skip the uninitilized vmalloc areas powerpc: mm: add VM_IOREMAP flag to the vmalloc area sh: mm: set VM_IOREMAP flag to the vmalloc area arch/powerpc/kernel/pci_64.c | 2 +- arch/sh/kernel/cpu/sh4/sq.c | 2 +- include/linux/vmalloc.h | 1 + mm/vmalloc.c | 127 ++++++++++++++++++++++++++++++----- 4 files changed, 112 insertions(+), 20 deletions(-)