linux/drivers/base/memory.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Memory subsystem support
   4 *
   5 * Written by Matt Tolentino <matthew.e.tolentino@intel.com>
   6 *            Dave Hansen <haveblue@us.ibm.com>
   7 *
   8 * This file provides the necessary infrastructure to represent
   9 * a SPARSEMEM-memory-model system's physical memory in /sysfs.
  10 * All arch-independent code that assumes MEMORY_HOTPLUG requires
  11 * SPARSEMEM should be contained here, or in mm/memory_hotplug.c.
  12 */
  13
  14#include <linux/module.h>
  15#include <linux/init.h>
  16#include <linux/topology.h>
  17#include <linux/capability.h>
  18#include <linux/device.h>
  19#include <linux/memory.h>
  20#include <linux/memory_hotplug.h>
  21#include <linux/mm.h>
  22#include <linux/stat.h>
  23#include <linux/slab.h>
  24#include <linux/xarray.h>
  25
  26#include <linux/atomic.h>
  27#include <linux/uaccess.h>
  28
  29#define MEMORY_CLASS_NAME       "memory"
  30
  31#define to_memory_block(dev) container_of(dev, struct memory_block, dev)
  32
  33static int sections_per_block;
  34
  35static inline unsigned long base_memory_block_id(unsigned long section_nr)
  36{
  37        return section_nr / sections_per_block;
  38}
  39
  40static inline unsigned long pfn_to_block_id(unsigned long pfn)
  41{
  42        return base_memory_block_id(pfn_to_section_nr(pfn));
  43}
  44
  45static inline unsigned long phys_to_block_id(unsigned long phys)
  46{
  47        return pfn_to_block_id(PFN_DOWN(phys));
  48}
  49
  50static int memory_subsys_online(struct device *dev);
  51static int memory_subsys_offline(struct device *dev);
  52
  53static struct bus_type memory_subsys = {
  54        .name = MEMORY_CLASS_NAME,
  55        .dev_name = MEMORY_CLASS_NAME,
  56        .online = memory_subsys_online,
  57        .offline = memory_subsys_offline,
  58};
  59
  60/*
  61 * Memory blocks are cached in a local radix tree to avoid
  62 * a costly linear search for the corresponding device on
  63 * the subsystem bus.
  64 */
  65static DEFINE_XARRAY(memory_blocks);
  66
  67static BLOCKING_NOTIFIER_HEAD(memory_chain);
  68
  69int register_memory_notifier(struct notifier_block *nb)
  70{
  71        return blocking_notifier_chain_register(&memory_chain, nb);
  72}
  73EXPORT_SYMBOL(register_memory_notifier);
  74
  75void unregister_memory_notifier(struct notifier_block *nb)
  76{
  77        blocking_notifier_chain_unregister(&memory_chain, nb);
  78}
  79EXPORT_SYMBOL(unregister_memory_notifier);
  80
  81static ATOMIC_NOTIFIER_HEAD(memory_isolate_chain);
  82
  83int register_memory_isolate_notifier(struct notifier_block *nb)
  84{
  85        return atomic_notifier_chain_register(&memory_isolate_chain, nb);
  86}
  87EXPORT_SYMBOL(register_memory_isolate_notifier);
  88
  89void unregister_memory_isolate_notifier(struct notifier_block *nb)
  90{
  91        atomic_notifier_chain_unregister(&memory_isolate_chain, nb);
  92}
  93EXPORT_SYMBOL(unregister_memory_isolate_notifier);
  94
  95static void memory_block_release(struct device *dev)
  96{
  97        struct memory_block *mem = to_memory_block(dev);
  98
  99        kfree(mem);
 100}
 101
 102unsigned long __weak memory_block_size_bytes(void)
 103{
 104        return MIN_MEMORY_BLOCK_SIZE;
 105}
 106
 107/*
 108 * use this as the physical section index that this memsection
 109 * uses.
 110 */
 111
 112static ssize_t phys_index_show(struct device *dev,
 113                               struct device_attribute *attr, char *buf)
 114{
 115        struct memory_block *mem = to_memory_block(dev);
 116        unsigned long phys_index;
 117
 118        phys_index = mem->start_section_nr / sections_per_block;
 119        return sprintf(buf, "%08lx\n", phys_index);
 120}
 121
 122/*
 123 * Show whether the section of memory is likely to be hot-removable
 124 */
 125static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
 126                              char *buf)
 127{
 128        struct memory_block *mem = to_memory_block(dev);
 129        unsigned long pfn;
 130        int ret = 1, i;
 131
 132        if (mem->state != MEM_ONLINE)
 133                goto out;
 134
 135        for (i = 0; i < sections_per_block; i++) {
 136                if (!present_section_nr(mem->start_section_nr + i))
 137                        continue;
 138                pfn = section_nr_to_pfn(mem->start_section_nr + i);
 139                ret &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
 140        }
 141
 142out:
 143        return sprintf(buf, "%d\n", ret);
 144}
 145
 146/*
 147 * online, offline, going offline, etc.
 148 */
 149static ssize_t state_show(struct device *dev, struct device_attribute *attr,
 150                          char *buf)
 151{
 152        struct memory_block *mem = to_memory_block(dev);
 153        ssize_t len = 0;
 154
 155        /*
 156         * We can probably put these states in a nice little array
 157         * so that they're not open-coded
 158         */
 159        switch (mem->state) {
 160        case MEM_ONLINE:
 161                len = sprintf(buf, "online\n");
 162                break;
 163        case MEM_OFFLINE:
 164                len = sprintf(buf, "offline\n");
 165                break;
 166        case MEM_GOING_OFFLINE:
 167                len = sprintf(buf, "going-offline\n");
 168                break;
 169        default:
 170                len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
 171                                mem->state);
 172                WARN_ON(1);
 173                break;
 174        }
 175
 176        return len;
 177}
 178
 179int memory_notify(unsigned long val, void *v)
 180{
 181        return blocking_notifier_call_chain(&memory_chain, val, v);
 182}
 183
 184int memory_isolate_notify(unsigned long val, void *v)
 185{
 186        return atomic_notifier_call_chain(&memory_isolate_chain, val, v);
 187}
 188
 189/*
 190 * The probe routines leave the pages uninitialized, just as the bootmem code
 191 * does. Make sure we do not access them, but instead use only information from
 192 * within sections.
 193 */
 194static bool pages_correctly_probed(unsigned long start_pfn)
 195{
 196        unsigned long section_nr = pfn_to_section_nr(start_pfn);
 197        unsigned long section_nr_end = section_nr + sections_per_block;
 198        unsigned long pfn = start_pfn;
 199
 200        /*
 201         * memmap between sections is not contiguous except with
 202         * SPARSEMEM_VMEMMAP. We lookup the page once per section
 203         * and assume memmap is contiguous within each section
 204         */
 205        for (; section_nr < section_nr_end; section_nr++) {
 206                if (WARN_ON_ONCE(!pfn_valid(pfn)))
 207                        return false;
 208
 209                if (!present_section_nr(section_nr)) {
 210                        pr_warn("section %ld pfn[%lx, %lx) not present",
 211                                section_nr, pfn, pfn + PAGES_PER_SECTION);
 212                        return false;
 213                } else if (!valid_section_nr(section_nr)) {
 214                        pr_warn("section %ld pfn[%lx, %lx) no valid memmap",
 215                                section_nr, pfn, pfn + PAGES_PER_SECTION);
 216                        return false;
 217                } else if (online_section_nr(section_nr)) {
 218                        pr_warn("section %ld pfn[%lx, %lx) is already online",
 219                                section_nr, pfn, pfn + PAGES_PER_SECTION);
 220                        return false;
 221                }
 222                pfn += PAGES_PER_SECTION;
 223        }
 224
 225        return true;
 226}
 227
 228/*
 229 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
 230 * OK to have direct references to sparsemem variables in here.
 231 */
 232static int
 233memory_block_action(unsigned long start_section_nr, unsigned long action,
 234                    int online_type)
 235{
 236        unsigned long start_pfn;
 237        unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
 238        int ret;
 239
 240        start_pfn = section_nr_to_pfn(start_section_nr);
 241
 242        switch (action) {
 243        case MEM_ONLINE:
 244                if (!pages_correctly_probed(start_pfn))
 245                        return -EBUSY;
 246
 247                ret = online_pages(start_pfn, nr_pages, online_type);
 248                break;
 249        case MEM_OFFLINE:
 250                ret = offline_pages(start_pfn, nr_pages);
 251                break;
 252        default:
 253                WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: "
 254                     "%ld\n", __func__, start_section_nr, action, action);
 255                ret = -EINVAL;
 256        }
 257
 258        return ret;
 259}
 260
 261static int memory_block_change_state(struct memory_block *mem,
 262                unsigned long to_state, unsigned long from_state_req)
 263{
 264        int ret = 0;
 265
 266        if (mem->state != from_state_req)
 267                return -EINVAL;
 268
 269        if (to_state == MEM_OFFLINE)
 270                mem->state = MEM_GOING_OFFLINE;
 271
 272        ret = memory_block_action(mem->start_section_nr, to_state,
 273                                mem->online_type);
 274
 275        mem->state = ret ? from_state_req : to_state;
 276
 277        return ret;
 278}
 279
 280/* The device lock serializes operations on memory_subsys_[online|offline] */
 281static int memory_subsys_online(struct device *dev)
 282{
 283        struct memory_block *mem = to_memory_block(dev);
 284        int ret;
 285
 286        if (mem->state == MEM_ONLINE)
 287                return 0;
 288
 289        /*
 290         * If we are called from state_store(), online_type will be
 291         * set >= 0 Otherwise we were called from the device online
 292         * attribute and need to set the online_type.
 293         */
 294        if (mem->online_type < 0)
 295                mem->online_type = MMOP_ONLINE_KEEP;
 296
 297        ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
 298
 299        /* clear online_type */
 300        mem->online_type = -1;
 301
 302        return ret;
 303}
 304
 305static int memory_subsys_offline(struct device *dev)
 306{
 307        struct memory_block *mem = to_memory_block(dev);
 308
 309        if (mem->state == MEM_OFFLINE)
 310                return 0;
 311
 312        /* Can't offline block with non-present sections */
 313        if (mem->section_count != sections_per_block)
 314                return -EINVAL;
 315
 316        return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
 317}
 318
 319static ssize_t state_store(struct device *dev, struct device_attribute *attr,
 320                           const char *buf, size_t count)
 321{
 322        struct memory_block *mem = to_memory_block(dev);
 323        int ret, online_type;
 324
 325        ret = lock_device_hotplug_sysfs();
 326        if (ret)
 327                return ret;
 328
 329        if (sysfs_streq(buf, "online_kernel"))
 330                online_type = MMOP_ONLINE_KERNEL;
 331        else if (sysfs_streq(buf, "online_movable"))
 332                online_type = MMOP_ONLINE_MOVABLE;
 333        else if (sysfs_streq(buf, "online"))
 334                online_type = MMOP_ONLINE_KEEP;
 335        else if (sysfs_streq(buf, "offline"))
 336                online_type = MMOP_OFFLINE;
 337        else {
 338                ret = -EINVAL;
 339                goto err;
 340        }
 341
 342        switch (online_type) {
 343        case MMOP_ONLINE_KERNEL:
 344        case MMOP_ONLINE_MOVABLE:
 345        case MMOP_ONLINE_KEEP:
 346                /* mem->online_type is protected by device_hotplug_lock */
 347                mem->online_type = online_type;
 348                ret = device_online(&mem->dev);
 349                break;
 350        case MMOP_OFFLINE:
 351                ret = device_offline(&mem->dev);
 352                break;
 353        default:
 354                ret = -EINVAL; /* should never happen */
 355        }
 356
 357err:
 358        unlock_device_hotplug();
 359
 360        if (ret < 0)
 361                return ret;
 362        if (ret)
 363                return -EINVAL;
 364
 365        return count;
 366}
 367
 368/*
 369 * phys_device is a bad name for this.  What I really want
 370 * is a way to differentiate between memory ranges that
 371 * are part of physical devices that constitute
 372 * a complete removable unit or fru.
 373 * i.e. do these ranges belong to the same physical device,
 374 * s.t. if I offline all of these sections I can then
 375 * remove the physical device?
 376 */
 377static ssize_t phys_device_show(struct device *dev,
 378                                struct device_attribute *attr, char *buf)
 379{
 380        struct memory_block *mem = to_memory_block(dev);
 381        return sprintf(buf, "%d\n", mem->phys_device);
 382}
 383
 384#ifdef CONFIG_MEMORY_HOTREMOVE
 385static void print_allowed_zone(char *buf, int nid, unsigned long start_pfn,
 386                unsigned long nr_pages, int online_type,
 387                struct zone *default_zone)
 388{
 389        struct zone *zone;
 390
 391        zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages);
 392        if (zone != default_zone) {
 393                strcat(buf, " ");
 394                strcat(buf, zone->name);
 395        }
 396}
 397
 398static ssize_t valid_zones_show(struct device *dev,
 399                                struct device_attribute *attr, char *buf)
 400{
 401        struct memory_block *mem = to_memory_block(dev);
 402        unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
 403        unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
 404        unsigned long valid_start_pfn, valid_end_pfn;
 405        struct zone *default_zone;
 406        int nid;
 407
 408        /*
 409         * Check the existing zone. Make sure that we do that only on the
 410         * online nodes otherwise the page_zone is not reliable
 411         */
 412        if (mem->state == MEM_ONLINE) {
 413                /*
 414                 * The block contains more than one zone can not be offlined.
 415                 * This can happen e.g. for ZONE_DMA and ZONE_DMA32
 416                 */
 417                if (!test_pages_in_a_zone(start_pfn, start_pfn + nr_pages,
 418                                          &valid_start_pfn, &valid_end_pfn))
 419                        return sprintf(buf, "none\n");
 420                start_pfn = valid_start_pfn;
 421                strcat(buf, page_zone(pfn_to_page(start_pfn))->name);
 422                goto out;
 423        }
 424
 425        nid = mem->nid;
 426        default_zone = zone_for_pfn_range(MMOP_ONLINE_KEEP, nid, start_pfn, nr_pages);
 427        strcat(buf, default_zone->name);
 428
 429        print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_KERNEL,
 430                        default_zone);
 431        print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_MOVABLE,
 432                        default_zone);
 433out:
 434        strcat(buf, "\n");
 435
 436        return strlen(buf);
 437}
 438static DEVICE_ATTR_RO(valid_zones);
 439#endif
 440
 441static DEVICE_ATTR_RO(phys_index);
 442static DEVICE_ATTR_RW(state);
 443static DEVICE_ATTR_RO(phys_device);
 444static DEVICE_ATTR_RO(removable);
 445
 446/*
 447 * Block size attribute stuff
 448 */
 449static ssize_t block_size_bytes_show(struct device *dev,
 450                                     struct device_attribute *attr, char *buf)
 451{
 452        return sprintf(buf, "%lx\n", memory_block_size_bytes());
 453}
 454
 455static DEVICE_ATTR_RO(block_size_bytes);
 456
 457/*
 458 * Memory auto online policy.
 459 */
 460
 461static ssize_t auto_online_blocks_show(struct device *dev,
 462                                       struct device_attribute *attr, char *buf)
 463{
 464        if (memhp_auto_online)
 465                return sprintf(buf, "online\n");
 466        else
 467                return sprintf(buf, "offline\n");
 468}
 469
 470static ssize_t auto_online_blocks_store(struct device *dev,
 471                                        struct device_attribute *attr,
 472                                        const char *buf, size_t count)
 473{
 474        if (sysfs_streq(buf, "online"))
 475                memhp_auto_online = true;
 476        else if (sysfs_streq(buf, "offline"))
 477                memhp_auto_online = false;
 478        else
 479                return -EINVAL;
 480
 481        return count;
 482}
 483
 484static DEVICE_ATTR_RW(auto_online_blocks);
 485
 486/*
 487 * Some architectures will have custom drivers to do this, and
 488 * will not need to do it from userspace.  The fake hot-add code
 489 * as well as ppc64 will do all of their discovery in userspace
 490 * and will require this interface.
 491 */
 492#ifdef CONFIG_ARCH_MEMORY_PROBE
 493static ssize_t probe_store(struct device *dev, struct device_attribute *attr,
 494                           const char *buf, size_t count)
 495{
 496        u64 phys_addr;
 497        int nid, ret;
 498        unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
 499
 500        ret = kstrtoull(buf, 0, &phys_addr);
 501        if (ret)
 502                return ret;
 503
 504        if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
 505                return -EINVAL;
 506
 507        ret = lock_device_hotplug_sysfs();
 508        if (ret)
 509                return ret;
 510
 511        nid = memory_add_physaddr_to_nid(phys_addr);
 512        ret = __add_memory(nid, phys_addr,
 513                           MIN_MEMORY_BLOCK_SIZE * sections_per_block);
 514
 515        if (ret)
 516                goto out;
 517
 518        ret = count;
 519out:
 520        unlock_device_hotplug();
 521        return ret;
 522}
 523
 524static DEVICE_ATTR_WO(probe);
 525#endif
 526
 527#ifdef CONFIG_MEMORY_FAILURE
 528/*
 529 * Support for offlining pages of memory
 530 */
 531
 532/* Soft offline a page */
 533static ssize_t soft_offline_page_store(struct device *dev,
 534                                       struct device_attribute *attr,
 535                                       const char *buf, size_t count)
 536{
 537        int ret;
 538        u64 pfn;
 539        if (!capable(CAP_SYS_ADMIN))
 540                return -EPERM;
 541        if (kstrtoull(buf, 0, &pfn) < 0)
 542                return -EINVAL;
 543        pfn >>= PAGE_SHIFT;
 544        if (!pfn_valid(pfn))
 545                return -ENXIO;
 546        /* Only online pages can be soft-offlined (esp., not ZONE_DEVICE). */
 547        if (!pfn_to_online_page(pfn))
 548                return -EIO;
 549        ret = soft_offline_page(pfn_to_page(pfn), 0);
 550        return ret == 0 ? count : ret;
 551}
 552
 553/* Forcibly offline a page, including killing processes. */
 554static ssize_t hard_offline_page_store(struct device *dev,
 555                                       struct device_attribute *attr,
 556                                       const char *buf, size_t count)
 557{
 558        int ret;
 559        u64 pfn;
 560        if (!capable(CAP_SYS_ADMIN))
 561                return -EPERM;
 562        if (kstrtoull(buf, 0, &pfn) < 0)
 563                return -EINVAL;
 564        pfn >>= PAGE_SHIFT;
 565        ret = memory_failure(pfn, 0);
 566        return ret ? ret : count;
 567}
 568
 569static DEVICE_ATTR_WO(soft_offline_page);
 570static DEVICE_ATTR_WO(hard_offline_page);
 571#endif
 572
 573/*
 574 * Note that phys_device is optional.  It is here to allow for
 575 * differentiation between which *physical* devices each
 576 * section belongs to...
 577 */
 578int __weak arch_get_memory_phys_device(unsigned long start_pfn)
 579{
 580        return 0;
 581}
 582
 583/*
 584 * A reference for the returned memory block device is acquired.
 585 *
 586 * Called under device_hotplug_lock.
 587 */
 588static struct memory_block *find_memory_block_by_id(unsigned long block_id)
 589{
 590        struct memory_block *mem;
 591
 592        mem = xa_load(&memory_blocks, block_id);
 593        if (mem)
 594                get_device(&mem->dev);
 595        return mem;
 596}
 597
 598/*
 599 * Called under device_hotplug_lock.
 600 */
 601struct memory_block *find_memory_block(struct mem_section *section)
 602{
 603        unsigned long block_id = base_memory_block_id(__section_nr(section));
 604
 605        return find_memory_block_by_id(block_id);
 606}
 607
 608static struct attribute *memory_memblk_attrs[] = {
 609        &dev_attr_phys_index.attr,
 610        &dev_attr_state.attr,
 611        &dev_attr_phys_device.attr,
 612        &dev_attr_removable.attr,
 613#ifdef CONFIG_MEMORY_HOTREMOVE
 614        &dev_attr_valid_zones.attr,
 615#endif
 616        NULL
 617};
 618
 619static struct attribute_group memory_memblk_attr_group = {
 620        .attrs = memory_memblk_attrs,
 621};
 622
 623static const struct attribute_group *memory_memblk_attr_groups[] = {
 624        &memory_memblk_attr_group,
 625        NULL,
 626};
 627
 628/*
 629 * register_memory - Setup a sysfs device for a memory block
 630 */
 631static
 632int register_memory(struct memory_block *memory)
 633{
 634        int ret;
 635
 636        memory->dev.bus = &memory_subsys;
 637        memory->dev.id = memory->start_section_nr / sections_per_block;
 638        memory->dev.release = memory_block_release;
 639        memory->dev.groups = memory_memblk_attr_groups;
 640        memory->dev.offline = memory->state == MEM_OFFLINE;
 641
 642        ret = device_register(&memory->dev);
 643        if (ret) {
 644                put_device(&memory->dev);
 645                return ret;
 646        }
 647        ret = xa_err(xa_store(&memory_blocks, memory->dev.id, memory,
 648                              GFP_KERNEL));
 649        if (ret) {
 650                put_device(&memory->dev);
 651                device_unregister(&memory->dev);
 652        }
 653        return ret;
 654}
 655
 656static int init_memory_block(struct memory_block **memory,
 657                             unsigned long block_id, unsigned long state)
 658{
 659        struct memory_block *mem;
 660        unsigned long start_pfn;
 661        int ret = 0;
 662
 663        mem = find_memory_block_by_id(block_id);
 664        if (mem) {
 665                put_device(&mem->dev);
 666                return -EEXIST;
 667        }
 668        mem = kzalloc(sizeof(*mem), GFP_KERNEL);
 669        if (!mem)
 670                return -ENOMEM;
 671
 672        mem->start_section_nr = block_id * sections_per_block;
 673        mem->end_section_nr = mem->start_section_nr + sections_per_block - 1;
 674        mem->state = state;
 675        start_pfn = section_nr_to_pfn(mem->start_section_nr);
 676        mem->phys_device = arch_get_memory_phys_device(start_pfn);
 677        mem->nid = NUMA_NO_NODE;
 678
 679        ret = register_memory(mem);
 680
 681        *memory = mem;
 682        return ret;
 683}
 684
 685static int add_memory_block(unsigned long base_section_nr)
 686{
 687        int ret, section_count = 0;
 688        struct memory_block *mem;
 689        unsigned long nr;
 690
 691        for (nr = base_section_nr; nr < base_section_nr + sections_per_block;
 692             nr++)
 693                if (present_section_nr(nr))
 694                        section_count++;
 695
 696        if (section_count == 0)
 697                return 0;
 698        ret = init_memory_block(&mem, base_memory_block_id(base_section_nr),
 699                                MEM_ONLINE);
 700        if (ret)
 701                return ret;
 702        mem->section_count = section_count;
 703        return 0;
 704}
 705
 706static void unregister_memory(struct memory_block *memory)
 707{
 708        if (WARN_ON_ONCE(memory->dev.bus != &memory_subsys))
 709                return;
 710
 711        WARN_ON(xa_erase(&memory_blocks, memory->dev.id) == NULL);
 712
 713        /* drop the ref. we got via find_memory_block() */
 714        put_device(&memory->dev);
 715        device_unregister(&memory->dev);
 716}
 717
 718/*
 719 * Create memory block devices for the given memory area. Start and size
 720 * have to be aligned to memory block granularity. Memory block devices
 721 * will be initialized as offline.
 722 *
 723 * Called under device_hotplug_lock.
 724 */
 725int create_memory_block_devices(unsigned long start, unsigned long size)
 726{
 727        const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
 728        unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
 729        struct memory_block *mem;
 730        unsigned long block_id;
 731        int ret = 0;
 732
 733        if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
 734                         !IS_ALIGNED(size, memory_block_size_bytes())))
 735                return -EINVAL;
 736
 737        for (block_id = start_block_id; block_id != end_block_id; block_id++) {
 738                ret = init_memory_block(&mem, block_id, MEM_OFFLINE);
 739                if (ret)
 740                        break;
 741                mem->section_count = sections_per_block;
 742        }
 743        if (ret) {
 744                end_block_id = block_id;
 745                for (block_id = start_block_id; block_id != end_block_id;
 746                     block_id++) {
 747                        mem = find_memory_block_by_id(block_id);
 748                        if (WARN_ON_ONCE(!mem))
 749                                continue;
 750                        mem->section_count = 0;
 751                        unregister_memory(mem);
 752                }
 753        }
 754        return ret;
 755}
 756
 757/*
 758 * Remove memory block devices for the given memory area. Start and size
 759 * have to be aligned to memory block granularity. Memory block devices
 760 * have to be offline.
 761 *
 762 * Called under device_hotplug_lock.
 763 */
 764void remove_memory_block_devices(unsigned long start, unsigned long size)
 765{
 766        const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
 767        const unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
 768        struct memory_block *mem;
 769        unsigned long block_id;
 770
 771        if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
 772                         !IS_ALIGNED(size, memory_block_size_bytes())))
 773                return;
 774
 775        for (block_id = start_block_id; block_id != end_block_id; block_id++) {
 776                mem = find_memory_block_by_id(block_id);
 777                if (WARN_ON_ONCE(!mem))
 778                        continue;
 779                mem->section_count = 0;
 780                unregister_memory_block_under_nodes(mem);
 781                unregister_memory(mem);
 782        }
 783}
 784
 785/* return true if the memory block is offlined, otherwise, return false */
 786bool is_memblock_offlined(struct memory_block *mem)
 787{
 788        return mem->state == MEM_OFFLINE;
 789}
 790
 791static struct attribute *memory_root_attrs[] = {
 792#ifdef CONFIG_ARCH_MEMORY_PROBE
 793        &dev_attr_probe.attr,
 794#endif
 795
 796#ifdef CONFIG_MEMORY_FAILURE
 797        &dev_attr_soft_offline_page.attr,
 798        &dev_attr_hard_offline_page.attr,
 799#endif
 800
 801        &dev_attr_block_size_bytes.attr,
 802        &dev_attr_auto_online_blocks.attr,
 803        NULL
 804};
 805
 806static struct attribute_group memory_root_attr_group = {
 807        .attrs = memory_root_attrs,
 808};
 809
 810static const struct attribute_group *memory_root_attr_groups[] = {
 811        &memory_root_attr_group,
 812        NULL,
 813};
 814
 815/*
 816 * Initialize the sysfs support for memory devices. At the time this function
 817 * is called, we cannot have concurrent creation/deletion of memory block
 818 * devices, the device_hotplug_lock is not needed.
 819 */
 820void __init memory_dev_init(void)
 821{
 822        int ret;
 823        unsigned long block_sz, nr;
 824
 825        /* Validate the configured memory block size */
 826        block_sz = memory_block_size_bytes();
 827        if (!is_power_of_2(block_sz) || block_sz < MIN_MEMORY_BLOCK_SIZE)
 828                panic("Memory block size not suitable: 0x%lx\n", block_sz);
 829        sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
 830
 831        ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
 832        if (ret)
 833                panic("%s() failed to register subsystem: %d\n", __func__, ret);
 834
 835        /*
 836         * Create entries for memory sections that were found
 837         * during boot and have been initialized
 838         */
 839        for (nr = 0; nr <= __highest_present_section_nr;
 840             nr += sections_per_block) {
 841                ret = add_memory_block(nr);
 842                if (ret)
 843                        panic("%s() failed to add memory block: %d\n", __func__,
 844                              ret);
 845        }
 846}
 847
 848/**
 849 * walk_memory_blocks - walk through all present memory blocks overlapped
 850 *                      by the range [start, start + size)
 851 *
 852 * @start: start address of the memory range
 853 * @size: size of the memory range
 854 * @arg: argument passed to func
 855 * @func: callback for each memory section walked
 856 *
 857 * This function walks through all present memory blocks overlapped by the
 858 * range [start, start + size), calling func on each memory block.
 859 *
 860 * In case func() returns an error, walking is aborted and the error is
 861 * returned.
 862 *
 863 * Called under device_hotplug_lock.
 864 */
 865int walk_memory_blocks(unsigned long start, unsigned long size,
 866                       void *arg, walk_memory_blocks_func_t func)
 867{
 868        const unsigned long start_block_id = phys_to_block_id(start);
 869        const unsigned long end_block_id = phys_to_block_id(start + size - 1);
 870        struct memory_block *mem;
 871        unsigned long block_id;
 872        int ret = 0;
 873
 874        if (!size)
 875                return 0;
 876
 877        for (block_id = start_block_id; block_id <= end_block_id; block_id++) {
 878                mem = find_memory_block_by_id(block_id);
 879                if (!mem)
 880                        continue;
 881
 882                ret = func(mem, arg);
 883                put_device(&mem->dev);
 884                if (ret)
 885                        break;
 886        }
 887        return ret;
 888}
 889
 890struct for_each_memory_block_cb_data {
 891        walk_memory_blocks_func_t func;
 892        void *arg;
 893};
 894
 895static int for_each_memory_block_cb(struct device *dev, void *data)
 896{
 897        struct memory_block *mem = to_memory_block(dev);
 898        struct for_each_memory_block_cb_data *cb_data = data;
 899
 900        return cb_data->func(mem, cb_data->arg);
 901}
 902
 903/**
 904 * for_each_memory_block - walk through all present memory blocks
 905 *
 906 * @arg: argument passed to func
 907 * @func: callback for each memory block walked
 908 *
 909 * This function walks through all present memory blocks, calling func on
 910 * each memory block.
 911 *
 912 * In case func() returns an error, walking is aborted and the error is
 913 * returned.
 914 */
 915int for_each_memory_block(void *arg, walk_memory_blocks_func_t func)
 916{
 917        struct for_each_memory_block_cb_data cb_data = {
 918                .func = func,
 919                .arg = arg,
 920        };
 921
 922        return bus_for_each_dev(&memory_subsys, NULL, &cb_data,
 923                                for_each_memory_block_cb);
 924}
 925