From patchwork Fri May 12 14:57:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= X-Patchwork-Id: 13239433 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 DA49EC77B7F for ; Fri, 12 May 2023 14:58:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 30D516B0071; Fri, 12 May 2023 10:58:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2BD866B0074; Fri, 12 May 2023 10:58:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1AC7E6B0075; Fri, 12 May 2023 10:58:01 -0400 (EDT) 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 0AAB96B0071 for ; Fri, 12 May 2023 10:58:01 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id D814C160E49 for ; Fri, 12 May 2023 14:58:00 +0000 (UTC) X-FDA: 80781907920.13.F475B8F Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf15.hostedemail.com (Postfix) with ESMTP id EC09BA0003 for ; Fri, 12 May 2023 14:57:57 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Pj6FNyKz; spf=pass (imf15.hostedemail.com: domain of bjorn@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=bjorn@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1683903478; a=rsa-sha256; cv=none; b=iIJUUrmflRJeaKc4qAoqql1ZR9WoLda1pJe72uM1MEMsqaCrjHUC9Ygvd2HxZLe1R0PaKY HJ2rrExC8zHxwJubim3fHUdPghCOb8zVx5djLZmnEbgs33TKMVN9sczXfE5wRiH0AIs6sN IOEOF9IEUTgNiCydlE7nAnVLbzNOCPE= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Pj6FNyKz; spf=pass (imf15.hostedemail.com: domain of bjorn@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=bjorn@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1683903478; 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=+7kC2K5AYNwgek0F6L3nlX3XTmu0fgt7ThZI07+Oy/Y=; b=f4gr8zhJWSffxl5zONjXXxOosrsVZcvcRbf08xGPsWraOVlon6STmMuEWiTK4p995BattO DovquJ0OT7koknUekJ7QGKhfnyuU4QMuMvQ1spTtWA29u5PdEsh6OAfviaeekr/pa2EnBc z69WzXbe6WVOdIy+N7I2EjUIqqrFi+0= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id BEAC061713; Fri, 12 May 2023 14:57:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 72EC6C433EF; Fri, 12 May 2023 14:57:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1683903476; bh=lQUbfvw+rG1d/xBNHe707+zVxdkdQoFXQhp1/m5+D2o=; h=From:To:Cc:Subject:Date:From; b=Pj6FNyKzYS1+2WsFAf60GrrqPD8Bmex/fW3XVKGpchoXvDFVdCzq5pdBn4CLPLVXT HUhE+59XO8/Wzj4AbJ4CpzpWU8GhJax++sLAiqeoqCvrFb26fbJweVhVIu8+8/ShtW 5xxS2gt3CMy+8uR+Uh/dbsXnUZQ/OW7fUfDsFOB9sjBoB4onucM2o++3XufmploeLt IToUIhUOICJmjt97dVVQn7bZqxLzfM2ZZQl1LSBWBymwWA1g9Ot3XItdXz4P1zf78H GHHMedHBLWkcPYUKroEaEtDo4RitN+wAPQcmwNItogJTgj79mJ1cXeuAh7lNssR//F kXU3t1IihxakQ== From: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= To: Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-riscv@lists.infradead.org Cc: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , linux-kernel@vger.kernel.org, linux-mm@kvack.org, David Hildenbrand , Oscar Salvador , virtualization@lists.linux-foundation.org, linux@rivosinc.com, Alexandre Ghiti Subject: [PATCH 0/7] riscv: Memory Hot(Un)Plug support Date: Fri, 12 May 2023 16:57:30 +0200 Message-Id: <20230512145737.985671-1-bjorn@kernel.org> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: EC09BA0003 X-Stat-Signature: czht98whr81zad93cwu1kzd8xpmid8pw X-HE-Tag: 1683903477-184259 X-HE-Meta: U2FsdGVkX1+zDmNr8YhfOeGcODHHTrHeiCV+jVW46H9bQfEa1SF1R+OWgYxFk4wmcJLyYUXBgqZvYEuXV28P6QnF9mshbc5EjS+qKcZiKLZdHcJ85JJGXj0pJUk0wnI+maLuHhxy7LMMRnourMnhsda6oizXmKcU+zIck9Lffz6evuD/7ZcMIrNGee/zgTQGoEjCA6+ZTNHQY3MQ607RiCzD1dfCTDN/4QEht7+e3AIfLlG0C9z+b0OBW5za+Ff9sgM5MQiHU4aMDbqX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IjEioIj4xlPuYI1fn8Uq5u0o2KQzGkjHALQJEsaysM+9Jc9SwIfwRJ5rxCnvDuqFmMxF9fI5mIvr3ZzLy8zX5Mte96gXbSAZQ1H1TGRup9kh8K3OhEh1MFtmUygWkLMp+Qk57oJz5d0dEVQ== 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: From: Björn Töpel Memory Hot(Un)Plug support for the RISC-V port ============================================== Introduction ------------ To quote "Documentation/admin-guide/mm/memory-hotplug.rst": "Memory hot(un)plug allows for increasing and decreasing the size of physical memory available to a machine at runtime." This series attempts to add memory hot(un)plug support for the RISC-V Linux port. I'm sending the series as a v1, but it's borderline RFC. It definitely needs more testing time, but it would be nice with some early input. Implementation -------------- From an arch perspective, a couple of callbacks needs to be implemented to support hot plugging: arch_add_memory() This callback is responsible for updating the linear/direct map, and call into the memory hot plugging generic code via __add_pages(). arch_remove_memory() In this callback the linear/direct map is tore down. vmemmap_free() The function tears down the vmemmap mappings (if CONFIG_SPARSEMEM_VMEMMAP is in-use), and also deallocates the backing vmemmap pages. Note that for persistent memory, an alternative allocator for the backing pages can be used -- the vmem_altmap. This means that when the backing pages are cleared, extra care is needed so that the correct deallocation method is used. Note that RISC-V populates the vmemmap using vmemmap_populate_basepages(), so currently no hugepages are used for the backing store. The page table unmap/teardown functions are heavily based (copied!) from the x86 tree. The same remove_pgd_mapping() is used in both vmemmap_free() and arch_remove_memory(), but in the latter function the backing pages are not removed. On RISC-V, the PGD level kernel mappings needs to synchronized with all page-tables (e.g. via sync_kernel_mappings()). Synchronization involves special care, like locking. Instead, this patch series takes a different approach (introduced by Jörg Rödel in the x86-tree); Pre-allocate the PGD-leaves (P4D, PUD, or PMD depending on the paging setup) at mem_init(), for vmemmap and the direct map. Pre-allocating the PGD-leaves waste some memory, but is only enabled for CONFIG_MEMORY_HOTPLUG. The number pages, potentially unused, are ~128 * 4K. Patch 1: Preparation for hotplugging support, by pre-allocating the PGD leaves. Patch 2: Changes the __init attribute to __meminit, to avoid that the functions are removed after init. __meminit keeps the functions after init, if memory hotplugging is enabled for the build. Patch 3: Refactor the direct map setup, so it can be used for hot add. Patch 4: The actual add/remove code. Mostly a page-table-walk exercise. Patch 5: Turn on the arch support in Kconfig Patch 6: Now that memory hotplugging is enabled, make virtio-mem usable for RISC-V Patch 7: Pre-allocate vmalloc PGD-leaves as well, which removes the need for vmalloc faulting. RFC --- * TLB flushes. The current series uses Big Hammer flush-it-all. * Pre-allocation vs explicit syncs Testing ------- ACPI support is still in the making for RISC-V, so tests that involve CXL and similar fanciness is currently not possible. Virtio-mem, however, works without proper ACPI support. In order to try this out in Qemu, some additional patches for Qemu are needed: * Enable virtio-mem for RISC-V * Add proper hotplug support for virtio-mem The patch for Qemu can be found is commit 5d90a7ef1bc0 ("hw/riscv/virt: Support for virtio-mem-pci"), and can be found here https://github.com/bjoto/qemu/tree/riscv-virtio-mem I will try to upstream that work in parallel with this. Thanks to David Hildenbrand for valuable input for the Qemu side of things. The series is based on the RISC-V fixes tree https://git.kernel.org/pub/scm/linux/kernel/git/riscv/linux.git/log/?h=fixes Thanks, Björn Björn Töpel (7): riscv: mm: Pre-allocate PGD leaves to avoid synchronization riscv: mm: Change attribute from __init to __meminit for page functions riscv: mm: Refactor create_linear_mapping_range() for hot add riscv: mm: Add memory hot add/remove support riscv: Enable memory hot add/remove arch kbuild support virtio-mem: Enable virtio-mem for RISC-V riscv: mm: Pre-allocate vmalloc PGD leaves arch/riscv/Kconfig | 2 + arch/riscv/include/asm/kasan.h | 4 +- arch/riscv/include/asm/mmu.h | 2 +- arch/riscv/include/asm/pgtable.h | 2 +- arch/riscv/mm/fault.c | 7 +- arch/riscv/mm/init.c | 387 ++++++++++++++++++++++++++++--- drivers/virtio/Kconfig | 2 +- 7 files changed, 364 insertions(+), 42 deletions(-) base-commit: 3b90b09af5be42491a8a74a549318cfa265b3029