From patchwork Tue Dec 10 22:05:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gregory Price X-Patchwork-Id: 13902355 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 A0EA8E77182 for ; Tue, 10 Dec 2024 22:05:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2C6468D001E; Tue, 10 Dec 2024 17:05:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 275A88D0017; Tue, 10 Dec 2024 17:05:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 07AA78D001E; Tue, 10 Dec 2024 17:05:50 -0500 (EST) 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 D97628D0017 for ; Tue, 10 Dec 2024 17:05:49 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 95DBCA0F8D for ; Tue, 10 Dec 2024 22:05:49 +0000 (UTC) X-FDA: 82880432166.28.07214DA Received: from mail-qv1-f44.google.com (mail-qv1-f44.google.com [209.85.219.44]) by imf13.hostedemail.com (Postfix) with ESMTP id 78EC420008 for ; Tue, 10 Dec 2024 22:05:25 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=gourry.net header.s=google header.b=YBt1OE8b; spf=pass (imf13.hostedemail.com: domain of gourry@gourry.net designates 209.85.219.44 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=1733868330; 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=60R1hVTxsjiamUbrEoUH7TiaG7L9jG3XfbmxuhEpKo4=; b=SVZFiHtF42Y6eTP1/LbOC38BB+li2tJK7y14OvDlNrpsefH1ZhLovMQobVwSaSlzvUAZJM BaPI56EDl7zb750dgBABIKZyDkU43AUJg/RtWAI6AuN7x2PBQ49TGeWVOymRW+d/s2BnSs ibL9N2Jq9dl3ncI/36/UvIhMZiElOog= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=gourry.net header.s=google header.b=YBt1OE8b; spf=pass (imf13.hostedemail.com: domain of gourry@gourry.net designates 209.85.219.44 as permitted sender) smtp.mailfrom=gourry@gourry.net; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733868330; a=rsa-sha256; cv=none; b=4zfSKGO+VOR8EEsQJef+Ao6/CbALEhN4qubnYxLkfMBHxYK9BDzi5/t8URUQnHXgETJF2i 5Q8oY9dYzjxuJCD+jP2p9Hn/aWCoymLm4AFzY7fu11I0V830qcCSaXpi0S5oekWs+Lhms0 xe8Iyn4d4aSmzsBNwtFxZZfSumQgkp0= Received: by mail-qv1-f44.google.com with SMTP id 6a1803df08f44-6d92cd1e811so766506d6.1 for ; Tue, 10 Dec 2024 14:05:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gourry.net; s=google; t=1733868347; x=1734473147; 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=60R1hVTxsjiamUbrEoUH7TiaG7L9jG3XfbmxuhEpKo4=; b=YBt1OE8bRC2ztawaeiBZ+uy5wLr9A8RGILb+TNYwhDWYjUTRSqSN/Bj4lzbkak3poz fGUMWP9MvfAHaVcpY26dyuFnIPGJLtIt/9LVaYZMkNv7PifUiNGR23JREn7ck1RFaQn5 dAPL5jboW/4347aTcMUvMIuiMqV3hXGm/3wTRrAItNICPoUF2ucYTOn6xddTKLK5cs9+ vpm0vjf1cEG1uK9YFevetA32Yn7cuDh1X42QYay5rXcmDV9tp3TmCw+ZoAWRbxDYbR7M 0/jduRBlqghGAhhACWE+oU4FTi4pPa2DobsSK39e3Rwa1Za6xugBaBl4qlXyKl8aTjih a/Jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733868347; x=1734473147; 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=60R1hVTxsjiamUbrEoUH7TiaG7L9jG3XfbmxuhEpKo4=; b=fzDtXHq2xWvHisM9MB9K2xbgshThFhQJOU+cSVXkKVl9PD1Wi2STpwnba6kX4xnXg3 tOomf+e3lqcsQTHoLvpsfha6kMa9ipT7sMnAGZPzYyvkvL03j/VfIVr7fMPbUSP4HHLm IABJVTEb1l0IowRcTd3fqW3dMafwp+q44lxvNMBeK0i1nIJKMKOSIlG9wGRPzABsNGmA pyGMU2FltrE49AxPvuPTwIaOCy/cX5AZ4NA1qGVyRJToLpSQiOrJJvDRKX8sp3+6Qdt/ gjCuMa61uJ5f/HO3VcXwwvE/1xBh1WKaW8RvPQ6JO4TlIqRqZC8QFSDav/6BodHOgywy sfKw== X-Forwarded-Encrypted: i=1; AJvYcCWVL0p6Zjn8pFQGx2T13XfEklmp6BhJ9tHyMTlsUq1WRxqm6Dtv82tzsOjCNVZ5C2JmkyjjJ+BiFg==@kvack.org X-Gm-Message-State: AOJu0Ywny3+aZNaL7a3YsEzo+IJwiHyQHo5z5dZky/NzJt/xVgnY3IfK xXq8A7AwtbtptFRV+22oRNw+1HBUeM3gtvlXW8XaVtJt33uR7qLVG2mCxkbwaYU= X-Gm-Gg: ASbGncsN6Zg873vMkGiqOYLSoRA2mQhNHv5Ftzkb6Jn/lA8bjyeuJDaGmYgi01iVGXJ MPNIFsEA906XbDnKJ4mFbdD2+LbqHa7zN1T5jmc67VA+XqBgWETonpHBy4+iCCWGTF5wNQLMdyx s4C9XtZ4vmar+ChR0dKexd4tPj74kq/KSAtFPhMQSo/oE/fd1fctrbgDrHV5KAcWJwk7MoCccmi d7TJVg5ab8KrTo8hVAc0cATr24OHvCglWCv4+BYrTqxpH8SIOg87B9Nylkd69ExR6Ghib9r2Fo6 Bf1ZfpNqVIHQ3SwbJDoFTSLb6ro8JWvYUjeNM9Y= X-Google-Smtp-Source: AGHT+IHc57rNPDT3ETou4ayUZZMOYLv4oTDJ4vzRb5FMnCPekUkkWfUgKrPMA1hVHVlHX0Bm8hfnCQ== X-Received: by 2002:ad4:5de6:0:b0:6d8:f50e:8036 with SMTP id 6a1803df08f44-6d935314e21mr9170026d6.20.1733868346746; Tue, 10 Dec 2024 14:05:46 -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 6a1803df08f44-6d8eeb132e1sm49707756d6.35.2024.12.10.14.05.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Dec 2024 14:05:45 -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, dan.j.williams@intel.com, ira.weiny@intel.com, gourry@gourry.net, dave.hansen@linux.intel.com, david@redhat.com, rppt@kernel.org, akpm@linux-foundation.org, luto@kernel.org, peterz@infradead.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, rafael@kernel.org, lenb@kernel.org, osalvador@suse.de, Fan Ni Subject: [PATCH v7 1/3] memory: implement memory_block_advise/probe_max_size Date: Tue, 10 Dec 2024 17:05:15 -0500 Message-ID: <20241210220517.3744-2-gourry@gourry.net> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241210220517.3744-1-gourry@gourry.net> References: <20241210220517.3744-1-gourry@gourry.net> MIME-Version: 1.0 X-Rspamd-Queue-Id: 78EC420008 X-Rspamd-Server: rspam12 X-Stat-Signature: btrntw3knbp1tykma9p43oi7a6bb7876 X-Rspam-User: X-HE-Tag: 1733868325-510959 X-HE-Meta: U2FsdGVkX19Okw7kZBBxwhAd9gPy0XQfCv7BKDiLZrWI/Ycep5k5Gc4bx6vJD6xyW/Z0eGUftyd9/DPaTuNyoIW+dogOedODC8bjpk6CQKWuz+57sD7bquYRt40vuDkQVsw9++gmJKaZeaouTDIA77Ds9ncsn6njCyFQnaxeFpn40IzWdWT5n0AtYIQFP3TyJxkfMFF4fEGgFvcmRxvMvm9LDLTQFoaYU1QZUnvTcprya2TslQoo3cNraiWP5vWy+F+gOvUQdcJU+xOpSzXzqe86bpWlx7HXOR0fdlrANZf2AdANqxUJpk0Y6Df4KoAeY8tPKi87nDewl/AQ/JdURbNhFuvGFoG5E8qdMR3N36n9rwvaGsxYLwVab2MeRw5QgnOA/sK+EFGqyce99bxucUC45pkhYTpawY+jJcFsDFdRU6vCMNwuSqdUwZ0xLScVUq0AZnHdfVWoq+Ev554cA0fLWQHqoujemq85y7RQhdqcPDojMuZxGsjo5Bipmi/GkJ+EfvXXYjFGKvk8vWds/8VMbXr/k0hbZrH1F7nn/tkCAhARBSu3EDLYqTd0AyBbqsNgJySMMxfkXTQRxWcJuJRkmOCyBpK3Kgm83ZAKq1AaQJJJ8cXy8mMn2ZIr9i2RN/G4n1Aa6fel7A+pxMtV5zEfQNRPdmmWBM0Y3KU5z2I2zNSXleR+whqcSgJtfVqtjmsqBXCmzjIUQhoOEj/Q6KbSpJNfre06Clpoadw3KuMdhAxS8aR8LidULpzcC1yVP30vEPRS7/YwIMoYT7/XgOxu0pHP88s5z2Metgh4Y6JAE1/Uuh6P8Te7lxwib47a/IP4Ddnn4heZ90znwVDzwTm9lgZari2Q9rbIkZyUVLR3eN3DNmzKxF/ePQYGoeeUrqjjd20Qc0UYG1dA0+8LysEIoDIGnXAY/FbdEnxp3odrjtBkgk0X+fEegKy0UPtuqowNZEozXhMDWBQM3lA 3u5pCF8t xGVI5Ylnn+Kn3lafLUnu3d+bpGsPK34G2kNbRae1w9b/qKVzhTxLXWpIRHwwo77PAXgV5kT1V5mRVBaMQpdV6VSnlUh/8taZdRVuP4wvd0SYzy9vkHunXwRYgdYMdNNNkwBldq8KWXaSiiK2CTnQ9NTXGvZzDyjAYdhMHcSf89X3kt5/FaaXebD9uVgwojGsCwinrVy0eEO38jVbZaawPf1+MRQdvIJXsyCiXJiQXPL3jagXfd+2IyEHMjL3OfknMC802kMKqoRWohP1E96ADxO83LznIDBCx1uCouZIetyp/RdrGj6AETLC5n/eSiXf74dnwK57FrIirJw+AdMzgxAWVtxa04aSUrUCus7ll5ZQUKx9HdYTHn85WH7jLmnLMGxBfK4Ow1F6CM8oKOtclxLypb6g9x4D+CuXxsD4JlCQqX7F3i9Bij0TSyvQXsT1ysrDs 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) Acked-by: Dan Williams Tested-by: Fan Ni --- 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 */ /*