linux-stable/drivers/base
Phil Auld e27d38b305 drivers/base: fix userspace break from using bin_attributes for cpumap and cpulist
commit 7ee951acd3 upstream.

Using bin_attributes with a 0 size causes fstat and friends to return that
0 size. This breaks userspace code that retrieves the size before reading
the file. Rather than reverting 75bd50fa84 ("drivers/base/node.c: use
bin_attribute to break the size limitation of cpumap ABI") let's put in a
size value at compile time.

For cpulist the maximum size is on the order of
	NR_CPUS * (ceil(log10(NR_CPUS)) + 1)/2

which for 8192 is 20480 (8192 * 5)/2. In order to get near that you'd need
a system with every other CPU on one node. For example: (0,2,4,8, ... ).
To simplify the math and support larger NR_CPUS in the future we are using
(NR_CPUS * 7)/2. We also set it to a min of PAGE_SIZE to retain the older
behavior for smaller NR_CPUS.

The cpumap file the size works out to be NR_CPUS/4 + NR_CPUS/32 - 1
(or NR_CPUS * 9/32 - 1) including the ","s.

Add a set of macros for these values to cpumask.h so they can be used in
multiple places. Apply these to the handful of such files in
drivers/base/topology.c as well as node.c.

As an example, on an 80 cpu 4-node system (NR_CPUS == 8192):

before:

-r--r--r--. 1 root root 0 Jul 12 14:08 system/node/node0/cpulist
-r--r--r--. 1 root root 0 Jul 11 17:25 system/node/node0/cpumap

after:

-r--r--r--. 1 root root 28672 Jul 13 11:32 system/node/node0/cpulist
-r--r--r--. 1 root root  4096 Jul 13 11:31 system/node/node0/cpumap

CONFIG_NR_CPUS = 16384
-r--r--r--. 1 root root 57344 Jul 13 14:03 system/node/node0/cpulist
-r--r--r--. 1 root root  4607 Jul 13 14:02 system/node/node0/cpumap

The actual number of cpus doesn't matter for the reported size since they
are based on NR_CPUS.

Fixes: 75bd50fa84 ("drivers/base/node.c: use bin_attribute to break the size limitation of cpumap ABI")
Fixes: bb9ec13d15 ("topology: use bin_attribute to break the size limitation of cpumap ABI")
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: "Rafael J. Wysocki" <rafael@kernel.org>
Cc: Yury Norov <yury.norov@gmail.com>
Cc: stable@vger.kernel.org
Acked-by: Yury Norov <yury.norov@gmail.com> (for include/linux/cpumask.h)
Signed-off-by: Phil Auld <pauld@redhat.com>
Link: https://lore.kernel.org/r/20220715134924.3466194-1-pauld@redhat.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2022-08-17 14:42:20 +02:00
..
firmware_loader firmware_loader: use kernel credentials when reading firmware 2022-05-06 10:00:03 +02:00
power PM: domains: Ensure genpd_debugfs_dir exists before remove 2022-08-17 14:40:39 +02:00
regmap regmap-irq: Fix offset/index mismatch in read_sub_irq_data() 2022-06-29 09:04:34 +02:00
test driver core: Simplify async probe test code by using ktime_ms_delta() 2021-12-29 10:57:22 +01:00
Kconfig devtmpfs: mount with noexec and nosuid 2021-12-30 13:54:42 +01:00
Makefile mm/memory_hotplug: remove CONFIG_MEMORY_HOTPLUG_SPARSE 2021-11-06 13:30:42 -07:00
arch_numa.c mm: percpu: add generic pcpu_populate_pte() function 2022-01-20 08:52:52 +02:00
arch_topology.c arch_topology: Do not set llc_sibling if llc_id is invalid 2022-04-20 18:57:59 +02:00
attribute_container.c driver core: attribute_container: fix W=1 warnings 2021-05-14 13:37:10 +02:00
auxiliary.c Documentation/auxiliary_bus: Move the text into the code 2021-12-03 16:41:50 +01:00
base.h software nodes: Split software_node_notify() 2021-07-16 19:17:05 +02:00
bus.c driver: base: fix UAF when driver_attach failed 2022-06-14 18:44:52 +02:00
cacheinfo.c cacheinfo: clear cache_leaves(cpu) in free_cache_attributes() 2021-07-21 17:29:40 +02:00
class.c block: remove genhd.h 2022-02-02 07:49:59 -07:00
component.c component: Add common helper for compare/release functions 2022-02-25 12:16:12 +01:00
container.c
core.c PM: runtime: Fix supplier device management during consumer probe 2022-07-12 16:42:16 +02:00
cpu.c x86/bugs: Report AMD retbleed vulnerability 2022-07-23 12:56:47 +02:00
dd.c driver core: fix potential deadlock in __driver_attach 2022-08-17 14:41:39 +02:00
devcoredump.c devcoredump: remove contact information 2021-06-04 15:05:44 +02:00
devres.c devres: fix typos in comments 2022-03-18 14:30:12 +01:00
devtmpfs.c Driver core changes for 5.18-rc1 2022-03-28 12:41:28 -07:00
driver.c
firmware.c
hypervisor.c
init.c init: Initialize noop_backing_dev_info early 2022-06-22 14:28:03 +02:00
isa.c bus: Make remove callback return void 2021-07-21 11:53:42 +02:00
map.c driver: base: Prefer unsigned int to bare use of unsigned 2021-07-21 17:30:09 +02:00
memory.c mm/memory-failure: disable unpoison once hw error happens 2022-06-29 09:04:41 +02:00
module.c
node.c drivers/base: fix userspace break from using bin_attributes for cpumap and cpulist 2022-08-17 14:42:20 +02:00
pinctrl.c
platform-msi.c platform-msi: Simplify platform device MSI code 2021-12-16 22:22:19 +01:00
platform.c platform: use dev_err_probe() in platform_get_irq_byname() 2022-02-08 12:22:07 +01:00
property.c device property: Allow error pointer to be passed to fwnode APIs 2022-06-09 10:29:58 +02:00
soc.c base: soc: Make soc_device_match() simpler and easier to read 2022-03-18 14:28:07 +01:00
swnode.c software node: fix wrong node passed to find nargs_prop 2021-12-22 18:26:18 +01:00
syscore.c
topology.c drivers/base: fix userspace break from using bin_attributes for cpumap and cpulist 2022-08-17 14:42:20 +02:00
trace.c devres: Enable trace events 2021-06-15 17:14:36 +02:00
trace.h devres: Enable trace events 2021-06-15 17:14:36 +02:00
transport_class.c