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 F0B65C001B0 for ; Wed, 26 Jul 2023 16:43:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 54F756B0072; Wed, 26 Jul 2023 12:43:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4FEF48D0001; Wed, 26 Jul 2023 12:43:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 352C46B007B; Wed, 26 Jul 2023 12:43:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 23DD46B0072 for ; Wed, 26 Jul 2023 12:43:53 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id EBF90C0160 for ; Wed, 26 Jul 2023 16:43:52 +0000 (UTC) X-FDA: 81054334704.01.787FE40 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf15.hostedemail.com (Postfix) with ESMTP id 50886A001F for ; Wed, 26 Jul 2023 16:43:49 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=QMrSZcnM; spf=pass (imf15.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690389829; 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=eYyLIb3cAz+pfX2bhGfoxws54fDawRj2oU07jpEMHJk=; b=D6t+51CUawU7baDaoTl5wxEyMoAwdTmFbs1qqAjp7jhKJtWQJXiVJJoAPoXxxP29m2gVLK N9+UiPVj9+ClR95sZGEvKgaRCMxaeykpDlDssEQ4HIickG4CNz/x+cbQjGkDB95hQFPCXU 98rdTlOa5cFa/4PKfVbYwIRg3ejsZcw= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=QMrSZcnM; spf=pass (imf15.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690389829; a=rsa-sha256; cv=none; b=Gwwc2X2XJKHX5mgUN+RQp0vYmlywiYjSqzGRp+62T8g42Gir7n1TtC1qH3N9d+OPzJt2K2 S1Z7Yky6MH1lbbhJA1tFaIAm9GdBWv8NseFK29vHz1jPZPLpfo2gVwtvIqDlDdJ3TVA6MD N6Nx7QdcA8KEKIIaorSk9kE0nXPTz9g= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1690389828; 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; bh=eYyLIb3cAz+pfX2bhGfoxws54fDawRj2oU07jpEMHJk=; b=QMrSZcnM6JKbLB1QRU8r9IFpZHleB5PTrt8+00HWJnJ6uQAWX4BKMH6sVZwQdAfilg+kYw Zwc31boIEE/xNkmGF9IsaI8AqqYoAveDT3Xv1BKXUd4ff3bwIpgFDNW0ZejectL2I6v0mN CuRTHAXc256elR75ENTHIo6QuD7pIM8= 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-408-kUHHbSrUOfyPg_7I9-9WBQ-1; Wed, 26 Jul 2023 12:43:47 -0400 X-MC-Unique: kUHHbSrUOfyPg_7I9-9WBQ-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-3fd22a4cf2dso35000665e9.2 for ; Wed, 26 Jul 2023 09:43:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690389826; x=1690994626; h=content-transfer-encoding:in-reply-to:organization: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=eYyLIb3cAz+pfX2bhGfoxws54fDawRj2oU07jpEMHJk=; b=T0GMESrvXTDsIjFMNVP8snwYQUtsnhNmWVY6hxJxwmNwYrLJfRYCh78soYBcGEGNid N4j1qFfS9lECGjWJ/q/bYyzMmMrjFJnof+WdxUaZdAmMEN6+69cfv3PYoRKA1Cz3c2r2 gLcCeo3+13F1DFmRbLOIyDcd1zcI6pGXq2yHCMQZqiCnqU1sQrvVZmgJ+Vye2jicpWck r3iRBtuDxCST/B2a5l/YEM9iIJaGq18X87gI0BB3EE3+2Hu0J+J+9TsQ8vGzMnCnlmmt OVigS5jzP3fBV5VcD7obeCf3BeiD5x0B5W8Ill+baLWc5Zfvo65yMqYr9QMZcyBjoXXN Bbhw== X-Gm-Message-State: ABy/qLZCPIyOclH7Cz+OALt8g/RTbFFX+WBUTappsMFGC6CgYuWc9N/r 6VAvf80Jc6y6AYVfHco+r0RDcVHJQsqp6cIa/jmGOaTBU0EEXTSGcJGNVv5O52BtHtt1jHnM4Ut qaCW1kISSIKE= X-Received: by 2002:a05:600c:234a:b0:3fb:a937:6024 with SMTP id 10-20020a05600c234a00b003fba9376024mr1856845wmq.29.1690389825916; Wed, 26 Jul 2023 09:43:45 -0700 (PDT) X-Google-Smtp-Source: APBJJlHTWQ0vXQsl72sdiZMvhOQa/+Rk0rY3XwcgMajaulp8YNtfY27UWEaCR+WRZNqpblW/Z9FbwA== X-Received: by 2002:a05:600c:234a:b0:3fb:a937:6024 with SMTP id 10-20020a05600c234a00b003fba9376024mr1856823wmq.29.1690389825430; Wed, 26 Jul 2023 09:43:45 -0700 (PDT) Received: from ?IPV6:2003:cb:c705:f600:a519:c50:799b:f1e3? (p200300cbc705f600a5190c50799bf1e3.dip0.t-ipconnect.de. [2003:cb:c705:f600:a519:c50:799b:f1e3]) by smtp.gmail.com with ESMTPSA id f9-20020a7bc8c9000000b003fa96fe2bd9sm2493280wml.22.2023.07.26.09.43.44 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 26 Jul 2023 09:43:45 -0700 (PDT) Message-ID: <65d2143d-ef76-cbe5-6d49-08953caa3b3b@redhat.com> Date: Wed, 26 Jul 2023 18:43:44 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Subject: Re: [PATCH v5 6/7] mm/hotplug: Embed vmem_altmap details in memory block To: "Aneesh Kumar K.V" , linux-mm@kvack.org, akpm@linux-foundation.org, mpe@ellerman.id.au, linuxppc-dev@lists.ozlabs.org, npiggin@gmail.com, christophe.leroy@csgroup.eu Cc: Oscar Salvador , Michal Hocko , Vishal Verma References: <20230725100212.531277-1-aneesh.kumar@linux.ibm.com> <20230725100212.531277-7-aneesh.kumar@linux.ibm.com> <87wmynvu9k.fsf@linux.ibm.com> From: David Hildenbrand Organization: Red Hat In-Reply-To: <87wmynvu9k.fsf@linux.ibm.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 50886A001F X-Rspam-User: X-Stat-Signature: n994chjhqby6iig4b7tmoes8h7zyejp9 X-Rsp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lpOVveASdP4ysgMS/oAJTCpXPccWHos/g0dXqFmkbFygjNkf+ZzCbHTj7mFUOS9eAIOUhggeso2i3yeapicUvw5cGmY+kbUetSYr/keOouHXIO1+knJh2jlTUuvQGBj5WtP1IVjJdvc2xMOBvY9nge5FNLaIEY5mp3GLFdefOnltlgVv4belSknrIcQDbxACXofOYEZq8qSzJDdWWT5drQuqw026Tt1OYE7w78DEYRdY5utN2J0YS/Ej0mFuIRH0UvumI/DdTTVooitsGpJm54rCK81V8c9GDDLP7Yrvbj9lLYYquAcNsXnQQHPxP0PEkPTd68KbgjjFUh/dArdYwsTjqiAvZJy1p1vvz/VgHUcy22MxlyurX2UBNNAGtwBjVyxI3wuA8eLMBHFqgbH+9x5ADbQZjNFMjfq8uk9bEI2R7E3V 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: On 26.07.23 12:31, Aneesh Kumar K.V wrote: > David Hildenbrand writes: > >> On 25.07.23 12:02, Aneesh Kumar K.V wrote: >>> With memmap on memory, some architecture needs more details w.r.t altmap >>> such as base_pfn, end_pfn, etc to unmap vmemmap memory. Instead of >>> computing them again when we remove a memory block, embed vmem_altmap >>> details in struct memory_block if we are using memmap on memory block >>> feature. >>> >>> No functional change in this patch >>> >>> Signed-off-by: Aneesh Kumar K.V >>> --- >> >> [...] >> >>> >>> static int add_memory_block(unsigned long block_id, unsigned long state, >>> - unsigned long nr_vmemmap_pages, >>> + struct vmem_altmap *altmap, >>> struct memory_group *group) >>> { >>> struct memory_block *mem; >>> @@ -744,7 +751,14 @@ static int add_memory_block(unsigned long block_id, unsigned long state, >>> mem->start_section_nr = block_id * sections_per_block; >>> mem->state = state; >>> mem->nid = NUMA_NO_NODE; >>> - mem->nr_vmemmap_pages = nr_vmemmap_pages; >>> + if (altmap) { >>> + mem->altmap = kmalloc(sizeof(struct vmem_altmap), GFP_KERNEL); >>> + if (!mem->altmap) { >>> + kfree(mem); >>> + return -ENOMEM; >>> + } >>> + memcpy(mem->altmap, altmap, sizeof(*altmap)); >>> + } >> >> I'm wondering if we should instead let the caller do the alloc/free. So we would alloc >> int the caller and would only store the pointer. >> >> Before removing the memory block, we would clear the pointer and free it in the caller. >> >> IOW, when removing a memory block and we still have an altmap set, something would be wrong. >> >> See below on try_remove_memory() handling. >> >> [...] >> >>> -static int get_nr_vmemmap_pages_cb(struct memory_block *mem, void *arg) >>> +static int get_vmemmap_altmap_cb(struct memory_block *mem, void *arg) >>> { >>> + struct vmem_altmap *altmap = (struct vmem_altmap *)arg; >>> /* >>> - * If not set, continue with the next block. >>> + * If we have any pages allocated from altmap >>> + * return the altmap details and break callback. >>> */ >>> - return mem->nr_vmemmap_pages; >>> + if (mem->altmap) { >>> + memcpy(altmap, mem->altmap, sizeof(struct vmem_altmap)); >>> + return 1; >>> + } >>> + return 0; >>> } >>> >>> static int check_cpu_on_node(int nid) >>> @@ -2146,9 +2152,8 @@ EXPORT_SYMBOL(try_offline_node); >>> >>> static int __ref try_remove_memory(u64 start, u64 size) >>> { >>> - struct vmem_altmap mhp_altmap = {}; >>> - struct vmem_altmap *altmap = NULL; >>> - unsigned long nr_vmemmap_pages; >>> + int ret; >>> + struct vmem_altmap mhp_altmap, *altmap = NULL; >>> int rc = 0, nid = NUMA_NO_NODE; >>> >>> BUG_ON(check_hotplug_memory_range(start, size)); >>> @@ -2171,24 +2176,15 @@ static int __ref try_remove_memory(u64 start, u64 size) >>> * the same granularity it was added - a single memory block. >>> */ >>> if (mhp_memmap_on_memory()) { >>> - nr_vmemmap_pages = walk_memory_blocks(start, size, NULL, >>> - get_nr_vmemmap_pages_cb); >>> - if (nr_vmemmap_pages) { >>> + ret = walk_memory_blocks(start, size, &mhp_altmap, >>> + get_vmemmap_altmap_cb); >>> + if (ret) { >>> if (size != memory_block_size_bytes()) { >>> pr_warn("Refuse to remove %#llx - %#llx," >>> "wrong granularity\n", >>> start, start + size); >>> return -EINVAL; >>> } >>> - >>> - /* >>> - * Let remove_pmd_table->free_hugepage_table do the >>> - * right thing if we used vmem_altmap when hot-adding >>> - * the range. >>> - */ >>> - mhp_altmap.base_pfn = PHYS_PFN(start); >>> - mhp_altmap.free = nr_vmemmap_pages; >>> - mhp_altmap.alloc = nr_vmemmap_pages; >>> altmap = &mhp_altmap; >>> } >> >> >> Instead of that, I suggest (whitespace damage expected): >> >> diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c >> index 3f231cf1b410..f6860df64549 100644 >> --- a/mm/memory_hotplug.c >> +++ b/mm/memory_hotplug.c >> @@ -1956,12 +1956,19 @@ static int check_memblock_offlined_cb(struct memory_block *mem, void *arg) >> return 0; >> } >> >> -static int get_nr_vmemmap_pages_cb(struct memory_block *mem, void *arg) >> +static int test_has_altmap_cb(struct memory_block *mem, void *arg) >> { >> - /* >> - * If not set, continue with the next block. >> - */ >> - return mem->nr_vmemmap_pages; >> + struct memory_block **mem_ptr = (struct memory_block **)arg; >> + >> + if (mem->altmap) { >> + /* >> + * We're not taking a reference on the memory block; it >> + * it cannot vanish while we're about to that memory ourselves. >> + */ >> + *mem_ptr = mem; >> + return 1; >> + } >> + return 0; >> } >> >> static int check_cpu_on_node(int nid) >> @@ -2036,9 +2043,7 @@ EXPORT_SYMBOL(try_offline_node); >> >> static int __ref try_remove_memory(u64 start, u64 size) >> { >> - struct vmem_altmap mhp_altmap = {}; >> struct vmem_altmap *altmap = NULL; >> - unsigned long nr_vmemmap_pages; >> int rc = 0, nid = NUMA_NO_NODE; >> >> BUG_ON(check_hotplug_memory_range(start, size)); >> @@ -2061,9 +2066,9 @@ static int __ref try_remove_memory(u64 start, u64 size) >> * the same granularity it was added - a single memory block. >> */ >> if (mhp_memmap_on_memory()) { >> - nr_vmemmap_pages = walk_memory_blocks(start, size, NULL, >> - get_nr_vmemmap_pages_cb); >> - if (nr_vmemmap_pages) { >> + struct memory_block *mem; >> + >> + if (walk_memory_blocks(start, size, &mem, test_has_altmap_cb)) { >> if (size != memory_block_size_bytes()) { >> pr_warn("Refuse to remove %#llx - %#llx," >> "wrong granularity\n", >> @@ -2072,12 +2077,11 @@ static int __ref try_remove_memory(u64 start, u64 size) >> } >> >> /* >> - * Let remove_pmd_table->free_hugepage_table do the >> - * right thing if we used vmem_altmap when hot-adding >> - * the range. >> + * Clear the altmap from the memory block before we >> + * remove it; we'll take care of freeing the altmap. >> */ >> - mhp_altmap.alloc = nr_vmemmap_pages; >> - altmap = &mhp_altmap; >> + altmap = mem->altmap; >> + mem->altmap = NULL; >> } >> } >> >> @@ -2094,6 +2098,9 @@ static int __ref try_remove_memory(u64 start, u64 size) >> >> arch_remove_memory(start, size, altmap); >> >> + if (altmap) >> + kfree(altmap); >> + >> if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK)) { >> memblock_phys_free(start, size); >> memblock_remove(start, size); >> > > Is this any better. Any specific reason we want the alloc and free in > the caller? IMHO if you only have a single instance you don't have to worry about any inconsistencies. At least to me it looks cleaner than this copying back and forth. Below is better, but as you pointed out, your get_vmemmap_altmap_cb() change is problematic/insufficient. -- Cheers, David / dhildenb