aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/base/node.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/base/node.c')
-rw-r--r--drivers/base/node.c196
1 files changed, 173 insertions, 23 deletions
diff --git a/drivers/base/node.c b/drivers/base/node.c
index 1fe5536d404f..70122791683d 100644
--- a/drivers/base/node.c
+++ b/drivers/base/node.c
@@ -173,6 +173,47 @@ static ssize_t node_read_distance(struct sys_device * dev,
173} 173}
174static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL); 174static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL);
175 175
176#ifdef CONFIG_HUGETLBFS
177/*
178 * hugetlbfs per node attributes registration interface:
179 * When/if hugetlb[fs] subsystem initializes [sometime after this module],
180 * it will register its per node attributes for all online nodes with
181 * memory. It will also call register_hugetlbfs_with_node(), below, to
182 * register its attribute registration functions with this node driver.
183 * Once these hooks have been initialized, the node driver will call into
184 * the hugetlb module to [un]register attributes for hot-plugged nodes.
185 */
186static node_registration_func_t __hugetlb_register_node;
187static node_registration_func_t __hugetlb_unregister_node;
188
189static inline bool hugetlb_register_node(struct node *node)
190{
191 if (__hugetlb_register_node &&
192 node_state(node->sysdev.id, N_HIGH_MEMORY)) {
193 __hugetlb_register_node(node);
194 return true;
195 }
196 return false;
197}
198
199static inline void hugetlb_unregister_node(struct node *node)
200{
201 if (__hugetlb_unregister_node)
202 __hugetlb_unregister_node(node);
203}
204
205void register_hugetlbfs_with_node(node_registration_func_t doregister,
206 node_registration_func_t unregister)
207{
208 __hugetlb_register_node = doregister;
209 __hugetlb_unregister_node = unregister;
210}
211#else
212static inline void hugetlb_register_node(struct node *node) {}
213
214static inline void hugetlb_unregister_node(struct node *node) {}
215#endif
216
176 217
177/* 218/*
178 * register_node - Setup a sysfs device for a node. 219 * register_node - Setup a sysfs device for a node.
@@ -196,6 +237,8 @@ int register_node(struct node *node, int num, struct node *parent)
196 sysdev_create_file(&node->sysdev, &attr_distance); 237 sysdev_create_file(&node->sysdev, &attr_distance);
197 238
198 scan_unevictable_register_node(node); 239 scan_unevictable_register_node(node);
240
241 hugetlb_register_node(node);
199 } 242 }
200 return error; 243 return error;
201} 244}
@@ -216,6 +259,7 @@ void unregister_node(struct node *node)
216 sysdev_remove_file(&node->sysdev, &attr_distance); 259 sysdev_remove_file(&node->sysdev, &attr_distance);
217 260
218 scan_unevictable_unregister_node(node); 261 scan_unevictable_unregister_node(node);
262 hugetlb_unregister_node(node); /* no-op, if memoryless node */
219 263
220 sysdev_unregister(&node->sysdev); 264 sysdev_unregister(&node->sysdev);
221} 265}
@@ -227,26 +271,43 @@ struct node node_devices[MAX_NUMNODES];
227 */ 271 */
228int register_cpu_under_node(unsigned int cpu, unsigned int nid) 272int register_cpu_under_node(unsigned int cpu, unsigned int nid)
229{ 273{
230 if (node_online(nid)) { 274 int ret;
231 struct sys_device *obj = get_cpu_sysdev(cpu); 275 struct sys_device *obj;
232 if (!obj)
233 return 0;
234 return sysfs_create_link(&node_devices[nid].sysdev.kobj,
235 &obj->kobj,
236 kobject_name(&obj->kobj));
237 }
238 276
239 return 0; 277 if (!node_online(nid))
278 return 0;
279
280 obj = get_cpu_sysdev(cpu);
281 if (!obj)
282 return 0;
283
284 ret = sysfs_create_link(&node_devices[nid].sysdev.kobj,
285 &obj->kobj,
286 kobject_name(&obj->kobj));
287 if (ret)
288 return ret;
289
290 return sysfs_create_link(&obj->kobj,
291 &node_devices[nid].sysdev.kobj,
292 kobject_name(&node_devices[nid].sysdev.kobj));
240} 293}
241 294
242int unregister_cpu_under_node(unsigned int cpu, unsigned int nid) 295int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
243{ 296{
244 if (node_online(nid)) { 297 struct sys_device *obj;
245 struct sys_device *obj = get_cpu_sysdev(cpu); 298
246 if (obj) 299 if (!node_online(nid))
247 sysfs_remove_link(&node_devices[nid].sysdev.kobj, 300 return 0;
248 kobject_name(&obj->kobj)); 301
249 } 302 obj = get_cpu_sysdev(cpu);
303 if (!obj)
304 return 0;
305
306 sysfs_remove_link(&node_devices[nid].sysdev.kobj,
307 kobject_name(&obj->kobj));
308 sysfs_remove_link(&obj->kobj,
309 kobject_name(&node_devices[nid].sysdev.kobj));
310
250 return 0; 311 return 0;
251} 312}
252 313
@@ -268,6 +329,7 @@ static int get_nid_for_pfn(unsigned long pfn)
268/* register memory section under specified node if it spans that node */ 329/* register memory section under specified node if it spans that node */
269int register_mem_sect_under_node(struct memory_block *mem_blk, int nid) 330int register_mem_sect_under_node(struct memory_block *mem_blk, int nid)
270{ 331{
332 int ret;
271 unsigned long pfn, sect_start_pfn, sect_end_pfn; 333 unsigned long pfn, sect_start_pfn, sect_end_pfn;
272 334
273 if (!mem_blk) 335 if (!mem_blk)
@@ -284,9 +346,15 @@ int register_mem_sect_under_node(struct memory_block *mem_blk, int nid)
284 continue; 346 continue;
285 if (page_nid != nid) 347 if (page_nid != nid)
286 continue; 348 continue;
287 return sysfs_create_link_nowarn(&node_devices[nid].sysdev.kobj, 349 ret = sysfs_create_link_nowarn(&node_devices[nid].sysdev.kobj,
288 &mem_blk->sysdev.kobj, 350 &mem_blk->sysdev.kobj,
289 kobject_name(&mem_blk->sysdev.kobj)); 351 kobject_name(&mem_blk->sysdev.kobj));
352 if (ret)
353 return ret;
354
355 return sysfs_create_link_nowarn(&mem_blk->sysdev.kobj,
356 &node_devices[nid].sysdev.kobj,
357 kobject_name(&node_devices[nid].sysdev.kobj));
290 } 358 }
291 /* mem section does not span the specified node */ 359 /* mem section does not span the specified node */
292 return 0; 360 return 0;
@@ -295,12 +363,16 @@ int register_mem_sect_under_node(struct memory_block *mem_blk, int nid)
295/* unregister memory section under all nodes that it spans */ 363/* unregister memory section under all nodes that it spans */
296int unregister_mem_sect_under_nodes(struct memory_block *mem_blk) 364int unregister_mem_sect_under_nodes(struct memory_block *mem_blk)
297{ 365{
298 nodemask_t unlinked_nodes; 366 NODEMASK_ALLOC(nodemask_t, unlinked_nodes, GFP_KERNEL);
299 unsigned long pfn, sect_start_pfn, sect_end_pfn; 367 unsigned long pfn, sect_start_pfn, sect_end_pfn;
300 368
301 if (!mem_blk) 369 if (!mem_blk) {
370 NODEMASK_FREE(unlinked_nodes);
302 return -EFAULT; 371 return -EFAULT;
303 nodes_clear(unlinked_nodes); 372 }
373 if (!unlinked_nodes)
374 return -ENOMEM;
375 nodes_clear(*unlinked_nodes);
304 sect_start_pfn = section_nr_to_pfn(mem_blk->phys_index); 376 sect_start_pfn = section_nr_to_pfn(mem_blk->phys_index);
305 sect_end_pfn = sect_start_pfn + PAGES_PER_SECTION - 1; 377 sect_end_pfn = sect_start_pfn + PAGES_PER_SECTION - 1;
306 for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) { 378 for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) {
@@ -311,11 +383,14 @@ int unregister_mem_sect_under_nodes(struct memory_block *mem_blk)
311 continue; 383 continue;
312 if (!node_online(nid)) 384 if (!node_online(nid))
313 continue; 385 continue;
314 if (node_test_and_set(nid, unlinked_nodes)) 386 if (node_test_and_set(nid, *unlinked_nodes))
315 continue; 387 continue;
316 sysfs_remove_link(&node_devices[nid].sysdev.kobj, 388 sysfs_remove_link(&node_devices[nid].sysdev.kobj,
317 kobject_name(&mem_blk->sysdev.kobj)); 389 kobject_name(&mem_blk->sysdev.kobj));
390 sysfs_remove_link(&mem_blk->sysdev.kobj,
391 kobject_name(&node_devices[nid].sysdev.kobj));
318 } 392 }
393 NODEMASK_FREE(unlinked_nodes);
319 return 0; 394 return 0;
320} 395}
321 396
@@ -345,9 +420,77 @@ static int link_mem_sections(int nid)
345 } 420 }
346 return err; 421 return err;
347} 422}
348#else 423
424#ifdef CONFIG_HUGETLBFS
425/*
426 * Handle per node hstate attribute [un]registration on transistions
427 * to/from memoryless state.
428 */
429static void node_hugetlb_work(struct work_struct *work)
430{
431 struct node *node = container_of(work, struct node, node_work);
432
433 /*
434 * We only get here when a node transitions to/from memoryless state.
435 * We can detect which transition occurred by examining whether the
436 * node has memory now. hugetlb_register_node() already check this
437 * so we try to register the attributes. If that fails, then the
438 * node has transitioned to memoryless, try to unregister the
439 * attributes.
440 */
441 if (!hugetlb_register_node(node))
442 hugetlb_unregister_node(node);
443}
444
445static void init_node_hugetlb_work(int nid)
446{
447 INIT_WORK(&node_devices[nid].node_work, node_hugetlb_work);
448}
449
450static int node_memory_callback(struct notifier_block *self,
451 unsigned long action, void *arg)
452{
453 struct memory_notify *mnb = arg;
454 int nid = mnb->status_change_nid;
455
456 switch (action) {
457 case MEM_ONLINE:
458 case MEM_OFFLINE:
459 /*
460 * offload per node hstate [un]registration to a work thread
461 * when transitioning to/from memoryless state.
462 */
463 if (nid != NUMA_NO_NODE)
464 schedule_work(&node_devices[nid].node_work);
465 break;
466
467 case MEM_GOING_ONLINE:
468 case MEM_GOING_OFFLINE:
469 case MEM_CANCEL_ONLINE:
470 case MEM_CANCEL_OFFLINE:
471 default:
472 break;
473 }
474
475 return NOTIFY_OK;
476}
477#endif /* CONFIG_HUGETLBFS */
478#else /* !CONFIG_MEMORY_HOTPLUG_SPARSE */
479
349static int link_mem_sections(int nid) { return 0; } 480static int link_mem_sections(int nid) { return 0; }
350#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */ 481#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
482
483#if !defined(CONFIG_MEMORY_HOTPLUG_SPARSE) || \
484 !defined(CONFIG_HUGETLBFS)
485static inline int node_memory_callback(struct notifier_block *self,
486 unsigned long action, void *arg)
487{
488 return NOTIFY_OK;
489}
490
491static void init_node_hugetlb_work(int nid) { }
492
493#endif
351 494
352int register_one_node(int nid) 495int register_one_node(int nid)
353{ 496{
@@ -371,6 +514,9 @@ int register_one_node(int nid)
371 514
372 /* link memory sections under this node */ 515 /* link memory sections under this node */
373 error = link_mem_sections(nid); 516 error = link_mem_sections(nid);
517
518 /* initialize work queue for memory hot plug */
519 init_node_hugetlb_work(nid);
374 } 520 }
375 521
376 return error; 522 return error;
@@ -460,13 +606,17 @@ static int node_states_init(void)
460 return err; 606 return err;
461} 607}
462 608
609#define NODE_CALLBACK_PRI 2 /* lower than SLAB */
463static int __init register_node_type(void) 610static int __init register_node_type(void)
464{ 611{
465 int ret; 612 int ret;
466 613
467 ret = sysdev_class_register(&node_class); 614 ret = sysdev_class_register(&node_class);
468 if (!ret) 615 if (!ret) {
469 ret = node_states_init(); 616 ret = node_states_init();
617 hotplug_memory_notifier(node_memory_callback,
618 NODE_CALLBACK_PRI);
619 }
470 620
471 /* 621 /*
472 * Note: we're not going to unregister the node class if we fail 622 * Note: we're not going to unregister the node class if we fail