Skip to content

Commit 902ce63

Browse files
David Hildenbrandtorvalds
authored andcommitted
driver/base/memory.c: validate memory block size early
Let's validate the memory block size early, when initializing the memory device infrastructure. Fail hard in case the value is not suitable. As nobody checks the return value of memory_dev_init(), turn it into a void function and fail with a panic in all scenarios instead. Otherwise, we'll crash later during boot when core/drivers expect that the memory device infrastructure (including memory_block_size_bytes()) works as expected. I think long term, we should move the whole memory block size configuration (set_memory_block_size_order() and memory_block_size_bytes()) into drivers/base/memory.c. Link: http://lkml.kernel.org/r/20190806090142.22709-1-david@redhat.com Signed-off-by: David Hildenbrand <david@redhat.com> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: "Rafael J. Wysocki" <rafael@kernel.org> Cc: Pavel Tatashin <pasha.tatashin@soleen.com> Cc: Michal Hocko <mhocko@suse.com> Cc: Dan Williams <dan.j.williams@intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent f915fb7 commit 902ce63

2 files changed

Lines changed: 12 additions & 25 deletions

File tree

‎drivers/base/memory.c‎

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -100,21 +100,6 @@ unsigned long __weak memory_block_size_bytes(void)
100100
}
101101
EXPORT_SYMBOL_GPL(memory_block_size_bytes);
102102

103-
static unsigned long get_memory_block_size(void)
104-
{
105-
unsigned long block_sz;
106-
107-
block_sz = memory_block_size_bytes();
108-
109-
/* Validate blk_sz is a power of 2 and not less than section size */
110-
if ((block_sz & (block_sz - 1)) || (block_sz < MIN_MEMORY_BLOCK_SIZE)) {
111-
WARN_ON(1);
112-
block_sz = MIN_MEMORY_BLOCK_SIZE;
113-
}
114-
115-
return block_sz;
116-
}
117-
118103
/*
119104
* Show the first physical section index (number) of this memory block.
120105
*/
@@ -461,7 +446,7 @@ static DEVICE_ATTR_RO(removable);
461446
static ssize_t block_size_bytes_show(struct device *dev,
462447
struct device_attribute *attr, char *buf)
463448
{
464-
return sprintf(buf, "%lx\n", get_memory_block_size());
449+
return sprintf(buf, "%lx\n", memory_block_size_bytes());
465450
}
466451

467452
static DEVICE_ATTR_RO(block_size_bytes);
@@ -812,19 +797,22 @@ static const struct attribute_group *memory_root_attr_groups[] = {
812797
/*
813798
* Initialize the sysfs support for memory devices...
814799
*/
815-
int __init memory_dev_init(void)
800+
void __init memory_dev_init(void)
816801
{
817802
int ret;
818803
int err;
819804
unsigned long block_sz, nr;
820805

806+
/* Validate the configured memory block size */
807+
block_sz = memory_block_size_bytes();
808+
if (!is_power_of_2(block_sz) || block_sz < MIN_MEMORY_BLOCK_SIZE)
809+
panic("Memory block size not suitable: 0x%lx\n", block_sz);
810+
sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
811+
821812
ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
822813
if (ret)
823814
goto out;
824815

825-
block_sz = get_memory_block_size();
826-
sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
827-
828816
/*
829817
* Create entries for memory sections that were found
830818
* during boot and have been initialized
@@ -840,8 +828,7 @@ int __init memory_dev_init(void)
840828

841829
out:
842830
if (ret)
843-
printk(KERN_ERR "%s() failed: %d\n", __func__, ret);
844-
return ret;
831+
panic("%s() failed: %d\n", __func__, ret);
845832
}
846833

847834
/**

‎include/linux/memory.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ struct mem_section;
8080
#define IPC_CALLBACK_PRI 10
8181

8282
#ifndef CONFIG_MEMORY_HOTPLUG_SPARSE
83-
static inline int memory_dev_init(void)
83+
static inline void memory_dev_init(void)
8484
{
85-
return 0;
85+
return;
8686
}
8787
static inline int register_memory_notifier(struct notifier_block *nb)
8888
{
@@ -113,7 +113,7 @@ extern int register_memory_isolate_notifier(struct notifier_block *nb);
113113
extern void unregister_memory_isolate_notifier(struct notifier_block *nb);
114114
int create_memory_block_devices(unsigned long start, unsigned long size);
115115
void remove_memory_block_devices(unsigned long start, unsigned long size);
116-
extern int memory_dev_init(void);
116+
extern void memory_dev_init(void);
117117
extern int memory_notify(unsigned long val, void *v);
118118
extern int memory_isolate_notify(unsigned long val, void *v);
119119
extern struct memory_block *find_memory_block(struct mem_section *);

0 commit comments

Comments
 (0)