Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp36965094rwd; Tue, 11 Jul 2023 07:52:17 -0700 (PDT) X-Google-Smtp-Source: APBJJlFhpUHDoeFz2vvNz5s8FRZnLOMhK7zbP348mv1ODtA6i1oYO9L265DqnqcK6vGVgz8xFfuL X-Received: by 2002:a05:6a20:3d0c:b0:112:cf5:d5cc with SMTP id y12-20020a056a203d0c00b001120cf5d5ccmr21401915pzi.1.1689087137248; Tue, 11 Jul 2023 07:52:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689087137; cv=none; d=google.com; s=arc-20160816; b=A+EDrFDsWfBeXwFhJ5MgbD68IG4n1DxBq/PFcQyLQsNWdZYtIpnU+d7MvIYmHn6jKs a5ZJ4s2uG8BvX0LJugIjaG322ZOj5lxvEmiBhJYq589sgwy7CCB/k+WcSz6NvZjvlpAc ucqxfVk4WsFQVqI+1QJFlg9mBX68pkqHKeoZFq1S2HCGEUGu/vzDKasqscw+x3ezJo/w F9rvXA157z/IrZpp9ENAjP/Q0vQzjHWa8tJwvChI71oPMkP5X/yWlwX1Q3CuGcx45dhd QlVNhIjWBpSGmNOFwbFwHfIr55SxGTo+iObCKYluQdCKAsN8EETZ7CrILtqJo8cdRdJO b5Tw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:dkim-signature; bh=k5whsS7kOg0w8NvsFZpkoENsflayjHUaVNSyPCSYX88=; fh=9YfwCNAaIAPdbInVM5+r5RPFnFEPb/bDu9eOjN7tzyQ=; b=hiZ6SqWfxaam3GN3odkDWHuqAtK+wNkSf5OGl6e/XwAFbFRzZX/THcMEyv6SeqB99w sucHp25zsPhLPrzfw/K4F9XZLT/shmDuMuIU0nfoTbEzet8W3KJbDokkkqIi3gehlhS0 v38YvMb0ZNdYsBzw3A0tWgjZYqlI2vmiRgCuTGaALz77H6rBd0sbsj23iTKQX4j4Mfri JigEFkmHCpBSFRlf57fOd0xa9lkZ8r3R5t903o7JUR31q7btm9iwX6tkNUaqtfX25eAs 2ruNcHW9a9Y5hI0KKmYcVJiAsbX5c2TnCl6/xJos6ADyeT130oq7ObaMJCVH5X5eH49b pDQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=tDpMpBSp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z9-20020aa78889000000b00677429ef6a2si1585767pfe.186.2023.07.11.07.52.04; Tue, 11 Jul 2023 07:52:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=tDpMpBSp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231394AbjGKObM (ORCPT + 99 others); Tue, 11 Jul 2023 10:31:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40994 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229970AbjGKObL (ORCPT ); Tue, 11 Jul 2023 10:31:11 -0400 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 31C46BC; Tue, 11 Jul 2023 07:31:10 -0700 (PDT) Received: from pps.filterd (m0356517.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 36BEPc1h030623; Tue, 11 Jul 2023 14:30:44 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : in-reply-to : references : date : message-id : mime-version : content-type; s=pp1; bh=k5whsS7kOg0w8NvsFZpkoENsflayjHUaVNSyPCSYX88=; b=tDpMpBSpW2TClzakharP0AXhWsKuI0unAzQSkGdXJOnGF1EuBeGPLUCknrAzsyuD1QKf vC1QIwfRSNRtIi4R+PTXglDhighTRgR6XEkXpSCiEp+BJCTVbXRhg0TCqxA5mDdLWxiQ XANeESjxIxDNSQzkfoXeC7CkjlUVy/RB8AJeKd2ySKq/Ro8cwiMWdOFN0bPC64hnOXd1 J5Cdm4K6jw0VwUIZiV5xubLcWVmws/r6QvM3vGEc1g77NHuE/m0w4GIxqUngz2blEkZo k//uQEo8vPEHYh6xJBZJWfFuJBlOWSY6vyD3UWNi2vdI84nXwbORYeZzUvhhxRBtxrY5 eg== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3rs8u2r4wc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 11 Jul 2023 14:30:44 +0000 Received: from m0356517.ppops.net (m0356517.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 36BESJOC006169; Tue, 11 Jul 2023 14:30:43 GMT Received: from ppma03dal.us.ibm.com (b.bd.3ea9.ip4.static.sl-reverse.com [169.62.189.11]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3rs8u2r4vc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 11 Jul 2023 14:30:43 +0000 Received: from pps.filterd (ppma03dal.us.ibm.com [127.0.0.1]) by ppma03dal.us.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 36B8lGFD026569; Tue, 11 Jul 2023 14:30:42 GMT Received: from smtprelay05.dal12v.mail.ibm.com ([9.208.130.101]) by ppma03dal.us.ibm.com (PPS) with ESMTPS id 3rpye5mxym-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 11 Jul 2023 14:30:42 +0000 Received: from smtpav03.dal12v.mail.ibm.com (smtpav03.dal12v.mail.ibm.com [10.241.53.102]) by smtprelay05.dal12v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 36BEUfW24522712 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 11 Jul 2023 14:30:41 GMT Received: from smtpav03.dal12v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id E4E3758063; Tue, 11 Jul 2023 14:30:40 +0000 (GMT) Received: from smtpav03.dal12v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 02AA45803F; Tue, 11 Jul 2023 14:30:35 +0000 (GMT) Received: from skywalker.linux.ibm.com (unknown [9.43.86.43]) by smtpav03.dal12v.mail.ibm.com (Postfix) with ESMTP; Tue, 11 Jul 2023 14:30:34 +0000 (GMT) X-Mailer: emacs 29.0.91 (via feedmail 11-beta-1 I) From: "Aneesh Kumar K.V" To: David Hildenbrand , Vishal Verma , "Rafael J. Wysocki" , Len Brown , Andrew Morton , Oscar Salvador , Dan Williams , Dave Jiang Cc: linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, nvdimm@lists.linux.dev, linux-cxl@vger.kernel.org, Huang Ying , Dave Hansen Subject: Re: [PATCH 3/3] dax/kmem: Always enroll hotplugged memory for memmap_on_memory In-Reply-To: References: <20230613-vv-kmem_memmap-v1-0-f6de9c6af2c6@intel.com> <20230613-vv-kmem_memmap-v1-3-f6de9c6af2c6@intel.com> Date: Tue, 11 Jul 2023 20:00:32 +0530 Message-ID: <87edleplkn.fsf@linux.ibm.com> MIME-Version: 1.0 Content-Type: text/plain X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: sBc0SZ-XJ6uR_-MUXidz4FusRLIp7phB X-Proofpoint-GUID: NONq6tF3YjFkZtCggSFopj4pkW66iNU1 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.957,Hydra:6.0.591,FMLib:17.11.176.26 definitions=2023-07-11_08,2023-07-11_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 adultscore=0 malwarescore=0 clxscore=1011 priorityscore=1501 phishscore=0 impostorscore=0 suspectscore=0 lowpriorityscore=0 spamscore=0 mlxscore=0 bulkscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2305260000 definitions=main-2307110127 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org David Hildenbrand writes: > On 16.06.23 00:00, Vishal Verma wrote: >> With DAX memory regions originating from CXL memory expanders or >> NVDIMMs, the kmem driver may be hot-adding huge amounts of system memory >> on a system without enough 'regular' main memory to support the memmap >> for it. To avoid this, ensure that all kmem managed hotplugged memory is >> added with the MHP_MEMMAP_ON_MEMORY flag to place the memmap on the >> new memory region being hot added. >> >> To do this, call add_memory() in chunks of memory_block_size_bytes() as >> that is a requirement for memmap_on_memory. Additionally, Use the >> mhp_flag to force the memmap_on_memory checks regardless of the >> respective module parameter setting. >> >> Cc: "Rafael J. Wysocki" >> Cc: Len Brown >> Cc: Andrew Morton >> Cc: David Hildenbrand >> Cc: Oscar Salvador >> Cc: Dan Williams >> Cc: Dave Jiang >> Cc: Dave Hansen >> Cc: Huang Ying >> Signed-off-by: Vishal Verma >> --- >> drivers/dax/kmem.c | 49 ++++++++++++++++++++++++++++++++++++------------- >> 1 file changed, 36 insertions(+), 13 deletions(-) >> >> diff --git a/drivers/dax/kmem.c b/drivers/dax/kmem.c >> index 7b36db6f1cbd..0751346193ef 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" >> >> @@ -105,6 +106,7 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax) >> data->mgid = rc; >> >> for (i = 0; i < dev_dax->nr_range; i++) { >> + u64 cur_start, cur_len, remaining; >> struct resource *res; >> struct range range; >> >> @@ -137,21 +139,42 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax) >> res->flags = IORESOURCE_SYSTEM_RAM; >> >> /* >> - * Ensure that future kexec'd kernels will not treat >> - * this as RAM automatically. >> + * Add memory in chunks of memory_block_size_bytes() so that >> + * it is considered for MHP_MEMMAP_ON_MEMORY >> + * @range has already been aligned to memory_block_size_bytes(), >> + * so the following loop will always break it down cleanly. >> */ >> - rc = add_memory_driver_managed(data->mgid, range.start, >> - range_len(&range), kmem_name, MHP_NID_IS_MGID); >> + cur_start = range.start; >> + cur_len = memory_block_size_bytes(); >> + remaining = range_len(&range); >> + while (remaining) { >> + mhp_t mhp_flags = MHP_NID_IS_MGID; >> >> - if (rc) { >> - dev_warn(dev, "mapping%d: %#llx-%#llx memory add failed\n", >> - i, range.start, range.end); >> - remove_resource(res); >> - kfree(res); >> - data->res[i] = NULL; >> - if (mapped) >> - continue; >> - goto err_request_mem; >> + if (mhp_supports_memmap_on_memory(cur_len, >> + MHP_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, cur_start, >> + cur_len, kmem_name, >> + mhp_flags); >> + >> + if (rc) { >> + dev_warn(dev, >> + "mapping%d: %#llx-%#llx memory add failed\n", >> + i, cur_start, cur_start + cur_len - 1); >> + remove_resource(res); >> + kfree(res); >> + data->res[i] = NULL; >> + if (mapped) >> + continue; >> + goto err_request_mem; >> + } >> + >> + cur_start += cur_len; >> + remaining -= cur_len; >> } >> mapped++; >> } >> > > Maybe the better alternative is teach > add_memory_resource()/try_remove_memory() to do that internally. > > In the add_memory_resource() case, it might be a loop around that > memmap_on_memory + arch_add_memory code path (well, and the error path > also needs adjustment): > > /* > * Self hosted memmap array > */ > if (mhp_flags & MHP_MEMMAP_ON_MEMORY) { > if (!mhp_supports_memmap_on_memory(size)) { > ret = -EINVAL; > goto error; > } > mhp_altmap.free = PHYS_PFN(size); > mhp_altmap.base_pfn = PHYS_PFN(start); > params.altmap = &mhp_altmap; > } > > /* call arch's memory hotadd */ > ret = arch_add_memory(nid, start, size, ¶ms); > if (ret < 0) > goto error; > > > Note that we want to handle that on a per memory-block basis, because we > don't want the vmemmap of memory block #2 to end up on memory block #1. > It all gets messy with memory onlining/offlining etc otherwise ... > I tried to implement this inside add_memory_driver_managed() and also within dax/kmem. IMHO doing the error handling inside dax/kmem is better. Here is how it looks: 1. If any blocks got added before (mapped > 0) we loop through all successful request_mem_regions 2. For each succesful request_mem_regions if any blocks got added, we keep the resource. If none got added, we will kfree the resource for (i = 0; i < dev_dax->nr_range; i++) { u64 cur_start, cur_len, remaining; struct resource *res; struct range range; bool block_added; rc = dax_kmem_range(dev_dax, i, &range); if (rc) continue; /* Region is permanently reserved if hotremove fails. */ res = request_mem_region(range.start, range_len(&range), data->res_name); if (!res) { dev_warn(dev, "mapping%d: %#llx-%#llx could not reserve region\n", i, range.start, range.end); /* * Once some memory has been onlined we can't * assume that it can be un-onlined safely. */ if (mapped) continue; rc = -EBUSY; goto err_request_mem; } data->res[i] = res; /* * Set flags appropriate for System RAM. Leave ..._BUSY clear * so that add_memory() can add a child resource. Do not * inherit flags from the parent since it may set new flags * unknown to us that will break add_memory() below. */ res->flags = IORESOURCE_SYSTEM_RAM; /* * Add memory in chunks of memory_block_size_bytes() so that * it is considered for MHP_MEMMAP_ON_MEMORY * @range has already been aligned to memory_block_size_bytes(), * so the following loop will always break it down cleanly. */ cur_start = range.start; cur_len = memory_block_size_bytes(); remaining = range_len(&range); block_added = false; while (remaining) { /* * If alignment rules are not satisified we will * fallback normal memmap allocation. */ mhp_t mhp_flags = MHP_NID_IS_MGID | MHP_MEMMAP_ON_MEMORY; /* * Ensure that future kexec'd kernels will not treat * this as RAM automatically. */ rc = add_memory_driver_managed(data->mgid, cur_start, cur_len, kmem_name, mhp_flags); if (rc) dev_warn(dev, "mapping%d: %#llx-%#llx memory add failed\n", i, cur_start, cur_start + cur_len - 1); else block_added = true; cur_start += cur_len; remaining -= cur_len; } if (!block_added) { /* * None of the blocks got added, remove the resource. */ remove_resource(res); kfree(res); data->res[i] = NULL; } else mapped++; } if (mapped) { dev_set_drvdata(dev, data); return 0; } err_request_mem: /* * If none of the resources got mapped. * unregister the group. */ memory_group_unregister(data->mgid); err_reg_mgid: kfree(data->res_name);