Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drivers/base: Use attribute groups to create sysfs memory files

Update the sysfs memory code to create/delete files at the time of device
and subsystem registration.

The current code creates files in the root memory directory explicitly through
the use of init_* routines. The files for each memory block are created and
deleted explicitly using the mem_[create|delete]_simple_file macros.

This patch creates attribute groups for the memory root files and files in
each memory block directory so that they are created and deleted implicitly
at subsys and device register and unregister time.

This did necessitate moving the register_memory() updating it to set the
dev.groups field.

Signed-off-by: Nathan Fontenot <nfont@linux.vnet.ibm.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Nathan Fontenot and committed by
Greg Kroah-Hartman
96b2c0fc 5b7cb7a1

+62 -81
+62 -81
drivers/base/memory.c
··· 77 77 kfree(mem); 78 78 } 79 79 80 - /* 81 - * register_memory - Setup a sysfs device for a memory block 82 - */ 83 - static 84 - int register_memory(struct memory_block *memory) 85 - { 86 - int error; 87 - 88 - memory->dev.bus = &memory_subsys; 89 - memory->dev.id = memory->start_section_nr / sections_per_block; 90 - memory->dev.release = memory_block_release; 91 - 92 - error = device_register(&memory->dev); 93 - return error; 94 - } 95 - 96 80 unsigned long __weak memory_block_size_bytes(void) 97 81 { 98 82 return MIN_MEMORY_BLOCK_SIZE; ··· 355 371 static DEVICE_ATTR(phys_device, 0444, show_phys_device, NULL); 356 372 static DEVICE_ATTR(removable, 0444, show_mem_removable, NULL); 357 373 358 - #define mem_create_simple_file(mem, attr_name) \ 359 - device_create_file(&mem->dev, &dev_attr_##attr_name) 360 - #define mem_remove_simple_file(mem, attr_name) \ 361 - device_remove_file(&mem->dev, &dev_attr_##attr_name) 362 - 363 374 /* 364 375 * Block size attribute stuff 365 376 */ ··· 366 387 } 367 388 368 389 static DEVICE_ATTR(block_size_bytes, 0444, print_block_size, NULL); 369 - 370 - static int block_size_init(void) 371 - { 372 - return device_create_file(memory_subsys.dev_root, 373 - &dev_attr_block_size_bytes); 374 - } 375 390 376 391 /* 377 392 * Some architectures will have custom drivers to do this, and ··· 402 429 out: 403 430 return ret; 404 431 } 405 - static DEVICE_ATTR(probe, S_IWUSR, NULL, memory_probe_store); 406 432 407 - static int memory_probe_init(void) 408 - { 409 - return device_create_file(memory_subsys.dev_root, &dev_attr_probe); 410 - } 411 - #else 412 - static inline int memory_probe_init(void) 413 - { 414 - return 0; 415 - } 433 + static DEVICE_ATTR(probe, S_IWUSR, NULL, memory_probe_store); 416 434 #endif 417 435 418 436 #ifdef CONFIG_MEMORY_FAILURE ··· 449 485 450 486 static DEVICE_ATTR(soft_offline_page, S_IWUSR, NULL, store_soft_offline_page); 451 487 static DEVICE_ATTR(hard_offline_page, S_IWUSR, NULL, store_hard_offline_page); 452 - 453 - static __init int memory_fail_init(void) 454 - { 455 - int err; 456 - 457 - err = device_create_file(memory_subsys.dev_root, 458 - &dev_attr_soft_offline_page); 459 - if (!err) 460 - err = device_create_file(memory_subsys.dev_root, 461 - &dev_attr_hard_offline_page); 462 - return err; 463 - } 464 - #else 465 - static inline int memory_fail_init(void) 466 - { 467 - return 0; 468 - } 469 488 #endif 470 489 471 490 /* ··· 493 546 return find_memory_block_hinted(section, NULL); 494 547 } 495 548 549 + static struct attribute *memory_memblk_attrs[] = { 550 + &dev_attr_phys_index.attr, 551 + &dev_attr_end_phys_index.attr, 552 + &dev_attr_state.attr, 553 + &dev_attr_phys_device.attr, 554 + &dev_attr_removable.attr, 555 + NULL 556 + }; 557 + 558 + static struct attribute_group memory_memblk_attr_group = { 559 + .attrs = memory_memblk_attrs, 560 + }; 561 + 562 + static const struct attribute_group *memory_memblk_attr_groups[] = { 563 + &memory_memblk_attr_group, 564 + NULL, 565 + }; 566 + 567 + /* 568 + * register_memory - Setup a sysfs device for a memory block 569 + */ 570 + static 571 + int register_memory(struct memory_block *memory) 572 + { 573 + int error; 574 + 575 + memory->dev.bus = &memory_subsys; 576 + memory->dev.id = memory->start_section_nr / sections_per_block; 577 + memory->dev.release = memory_block_release; 578 + memory->dev.groups = memory_memblk_attr_groups; 579 + 580 + error = device_register(&memory->dev); 581 + return error; 582 + } 583 + 496 584 static int init_memory_block(struct memory_block **memory, 497 585 struct mem_section *section, unsigned long state) 498 586 { ··· 551 569 mem->phys_device = arch_get_memory_phys_device(start_pfn); 552 570 553 571 ret = register_memory(mem); 554 - if (!ret) 555 - ret = mem_create_simple_file(mem, phys_index); 556 - if (!ret) 557 - ret = mem_create_simple_file(mem, end_phys_index); 558 - if (!ret) 559 - ret = mem_create_simple_file(mem, state); 560 - if (!ret) 561 - ret = mem_create_simple_file(mem, phys_device); 562 - if (!ret) 563 - ret = mem_create_simple_file(mem, removable); 564 572 565 573 *memory = mem; 566 574 return ret; ··· 628 656 unregister_mem_sect_under_nodes(mem, __section_nr(section)); 629 657 630 658 mem->section_count--; 631 - if (mem->section_count == 0) { 632 - mem_remove_simple_file(mem, phys_index); 633 - mem_remove_simple_file(mem, end_phys_index); 634 - mem_remove_simple_file(mem, state); 635 - mem_remove_simple_file(mem, phys_device); 636 - mem_remove_simple_file(mem, removable); 659 + if (mem->section_count == 0) 637 660 unregister_memory(mem); 638 - } else 661 + else 639 662 kobject_put(&mem->dev.kobj); 640 663 641 664 mutex_unlock(&mem_sysfs_mutex); ··· 667 700 return mem->state == MEM_OFFLINE; 668 701 } 669 702 703 + static struct attribute *memory_root_attrs[] = { 704 + #ifdef CONFIG_ARCH_MEMORY_PROBE 705 + &dev_attr_probe.attr, 706 + #endif 707 + 708 + #ifdef CONFIG_MEMORY_FAILURE 709 + &dev_attr_soft_offline_page.attr, 710 + &dev_attr_hard_offline_page.attr, 711 + #endif 712 + 713 + &dev_attr_block_size_bytes.attr, 714 + NULL 715 + }; 716 + 717 + static struct attribute_group memory_root_attr_group = { 718 + .attrs = memory_root_attrs, 719 + }; 720 + 721 + static const struct attribute_group *memory_root_attr_groups[] = { 722 + &memory_root_attr_group, 723 + NULL, 724 + }; 725 + 670 726 /* 671 727 * Initialize the sysfs support for memory devices... 672 728 */ ··· 701 711 unsigned long block_sz; 702 712 struct memory_block *mem = NULL; 703 713 704 - ret = subsys_system_register(&memory_subsys, NULL); 714 + ret = subsys_system_register(&memory_subsys, memory_root_attr_groups); 705 715 if (ret) 706 716 goto out; 707 717 ··· 724 734 ret = err; 725 735 } 726 736 727 - err = memory_probe_init(); 728 - if (!ret) 729 - ret = err; 730 - err = memory_fail_init(); 731 - if (!ret) 732 - ret = err; 733 - err = block_size_init(); 734 - if (!ret) 735 - ret = err; 736 737 out: 737 738 if (ret) 738 739 printk(KERN_ERR "%s() failed: %d\n", __func__, ret);