From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qk1-f180.google.com (mail-qk1-f180.google.com [209.85.222.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5739114B075 for ; Mon, 27 Jan 2025 15:34:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737992060; cv=none; b=ZGNFJB/0DhEWxT+D4i4Bngqpo+YgHKue/3IUgcdqxmjW8tA16CDLUnSBxSGxqerPciBoEcboa4+PGNmrhBSbLSYC2oYl7kGHAKPky9vNr+Q6CinezfM+WMMbVZofPc9LJR4JW88zOt48K7odVH5SgytTkRxRcIUYp7Ke6PTnUao= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737992060; c=relaxed/simple; bh=JlIRardHN3T/rAzbVZsKRhQoD9nvYKKMQjOClOHhgOw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dh9ClcOwZ1rVZyByFJCXaZmZMK3R+1gyjoRgDg68HiZlN+UjQo1BJKZJl3+ma+mE5J3ggxGLqhxZLtMN3a+ajtdB9qZZnNwvV3Wk+hpXkC4AC1CGWl5lj9uld4sqF/hCKa/ikvp7W12pyNRTQKPUVP+3UhU2kJJxwqtJPdQR2vA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=gourry.net; spf=pass smtp.mailfrom=gourry.net; dkim=pass (2048-bit key) header.d=gourry.net header.i=@gourry.net header.b=dj2HA8rn; arc=none smtp.client-ip=209.85.222.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=gourry.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gourry.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gourry.net header.i=@gourry.net header.b="dj2HA8rn" Received: by mail-qk1-f180.google.com with SMTP id af79cd13be357-7b6e9db19c8so444085085a.3 for ; Mon, 27 Jan 2025 07:34:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gourry.net; s=google; t=1737992054; x=1738596854; darn=vger.kernel.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=WEKgJJf51nUXgtjwZFRtEbK89HKdrqyuzjYJ58simEs=; b=dj2HA8rnoFiTxa2S//p5IKk2K5BIQjRfAn3abJ2mMWWy6TnQtD9xorztaejAujGUWU zBL2Kl4tUIZhWpPiX+RQz378HZ8Qv6rYKStTbmptjz/70QrEQ9PchLOqUJFTyzn1+Ldw 4Ej7sxIqBhCNYYDSLn4IARkaepc8eCLxA66Np4KXPiCC9HFGePp6B43njBxjb2D7VZwh 6KmR/iIY0/n31hVt4WtH5BK/RCZRrKtNF/WweJeOcp7lhTXazamDfS4icCDgwHJRBobT HBRydsXIFvgYE8InEmsFB2IaeUJjicUYV7VDlpu7us7cBeoyCiuFk8uDTsADIt3R6TE0 aMjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737992054; x=1738596854; 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=WEKgJJf51nUXgtjwZFRtEbK89HKdrqyuzjYJ58simEs=; b=Yd9vdPEiJzhOLVbLOZi1pWVcF6Q9YF2MMU7xNdU9SgCU08SDnvYA0SWR1OD7XVsgPE CPPeSjVAMLPeGuIx9di1Gl1vnslTGvKZ3n1UHUUbstgPo5ovtLk5bo9dOMCStEeq603b ce9nlW1qy5+L/6wDAxX6LVas1GgWWTshQc6gxgY3WtGfy2iPy1RocvxQowKyQVC7Es/J 4lakYQIDsjxUpvS/FNLN3R2+GsUVv/Zqg8A6hMjo9A3DdnS/4PmistqfrA6MICarOh4x Ov+/YI6PNRYOPODlZkLwIw6RHi4PAGl6SBPqjhVBiUt45m9EwYdhgLnySFSc22aXG79K prbA== X-Forwarded-Encrypted: i=1; AJvYcCUKWxCNYe9PfFwn0+dUfA6vkRwh/rQ+a+oYREvbbxpmOK+KyzmMLvmuQf0VDg4k59yLyZsVUcT+cGGGeX4=@vger.kernel.org X-Gm-Message-State: AOJu0YxlTfSY3qbFImnYawCLpERkBXVpqTgw/Y4ZoCAroxIXyPzMEWXm Qcf0tcj5YJ6/6Uolgp6gICOek2ohTrOCQVVKCz0/ZFyn0XEoQxbfka7rTKEiGPbmsi+5MhQosic d X-Gm-Gg: ASbGnctNmgigui6S8Y/pem1cEerDKEdS8ut33jWF76mEnWIH3cUhLEaj3YA/DpV1+RR AWro4e7afLqXJ9sxGbb7BG4EZ93U4o4YY1pgTxxnOQrduDtbfSbObOAqYLD0pynqdhFWZ585fo1 M4TA/PKFTvZ44XuFuc49pkSCkk7u7kP/NWIOyM7aixoh2BmBcE+jPMbhx/UyGqA6VFDG3m5mzyb +uM2AwPX+NW6tsIlC4SoAenvJWP8p7cNWbA514Zoli9WJGSDYexhpMiNSvBsRDy7HLGSFizZJvI rMW/tAxdqhtP53kbPZPyYTKoFI0hxjXe2qPKu/7TxR3NNacGBZSi7Ly1JH0VJVQDGvEE X-Google-Smtp-Source: AGHT+IGoKs+d0lqDKVI9i+teoRAx9CQCD+e8LPsJoU6HHbn9z9w7GdlOVjcgcKDj1hULyXcfAypH2A== X-Received: by 2002:a05:6214:4b0f:b0:6e1:a36b:3445 with SMTP id 6a1803df08f44-6e1b21857c6mr708814736d6.19.1737992054132; Mon, 27 Jan 2025 07:34:14 -0800 (PST) Received: from gourry-fedora-PF4VCD3F.lan (pool-173-79-56-208.washdc.fios.verizon.net. [173.79.56.208]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6e2058c2a51sm35380476d6.109.2025.01.27.07.34.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Jan 2025 07:34:13 -0800 (PST) From: Gregory Price To: linux-mm@kvack.org, linux-acpi@vger.kernel.org Cc: kernel-team@meta.com, x86@kernel.org, linux-kernel@vger.kernel.org, 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, dan.j.williams@intel.com, Jonathan.Cameron@huawei.com, alison.schofield@intel.com, rrichter@amd.com, rppt@kernel.org, gourry@gourry.net, bfaccini@nvidia.com, haibo1.xu@intel.com, dave.jiang@intel.com, Ira Weiny , Fan Ni Subject: [PATCH v8 1/3] memory: implement memory_block_advise/probe_max_size Date: Mon, 27 Jan 2025 10:34:03 -0500 Message-ID: <20250127153405.3379117-2-gourry@gourry.net> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250127153405.3379117-1-gourry@gourry.net> References: <20250127153405.3379117-1-gourry@gourry.net> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Acked-by: David Hildenbrand Acked-by: Mike Rapoport (Microsoft) Acked-by: Dan Williams Tested-by: Fan Ni Reviewed-by: Ira Weiny Signed-off-by: Gregory Price --- drivers/base/memory.c | 51 ++++++++++++++++++++++++++++++++++++++++++ include/linux/memory.h | 10 +++++++++ 2 files changed, 61 insertions(+) diff --git a/drivers/base/memory.c b/drivers/base/memory.c index 348c5dbbfa68..69feacbac3da 100644 --- a/drivers/base/memory.c +++ b/drivers/base/memory.c @@ -110,6 +110,57 @@ 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 */ /* -- 2.48.1