From patchwork Thu Sep 28 20:30:10 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: 13403513 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 AF29BE743C2 for ; Thu, 28 Sep 2023 20:30:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 40C7E8D00DD; Thu, 28 Sep 2023 16:30:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3BC438D0053; Thu, 28 Sep 2023 16:30:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2362C8D00DD; Thu, 28 Sep 2023 16:30:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 13F758D0053 for ; Thu, 28 Sep 2023 16:30:58 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id AE05480A66 for ; Thu, 28 Sep 2023 20:30:57 +0000 (UTC) X-FDA: 81287150154.26.E2B39FF Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.126]) by imf30.hostedemail.com (Postfix) with ESMTP id AB2E28000B for ; Thu, 28 Sep 2023 20:30:53 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="FN/4Gh4W"; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf30.hostedemail.com: domain of vishal.l.verma@intel.com designates 134.134.136.126 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=1695933054; 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=E1Wrlefi6zuOH8/nzfeQc3iQPtW/E6lSbiZ9RgFASnw=; b=XTquPHCRxAGycGBRiqaAIx4tqVM1mkHOAkdxf7L9jQKHBLHfNEaosZuUl3RBH8iFgtS3bn T0Xcdl3lQeULRUQem0bL7GJTQq1q/0iNXoXrnKMe4MH0vV7OzlSaUZUS/5Ck5gja/vcmta dyTmFIOV0zO/ZsONURUwRMbZf5KlRqc= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="FN/4Gh4W"; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf30.hostedemail.com: domain of vishal.l.verma@intel.com designates 134.134.136.126 as permitted sender) smtp.mailfrom=vishal.l.verma@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695933054; a=rsa-sha256; cv=none; b=A0qp3W8ijKjZHNFTmCFgNbBnt8F5wgwMICXPXduiVKy439bu6ORJTJuDGEXaEj02CA8FNt kGIKarWDQLVRStxv9CrQ4ednQ4zzGPEkVdl3XtJ2op7fgChS1ZzObrXobF2rdqPegI1r7c 0f1qDYJvdW9UQY2nBhKV+UHxCgu8iKQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1695933053; x=1727469053; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=H/aRLMgJ+0o4ZU3Uqc3hhl5jnLuNWUSJWpDXLqwmub4=; b=FN/4Gh4W+71nB0ZKi9chugRdcciRyjMVWWjyNg7snhqC/XO0h5eW1YCk 5vh9QG5YMBz2DYu6OfHxagFLjyPMoUsse19xFkmcGtJ4RLyUcKxwkhWK0 EhhEsCxD5/NdVtW9w1RxyVxzxN6epFGf8omABns4/2/zpQpV37fnBy1sy unvOgs6BY91pcfDSMjYtfddJcB8l2uK3zVs4GZMV5DaoiPRNqz+sU2ad3 MFrrLU9Lp9gzwIJ1FCAyPMJ/9HyPuZFRCN3c+TNr1gyTSpTbc7llRcnFY 5CSpweQBRCLnLqoPm9+AmC1VsmnEyBfoYARajNWFVkiHSsj9+r9HujPjf Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10847"; a="367229678" X-IronPort-AV: E=Sophos;i="6.03,185,1694761200"; d="scan'208";a="367229678" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Sep 2023 13:30:31 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10847"; a="815374356" X-IronPort-AV: E=Sophos;i="6.03,185,1694761200"; d="scan'208";a="815374356" Received: from bdsebast-mobl1.amr.corp.intel.com (HELO [192.168.1.200]) ([10.212.125.211]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Sep 2023 13:30:30 -0700 From: Vishal Verma Date: Thu, 28 Sep 2023 14:30:10 -0600 Subject: [PATCH v4 1/2] mm/memory_hotplug: split memmap_on_memory requests across memblocks MIME-Version: 1.0 Message-Id: <20230928-vv-kmem_memmap-v4-1-6ff73fec519a@intel.com> References: <20230928-vv-kmem_memmap-v4-0-6ff73fec519a@intel.com> In-Reply-To: <20230928-vv-kmem_memmap-v4-0-6ff73fec519a@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=8303; i=vishal.l.verma@intel.com; h=from:subject:message-id; bh=H/aRLMgJ+0o4ZU3Uqc3hhl5jnLuNWUSJWpDXLqwmub4=; b=owGbwMvMwCXGf25diOft7jLG02pJDKmij9Jup70Lb+j7w8dyLPWqvxbHrJnR7GZ7nfZNfbRSW vN5w8FdHaUsDGJcDLJiiix/93xkPCa3PZ8nMMERZg4rE8gQBi5OAZjI7XUM/7SVem+UZJvd3HhF T+XoubrovyKsLgwWff1mdzin+G67+Izhn1V9725375+Rcx5en7lKKa6Q6W15cuPRRVO5lTtX+5w /wgcA X-Developer-Key: i=vishal.l.verma@intel.com; a=openpgp; fpr=F8682BE134C67A12332A2ED07AFA61BEA3B84DFF X-Rspamd-Queue-Id: AB2E28000B X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 3kiir58ihncjkj5zsyjza6sjjdwg1wgj X-HE-Tag: 1695933053-930918 X-HE-Meta: U2FsdGVkX1/q1mCeWUtlA01hz6+NfWpcv+DR+rTK5utIzCSM4mFckeMOlr9oggl7Wn3ZI5t6Z1QsZmtLfkQo2ERanh0x4tB/C0a9o8AJ7DSVMZB/W9n+zzCK438i+LNNhiBjkvgcV/ZKPQ69/QmqzPpfG5iouaQQ0AmEKqEdK1ktkuKM8MyQlLg88qh86OOYUD5bNGlZzqdkrxYvXdREfiwvqmDj7Y27lAeaubbwuylPBCEe9pAjNRjnm9mmYSpPqhGIaY94WnvtyTC+418vQ9TchVQgGoJUafHOUPlce7Ar9VayWsqnWQ0t/YPPXhoBJAlnjc5+CTtRlwZ5dnCSUQVF0g8fACmSTbGESUz4Z4UZIiuEBuOlSzgM7XYYd2YA5yNk4gmQrR14zTREqnWxwzT8ICwNWHX+vbZL6v+MMp6+xaotHukDsZj1ZrIs356HKsoqMCcm6R5BqQEVjJr1AzRKc7OxtC5Xgec+UKt2tIBzNyK1Tht6BzCjpm3+K2lzRei0H6Ja6t3pc0ef7e/0jVKqapusCaOhHLeegdN763qXs2+OUqDwdHPbwK3AdE0D/6yHyH0aZrKBBEJmr6PVBIUGrnhW6wcNowbz6WOAUSDcOdrf6jfHTNDyYA17f+L8O1zkPsnVeMWBlCdNcM0diQ0g1xOcyN/qMMsr0LXMlhrUSfaeNBe1vpLgC8macsyOZT0Y7d/7MOYNtsNqqwbTlT/xFApU0o3Xm630UAnknOAsWUe0Xz8TadvBJ/Z+NDdZNICCTIWChMlK0XoEPqtfZeT/uwaowvC83i13YscWdniarFVJpPFsdAo4PLKkD+xKuoNEDeaWlqMVK+cbz2i6IBktVex36PIUllYplC/BMBC3XMNbggMtalRHzVk0HK0SgSj1DYxkk2wPCOMB0ctOfTcPCMw22t4vklYkz9f0HmzxHzztvpmuztm2wEXwJU2VMlNkyjXdGPk29YuOrIo ErO0QobD y53+t6QdhAoPJYN7vUZiElE+ZEfeaCoYDppnin5c/AAB6V1ccW/unxuxKy7GfAqGik6OsBrf7xQN4esugl4qMkjYZo5mPDZxbO09640O1IBgYmO9OqUq7EtYHrDiCT4NOgHCqvwEz/Xr7LCuDPaJXYb4wbmfXvVCaaPkZ4qWqqfmsL6ZMq6Y8FS4wE8EOpS+xC1UTo6IGOCJItjFEtaEFPs3NTa2a2iN8GGD2+eF4zcMehHUHRGkRFfIW63wvME2D4UPiK9InJi2NkK5bWofQwo8syi1bRCcXCx1viYjoKMNiRUnUrowjoJW0oPUtC7+q86K+zDVPAtugy4dmw+kwQ8TWv8hLTV2VUlYAJ8Q3dNJj4zk= 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 --- mm/memory_hotplug.c | 165 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 100 insertions(+), 65 deletions(-) diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index f8d3e7427e32..43dbd71a4910 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,47 +2171,20 @@ void try_offline_node(int nid) } EXPORT_SYMBOL(try_offline_node); -static int __ref try_remove_memory(u64 start, u64 size) +static void __ref __try_remove_memory(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; - BUG_ON(check_hotplug_memory_range(start, size)); - - /* - * All memory blocks must be offlined before removing memory. Check - * whether all memory blocks in question are offline and return error - * if this is not the case. - * - * While at it, determine the nid. Note that if we'd have mixed nodes, - * we'd only try to offline the last determined one -- which is good - * enough for the cases we care about. - */ - rc = walk_memory_blocks(start, size, &nid, check_memblock_offlined_cb); - if (rc) - return rc; - - /* - * We only support removing memory added with MHP_MEMMAP_ON_MEMORY in - * the same granularity it was added - a single memory block. - */ - 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; - } + 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; } /* remove memmap entry */ @@ -2219,6 +2217,43 @@ static int __ref try_remove_memory(u64 start, u64 size) try_offline_node(nid); mem_hotplug_done(); +} + +static int __ref try_remove_memory(u64 start, u64 size) +{ + int rc, nid = NUMA_NO_NODE; + + BUG_ON(check_hotplug_memory_range(start, size)); + + /* + * All memory blocks must be offlined before removing memory. Check + * whether all memory blocks in question are offline and return error + * if this is not the case. + * + * While at it, determine the nid. Note that if we'd have mixed nodes, + * we'd only try to offline the last determined one -- which is good + * enough for the cases we care about. + */ + rc = walk_memory_blocks(start, size, &nid, check_memblock_offlined_cb); + if (rc) + return rc; + + /* + * 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()) { + unsigned long memblock_size = memory_block_size_bytes(); + u64 cur_start; + + for (cur_start = start; cur_start < start + size; + cur_start += memblock_size) + __try_remove_memory(nid, cur_start, memblock_size); + } else { + __try_remove_memory(nid, start, size); + } + return 0; } From patchwork Thu Sep 28 20:30:11 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: 13403514 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 39535E743C0 for ; Thu, 28 Sep 2023 20:31:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 305B68D00DE; Thu, 28 Sep 2023 16:30:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2AADB8D0053; Thu, 28 Sep 2023 16:30:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 14CBD8D00DE; Thu, 28 Sep 2023 16:30:59 -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 F3BF38D0053 for ; Thu, 28 Sep 2023 16:30:58 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id BA4F61A0A05 for ; Thu, 28 Sep 2023 20:30:58 +0000 (UTC) X-FDA: 81287150196.12.8FD4166 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.126]) by imf01.hostedemail.com (Postfix) with ESMTP id 364AD40024 for ; Thu, 28 Sep 2023 20:30:55 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=n8oBIXOX; spf=pass (imf01.hostedemail.com: domain of vishal.l.verma@intel.com designates 134.134.136.126 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=1695933055; 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=vFgf1IEQyRFkWFGKi1TTSuKtokiG4/ccGifKmDse5Oc=; b=E/wbI52+OBYwAc0F5B2I3MHadr1RdogyWdu5JufnSs9kabpvk397gUIybp+xS38vix1/g6 3joCfyqjj7GjUPtBgtX+1+aar+w1bXFMZBjcR0euiWJMr7Yid8ofENz59RLhqDf6JT9UbD rKm9ahTWD4ST3ecmPHuOwnikdURIJgY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695933055; a=rsa-sha256; cv=none; b=eTWd/PlP6BYI/e2VKopV65NBybHp79driGfFcf9TM8aWxubagogDILUX9ljW+Wz3Xea3De L4Ttc4wegF1czBOsWAtuiyXUYHJGBZ3rNfyTnHvfXwDkq4XXY8guzTOjFnQ5Zhvq1EeK+C 8cyLKMvLm3bzzc6eTPJJQzI3hL2AqJ8= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=n8oBIXOX; spf=pass (imf01.hostedemail.com: domain of vishal.l.verma@intel.com designates 134.134.136.126 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=1695933055; x=1727469055; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=vr4yVCTT0Ug5tj83kbU0iH+2ATewvlAUFs0OqOZ9pYo=; b=n8oBIXOXzo3D+jEZbrFdh7yQA76Kqyz+/Chgts6A+Xg/2qC1wt8dI6uk wvVFm6V+9xUiezv4CEitJ+wQHlJccGatiRIZgdC8w/yFFqDaZlymHZUpj y+8GowOI+9io8Yh2Jvz4GmNCtmsZ6YzJ9VP1gcXMu3VItXN4p2weUYPKg bwtUxWkj/+UmsXdnwLoV0m1GDDf05j7VYDWQSnP5mVn1KAmUAXOECZioV eIQf1/qb7kNia3EqlSYDuf/zu1NqBksmnfJLj+0JofIP1hKm87gh4kfNu exaBsJ4l/UY8I3XFJ4AlTdh6uss2wu4s5KYY74SUvg0sUpX+uHqWIOE2Z A==; X-IronPort-AV: E=McAfee;i="6600,9927,10847"; a="367229689" X-IronPort-AV: E=Sophos;i="6.03,185,1694761200"; d="scan'208";a="367229689" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Sep 2023 13:30:32 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10847"; a="815374363" X-IronPort-AV: E=Sophos;i="6.03,185,1694761200"; d="scan'208";a="815374363" Received: from bdsebast-mobl1.amr.corp.intel.com (HELO [192.168.1.200]) ([10.212.125.211]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Sep 2023 13:30:31 -0700 From: Vishal Verma Date: Thu, 28 Sep 2023 14:30:11 -0600 Subject: [PATCH v4 2/2] dax/kmem: allow kmem to add memory with memmap_on_memory MIME-Version: 1.0 Message-Id: <20230928-vv-kmem_memmap-v4-2-6ff73fec519a@intel.com> References: <20230928-vv-kmem_memmap-v4-0-6ff73fec519a@intel.com> In-Reply-To: <20230928-vv-kmem_memmap-v4-0-6ff73fec519a@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=6393; i=vishal.l.verma@intel.com; h=from:subject:message-id; bh=vr4yVCTT0Ug5tj83kbU0iH+2ATewvlAUFs0OqOZ9pYo=; b=owGbwMvMwCXGf25diOft7jLG02pJDKmij9I6HhqsZr2d+tb0l8SvmKX50aodnsfPvec6urJv+ 7aenSWTO0pYGMS4GGTFFFn+7vnIeExuez5PYIIjzBxWJpAhDFycAjCRKZsYGSadL24tmKJzmHfK mUZeY2eVgORi5TOr7ALrJVKrFh126GT4XjHFKuuhU9kVLYFDb+RvWu6x4l4mxfegO8Cg7ra/MhM jAA== X-Developer-Key: i=vishal.l.verma@intel.com; a=openpgp; fpr=F8682BE134C67A12332A2ED07AFA61BEA3B84DFF X-Stat-Signature: 5jfj3s6ouyokt3q853c4rz7xhcsos35w X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 364AD40024 X-Rspam-User: X-HE-Tag: 1695933055-18164 X-HE-Meta: U2FsdGVkX18VWlGdWUC8P4VIsAkbYROMsAnW2u0j7i9ACW8DjA6uojG11SzioFp64AOjIgI8gBr5yzfSmaFpt7Izb10ObV5yyt3wbQmMWMKkVpKfFBCwqyg+UrEpm9iGyG4fIr/z2QUUN9LEaDMV8MkUlWXjMhUSL8sNqjruaICjn8flKnqdAipyHiqem+Ppr96ckZxM24n/SM1Pw/zL1T5PA/ogNXruoCA56gzp1VzIHydhMQKFqUdOMUKWApXUyTZ2fl+zaaaspEWOnU9orL0bfhadRpsGoAJyEbvsDQ1Kaw5TCs8fI8M/2eNy2ShFNTp++2ODAsZuxSsgZgueoFZ2Wa4Kd4LXiD0M9HUbWULHhQ2jozU/MbyZ9P4d1peKrmXs0kkmEob+aii7w6nvUzP+aB7E+Lph3qgFnVewfZEMognQPngujV2AxHeU1MUwNPWu3FNAcWA7LjolzwAE8fnjgfvyWzAMVhxFv2x3q5i9P4fovdfjvfDGghqDX6AlxgLYkellsx22kW0NrTa9TSR4zGEo23yKTG3njTWFTYUTRDpeCCvlf1PoRf0E60/yCFzahPZEwGaP1A8ubmLwu/4rTeu4aslRNa66xMfW2pU+a5M8uG6oeGRMqggqezESRd4ql1NtbyOqJC8m1xxLycLVyZd5IDElAlvDUmo1dyoVwbkQuI2x7+OzlqSuf82tsPBzOzxUQubzo0lKAHn1fj2tR+jzoxiwR+wiKz0ZAol40YNmHWdvP83alW3CsXzy9+RL+qZhdsSAFYoYpfnMSGsNyC3w/40Y9kAuhSVHYgINjy5NltZ4OoAozrhO1IslHZaE2UZxHv2j0Z7eGyRizReQZDl0w7C/UhSCT/OHXyzLZfFRxFy3lwSnCzpvApKsy+NfA7lllz/Qv2va0pcZ742NCYRkA1HkxtKgLV+fTjlT/O0lAieR401InQ1EGBQlMJpOm391KnImIWiQKPd uNPzHgCR DQ8+Fvy4AEbf7kKz5WVpGdxM27d2S6MgGTz9OU82YJzIaFdeW9RsW3NAf0lmNiZvB2BnhBSEY/Koi2Edk4BsdJRQFdnjp5nl7YYb6jR4x7hvztSukELIHOLGx0IndMepFdq/NI9lhuiwBsqou+ZTqX+rn474cmANS+BjbMf9x13a1jNwWEDh0Kfzf6hJdhF39jaAtd2CBp/gXUXnEoS0okr++0W7XuX/O+xc8/erWlUgpNNa5G4FkrC0tPRtbNeU2EykSvLiaqkF/UxVk+Xwel3xireZ+FcawhSkemF9sHzBt8zXNrnCpvoB5cSadXq8/DGr5dTl7BTvF9Z/xq4q2V15oYcxRAY/Z6gohjq1zP2MCmh8kaXjavt6zIUrC4pUV1ipt 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 Signed-off-by: Vishal Verma Reviewed-by: David Hildenbrand --- 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..81351eb69884 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 rc == 0 ? len : rc; +} +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);