From patchwork Wed Nov 6 15:58:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gregory Price X-Patchwork-Id: 13865122 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 10C7CD59F4A for ; Wed, 6 Nov 2024 15:59:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 88A786B00AA; Wed, 6 Nov 2024 10:59:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 813396B00AC; Wed, 6 Nov 2024 10:59:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4DDDA6B00AD; Wed, 6 Nov 2024 10:59:05 -0500 (EST) 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 289496B00AA for ; Wed, 6 Nov 2024 10:59:05 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id A4D91ADDBA for ; Wed, 6 Nov 2024 15:59:04 +0000 (UTC) X-FDA: 82756126614.17.DE36C20 Received: from mail-vk1-f171.google.com (mail-vk1-f171.google.com [209.85.221.171]) by imf14.hostedemail.com (Postfix) with ESMTP id 66E70100018 for ; Wed, 6 Nov 2024 15:58:24 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=gourry.net header.s=google header.b=WceVXzF8; spf=pass (imf14.hostedemail.com: domain of gourry@gourry.net designates 209.85.221.171 as permitted sender) smtp.mailfrom=gourry@gourry.net; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730908605; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=GrV9O4GPEUiGe4j9uKnxvw7X+oP7rfAOpfBnY6P0I08=; b=2EqecmOETPXRwU8xwoqxZGA6n/E+9XphlcUpm5EFSY0CGq6GyUCImAn8R9QFqxW2HCtsmc kMfiZW2blj0YAzFk/YCpDSixxU9oNLkBx/iximG4/AnOBUehPE3Q8hygz9D1vZJ0VeM9x0 VE6LBJfhEe0ctY6YQcQfFoplnNNvOW0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730908605; a=rsa-sha256; cv=none; b=nPxIsZHwUF1Njd4i3Y/83olmS7XZRdPSVpf/WN97kbFiDw7ByPAOFQ+TJgspR1S/AnvvQz EYEUVl4JtlJQtr/WLZmpsOL6wedTVw/FzDzxW1d4wtxSF4dnc9ccYJlwonx7+Jofumu7j+ oDyjggIYjePPym6e+oQKjtVRZdZO/oo= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=gourry.net header.s=google header.b=WceVXzF8; spf=pass (imf14.hostedemail.com: domain of gourry@gourry.net designates 209.85.221.171 as permitted sender) smtp.mailfrom=gourry@gourry.net; dmarc=none Received: by mail-vk1-f171.google.com with SMTP id 71dfb90a1353d-5139cd01814so1733077e0c.0 for ; Wed, 06 Nov 2024 07:59:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gourry.net; s=google; t=1730908742; x=1731513542; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=GrV9O4GPEUiGe4j9uKnxvw7X+oP7rfAOpfBnY6P0I08=; b=WceVXzF8T3f9u+RxtWYoNNshY+zqb5Zr9OdzFoBCVVT2PsQ8ShXIrBa/PseXrP5J3T BFLlharFluqSqmaIeZttJ4kwAHwHcVv+Wsv0Tif8ZqaSxk9DhUot1wnigcq5dcWtL0dQ 0DdwIc0bx6b5qCDCch2ZhYL685H1oHqZJVUeYESW9txvi98R8om4NBKVQ48QrfU8q0zw zQqSjHyjJRtLAab2se3yNeR0wFh9pDcE17Srx8qJz7leNoKrVKUEZeJ3Rk8LOzCsMq3m YtEU4/TxFm5JzUW/C8++fbUPQvTVpDYXsGOU3Wpn4XaUlkETI+Ei9mRcqxr9s4V9LhOP 2xLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730908742; x=1731513542; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GrV9O4GPEUiGe4j9uKnxvw7X+oP7rfAOpfBnY6P0I08=; b=w5L1AQ+p/ofoW7fFID5gyGA7zYEmw3SVe6+aO2SfwKWEyNJEvt9a72ZpI8yXcfI+zJ SzQmFBcyLYu5/11kWW8VYBbZiLtva247n/2sqWiG42wzOMZHs2F/Zkrobz4WzDoVd4tK R8WFai6p1p3kTgJAObj+kMQSc4dCxcVMc9c+O18YPaKscPgWCPFu0xOnMBq4f4MhF+sa VtiVpQIfNvisqrlFMezOUY4mMxFc48l5jfXLZnI23xLQZVG3kB0UHkE0GCxh+iuQZp/m pnCFcntc09PC/kqfulj6lIwCp6l6j7dFFbwXAxJlN5bbEwF5dcjhl1YsnhyomvSQB+Ll 51eA== X-Forwarded-Encrypted: i=1; AJvYcCUH6mXlLUn4D+K2Vokp670k1SMT6LWVCNFbmLquwRMWUbC7eNyJ+/FXcwtdVC5UGApnqqbbkQZaww==@kvack.org X-Gm-Message-State: AOJu0YyQbh2A1PixFOdL+MyEgJyOMme8nh7CzaeAcp8qDJzM396Hb/BE oX80BaineTFW5mcnUvEtmVCVWT/BMqf7LzSBmcbgrmBcv2xBuHwD0oSoG4XhMHw= X-Google-Smtp-Source: AGHT+IFsdN7WBxVaMsC4Loys1Mpdq5x23y2sQKFk/taNoch7FUZg+El0slCCEmCVOauncv36ulU/bg== X-Received: by 2002:a05:6122:2a41:b0:4f6:aa3e:aa4c with SMTP id 71dfb90a1353d-51014ff7a70mr36295778e0c.3.1730908741730; Wed, 06 Nov 2024 07:59:01 -0800 (PST) Received: from PC2K9PVX.TheFacebook.com (pool-173-79-56-208.washdc.fios.verizon.net. [173.79.56.208]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7b2f39f7b85sm641377285a.31.2024.11.06.07.59.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Nov 2024 07:59:01 -0800 (PST) From: Gregory Price To: x86@kernel.org, linux-kernel@vger.kernel.org, linux-acpi@vger.kernel.org, linux-mm@kvack.org Cc: linux-cxl@vger.kernel.org, kernel-team@meta.com, Jonathan.Cameron@huawei.com, dan.j.williams@intel.com, rrichter@amd.com, Terry.Bowman@amd.com, dave.jiang@intel.com, ira.weiny@intel.com, alison.schofield@intel.com, gourry@gourry.net, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, rafael@kernel.org, lenb@kernel.org, david@redhat.com, osalvador@suse.de, gregkh@linuxfoundation.org, akpm@linux-foundation.org, rppt@kernel.org Subject: [PATCH v6 1/3] memory: implement memory_block_advise/probe_max_size Date: Wed, 6 Nov 2024 10:58:45 -0500 Message-ID: <20241106155847.7985-2-gourry@gourry.net> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241106155847.7985-1-gourry@gourry.net> References: <20241106155847.7985-1-gourry@gourry.net> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 66E70100018 X-Stat-Signature: zhs4icgnfr1expnpb7761yt5wjyem1qt X-HE-Tag: 1730908704-782427 X-HE-Meta: U2FsdGVkX187nEE1pCTPMPPAQjKzTQQNa6wzpGGW0R6qSaG3ZbCAiJA9zns5XmyKx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kSqAayI44UbcW/5M3Mlj+gvRDNUsvmOzX8lpiGZNdGtPAlDdXRCE3M2f0zj/Iq7advZyxLwZRpbPlfJCLOCQfhfGDjV3I91qY22YqY4UBp0hb94LgWM3hi0YUSyn6mdVMRXOWEsQmW2d67NOv5rpYuy/Qc8pniVs6e2mLqaRkFcd1WoEMX5sc0U0of7z5lEhg39LkF9t9WF8DsT92Aon7L6pFJ51LvWBsmw49FAHP80ZwsAZ8iJoFMNlA= 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: List-Subscribe: List-Unsubscribe: Hotplug memory sources may have opinions on what the memblock size should be - usually for alignment purposes. For example, CXL memory extents can be 256MB with a matching alignment. If this size/alignment is smaller than the block size, it can result in stranded capacity. Implement memory_block_advise_max_size for use prior to allocator init, for software to advise the system on the max block size. Implement memory_block_probe_max_size for use by arch init code to calculate the best block size. Use of advice is architecture defined. The probe value can never change after first probe. Calls to advise after probe will return -EBUSY to aid debugging. On systems without hotplug, always return -ENODEV and 0 respectively. Suggested-by: Ira Weiny Signed-off-by: Gregory Price Acked-by: David Hildenbrand Acked-by: Mike Rapoport (Microsoft) --- drivers/base/memory.c | 53 ++++++++++++++++++++++++++++++++++++++++++ include/linux/memory.h | 10 ++++++++ 2 files changed, 63 insertions(+) diff --git a/drivers/base/memory.c b/drivers/base/memory.c index 67858eeb92ed..835793150b41 100644 --- a/drivers/base/memory.c +++ b/drivers/base/memory.c @@ -110,6 +110,59 @@ static void memory_block_release(struct device *dev) kfree(mem); } + +/* Max block size to be set by memory_block_advise_max_size */ +static unsigned long memory_block_advised_size; +static bool memory_block_advised_size_queried; + +/** + * memory_block_advise_max_size() - advise memory hotplug on the max suggested + * block size, usually for alignment. + * @size: suggestion for maximum block size. must be aligned on power of 2. + * + * Early boot software (pre-allocator init) may advise archs on the max block + * size. This value can only decrease after initialization, as the intent is + * to identify the largest supported alignment for all sources. + * + * Use of this value is arch-defined, as is min/max block size. + * + * Return: 0 on success + * -EINVAL if size is 0 or not pow2 aligned + * -EBUSY if value has already been probed + */ +int __init memory_block_advise_max_size(unsigned long size) +{ + if (!size || !is_power_of_2(size)) + return -EINVAL; + + if (memory_block_advised_size_queried) + return -EBUSY; + + if (memory_block_advised_size) { + memory_block_advised_size = min(memory_block_advised_size, + size); + } else { + memory_block_advised_size = size; + } + + return 0; +} + +/** + * memory_block_advised_max_size() - query advised max hotplug block size. + * + * After the first call, the value can never change. Callers looking for the + * actual block size should use memory_block_size_bytes. This interface is + * intended for use by arch-init when initializing the hotplug block size. + * + * Return: advised size in bytes, or 0 if never set. + */ +unsigned long memory_block_advised_max_size(void) +{ + memory_block_advised_size_queried = true; + return memory_block_advised_size; +} + unsigned long __weak memory_block_size_bytes(void) { return MIN_MEMORY_BLOCK_SIZE; diff --git a/include/linux/memory.h b/include/linux/memory.h index c0afee5d126e..8202d0efbf46 100644 --- a/include/linux/memory.h +++ b/include/linux/memory.h @@ -149,6 +149,14 @@ static inline int hotplug_memory_notifier(notifier_fn_t fn, int pri) { return 0; } +static inline int memory_block_advise_max_size(unsigned long size) +{ + return -ENODEV; +} +static inline unsigned long memory_block_advised_max_size(void) +{ + return 0; +} #else /* CONFIG_MEMORY_HOTPLUG */ extern int register_memory_notifier(struct notifier_block *nb); extern void unregister_memory_notifier(struct notifier_block *nb); @@ -181,6 +189,8 @@ int walk_dynamic_memory_groups(int nid, walk_memory_groups_func_t func, void memory_block_add_nid(struct memory_block *mem, int nid, enum meminit_context context); #endif /* CONFIG_NUMA */ +int memory_block_advise_max_size(unsigned long size); +unsigned long memory_block_advised_max_size(void); #endif /* CONFIG_MEMORY_HOTPLUG */ /*