Driver core: Introduce offline/online callbacks for memory blocks
Introduce .offline() and .online() callbacks for memory_subsys that will allow the generic device_offline() and device_online() to be used with device objects representing memory blocks. That, in turn, allows the ACPI subsystem to use device_offline() to put removable memory blocks offline, if possible, before removing memory modules holding them. The 'online' sysfs attribute of memory block devices will attempt to put them offline if 0 is written to it and will attempt to apply the previously used online type when onlining them (i.e. when 1 is written to it). Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Tested-by: Vasilis Liaskovitis <vasilis.liaskovitis@profitbricks.com> Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Reviewed-by: Toshi Kani <toshi.kani@hp.com>
This commit is contained in:
		
					parent
					
						
							
								e2ff39400d
							
						
					
				
			
			
				commit
				
					
						4960e05e22
					
				
			
		
					 2 changed files with 88 additions and 25 deletions
				
			
		|  | @ -37,9 +37,14 @@ static inline int base_memory_block_id(int section_nr) | ||||||
| 	return section_nr / sections_per_block; | 	return section_nr / sections_per_block; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | static int memory_subsys_online(struct device *dev); | ||||||
|  | static int memory_subsys_offline(struct device *dev); | ||||||
|  | 
 | ||||||
| static struct bus_type memory_subsys = { | static struct bus_type memory_subsys = { | ||||||
| 	.name = MEMORY_CLASS_NAME, | 	.name = MEMORY_CLASS_NAME, | ||||||
| 	.dev_name = MEMORY_CLASS_NAME, | 	.dev_name = MEMORY_CLASS_NAME, | ||||||
|  | 	.online = memory_subsys_online, | ||||||
|  | 	.offline = memory_subsys_offline, | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| static BLOCKING_NOTIFIER_HEAD(memory_chain); | static BLOCKING_NOTIFIER_HEAD(memory_chain); | ||||||
|  | @ -88,6 +93,7 @@ int register_memory(struct memory_block *memory) | ||||||
| 	memory->dev.bus = &memory_subsys; | 	memory->dev.bus = &memory_subsys; | ||||||
| 	memory->dev.id = memory->start_section_nr / sections_per_block; | 	memory->dev.id = memory->start_section_nr / sections_per_block; | ||||||
| 	memory->dev.release = memory_block_release; | 	memory->dev.release = memory_block_release; | ||||||
|  | 	memory->dev.offline = memory->state == MEM_OFFLINE; | ||||||
| 
 | 
 | ||||||
| 	error = device_register(&memory->dev); | 	error = device_register(&memory->dev); | ||||||
| 	return error; | 	return error; | ||||||
|  | @ -278,22 +284,59 @@ static int __memory_block_change_state(struct memory_block *mem, | ||||||
| { | { | ||||||
| 	int ret = 0; | 	int ret = 0; | ||||||
| 
 | 
 | ||||||
| 	if (mem->state != from_state_req) { | 	if (mem->state != from_state_req) | ||||||
| 		ret = -EINVAL; | 		return -EINVAL; | ||||||
| 		goto out; |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	if (to_state == MEM_OFFLINE) | 	if (to_state == MEM_OFFLINE) | ||||||
| 		mem->state = MEM_GOING_OFFLINE; | 		mem->state = MEM_GOING_OFFLINE; | ||||||
| 
 | 
 | ||||||
| 	ret = memory_block_action(mem->start_section_nr, to_state, online_type); | 	ret = memory_block_action(mem->start_section_nr, to_state, online_type); | ||||||
| 
 |  | ||||||
| 	if (ret) { | 	if (ret) { | ||||||
| 		mem->state = from_state_req; | 		mem->state = from_state_req; | ||||||
| 		goto out; | 	} else { | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 		mem->state = to_state; | 		mem->state = to_state; | ||||||
|  | 		if (to_state == MEM_ONLINE) | ||||||
|  | 			mem->last_online = online_type; | ||||||
|  | 	} | ||||||
|  | 	return ret; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | static int memory_subsys_online(struct device *dev) | ||||||
|  | { | ||||||
|  | 	struct memory_block *mem = container_of(dev, struct memory_block, dev); | ||||||
|  | 	int ret; | ||||||
|  | 
 | ||||||
|  | 	mutex_lock(&mem->state_mutex); | ||||||
|  | 
 | ||||||
|  | 	ret = mem->state == MEM_ONLINE ? 0 : | ||||||
|  | 		__memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE, | ||||||
|  | 					    mem->last_online); | ||||||
|  | 
 | ||||||
|  | 	mutex_unlock(&mem->state_mutex); | ||||||
|  | 	return ret; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | static int memory_subsys_offline(struct device *dev) | ||||||
|  | { | ||||||
|  | 	struct memory_block *mem = container_of(dev, struct memory_block, dev); | ||||||
|  | 	int ret; | ||||||
|  | 
 | ||||||
|  | 	mutex_lock(&mem->state_mutex); | ||||||
|  | 
 | ||||||
|  | 	ret = mem->state == MEM_OFFLINE ? 0 : | ||||||
|  | 		__memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE, -1); | ||||||
|  | 
 | ||||||
|  | 	mutex_unlock(&mem->state_mutex); | ||||||
|  | 	return ret; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | static int __memory_block_change_state_uevent(struct memory_block *mem, | ||||||
|  | 		unsigned long to_state, unsigned long from_state_req, | ||||||
|  | 		int online_type) | ||||||
|  | { | ||||||
|  | 	int ret = __memory_block_change_state(mem, to_state, from_state_req, | ||||||
|  | 					      online_type); | ||||||
|  | 	if (!ret) { | ||||||
| 		switch (mem->state) { | 		switch (mem->state) { | ||||||
| 		case MEM_OFFLINE: | 		case MEM_OFFLINE: | ||||||
| 			kobject_uevent(&mem->dev.kobj, KOBJ_OFFLINE); | 			kobject_uevent(&mem->dev.kobj, KOBJ_OFFLINE); | ||||||
|  | @ -304,7 +347,7 @@ static int __memory_block_change_state(struct memory_block *mem, | ||||||
| 		default: | 		default: | ||||||
| 			break; | 			break; | ||||||
| 		} | 		} | ||||||
| out: | 	} | ||||||
| 	return ret; | 	return ret; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -315,7 +358,7 @@ static int memory_block_change_state(struct memory_block *mem, | ||||||
| 	int ret; | 	int ret; | ||||||
| 
 | 
 | ||||||
| 	mutex_lock(&mem->state_mutex); | 	mutex_lock(&mem->state_mutex); | ||||||
| 	ret = __memory_block_change_state(mem, to_state, from_state_req, | 	ret = __memory_block_change_state_uevent(mem, to_state, from_state_req, | ||||||
| 						 online_type); | 						 online_type); | ||||||
| 	mutex_unlock(&mem->state_mutex); | 	mutex_unlock(&mem->state_mutex); | ||||||
| 
 | 
 | ||||||
|  | @ -326,22 +369,34 @@ store_mem_state(struct device *dev, | ||||||
| 		struct device_attribute *attr, const char *buf, size_t count) | 		struct device_attribute *attr, const char *buf, size_t count) | ||||||
| { | { | ||||||
| 	struct memory_block *mem; | 	struct memory_block *mem; | ||||||
|  | 	bool offline; | ||||||
| 	int ret = -EINVAL; | 	int ret = -EINVAL; | ||||||
| 
 | 
 | ||||||
| 	mem = container_of(dev, struct memory_block, dev); | 	mem = container_of(dev, struct memory_block, dev); | ||||||
| 
 | 
 | ||||||
| 	if (!strncmp(buf, "online_kernel", min_t(int, count, 13))) | 	lock_device_hotplug(); | ||||||
|  | 
 | ||||||
|  | 	if (!strncmp(buf, "online_kernel", min_t(int, count, 13))) { | ||||||
|  | 		offline = false; | ||||||
| 		ret = memory_block_change_state(mem, MEM_ONLINE, | 		ret = memory_block_change_state(mem, MEM_ONLINE, | ||||||
| 						MEM_OFFLINE, ONLINE_KERNEL); | 						MEM_OFFLINE, ONLINE_KERNEL); | ||||||
| 	else if (!strncmp(buf, "online_movable", min_t(int, count, 14))) | 	} else if (!strncmp(buf, "online_movable", min_t(int, count, 14))) { | ||||||
|  | 		offline = false; | ||||||
| 		ret = memory_block_change_state(mem, MEM_ONLINE, | 		ret = memory_block_change_state(mem, MEM_ONLINE, | ||||||
| 						MEM_OFFLINE, ONLINE_MOVABLE); | 						MEM_OFFLINE, ONLINE_MOVABLE); | ||||||
| 	else if (!strncmp(buf, "online", min_t(int, count, 6))) | 	} else if (!strncmp(buf, "online", min_t(int, count, 6))) { | ||||||
|  | 		offline = false; | ||||||
| 		ret = memory_block_change_state(mem, MEM_ONLINE, | 		ret = memory_block_change_state(mem, MEM_ONLINE, | ||||||
| 						MEM_OFFLINE, ONLINE_KEEP); | 						MEM_OFFLINE, ONLINE_KEEP); | ||||||
| 	else if(!strncmp(buf, "offline", min_t(int, count, 7))) | 	} else if(!strncmp(buf, "offline", min_t(int, count, 7))) { | ||||||
|  | 		offline = true; | ||||||
| 		ret = memory_block_change_state(mem, MEM_OFFLINE, | 		ret = memory_block_change_state(mem, MEM_OFFLINE, | ||||||
| 						MEM_ONLINE, -1); | 						MEM_ONLINE, -1); | ||||||
|  | 	} | ||||||
|  | 	if (!ret) | ||||||
|  | 		dev->offline = offline; | ||||||
|  | 
 | ||||||
|  | 	unlock_device_hotplug(); | ||||||
| 
 | 
 | ||||||
| 	if (ret) | 	if (ret) | ||||||
| 		return ret; | 		return ret; | ||||||
|  | @ -563,6 +618,7 @@ static int init_memory_block(struct memory_block **memory, | ||||||
| 			base_memory_block_id(scn_nr) * sections_per_block; | 			base_memory_block_id(scn_nr) * sections_per_block; | ||||||
| 	mem->end_section_nr = mem->start_section_nr + sections_per_block - 1; | 	mem->end_section_nr = mem->start_section_nr + sections_per_block - 1; | ||||||
| 	mem->state = state; | 	mem->state = state; | ||||||
|  | 	mem->last_online = ONLINE_KEEP; | ||||||
| 	mem->section_count++; | 	mem->section_count++; | ||||||
| 	mutex_init(&mem->state_mutex); | 	mutex_init(&mem->state_mutex); | ||||||
| 	start_pfn = section_nr_to_pfn(mem->start_section_nr); | 	start_pfn = section_nr_to_pfn(mem->start_section_nr); | ||||||
|  | @ -681,14 +737,20 @@ int unregister_memory_section(struct mem_section *section) | ||||||
| 
 | 
 | ||||||
| /*
 | /*
 | ||||||
|  * offline one memory block. If the memory block has been offlined, do nothing. |  * offline one memory block. If the memory block has been offlined, do nothing. | ||||||
|  |  * | ||||||
|  |  * Call under device_hotplug_lock. | ||||||
|  */ |  */ | ||||||
| int offline_memory_block(struct memory_block *mem) | int offline_memory_block(struct memory_block *mem) | ||||||
| { | { | ||||||
| 	int ret = 0; | 	int ret = 0; | ||||||
| 
 | 
 | ||||||
| 	mutex_lock(&mem->state_mutex); | 	mutex_lock(&mem->state_mutex); | ||||||
| 	if (mem->state != MEM_OFFLINE) | 	if (mem->state != MEM_OFFLINE) { | ||||||
| 		ret = __memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE, -1); | 		ret = __memory_block_change_state_uevent(mem, MEM_OFFLINE, | ||||||
|  | 							 MEM_ONLINE, -1); | ||||||
|  | 		if (!ret) | ||||||
|  | 			mem->dev.offline = true; | ||||||
|  | 	} | ||||||
| 	mutex_unlock(&mem->state_mutex); | 	mutex_unlock(&mem->state_mutex); | ||||||
| 
 | 
 | ||||||
| 	return ret; | 	return ret; | ||||||
|  |  | ||||||
|  | @ -26,6 +26,7 @@ struct memory_block { | ||||||
| 	unsigned long start_section_nr; | 	unsigned long start_section_nr; | ||||||
| 	unsigned long end_section_nr; | 	unsigned long end_section_nr; | ||||||
| 	unsigned long state; | 	unsigned long state; | ||||||
|  | 	int last_online; | ||||||
| 	int section_count; | 	int section_count; | ||||||
| 
 | 
 | ||||||
| 	/*
 | 	/*
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Rafael J. Wysocki
				Rafael J. Wysocki