Received: by 2002:ab2:6a05:0:b0:1f8:1780:a4ed with SMTP id w5csp2805251lqo; Tue, 14 May 2024 09:34:58 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXWQr/bcgXiUc3gAqnaKnTe7vLVfy3AvMzkhOtvzPD2ObUT026MUgq4TcQWuOS094g4sP3D3mYd4UHXsQDWF48ZQeYz89R5nyu2Tt+G5Q== X-Google-Smtp-Source: AGHT+IGyANnWedZ7azeghI62jt399UtodfyzzAoyv7n4M8SZnMvaBN4S+EDFCO36p6ALAs6/4oy5 X-Received: by 2002:a17:906:b1d3:b0:a59:9db2:d988 with SMTP id a640c23a62f3a-a5a2d65f404mr860881666b.50.1715704498778; Tue, 14 May 2024 09:34:58 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715704498; cv=pass; d=google.com; s=arc-20160816; b=q+x6kkOK6/srRWLM9hc99E+g9y4mDsi1vC6ZTXK989u3DWIKoeMlxln4f2dezm/Wj5 kc69mmHmS7v34Jq4gNwcR5NznuQgnRdVrxcpVjknY4IW0GW21zd7Z0aPTvGhLJ9zvFsH hkhDj3VWKIVT2XLtszSTW5OZDAESGQRKuiFWL8evkBqFaxW0i5uJnADE1kGOhjbyDteB jfMAD94vtEQjAEKv/7vR3VXwnXkGCzb2kjcXLeiaY9rrbGVDTnn2sdYRt9uBSF8vB7f2 cbuMkzc8l55PWwXzr/f5JupA9jgWnBannf58iVEix+wtmuAxDcJY89yU5j7kV7hzAFAo jcRA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:references :in-reply-to:subject:cc:to:from:dkim-signature; bh=WttioydahafaKu8oxrBy+hstlRQWgLvk/Frp/hYufBU=; fh=yTPHybxnuha4kXO4Bg1kNjBfaLVxcdUApJREPZa38IQ=; b=CQhfbHiETlC/U9RLt/HApEhszE4HOCeVNEwgT1NyUxIZy//zTK3tr4bZQbAP1/3A6l r7JO8MyPb8NyDgJ30Wo24sQkVbtw+hTO691tSvgaxSGKEiBlp0hDsE1jbiAre8DBRDn7 T0WHUWhoeRj12bTYhcj91DXq3VzCnQjOG4vZzN6Eyp13Iqth21yjKD4b4Qw9zguNdrmH J32PtKI+dgiaqOMsv7BOlcKqViyWFr3G4SWQQfoDD6IqzhYOp6qQUcMG41f6Yi9Oku8E WRVuut1vzQdabi6UgE02K7erQlnGOCUeSSDc+53dpHLJkDuORptGXwALd89/m2kh9fx9 oqFQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=FYMEDT0v; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-178958-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-178958-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id a640c23a62f3a-a5a1793c2cesi682684566b.33.2024.05.14.09.34.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 May 2024 09:34:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-178958-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=FYMEDT0v; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-178958-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-178958-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 831781F21585 for ; Tue, 14 May 2024 16:34:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AF75D339A0; Tue, 14 May 2024 16:34:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="FYMEDT0v" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D82E8365 for ; Tue, 14 May 2024 16:34:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715704490; cv=none; b=JMSxlcHRukQdD5SjFj2N3Oy9OqyJMjNB74+ooEK40PBlIpBPLaHZY0tLuwNr4m4ftY12399AKEPdbeLX2Gh5HLgacp1m7kOgsL6kb9fWeeWUq8foJJHxQN0XH5b/8rU47IUWQaYwB04320iayruG5nAKCjDN6BD4nxd039tcmHk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715704490; c=relaxed/simple; bh=bquuasHCV5I/L0YtYX6erf/Sqf5ntgz+jwR6bM97uoI=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=VpoEjLJkRH6c2rwMwLOGEx9/hTnoepqeukvcyT+8QuwlKniiBWcvoVo/Ky+qpzz9I3FxG4XbrF7aocnDVYHTcASGSwSUtfUb/8uTBzMT0FY7yWagSVXDxAwtx84tM5vKJjEznyJN74HvraLOhZNYQfP9Y3OLHaxn55klskAgWgw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FYMEDT0v; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id E3170C2BD10; Tue, 14 May 2024 16:34:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715704490; bh=bquuasHCV5I/L0YtYX6erf/Sqf5ntgz+jwR6bM97uoI=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=FYMEDT0vkpzQD1yVnU2etV+jk/xcPPcRTnjJp1dXIe/kvA6wjKMmb83lmvnh8j5Dn FwLVn5mOCLtsTAdqA8e9IKWDT8E1G/31jkuFdgTQUCFVqcwxZht7KJGKevoP0JH57J zIBXGFM5s+eE2aQNps/yAwPufE7fmtUFgh5S+pdLnqdIwIE0yH8pddlodbrR/Hh68c J+fx3cQBjp5VlcuCQVssVUXpZBD4CM8gk4ClcVRitd++N7JJMo/HaUn7xM2sRhbxoc Y6npf+jqNIb0LcL/u5Ot+hkLv2CRV/M1fhsdaJAuwPjh2aZYzFqDbdqvwGc5WIeaJ4 ZKaBEjhF8DWgw== From: =?utf-8?B?QmrDtnJuIFTDtnBlbA==?= To: David Hildenbrand , Alexandre Ghiti , Albert Ou , Palmer Dabbelt , Paul Walmsley , linux-riscv@lists.infradead.org Cc: =?utf-8?B?QmrDtnJuIFTDtnBlbA==?= , Andrew Bresticker , Chethan Seshadri , Lorenzo Stoakes , Oscar Salvador , Santosh Mamila , Sivakumar Munnangi , Sunil V L , linux-kernel@vger.kernel.org, linux-mm@kvack.org, virtualization@lists.linux-foundation.org Subject: Re: [PATCH v2 4/8] riscv: mm: Add memory hotplugging support In-Reply-To: <267be75e-ed4a-45d0-883f-51c7324c30f1@redhat.com> References: <20240514140446.538622-1-bjorn@kernel.org> <20240514140446.538622-5-bjorn@kernel.org> <267be75e-ed4a-45d0-883f-51c7324c30f1@redhat.com> Date: Tue, 14 May 2024 18:34:46 +0200 Message-ID: <87r0e4tkrd.fsf@all.your.base.are.belong.to.us> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable David Hildenbrand writes: > On 14.05.24 16:04, Bj=C3=B6rn T=C3=B6pel wrote: >> From: Bj=C3=B6rn T=C3=B6pel >>=20 >> For an architecture to support memory hotplugging, a couple of >> callbacks needs to be implemented: >>=20 >> arch_add_memory() >> This callback is responsible for adding the physical memory into the >> direct map, and call into the memory hotplugging generic code via >> __add_pages() that adds the corresponding struct page entries, and >> updates the vmemmap mapping. >>=20 >> arch_remove_memory() >> This is the inverse of the callback above. >>=20 >> vmemmap_free() >> This function tears down the vmemmap mappings (if >> CONFIG_SPARSEMEM_VMEMMAP is enabled), 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. >>=20 >> arch_get_mappable_range() >> This functions returns the PA range that the direct map can map. >> Used by the MHP internals for sanity checks. >>=20 >> The page table unmap/teardown functions are heavily based on code from >> the x86 tree. The same remove_pgd_mapping() function is used in both >> vmemmap_free() and arch_remove_memory(), but in the latter function >> the backing pages are not removed. >>=20 >> Signed-off-by: Bj=C3=B6rn T=C3=B6pel >> --- >> arch/riscv/mm/init.c | 242 +++++++++++++++++++++++++++++++++++++++++++ >> 1 file changed, 242 insertions(+) >>=20 >> diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c >> index 6f72b0b2b854..7f0b921a3d3a 100644 >> --- a/arch/riscv/mm/init.c >> +++ b/arch/riscv/mm/init.c >> @@ -1493,3 +1493,245 @@ void __init pgtable_cache_init(void) >> } >> } >> #endif >> + >> +#ifdef CONFIG_MEMORY_HOTPLUG >> +static void __meminit free_pte_table(pte_t *pte_start, pmd_t *pmd) >> +{ >> + pte_t *pte; >> + int i; >> + >> + for (i =3D 0; i < PTRS_PER_PTE; i++) { >> + pte =3D pte_start + i; >> + if (!pte_none(*pte)) >> + return; >> + } >> + >> + free_pages((unsigned long)page_address(pmd_page(*pmd)), 0); >> + pmd_clear(pmd); >> +} >> + >> +static void __meminit free_pmd_table(pmd_t *pmd_start, pud_t *pud) >> +{ >> + pmd_t *pmd; >> + int i; >> + >> + for (i =3D 0; i < PTRS_PER_PMD; i++) { >> + pmd =3D pmd_start + i; >> + if (!pmd_none(*pmd)) >> + return; >> + } >> + >> + free_pages((unsigned long)page_address(pud_page(*pud)), 0); >> + pud_clear(pud); >> +} >> + >> +static void __meminit free_pud_table(pud_t *pud_start, p4d_t *p4d) >> +{ >> + pud_t *pud; >> + int i; >> + >> + for (i =3D 0; i < PTRS_PER_PUD; i++) { >> + pud =3D pud_start + i; >> + if (!pud_none(*pud)) >> + return; >> + } >> + >> + free_pages((unsigned long)page_address(p4d_page(*p4d)), 0); >> + p4d_clear(p4d); >> +} >> + >> +static void __meminit free_vmemmap_storage(struct page *page, size_t si= ze, >> + struct vmem_altmap *altmap) >> +{ >> + if (altmap) >> + vmem_altmap_free(altmap, size >> PAGE_SHIFT); >> + else >> + free_pages((unsigned long)page_address(page), get_order(size)); > > If you unplug a DIMM that was added during boot (can happen on x86-64,=20 > can it happen on riscv?), free_pages() would not be sufficient. You'd be= =20 > freeing a PG_reserved page that has to be freed differently. I'd say if it can happen on x86-64, it probably can on RISC-V. I'll look into this for the next spin! Thanks for spending time on the series! Cheers, Bj=C3=B6rn