From patchwork Thu Oct 5 18:31:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Verma, Vishal L" X-Patchwork-Id: 13410614 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 23998E92729 for ; Thu, 5 Oct 2023 18:32:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7F0498D0006; Thu, 5 Oct 2023 14:32:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 79CBF8E0007; Thu, 5 Oct 2023 14:32:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 63DF18D00C0; Thu, 5 Oct 2023 14:32:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 570E58D0006 for ; Thu, 5 Oct 2023 14:32:05 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 1B859C03DA for ; Thu, 5 Oct 2023 18:32:05 +0000 (UTC) X-FDA: 81312252210.08.5C9BA82 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.151]) by imf05.hostedemail.com (Postfix) with ESMTP id 93E68100016 for ; Thu, 5 Oct 2023 18:32:02 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=WxgFs2EY; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf05.hostedemail.com: domain of vishal.l.verma@intel.com designates 192.55.52.151 as permitted sender) smtp.mailfrom=vishal.l.verma@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696530722; 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=nFi/UHhVlx0+FJZ9TcTGgT5mbEkbuGrIBBN6C7W+WA4=; b=1cOPNu6SIOMs+k+sYH23vEVVBLfRcrNZ9EbKccFFvgS+PQuUh/MvR8eJJiTq0mwlECcEN5 aviY/ydU8aAbhPuFN+VNnAXBfRyvDcUoDLbegXf7lfIFrUq7Jm1UyqOnsKGRSFKjmbdedO IXq0AYXTXVal5TJAbiZrh5ZoM88giGA= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=WxgFs2EY; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf05.hostedemail.com: domain of vishal.l.verma@intel.com designates 192.55.52.151 as permitted sender) smtp.mailfrom=vishal.l.verma@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696530722; a=rsa-sha256; cv=none; b=FGIzgoVLrgizNelvjXkJuSm8LaWHk+U2SEpCo2N8oNSZoOjvyzRZYFhPWMeeUgvuQFSORk l/xzsGJIX/ALmcXtZpQiCToBlfjAZ0hQb5ORI5BS3sVGuHqdDjoveE+r9rKKhWQ6X/j3ii 7SVQ09d0iiHoksvUdoGQCozKikbPXsA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1696530722; x=1728066722; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=YjfMcrhNvXHMJFZBqykeVf1EucGf0umzz4JhkLD/Qkk=; b=WxgFs2EY1MMMzq/57rwiXY8uDjNbigGK+mgRXf0958fYPxmWElO6lIY5 qHRsuWz3Vp4juB3cCH8j9NGqHG7o/RoibDYbFB6awJBfZfp1qFLvEBMNS 3enFI9QYtX9RAINKqIfZAyS9/yBA6b44aD1meIkDZwVXnxfsPEQyQurQ0 TK1VTdwd37NcsWMRPjUi8ZBQdZfq7RDm9c7ReP1dmWWZgMag47lRREdXM FTvG6ztPZH4CrDqcBPG0eHRQY/1eUn/KH/SXx8/ubKXf2UbgQWyVfw3Ao I4JfRPOnVJrQt7fxyJqiUNjK8HkbIa56FVXaKAaZ2RuLB7u3p1xkRDYzh g==; X-IronPort-AV: E=McAfee;i="6600,9927,10854"; a="363860737" X-IronPort-AV: E=Sophos;i="6.03,203,1694761200"; d="scan'208";a="363860737" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Oct 2023 11:31:54 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10854"; a="781342828" X-IronPort-AV: E=Sophos;i="6.03,203,1694761200"; d="scan'208";a="781342828" Received: from amykuo-mobl.amr.corp.intel.com (HELO [192.168.1.200]) ([10.212.12.247]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Oct 2023 11:31:53 -0700 From: Vishal Verma Date: Thu, 05 Oct 2023 12:31:39 -0600 Subject: [PATCH v5 1/2] mm/memory_hotplug: split memmap_on_memory requests across memblocks MIME-Version: 1.0 Message-Id: <20231005-vv-kmem_memmap-v5-1-a54d1981f0a3@intel.com> References: <20231005-vv-kmem_memmap-v5-0-a54d1981f0a3@intel.com> In-Reply-To: <20231005-vv-kmem_memmap-v5-0-a54d1981f0a3@intel.com> To: Andrew Morton , David Hildenbrand , Oscar Salvador , Dan Williams , Dave Jiang Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, nvdimm@lists.linux.dev, linux-cxl@vger.kernel.org, Huang Ying , Dave Hansen , "Aneesh Kumar K.V" , Michal Hocko , Jonathan Cameron , Jeff Moyer , Vishal Verma X-Mailer: b4 0.12.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=8397; i=vishal.l.verma@intel.com; h=from:subject:message-id; bh=YjfMcrhNvXHMJFZBqykeVf1EucGf0umzz4JhkLD/Qkk=; b=owGbwMvMwCXGf25diOft7jLG02pJDKnyjBJBclPPXj5WJ9n00skx2Nnbcs2GS9KRlhWtZq++T wx+vrCyo5SFQYyLQVZMkeXvno+Mx+S25/MEJjjCzGFlAhnCwMUpABO5ksjwv840ZvXXpHb/uGd+ V5R/Zp9v9xZqTf16WiFg1e9arjUW+xgZ3mbzL1uzNHTCrsr5mpUrDbtMa13sVv3Nn8TFrinFwWX ABwA= X-Developer-Key: i=vishal.l.verma@intel.com; a=openpgp; fpr=F8682BE134C67A12332A2ED07AFA61BEA3B84DFF X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 93E68100016 X-Stat-Signature: 6q4s88ehrcawst1gox5f4czwcuocok5p X-HE-Tag: 1696530722-955346 X-HE-Meta: U2FsdGVkX18NfTCcB/dfDZboR46lcZtVDGO3HUBlkOrjtPz7YGoOyF2laIYqaWJRq/jDx+xgLEExX3w06BqMEsOx7pcN5JX7LOnK7rieS8yr129jEU3qMki0vcqKh/T8U2MNm2lQKFFHn336TWZWqstURoQFD9NOcawyfwbT9K0Hg7U+CXDQaxm8yG1we1Npvp3ROqu7dBnpx72Q1JwxqaKFXkLhzjlRBbEWzpiqBhdhnwxmBgeTvrZFwAMRT32Mf2eieF+QLpnD6bTIUaa6kO7eLV/P+WjzfTAo1gwnQEZS55aknQtHCgY5s3hMuDnwjtz+efo6u9y5gEuw31eVrpyVeSQp1+GxyukVEtxJ3OflFPRW/EV902XYVYdkpD2qDi2ta7rEXg5uwNKAydlUJOejAEj07Le4Uv5NAoFgFh4pEdmWhgFFEYXu5ttpDUsGr4apxs0M49e1RZeT4+8hlLd1KizxPyhqFpBC+2M0JLeKC6e7FDcVBPy6ylbGHc7F1xdSLj/VY3tLLFD7Ea1Bb6dyD45w8MJTC1MPDFLOD1CT8PNCRxUo/R5MQq12Fh53aAiknTUUrRQFwuHxtXg5+ZVwF1g+pZ9MqIQsctuKVBu7rGvZiwsdqpQ1enT5Z7Oo4FD6pXfasQXP28dAjgtmiwdBVqlPKGTXceafcMIl8DuJq/Io+x4hqfPlXX5OxTbO1XK2/eyaXubr4IiNpTts/FLWWgtPyu3RjzSFT6zT8phKzI3MMJ46ZdJgQ1+2SvCPTvOQ4IZ/IzchkRUmV3FACYD76xbvvvqHIbCDqazkV/BaS6LgTQT50rT5ChFC3AmePbWq0TaxTYD1QF81hUud1kRwcB5r9Sj2RlySjiCzv4JeAdeDln6RXtXaFgeporc1C1pukx+CryDnGhwwSBbORykaZ7r1BBWrHfKrfgXyb1cZt9pZT1I0r0xyP0MEaFvoddw/FeKdadj25j/j9wR p6gyV1eC 1eUdMv7zRcZ3XovzRv3g83Ao7sZGkmmIVxD9pUgLudizkPACZo0/sV0b5Vngjz9y11S9MiNKqkK6HoGMDlh5H2n0Q0IP1sYrMdNukI/K0s1ldwud0kAxRSoux9jn9QQjy0ciOFVDrt7TYKKIjWHrzb/63tM/uEWIrXlQh3gXzuFtTeONy0sp06+t+dUegXkaGAErDoaA4EHBjVW9F3lPZ6iagwjiKguz44HdQGBJmciab9f63cN6+l6bg1vhEIXcAC/XX0+LeETnsMDEqLcR01tsLOkvLWdwliHexSafbmTWq2iDZ4fYuTQaMJrA65y5Ge/pYHrGkNFTq4yyhtN37rfVScgvIQ9dX25twxIx+YtUVVUc= 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: The MHP_MEMMAP_ON_MEMORY flag for hotplugged memory is restricted to 'memblock_size' chunks of memory being added. Adding a larger span of memory precludes memmap_on_memory semantics. For users of hotplug such as kmem, large amounts of memory might get added from the CXL subsystem. In some cases, this amount may exceed the available 'main memory' to store the memmap for the memory being added. In this case, it is useful to have a way to place the memmap on the memory being added, even if it means splitting the addition into memblock-sized chunks. Change add_memory_resource() to loop over memblock-sized chunks of memory if caller requested memmap_on_memory, and if other conditions for it are met. Teach try_remove_memory() to also expect that a memory range being removed might have been split up into memblock sized chunks, and to loop through those as needed. Cc: Andrew Morton Cc: David Hildenbrand Cc: Michal Hocko Cc: Oscar Salvador Cc: Dan Williams Cc: Dave Jiang Cc: Dave Hansen Cc: Huang Ying Suggested-by: David Hildenbrand Signed-off-by: Vishal Verma Reviewed-by: Dan Williams --- mm/memory_hotplug.c | 162 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 99 insertions(+), 63 deletions(-) diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index f8d3e7427e32..77ec6f15f943 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -1380,6 +1380,44 @@ static bool mhp_supports_memmap_on_memory(unsigned long size) return arch_supports_memmap_on_memory(vmemmap_size); } +static int add_memory_create_devices(int nid, struct memory_group *group, + u64 start, u64 size, mhp_t mhp_flags) +{ + struct mhp_params params = { .pgprot = pgprot_mhp(PAGE_KERNEL) }; + struct vmem_altmap mhp_altmap = { + .base_pfn = PHYS_PFN(start), + .end_pfn = PHYS_PFN(start + size - 1), + }; + int ret; + + if ((mhp_flags & MHP_MEMMAP_ON_MEMORY)) { + mhp_altmap.free = memory_block_memmap_on_memory_pages(); + params.altmap = kmalloc(sizeof(struct vmem_altmap), GFP_KERNEL); + if (!params.altmap) + return -ENOMEM; + + memcpy(params.altmap, &mhp_altmap, sizeof(mhp_altmap)); + } + + /* call arch's memory hotadd */ + ret = arch_add_memory(nid, start, size, ¶ms); + if (ret < 0) + goto error; + + /* create memory block devices after memory was added */ + ret = create_memory_block_devices(start, size, params.altmap, group); + if (ret) + goto err_bdev; + + return 0; + +err_bdev: + arch_remove_memory(start, size, NULL); +error: + kfree(params.altmap); + return ret; +} + /* * NOTE: The caller must call lock_device_hotplug() to serialize hotplug * and online/offline operations (triggered e.g. by sysfs). @@ -1388,14 +1426,10 @@ static bool mhp_supports_memmap_on_memory(unsigned long size) */ int __ref add_memory_resource(int nid, struct resource *res, mhp_t mhp_flags) { - struct mhp_params params = { .pgprot = pgprot_mhp(PAGE_KERNEL) }; + unsigned long memblock_size = memory_block_size_bytes(); enum memblock_flags memblock_flags = MEMBLOCK_NONE; - struct vmem_altmap mhp_altmap = { - .base_pfn = PHYS_PFN(res->start), - .end_pfn = PHYS_PFN(res->end), - }; struct memory_group *group = NULL; - u64 start, size; + u64 start, size, cur_start; bool new_node = false; int ret; @@ -1436,28 +1470,21 @@ int __ref add_memory_resource(int nid, struct resource *res, mhp_t mhp_flags) /* * Self hosted memmap array */ - if (mhp_flags & MHP_MEMMAP_ON_MEMORY) { - if (mhp_supports_memmap_on_memory(size)) { - mhp_altmap.free = memory_block_memmap_on_memory_pages(); - params.altmap = kmalloc(sizeof(struct vmem_altmap), GFP_KERNEL); - if (!params.altmap) + if ((mhp_flags & MHP_MEMMAP_ON_MEMORY) && + mhp_supports_memmap_on_memory(memblock_size)) { + for (cur_start = start; cur_start < start + size; + cur_start += memblock_size) { + ret = add_memory_create_devices(nid, group, cur_start, + memblock_size, + mhp_flags); + if (ret) goto error; - - memcpy(params.altmap, &mhp_altmap, sizeof(mhp_altmap)); } - /* fallback to not using altmap */ - } - - /* call arch's memory hotadd */ - ret = arch_add_memory(nid, start, size, ¶ms); - if (ret < 0) - goto error_free; - - /* create memory block devices after memory was added */ - ret = create_memory_block_devices(start, size, params.altmap, group); - if (ret) { - arch_remove_memory(start, size, NULL); - goto error_free; + } else { + ret = add_memory_create_devices(nid, group, start, size, + mhp_flags); + if (ret) + goto error; } if (new_node) { @@ -1494,8 +1521,6 @@ int __ref add_memory_resource(int nid, struct resource *res, mhp_t mhp_flags) walk_memory_blocks(start, size, NULL, online_memory_block); return ret; -error_free: - kfree(params.altmap); error: if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK)) memblock_remove(start, size); @@ -2146,12 +2171,41 @@ void try_offline_node(int nid) } EXPORT_SYMBOL(try_offline_node); -static int __ref try_remove_memory(u64 start, u64 size) +static void __ref remove_memory_block_and_altmap(int nid, u64 start, u64 size) { + int rc = 0; struct memory_block *mem; - int rc = 0, nid = NUMA_NO_NODE; struct vmem_altmap *altmap = NULL; + rc = walk_memory_blocks(start, size, &mem, test_has_altmap_cb); + if (rc) { + altmap = mem->altmap; + /* + * Mark altmap NULL so that we can add a debug + * check on memblock free. + */ + mem->altmap = NULL; + } + + /* + * Memory block device removal under the device_hotplug_lock is + * a barrier against racing online attempts. + */ + remove_memory_block_devices(start, size); + + arch_remove_memory(start, size, altmap); + + /* Verify that all vmemmap pages have actually been freed. */ + if (altmap) { + WARN(altmap->alloc, "Altmap not fully unmapped"); + kfree(altmap); + } +} + +static int __ref try_remove_memory(u64 start, u64 size) +{ + int rc, nid = NUMA_NO_NODE; + BUG_ON(check_hotplug_memory_range(start, size)); /* @@ -2167,47 +2221,28 @@ static int __ref try_remove_memory(u64 start, u64 size) if (rc) return rc; + mem_hotplug_begin(); + /* - * We only support removing memory added with MHP_MEMMAP_ON_MEMORY in - * the same granularity it was added - a single memory block. + * For memmap_on_memory, the altmaps could have been added on + * a per-memblock basis. Loop through the entire range if so, + * and remove each memblock and its altmap. */ if (mhp_memmap_on_memory()) { - rc = walk_memory_blocks(start, size, &mem, test_has_altmap_cb); - if (rc) { - if (size != memory_block_size_bytes()) { - pr_warn("Refuse to remove %#llx - %#llx," - "wrong granularity\n", - start, start + size); - return -EINVAL; - } - altmap = mem->altmap; - /* - * Mark altmap NULL so that we can add a debug - * check on memblock free. - */ - mem->altmap = NULL; - } + unsigned long memblock_size = memory_block_size_bytes(); + u64 cur_start; + + for (cur_start = start; cur_start < start + size; + cur_start += memblock_size) + remove_memory_block_and_altmap(nid, cur_start, + memblock_size); + } else { + remove_memory_block_and_altmap(nid, start, size); } /* remove memmap entry */ firmware_map_remove(start, start + size, "System RAM"); - /* - * Memory block device removal under the device_hotplug_lock is - * a barrier against racing online attempts. - */ - remove_memory_block_devices(start, size); - - mem_hotplug_begin(); - - arch_remove_memory(start, size, altmap); - - /* Verify that all vmemmap pages have actually been freed. */ - if (altmap) { - WARN(altmap->alloc, "Altmap not fully unmapped"); - kfree(altmap); - } - if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK)) { memblock_phys_free(start, size); memblock_remove(start, size); @@ -2219,6 +2254,7 @@ static int __ref try_remove_memory(u64 start, u64 size) try_offline_node(nid); mem_hotplug_done(); + return 0; }