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; } From patchwork Thu Oct 5 18:31:40 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: 13410615 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 6A7CEE92725 for ; Thu, 5 Oct 2023 18:32:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 556AF8E0008; Thu, 5 Oct 2023 14:32:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 52CE88E0007; Thu, 5 Oct 2023 14:32:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3F42F8E0008; Thu, 5 Oct 2023 14:32:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 320298E0007 for ; Thu, 5 Oct 2023 14:32:07 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id E5872A0377 for ; Thu, 5 Oct 2023 18:32:06 +0000 (UTC) X-FDA: 81312252252.14.288436B Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.151]) by imf02.hostedemail.com (Postfix) with ESMTP id 9BCB180017 for ; Thu, 5 Oct 2023 18:32:04 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=LVBfAiVu; spf=pass (imf02.hostedemail.com: domain of vishal.l.verma@intel.com designates 192.55.52.151 as permitted sender) smtp.mailfrom=vishal.l.verma@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696530724; 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=RmxXppi107uDu2rBa7tN0eETDD29jKNqd+LtByEXgBQ=; b=SuJ3+X2EDtHQSiao/tOdkaAhrMb3Rfzu4ZT4wCT6b0wQZnNvf8pdel7oKPxjExUu1QbLoX Fv+TyyrV2xV3zI006CAYzYwzCl8mX2z6yn66WSKEO2AKvwnZ6lnGpEEDUa2V1K0LghyuN8 y7KfljbUrmwhkV7CDvKSj+egkxPfbS0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696530724; a=rsa-sha256; cv=none; b=l6w8SHIeQCGxxjIkP9Dh0EQMh0H8mXKZiIHdicH6wcVaC8hgCrfdZs39RT7ihMC4h3TK90 /zLfX6VDUx5WeDLUzrGDnDQ3FosfwhImKNA/a9OkT2jPCdWl3J6Jl7TuiZBD+7bxGu4Qsz kcOWixlnMf49Hc8luJii+1Frzc582bA= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=LVBfAiVu; spf=pass (imf02.hostedemail.com: domain of vishal.l.verma@intel.com designates 192.55.52.151 as permitted sender) smtp.mailfrom=vishal.l.verma@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1696530724; x=1728066724; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=6MPyfJ9TCOOWqIqYtO0O+rswGranA361S3pLp//iGZE=; b=LVBfAiVuzDVdwdLp9P148x/BuvvbOU3F6Hv3hl/C6I/H3XhNaz1BP7M0 Zv1jlfdDUWbYRX2OmDIuqi1aKAIQ2nr+7rwS7hSdsMCt2Dv0kXVFxgzwV Pog9rEjoDUHm6giiHZYZuXKvRBHQM043f5vizm9DEIOGpepgZQqtgFsaF KdrpAIdz8oPHcHA6iIhaG0IuMiLic9b6r5BrXBJiT8DLKioGGcuUaL5wy j8v6WTxrClzn4mSOmNuVeosEE+Gi6m6ljbjzuOxSfpHxIjjjaUUDxHeND /sJTVXtiL1ioVfYlQqp0RhHoH9Gu3M1Sw9F8HPi+7pIKFmeS4aVsKptU8 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10854"; a="363860747" X-IronPort-AV: E=Sophos;i="6.03,203,1694761200"; d="scan'208";a="363860747" 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:55 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10854"; a="781342843" X-IronPort-AV: E=Sophos;i="6.03,203,1694761200"; d="scan'208";a="781342843" 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:54 -0700 From: Vishal Verma Date: Thu, 05 Oct 2023 12:31:40 -0600 Subject: [PATCH v5 2/2] dax/kmem: allow kmem to add memory with memmap_on_memory MIME-Version: 1.0 Message-Id: <20231005-vv-kmem_memmap-v5-2-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 , Jonathan Cameron X-Mailer: b4 0.12.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=6429; i=vishal.l.verma@intel.com; h=from:subject:message-id; bh=6MPyfJ9TCOOWqIqYtO0O+rswGranA361S3pLp//iGZE=; b=owGbwMvMwCXGf25diOft7jLG02pJDKnyjBJZMtvzxY/rnZRnS5B6+WzZG5k0I03eqFmzWPlE1 cIS7t/oKGVhEONikBVTZPm75yPjMbnt+TyBCY4wc1iZQIYwcHEKwEQ4fzL8sxL8VRjvf69bNlib 57zYhYK3GvWrD3OvLEkq+ro53MRJl+F/8UOVGYFydm9mF+84vt5AfMbKXSJ/HtWz9LQ/7dnsEP6 YCwA= X-Developer-Key: i=vishal.l.verma@intel.com; a=openpgp; fpr=F8682BE134C67A12332A2ED07AFA61BEA3B84DFF X-Stat-Signature: a9rqwdgx8sphsd1umy5g96yiaeoa81dc X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 9BCB180017 X-Rspam-User: X-HE-Tag: 1696530724-333234 X-HE-Meta: U2FsdGVkX1+csmDgxHNCOKZvGjnZQk4Ztb6fPyaY8zy8g75nFB87hISDNnj8Pmt0Wvu2wCLttdvMLnGYZsZICAxvbDuBP2X01OGe71pGcko9iHft8tzwRWzkBEsHoRHghy6tOr6FQXeD8H9lufwmSadawBhL6doOFNpnF6KHKm6A+KsO4CfRh+qosq6qIz4oum4OVvskhcIyg1IKhAuaqJTMr7P6Qie43Yei2ADrN5mwdCXfnRb0eWtJIixJFEYpZpzsBWlcMsbAOy4z5WG6sI52maYFH7pQqp9Oyn4ijvp6/uPDEnaq/0uPIr6nbLhYdTaYDJSrPe+Wx0ib8FEeytkkL6o9R2zOcaFmM86ARrR0joQvXpcXpBNSCbXpz/LVEvNMNnHeb8ki7trwVUCMNk+ildbZu+AZj04FHycsWaWcQG3H5lxJB+lj81uao9leyx84GfYMbel1sTD/zKqYM2tQNPt7kBLktMyHKoN4clpUxlSgHSdjrZV2RpEcvqBbWkdnC8KpfCnv1ByNW2Kl61Y9YM4dTUub6lFBaoSnlFfW5EyDMQHyDQrngPcy6HYfZqMCvrBJgdmL1APcLYGCjZNQSpB/uuNNRYoaMkAoYZmGoiIoQGlyGwAaMMuIsrzcKcJQXDxtARk09ZFC6wmmIoIaoK7qFh5Q3pjpwvZ4sxiWf+IQFxGcuPRxkndTQAZLuCBwqnonZxcaRvh1Bo4S98YbhPcJ/jloJjTYww9THwzWjR/FfFY8jvK9jdd7Jdbyv7KTxvMiy/F1aUW1ArTisTl71Df0EYuHXnB0JcxldFdMi28GfCkZPdZaszgUtVov+yQvVaxtRi75OxQ93S3Bo6iTr39b/Ik3o7jTL36fiuI6SeIjnrWDZA7B8XDBAkG610ySzCgPNfXXXe0lMp7nvRhex6ELLzbqoKX7klf8NP23Tw7HKYx9O/FkHVYUcuPxaJ/Rv7jcbTrmxMTrOv0 SyM6dsQs RuODFJZ2NLDXe/lCnKlkXqyPjOassXcnm3iyjL4EQwdsim9ucT7804BSnE1P59on5AFOQNJsjTUOcLJkXpxsDEitxk53nmBGAgenLVdB8Kqpub806fiUDf5pEDo/hvY4XbCcijsUOXw72pFio06scYq+MD9OHKDGRtH3gR+4LlHS3Vmm/JmBrksIVrAoSZgtzB8x1k5Rd4TRB800rLvrqspuxL0JwpODR7WL2uFlxxEyuheXmImV5v3FA6URC5IMRWFkGe9BHfuCtTtzvaaLE87n300lJim9cFCSa2/WZuQQQXmf3EwyH50HMcDbzFmCQzwhxrF7zwaKxLJ6GMG7MbCYuUdBFLUYPs7DY/HnlD8EvjYKwimUUTOuPzAzj/o6eEYoW 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: Large amounts of memory managed by the kmem driver may come in via CXL, and it is often desirable to have the memmap for this memory on the new memory itself. Enroll kmem-managed memory for memmap_on_memory semantics if the dax region originates via CXL. For non-CXL dax regions, retain the existing default behavior of hot adding without memmap_on_memory semantics. Add a sysfs override under the dax device to control this behavior and override either default. Cc: Andrew Morton Cc: David Hildenbrand Cc: Michal Hocko Cc: Oscar Salvador Cc: Dan Williams Cc: Dave Jiang Cc: Dave Hansen Cc: Huang Ying Reviewed-by: Jonathan Cameron Reviewed-by: David Hildenbrand Signed-off-by: Vishal Verma --- drivers/dax/bus.h | 1 + drivers/dax/dax-private.h | 1 + drivers/dax/bus.c | 38 ++++++++++++++++++++++++++++++++++++++ drivers/dax/cxl.c | 1 + drivers/dax/hmem/hmem.c | 1 + drivers/dax/kmem.c | 8 +++++++- drivers/dax/pmem.c | 1 + 7 files changed, 50 insertions(+), 1 deletion(-) diff --git a/drivers/dax/bus.h b/drivers/dax/bus.h index 1ccd23360124..cbbf64443098 100644 --- a/drivers/dax/bus.h +++ b/drivers/dax/bus.h @@ -23,6 +23,7 @@ struct dev_dax_data { struct dev_pagemap *pgmap; resource_size_t size; int id; + bool memmap_on_memory; }; struct dev_dax *devm_create_dev_dax(struct dev_dax_data *data); diff --git a/drivers/dax/dax-private.h b/drivers/dax/dax-private.h index 27cf2daaaa79..446617b73aea 100644 --- a/drivers/dax/dax-private.h +++ b/drivers/dax/dax-private.h @@ -70,6 +70,7 @@ struct dev_dax { struct ida ida; struct device dev; struct dev_pagemap *pgmap; + bool memmap_on_memory; int nr_range; struct dev_dax_range { unsigned long pgoff; diff --git a/drivers/dax/bus.c b/drivers/dax/bus.c index 0ee96e6fc426..43be95a231c9 100644 --- a/drivers/dax/bus.c +++ b/drivers/dax/bus.c @@ -367,6 +367,7 @@ static ssize_t create_store(struct device *dev, struct device_attribute *attr, .dax_region = dax_region, .size = 0, .id = -1, + .memmap_on_memory = false, }; struct dev_dax *dev_dax = devm_create_dev_dax(&data); @@ -1269,6 +1270,40 @@ static ssize_t numa_node_show(struct device *dev, } static DEVICE_ATTR_RO(numa_node); +static ssize_t memmap_on_memory_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct dev_dax *dev_dax = to_dev_dax(dev); + + return sprintf(buf, "%d\n", dev_dax->memmap_on_memory); +} + +static ssize_t memmap_on_memory_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t len) +{ + struct dev_dax *dev_dax = to_dev_dax(dev); + struct dax_region *dax_region = dev_dax->region; + ssize_t rc; + bool val; + + rc = kstrtobool(buf, &val); + if (rc) + return rc; + + device_lock(dax_region->dev); + if (!dax_region->dev->driver) { + device_unlock(dax_region->dev); + return -ENXIO; + } + + dev_dax->memmap_on_memory = val; + + device_unlock(dax_region->dev); + return len; +} +static DEVICE_ATTR_RW(memmap_on_memory); + static umode_t dev_dax_visible(struct kobject *kobj, struct attribute *a, int n) { struct device *dev = container_of(kobj, struct device, kobj); @@ -1295,6 +1330,7 @@ static struct attribute *dev_dax_attributes[] = { &dev_attr_align.attr, &dev_attr_resource.attr, &dev_attr_numa_node.attr, + &dev_attr_memmap_on_memory.attr, NULL, }; @@ -1400,6 +1436,8 @@ struct dev_dax *devm_create_dev_dax(struct dev_dax_data *data) dev_dax->align = dax_region->align; ida_init(&dev_dax->ida); + dev_dax->memmap_on_memory = data->memmap_on_memory; + inode = dax_inode(dax_dev); dev->devt = inode->i_rdev; dev->bus = &dax_bus_type; diff --git a/drivers/dax/cxl.c b/drivers/dax/cxl.c index 8bc9d04034d6..c696837ab23c 100644 --- a/drivers/dax/cxl.c +++ b/drivers/dax/cxl.c @@ -26,6 +26,7 @@ static int cxl_dax_region_probe(struct device *dev) .dax_region = dax_region, .id = -1, .size = range_len(&cxlr_dax->hpa_range), + .memmap_on_memory = true, }; return PTR_ERR_OR_ZERO(devm_create_dev_dax(&data)); diff --git a/drivers/dax/hmem/hmem.c b/drivers/dax/hmem/hmem.c index 5d2ddef0f8f5..b9da69f92697 100644 --- a/drivers/dax/hmem/hmem.c +++ b/drivers/dax/hmem/hmem.c @@ -36,6 +36,7 @@ static int dax_hmem_probe(struct platform_device *pdev) .dax_region = dax_region, .id = -1, .size = region_idle ? 0 : range_len(&mri->range), + .memmap_on_memory = false, }; return PTR_ERR_OR_ZERO(devm_create_dev_dax(&data)); diff --git a/drivers/dax/kmem.c b/drivers/dax/kmem.c index c57acb73e3db..0aa6c45a4e5a 100644 --- a/drivers/dax/kmem.c +++ b/drivers/dax/kmem.c @@ -12,6 +12,7 @@ #include #include #include +#include #include "dax-private.h" #include "bus.h" @@ -56,6 +57,7 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax) unsigned long total_len = 0; struct dax_kmem_data *data; int i, rc, mapped = 0; + mhp_t mhp_flags; int numa_node; /* @@ -136,12 +138,16 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax) */ res->flags = IORESOURCE_SYSTEM_RAM; + mhp_flags = MHP_NID_IS_MGID; + if (dev_dax->memmap_on_memory) + mhp_flags |= MHP_MEMMAP_ON_MEMORY; + /* * Ensure that future kexec'd kernels will not treat * this as RAM automatically. */ rc = add_memory_driver_managed(data->mgid, range.start, - range_len(&range), kmem_name, MHP_NID_IS_MGID); + range_len(&range), kmem_name, mhp_flags); if (rc) { dev_warn(dev, "mapping%d: %#llx-%#llx memory add failed\n", diff --git a/drivers/dax/pmem.c b/drivers/dax/pmem.c index ae0cb113a5d3..f3c6c67b8412 100644 --- a/drivers/dax/pmem.c +++ b/drivers/dax/pmem.c @@ -63,6 +63,7 @@ static struct dev_dax *__dax_pmem_probe(struct device *dev) .id = id, .pgmap = &pgmap, .size = range_len(&range), + .memmap_on_memory = false, }; return devm_create_dev_dax(&data);