Received: by 2002:a05:7412:b795:b0:e2:908c:2ebd with SMTP id iv21csp233051rdb; Thu, 2 Nov 2023 01:57:08 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEKMjkEuuJuJETAoctQoHVX2z1iPCsOjYqjMZpj4gyOzC4XHiSKOMmx3TInorlhWr+EJ22n X-Received: by 2002:a05:6a21:6da2:b0:17e:2afd:407b with SMTP id wl34-20020a056a216da200b0017e2afd407bmr23684996pzb.9.1698915428633; Thu, 02 Nov 2023 01:57:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698915428; cv=none; d=google.com; s=arc-20160816; b=FxYsvDJq/epf5XM9tvPBzBD+yNZFTYtOaOnOrtrVUruJS3KcU5f5QDmdj1ocbfOllB 6Y5DsFKyq2TUCM1BUHFtyVIq3QTZjJDGSeqkN9LpJQXnywLOnx0I15LfbGc4jFErnwU6 c42IzvfHQqQfc2qQCvPzjwhxsuVReJhgXH20BHvwjd9pO66qebgabl4nwvrIQV1PqdeV ZX9C0xWRgd9mtkOEZS+GPbkKuhAT5wrUsZpMTp/EKEXanWbu/roHvy6uu4o3cR/84dHA F53f+Bl5D1/vYcsYmRb7RgobUjhct14FatlVGCtVXMxUxcTVqsKwanKzx9iSQviZvpGC vDZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :organization:autocrypt:from:references:cc:to:content-language :subject:user-agent:mime-version:date:message-id:dkim-signature; bh=hhuacmLeEt55NlKgJNjvhJFIwCxPsLqMGQxNmIQI81g=; fh=1y1w3Z2I2TvbAb3XHQdUPt2+yXy/gePQGCzeWbR7iKg=; b=JhNU3gZCiK9lgb4/5WKPQLsR/jUZT4p/w5P03i9ncYp9m/DMUEdQBznt/jvc6vA4S8 HqpuTn3Gt9QL4PkCkgJngJga/J5s13WOyPCFfp/Mpg0XSIbsKUrFVGd9/bBcBoMikYyB kNlmCxdGeES1gs4k9uWtqS+01hy22cZZ/6NnVpoZvhFXuwsn8T3+1069DkQiUNdpuhnk XSNTciaNYKtr7FtSmqwsJinlkt61V1psjpwbw7q3W8ZA2xHBA6mMN5iDjOxywYIHmwWz RmedHqeaSsRFimHtq0b+HTwmF8h/WF+a8aTnMGitLqy/t/0FUkPDXi2ubkov0tjvB/Hq KRlg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=fpKcaCHR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id a13-20020a65604d000000b005b8ee1c0c68si1655548pgp.605.2023.11.02.01.57.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 01:57:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=fpKcaCHR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id D93598077512; Thu, 2 Nov 2023 01:57:05 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235508AbjKBIzT (ORCPT + 99 others); Thu, 2 Nov 2023 04:55:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54354 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229818AbjKBIzF (ORCPT ); Thu, 2 Nov 2023 04:55:05 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 91F94189 for ; Thu, 2 Nov 2023 01:54:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698915256; h=from:from: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:autocrypt:autocrypt; bh=hhuacmLeEt55NlKgJNjvhJFIwCxPsLqMGQxNmIQI81g=; b=fpKcaCHRS23x9k968/hdwc/t+Mu4BjtaUaR7PCHtUxiHVrdaGXNK0PS5q6ntCbkT1mOKM2 Btr0yfLXC1IDHHvhBcroyLfgqSLGoTmDWKbBeVtzlyW3mW3b8Wc4a88u8hU41ou6jZx0Ku D4HL3iAviM2wrqLlzANgkqg32DzBDfM= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-416-BPXn1iLKNTCohFiDrKqHNw-1; Thu, 02 Nov 2023 04:54:15 -0400 X-MC-Unique: BPXn1iLKNTCohFiDrKqHNw-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-4091c9bdb8eso10597785e9.1 for ; Thu, 02 Nov 2023 01:54:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698915254; x=1699520054; h=content-transfer-encoding:in-reply-to:organization:autocrypt:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=hhuacmLeEt55NlKgJNjvhJFIwCxPsLqMGQxNmIQI81g=; b=aQU8v6y1Z8IhjPjB4EgCnebAqRRejnu5HNxkanyYwNvDMGhKWR4qgze2RJQS89/s8D 6XCQ8jIg9WnYGxpRY2lh4v8tmpzN6mFUmPYkPmXtgEgUjWV3a0oqPXsXxbScIx4fAlAO ZGfK6BDHVXzu4okFUdwIBWejtmwK2wLhVGw7E/VAV86nLOUhebwtTV62i7RS2ayolh7k ar5a0X9TVCmA2lBgGWOcW63vtrnceCNLfLbL/lCQzBDpoOgZOqPs/cn4jOaOEutZyTe1 5JEvIJs6tTB6u+AV2/EzLIpUtbpA9WPQ34eArJmJ7rDXico4JLZ4XRiGWHzguMDE4pw0 tMhQ== X-Gm-Message-State: AOJu0YxKVUUiZJ8zXgi1oTx1yZpCzr9st2Qe2bjZXL1GKSvw8BYra1xa dGJ6Tm0lVsO9RUtwh7Upuv5ke1UXc3hpvODmW7yvuYXX+RzaXfi3dl12386qEE74W059QW9/q8t Gbw4OTilg1mxpnrrDCtT9tV4W X-Received: by 2002:a05:600c:4f49:b0:405:3924:3cad with SMTP id m9-20020a05600c4f4900b0040539243cadmr9416388wmq.15.1698915254052; Thu, 02 Nov 2023 01:54:14 -0700 (PDT) X-Received: by 2002:a05:600c:4f49:b0:405:3924:3cad with SMTP id m9-20020a05600c4f4900b0040539243cadmr9416375wmq.15.1698915253591; Thu, 02 Nov 2023 01:54:13 -0700 (PDT) Received: from ?IPV6:2003:cb:c716:3000:f155:cef2:ff4d:c7? (p200300cbc7163000f155cef2ff4d00c7.dip0.t-ipconnect.de. [2003:cb:c716:3000:f155:cef2:ff4d:c7]) by smtp.gmail.com with ESMTPSA id l26-20020a05600c1d1a00b003fef5e76f2csm4257226wms.0.2023.11.02.01.54.12 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 02 Nov 2023 01:54:13 -0700 (PDT) Message-ID: Date: Thu, 2 Nov 2023 09:54:11 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v8 2/3] mm/memory_hotplug: split memmap_on_memory requests across memblocks Content-Language: en-US To: Vishal Verma , Andrew Morton , 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 References: <20231101-vv-kmem_memmap-v8-0-5e4a83331388@intel.com> <20231101-vv-kmem_memmap-v8-2-5e4a83331388@intel.com> From: David Hildenbrand Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RMq3aeErY5tujekBS32jfC/7AnH7I0v1v1TbbK3Gp XNeiN4QroO+5qaSr0ID2sz5jtBLRb15RMre27E1ImpaIv2Jw8NJgW0k/D1RyKCwaTsgRdwuK Kx/Y91XuSBdz0uOyU/S8kM1+ag0wvsGlpBVxRR/xw/E8M7TEwuCZQArqqTCmkG6HGcXFT0V9 PXFNNgV5jXMQRwU0O/ztJIQqsE5LsUomE//bLwzj9IVsaQpKDqW6TAPjcdBDPLHvriq7kGjt WhVhdl0qEYB8lkBEU7V2Yb+SYhmhpDrti9Fq1EsmhiHSkxJcGREoMK/63r9WLZYI3+4W2rAc UucZa4OT27U5ZISjNg3Ev0rxU5UH2/pT4wJCfxwocmqaRr6UYmrtZmND89X0KigoFD/XSeVv jwBRNjPAubK9/k5NoRrYqztM9W6sJqrH8+UWZ1Idd/DdmogJh0gNC0+N42Za9yBRURfIdKSb B3JfpUqcWwE7vUaYrHG1nw54pLUoPG6sAA7Mehl3nd4pZUALHwARAQABzSREYXZp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ZO0awwTIMgVEzmTyBphDg/Gx7dZU1Xf8HFuE+UZ5UDHDTnwgv7E th6RC9+WrhDNspZ9fJjKWRbveQgUFCpe1sa77LAw+XFrKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: <20231101-vv-kmem_memmap-v8-2-5e4a83331388@intel.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-1.3 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Thu, 02 Nov 2023 01:57:06 -0700 (PDT) On 01.11.23 23:51, Vishal Verma wrote: > 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. > > This does preclude being able to use PUD mappings in the direct map; a > proposal to how this could be optimized in the future is laid out > here[1]. > > [1]: https://lore.kernel.org/linux-mm/b6753402-2de9-25b2-36e9-eacd49752b19@redhat.com/ > > 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 > Reviewed-by: Dan Williams > Signed-off-by: Vishal Verma > --- > mm/memory_hotplug.c | 213 ++++++++++++++++++++++++++++++++++------------------ > 1 file changed, 138 insertions(+), 75 deletions(-) > > diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c > index 6be7de9efa55..d242e49d7f7b 100644 > --- a/mm/memory_hotplug.c > +++ b/mm/memory_hotplug.c > @@ -1380,6 +1380,84 @@ static bool mhp_supports_memmap_on_memory(unsigned long size) > return arch_supports_memmap_on_memory(vmemmap_size); > } > > +static void __ref remove_memory_blocks_and_altmaps(u64 start, u64 size) > +{ > + unsigned long memblock_size = memory_block_size_bytes(); > + u64 cur_start; > + > + /* > + * For memmap_on_memory, the altmaps were added on a per-memblock > + * basis; we have to process each individual memory block. > + */ > + for (cur_start = start; cur_start < start + size; > + cur_start += memblock_size) { > + struct vmem_altmap *altmap = NULL; > + struct memory_block *mem; > + > + mem = find_memory_block(pfn_to_section_nr(PFN_DOWN(cur_start))); > + WARN_ON_ONCE(!mem); > + if (!mem) > + continue; Nit: if (WARN_ON_ONCE(!mem)) continue; > + for (cur_start = start; cur_start < start + size; > + cur_start += memblock_size) { > + struct mhp_params params = { .pgprot = > + pgprot_mhp(PAGE_KERNEL) }; > + struct vmem_altmap mhp_altmap = { > + .base_pfn = PHYS_PFN(cur_start), > + .end_pfn = PHYS_PFN(cur_start + memblock_size - 1), > + }; > + > + mhp_altmap.free = memory_block_memmap_on_memory_pages(); > + params.altmap = kmemdup(&mhp_altmap, sizeof(struct vmem_altmap), > + GFP_KERNEL); > + if (!params.altmap) > + return -ENOMEM; As already spotted, we have to cleanup. > + > + /* call arch's memory hotadd */ > + ret = arch_add_memory(nid, cur_start, memblock_size, ¶ms); > + if (ret < 0) { > + kfree(params.altmap); > + goto out; > + } > + > + /* create memory block devices after memory was added */ > + ret = create_memory_block_devices(cur_start, memblock_size, > + params.altmap, group); > + if (ret) { > + arch_remove_memory(cur_start, memblock_size, NULL); > + kfree(params.altmap); > + goto out; > + } > + } > + > + return 0; > +out: > + if (ret && (cur_start != start)) Nit: I think you can drop the inner parentheses. > @@ -2146,11 +2208,31 @@ void try_offline_node(int nid) > } > EXPORT_SYMBOL(try_offline_node); > > +static int memory_blocks_have_altmaps(u64 start, u64 size) > +{ > + u64 num_memblocks = size / memory_block_size_bytes(); > + u64 num_altmaps = 0; > + > + if (!mhp_memmap_on_memory()) > + return 0; > + > + walk_memory_blocks(start, size, &num_altmaps, > + count_memory_range_altmaps_cb); > + > + if (num_altmaps == 0) > + return 0; > + > + if (num_memblocks != num_altmaps) { > + WARN_ONCE(1, "Not all memblocks in range have altmaps"); Nit: if (WARN_ON_ONCE(num_memblocks != num_altmaps)) return -EINVAL; Should be sufficient. [...] > /* 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); > + rc = memory_blocks_have_altmaps(start, size); > + if (rc < 0) { > + goto err; Nit: Maybe better to just if (rc < 0) { mem_hotplug_done(); return rc } else ... And avoid the error label below. Makes the code easier to read. > + } else if (rc == 0) { Nit: else if (!rc) With the cleanup fixed, Acked-by: David Hildenbrand -- Cheers, David / dhildenb