linux/mm/compaction.c
<<
>>
Prefs
   1/*
   2 * linux/mm/compaction.c
   3 *
   4 * Memory compaction for the reduction of external fragmentation. Note that
   5 * this heavily depends upon page migration to do all the real heavy
   6 * lifting
   7 *
   8 * Copyright IBM Corp. 2007-2010 Mel Gorman <mel@csn.ul.ie>
   9 */
  10#include <linux/swap.h>
  11#include <linux/migrate.h>
  12#include <linux/compaction.h>
  13#include <linux/mm_inline.h>
  14#include <linux/backing-dev.h>
  15#include <linux/sysctl.h>
  16#include <linux/sysfs.h>
  17#include "internal.h"
  18
  19#if defined CONFIG_COMPACTION || defined CONFIG_CMA
  20
  21#define CREATE_TRACE_POINTS
  22#include <trace/events/compaction.h>
  23
  24static unsigned long release_freepages(struct list_head *freelist)
  25{
  26        struct page *page, *next;
  27        unsigned long count = 0;
  28
  29        list_for_each_entry_safe(page, next, freelist, lru) {
  30                list_del(&page->lru);
  31                __free_page(page);
  32                count++;
  33        }
  34
  35        return count;
  36}
  37
  38static void map_pages(struct list_head *list)
  39{
  40        struct page *page;
  41
  42        list_for_each_entry(page, list, lru) {
  43                arch_alloc_page(page, 0);
  44                kernel_map_pages(page, 1, 1);
  45        }
  46}
  47
  48static inline bool migrate_async_suitable(int migratetype)
  49{
  50        return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
  51}
  52
  53#ifdef CONFIG_COMPACTION
  54/* Returns true if the pageblock should be scanned for pages to isolate. */
  55static inline bool isolation_suitable(struct compact_control *cc,
  56                                        struct page *page)
  57{
  58        if (cc->ignore_skip_hint)
  59                return true;
  60
  61        return !get_pageblock_skip(page);
  62}
  63
  64/*
  65 * This function is called to clear all cached information on pageblocks that
  66 * should be skipped for page isolation when the migrate and free page scanner
  67 * meet.
  68 */
  69static void __reset_isolation_suitable(struct zone *zone)
  70{
  71        unsigned long start_pfn = zone->zone_start_pfn;
  72        unsigned long end_pfn = zone->zone_start_pfn + zone->spanned_pages;
  73        unsigned long pfn;
  74
  75        zone->compact_cached_migrate_pfn = start_pfn;
  76        zone->compact_cached_free_pfn = end_pfn;
  77        zone->compact_blockskip_flush = false;
  78
  79        /* Walk the zone and mark every pageblock as suitable for isolation */
  80        for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
  81                struct page *page;
  82
  83                cond_resched();
  84
  85                if (!pfn_valid(pfn))
  86                        continue;
  87
  88                page = pfn_to_page(pfn);
  89                if (zone != page_zone(page))
  90                        continue;
  91
  92                clear_pageblock_skip(page);
  93        }
  94}
  95
  96void reset_isolation_suitable(pg_data_t *pgdat)
  97{
  98        int zoneid;
  99
 100        for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
 101                struct zone *zone = &pgdat->node_zones[zoneid];
 102                if (!populated_zone(zone))
 103                        continue;
 104
 105                /* Only flush if a full compaction finished recently */
 106                if (zone->compact_blockskip_flush)
 107                        __reset_isolation_suitable(zone);
 108        }
 109}
 110
 111/*
 112 * If no pages were isolated then mark this pageblock to be skipped in the
 113 * future. The information is later cleared by __reset_isolation_suitable().
 114 */
 115static void update_pageblock_skip(struct compact_control *cc,
 116                        struct page *page, unsigned long nr_isolated,
 117                        bool migrate_scanner)
 118{
 119        struct zone *zone = cc->zone;
 120        if (!page)
 121                return;
 122
 123        if (!nr_isolated) {
 124                unsigned long pfn = page_to_pfn(page);
 125                set_pageblock_skip(page);
 126
 127                /* Update where compaction should restart */
 128                if (migrate_scanner) {
 129                        if (!cc->finished_update_migrate &&
 130                            pfn > zone->compact_cached_migrate_pfn)
 131                                zone->compact_cached_migrate_pfn = pfn;
 132                } else {
 133                        if (!cc->finished_update_free &&
 134                            pfn < zone->compact_cached_free_pfn)
 135                                zone->compact_cached_free_pfn = pfn;
 136                }
 137        }
 138}
 139#else
 140static inline bool isolation_suitable(struct compact_control *cc,
 141                                        struct page *page)
 142{
 143        return true;
 144}
 145
 146static void update_pageblock_skip(struct compact_control *cc,
 147                        struct page *page, unsigned long nr_isolated,
 148                        bool migrate_scanner)
 149{
 150}
 151#endif /* CONFIG_COMPACTION */
 152
 153static inline bool should_release_lock(spinlock_t *lock)
 154{
 155        return need_resched() || spin_is_contended(lock);
 156}
 157
 158/*
 159 * Compaction requires the taking of some coarse locks that are potentially
 160 * very heavily contended. Check if the process needs to be scheduled or
 161 * if the lock is contended. For async compaction, back out in the event
 162 * if contention is severe. For sync compaction, schedule.
 163 *
 164 * Returns true if the lock is held.
 165 * Returns false if the lock is released and compaction should abort
 166 */
 167static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags,
 168                                      bool locked, struct compact_control *cc)
 169{
 170        if (should_release_lock(lock)) {
 171                if (locked) {
 172                        spin_unlock_irqrestore(lock, *flags);
 173                        locked = false;
 174                }
 175
 176                /* async aborts if taking too long or contended */
 177                if (!cc->sync) {
 178                        cc->contended = true;
 179                        return false;
 180                }
 181
 182                cond_resched();
 183        }
 184
 185        if (!locked)
 186                spin_lock_irqsave(lock, *flags);
 187        return true;
 188}
 189
 190static inline bool compact_trylock_irqsave(spinlock_t *lock,
 191                        unsigned long *flags, struct compact_control *cc)
 192{
 193        return compact_checklock_irqsave(lock, flags, false, cc);
 194}
 195
 196/* Returns true if the page is within a block suitable for migration to */
 197static bool suitable_migration_target(struct page *page)
 198{
 199        int migratetype = get_pageblock_migratetype(page);
 200
 201        /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
 202        if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
 203                return false;
 204
 205        /* If the page is a large free page, then allow migration */
 206        if (PageBuddy(page) && page_order(page) >= pageblock_order)
 207                return true;
 208
 209        /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
 210        if (migrate_async_suitable(migratetype))
 211                return true;
 212
 213        /* Otherwise skip the block */
 214        return false;
 215}
 216
 217/*
 218 * Isolate free pages onto a private freelist. Caller must hold zone->lock.
 219 * If @strict is true, will abort returning 0 on any invalid PFNs or non-free
 220 * pages inside of the pageblock (even though it may still end up isolating
 221 * some pages).
 222 */
 223static unsigned long isolate_freepages_block(struct compact_control *cc,
 224                                unsigned long blockpfn,
 225                                unsigned long end_pfn,
 226                                struct list_head *freelist,
 227                                bool strict)
 228{
 229        int nr_scanned = 0, total_isolated = 0;
 230        struct page *cursor, *valid_page = NULL;
 231        unsigned long nr_strict_required = end_pfn - blockpfn;
 232        unsigned long flags;
 233        bool locked = false;
 234
 235        cursor = pfn_to_page(blockpfn);
 236
 237        /* Isolate free pages. */
 238        for (; blockpfn < end_pfn; blockpfn++, cursor++) {
 239                int isolated, i;
 240                struct page *page = cursor;
 241
 242                nr_scanned++;
 243                if (!pfn_valid_within(blockpfn))
 244                        continue;
 245                if (!valid_page)
 246                        valid_page = page;
 247                if (!PageBuddy(page))
 248                        continue;
 249
 250                /*
 251                 * The zone lock must be held to isolate freepages.
 252                 * Unfortunately this is a very coarse lock and can be
 253                 * heavily contended if there are parallel allocations
 254                 * or parallel compactions. For async compaction do not
 255                 * spin on the lock and we acquire the lock as late as
 256                 * possible.
 257                 */
 258                locked = compact_checklock_irqsave(&cc->zone->lock, &flags,
 259                                                                locked, cc);
 260                if (!locked)
 261                        break;
 262
 263                /* Recheck this is a suitable migration target under lock */
 264                if (!strict && !suitable_migration_target(page))
 265                        break;
 266
 267                /* Recheck this is a buddy page under lock */
 268                if (!PageBuddy(page))
 269                        continue;
 270
 271                /* Found a free page, break it into order-0 pages */
 272                isolated = split_free_page(page);
 273                if (!isolated && strict)
 274                        break;
 275                total_isolated += isolated;
 276                for (i = 0; i < isolated; i++) {
 277                        list_add(&page->lru, freelist);
 278                        page++;
 279                }
 280
 281                /* If a page was split, advance to the end of it */
 282                if (isolated) {
 283                        blockpfn += isolated - 1;
 284                        cursor += isolated - 1;
 285                }
 286        }
 287
 288        trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
 289
 290        /*
 291         * If strict isolation is requested by CMA then check that all the
 292         * pages requested were isolated. If there were any failures, 0 is
 293         * returned and CMA will fail.
 294         */
 295        if (strict && nr_strict_required > total_isolated)
 296                total_isolated = 0;
 297
 298        if (locked)
 299                spin_unlock_irqrestore(&cc->zone->lock, flags);
 300
 301        /* Update the pageblock-skip if the whole pageblock was scanned */
 302        if (blockpfn == end_pfn)
 303                update_pageblock_skip(cc, valid_page, total_isolated, false);
 304
 305        return total_isolated;
 306}
 307
 308/**
 309 * isolate_freepages_range() - isolate free pages.
 310 * @start_pfn: The first PFN to start isolating.
 311 * @end_pfn:   The one-past-last PFN.
 312 *
 313 * Non-free pages, invalid PFNs, or zone boundaries within the
 314 * [start_pfn, end_pfn) range are considered errors, cause function to
 315 * undo its actions and return zero.
 316 *
 317 * Otherwise, function returns one-past-the-last PFN of isolated page
 318 * (which may be greater then end_pfn if end fell in a middle of
 319 * a free page).
 320 */
 321unsigned long
 322isolate_freepages_range(struct compact_control *cc,
 323                        unsigned long start_pfn, unsigned long end_pfn)
 324{
 325        unsigned long isolated, pfn, block_end_pfn;
 326        LIST_HEAD(freelist);
 327
 328        for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
 329                if (!pfn_valid(pfn) || cc->zone != page_zone(pfn_to_page(pfn)))
 330                        break;
 331
 332                /*
 333                 * On subsequent iterations ALIGN() is actually not needed,
 334                 * but we keep it that we not to complicate the code.
 335                 */
 336                block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
 337                block_end_pfn = min(block_end_pfn, end_pfn);
 338
 339                isolated = isolate_freepages_block(cc, pfn, block_end_pfn,
 340                                                   &freelist, true);
 341
 342                /*
 343                 * In strict mode, isolate_freepages_block() returns 0 if
 344                 * there are any holes in the block (ie. invalid PFNs or
 345                 * non-free pages).
 346                 */
 347                if (!isolated)
 348                        break;
 349
 350                /*
 351                 * If we managed to isolate pages, it is always (1 << n) *
 352                 * pageblock_nr_pages for some non-negative n.  (Max order
 353                 * page may span two pageblocks).
 354                 */
 355        }
 356
 357        /* split_free_page does not map the pages */
 358        map_pages(&freelist);
 359
 360        if (pfn < end_pfn) {
 361                /* Loop terminated early, cleanup. */
 362                release_freepages(&freelist);
 363                return 0;
 364        }
 365
 366        /* We don't use freelists for anything. */
 367        return pfn;
 368}
 369
 370/* Update the number of anon and file isolated pages in the zone */
 371static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc)
 372{
 373        struct page *page;
 374        unsigned int count[2] = { 0, };
 375
 376        list_for_each_entry(page, &cc->migratepages, lru)
 377                count[!!page_is_file_cache(page)]++;
 378
 379        /* If locked we can use the interrupt unsafe versions */
 380        if (locked) {
 381                __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
 382                __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
 383        } else {
 384                mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
 385                mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
 386        }
 387}
 388
 389/* Similar to reclaim, but different enough that they don't share logic */
 390static bool too_many_isolated(struct zone *zone)
 391{
 392        unsigned long active, inactive, isolated;
 393
 394        inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
 395                                        zone_page_state(zone, NR_INACTIVE_ANON);
 396        active = zone_page_state(zone, NR_ACTIVE_FILE) +
 397                                        zone_page_state(zone, NR_ACTIVE_ANON);
 398        isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
 399                                        zone_page_state(zone, NR_ISOLATED_ANON);
 400
 401        return isolated > (inactive + active) / 2;
 402}
 403
 404/**
 405 * isolate_migratepages_range() - isolate all migrate-able pages in range.
 406 * @zone:       Zone pages are in.
 407 * @cc:         Compaction control structure.
 408 * @low_pfn:    The first PFN of the range.
 409 * @end_pfn:    The one-past-the-last PFN of the range.
 410 * @unevictable: true if it allows to isolate unevictable pages
 411 *
 412 * Isolate all pages that can be migrated from the range specified by
 413 * [low_pfn, end_pfn).  Returns zero if there is a fatal signal
 414 * pending), otherwise PFN of the first page that was not scanned
 415 * (which may be both less, equal to or more then end_pfn).
 416 *
 417 * Assumes that cc->migratepages is empty and cc->nr_migratepages is
 418 * zero.
 419 *
 420 * Apart from cc->migratepages and cc->nr_migratetypes this function
 421 * does not modify any cc's fields, in particular it does not modify
 422 * (or read for that matter) cc->migrate_pfn.
 423 */
 424unsigned long
 425isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
 426                unsigned long low_pfn, unsigned long end_pfn, bool unevictable)
 427{
 428        unsigned long last_pageblock_nr = 0, pageblock_nr;
 429        unsigned long nr_scanned = 0, nr_isolated = 0;
 430        struct list_head *migratelist = &cc->migratepages;
 431        isolate_mode_t mode = 0;
 432        struct lruvec *lruvec;
 433        unsigned long flags;
 434        bool locked = false;
 435        struct page *page = NULL, *valid_page = NULL;
 436
 437        /*
 438         * Ensure that there are not too many pages isolated from the LRU
 439         * list by either parallel reclaimers or compaction. If there are,
 440         * delay for some time until fewer pages are isolated
 441         */
 442        while (unlikely(too_many_isolated(zone))) {
 443                /* async migration should just abort */
 444                if (!cc->sync)
 445                        return 0;
 446
 447                congestion_wait(BLK_RW_ASYNC, HZ/10);
 448
 449                if (fatal_signal_pending(current))
 450                        return 0;
 451        }
 452
 453        /* Time to isolate some pages for migration */
 454        cond_resched();
 455        for (; low_pfn < end_pfn; low_pfn++) {
 456                /* give a chance to irqs before checking need_resched() */
 457                if (locked && !((low_pfn+1) % SWAP_CLUSTER_MAX)) {
 458                        if (should_release_lock(&zone->lru_lock)) {
 459                                spin_unlock_irqrestore(&zone->lru_lock, flags);
 460                                locked = false;
 461                        }
 462                }
 463
 464                /*
 465                 * migrate_pfn does not necessarily start aligned to a
 466                 * pageblock. Ensure that pfn_valid is called when moving
 467                 * into a new MAX_ORDER_NR_PAGES range in case of large
 468                 * memory holes within the zone
 469                 */
 470                if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
 471                        if (!pfn_valid(low_pfn)) {
 472                                low_pfn += MAX_ORDER_NR_PAGES - 1;
 473                                continue;
 474                        }
 475                }
 476
 477                if (!pfn_valid_within(low_pfn))
 478                        continue;
 479                nr_scanned++;
 480
 481                /*
 482                 * Get the page and ensure the page is within the same zone.
 483                 * See the comment in isolate_freepages about overlapping
 484                 * nodes. It is deliberate that the new zone lock is not taken
 485                 * as memory compaction should not move pages between nodes.
 486                 */
 487                page = pfn_to_page(low_pfn);
 488                if (page_zone(page) != zone)
 489                        continue;
 490
 491                if (!valid_page)
 492                        valid_page = page;
 493
 494                /* If isolation recently failed, do not retry */
 495                pageblock_nr = low_pfn >> pageblock_order;
 496                if (!isolation_suitable(cc, page))
 497                        goto next_pageblock;
 498
 499                /* Skip if free */
 500                if (PageBuddy(page))
 501                        continue;
 502
 503                /*
 504                 * For async migration, also only scan in MOVABLE blocks. Async
 505                 * migration is optimistic to see if the minimum amount of work
 506                 * satisfies the allocation
 507                 */
 508                if (!cc->sync && last_pageblock_nr != pageblock_nr &&
 509                    !migrate_async_suitable(get_pageblock_migratetype(page))) {
 510                        cc->finished_update_migrate = true;
 511                        goto next_pageblock;
 512                }
 513
 514                /* Check may be lockless but that's ok as we recheck later */
 515                if (!PageLRU(page))
 516                        continue;
 517
 518                /*
 519                 * PageLRU is set. lru_lock normally excludes isolation
 520                 * splitting and collapsing (collapsing has already happened
 521                 * if PageLRU is set) but the lock is not necessarily taken
 522                 * here and it is wasteful to take it just to check transhuge.
 523                 * Check TransHuge without lock and skip the whole pageblock if
 524                 * it's either a transhuge or hugetlbfs page, as calling
 525                 * compound_order() without preventing THP from splitting the
 526                 * page underneath us may return surprising results.
 527                 */
 528                if (PageTransHuge(page)) {
 529                        if (!locked)
 530                                goto next_pageblock;
 531                        low_pfn += (1 << compound_order(page)) - 1;
 532                        continue;
 533                }
 534
 535                /* Check if it is ok to still hold the lock */
 536                locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
 537                                                                locked, cc);
 538                if (!locked || fatal_signal_pending(current))
 539                        break;
 540
 541                /* Recheck PageLRU and PageTransHuge under lock */
 542                if (!PageLRU(page))
 543                        continue;
 544                if (PageTransHuge(page)) {
 545                        low_pfn += (1 << compound_order(page)) - 1;
 546                        continue;
 547                }
 548
 549                if (!cc->sync)
 550                        mode |= ISOLATE_ASYNC_MIGRATE;
 551
 552                if (unevictable)
 553                        mode |= ISOLATE_UNEVICTABLE;
 554
 555                lruvec = mem_cgroup_page_lruvec(page, zone);
 556
 557                /* Try isolate the page */
 558                if (__isolate_lru_page(page, mode) != 0)
 559                        continue;
 560
 561                VM_BUG_ON(PageTransCompound(page));
 562
 563                /* Successfully isolated */
 564                cc->finished_update_migrate = true;
 565                del_page_from_lru_list(page, lruvec, page_lru(page));
 566                list_add(&page->lru, migratelist);
 567                cc->nr_migratepages++;
 568                nr_isolated++;
 569
 570                /* Avoid isolating too much */
 571                if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
 572                        ++low_pfn;
 573                        break;
 574                }
 575
 576                continue;
 577
 578next_pageblock:
 579                low_pfn += pageblock_nr_pages;
 580                low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
 581                last_pageblock_nr = pageblock_nr;
 582        }
 583
 584        acct_isolated(zone, locked, cc);
 585
 586        if (locked)
 587                spin_unlock_irqrestore(&zone->lru_lock, flags);
 588
 589        /* Update the pageblock-skip if the whole pageblock was scanned */
 590        if (low_pfn == end_pfn)
 591                update_pageblock_skip(cc, valid_page, nr_isolated, true);
 592
 593        trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
 594
 595        return low_pfn;
 596}
 597
 598#endif /* CONFIG_COMPACTION || CONFIG_CMA */
 599#ifdef CONFIG_COMPACTION
 600/*
 601 * Based on information in the current compact_control, find blocks
 602 * suitable for isolating free pages from and then isolate them.
 603 */
 604static void isolate_freepages(struct zone *zone,
 605                                struct compact_control *cc)
 606{
 607        struct page *page;
 608        unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
 609        int nr_freepages = cc->nr_freepages;
 610        struct list_head *freelist = &cc->freepages;
 611
 612        /*
 613         * Initialise the free scanner. The starting point is where we last
 614         * scanned from (or the end of the zone if starting). The low point
 615         * is the end of the pageblock the migration scanner is using.
 616         */
 617        pfn = cc->free_pfn;
 618        low_pfn = cc->migrate_pfn + pageblock_nr_pages;
 619
 620        /*
 621         * Take care that if the migration scanner is at the end of the zone
 622         * that the free scanner does not accidentally move to the next zone
 623         * in the next isolation cycle.
 624         */
 625        high_pfn = min(low_pfn, pfn);
 626
 627        zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
 628
 629        /*
 630         * Isolate free pages until enough are available to migrate the
 631         * pages on cc->migratepages. We stop searching if the migrate
 632         * and free page scanners meet or enough free pages are isolated.
 633         */
 634        for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
 635                                        pfn -= pageblock_nr_pages) {
 636                unsigned long isolated;
 637
 638                if (!pfn_valid(pfn))
 639                        continue;
 640
 641                /*
 642                 * Check for overlapping nodes/zones. It's possible on some
 643                 * configurations to have a setup like
 644                 * node0 node1 node0
 645                 * i.e. it's possible that all pages within a zones range of
 646                 * pages do not belong to a single zone.
 647                 */
 648                page = pfn_to_page(pfn);
 649                if (page_zone(page) != zone)
 650                        continue;
 651
 652                /* Check the block is suitable for migration */
 653                if (!suitable_migration_target(page))
 654                        continue;
 655
 656                /* If isolation recently failed, do not retry */
 657                if (!isolation_suitable(cc, page))
 658                        continue;
 659
 660                /* Found a block suitable for isolating free pages from */
 661                isolated = 0;
 662
 663                /*
 664                 * As pfn may not start aligned, pfn+pageblock_nr_page
 665                 * may cross a MAX_ORDER_NR_PAGES boundary and miss
 666                 * a pfn_valid check. Ensure isolate_freepages_block()
 667                 * only scans within a pageblock
 668                 */
 669                end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
 670                end_pfn = min(end_pfn, zone_end_pfn);
 671                isolated = isolate_freepages_block(cc, pfn, end_pfn,
 672                                                   freelist, false);
 673                nr_freepages += isolated;
 674
 675                /*
 676                 * Record the highest PFN we isolated pages from. When next
 677                 * looking for free pages, the search will restart here as
 678                 * page migration may have returned some pages to the allocator
 679                 */
 680                if (isolated) {
 681                        cc->finished_update_free = true;
 682                        high_pfn = max(high_pfn, pfn);
 683                }
 684        }
 685
 686        /* split_free_page does not map the pages */
 687        map_pages(freelist);
 688
 689        cc->free_pfn = high_pfn;
 690        cc->nr_freepages = nr_freepages;
 691}
 692
 693/*
 694 * This is a migrate-callback that "allocates" freepages by taking pages
 695 * from the isolated freelists in the block we are migrating to.
 696 */
 697static struct page *compaction_alloc(struct page *migratepage,
 698                                        unsigned long data,
 699                                        int **result)
 700{
 701        struct compact_control *cc = (struct compact_control *)data;
 702        struct page *freepage;
 703
 704        /* Isolate free pages if necessary */
 705        if (list_empty(&cc->freepages)) {
 706                isolate_freepages(cc->zone, cc);
 707
 708                if (list_empty(&cc->freepages))
 709                        return NULL;
 710        }
 711
 712        freepage = list_entry(cc->freepages.next, struct page, lru);
 713        list_del(&freepage->lru);
 714        cc->nr_freepages--;
 715
 716        return freepage;
 717}
 718
 719/*
 720 * We cannot control nr_migratepages and nr_freepages fully when migration is
 721 * running as migrate_pages() has no knowledge of compact_control. When
 722 * migration is complete, we count the number of pages on the lists by hand.
 723 */
 724static void update_nr_listpages(struct compact_control *cc)
 725{
 726        int nr_migratepages = 0;
 727        int nr_freepages = 0;
 728        struct page *page;
 729
 730        list_for_each_entry(page, &cc->migratepages, lru)
 731                nr_migratepages++;
 732        list_for_each_entry(page, &cc->freepages, lru)
 733                nr_freepages++;
 734
 735        cc->nr_migratepages = nr_migratepages;
 736        cc->nr_freepages = nr_freepages;
 737}
 738
 739/* possible outcome of isolate_migratepages */
 740typedef enum {
 741        ISOLATE_ABORT,          /* Abort compaction now */
 742        ISOLATE_NONE,           /* No pages isolated, continue scanning */
 743        ISOLATE_SUCCESS,        /* Pages isolated, migrate */
 744} isolate_migrate_t;
 745
 746/*
 747 * Isolate all pages that can be migrated from the block pointed to by
 748 * the migrate scanner within compact_control.
 749 */
 750static isolate_migrate_t isolate_migratepages(struct zone *zone,
 751                                        struct compact_control *cc)
 752{
 753        unsigned long low_pfn, end_pfn;
 754
 755        /* Do not scan outside zone boundaries */
 756        low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
 757
 758        /* Only scan within a pageblock boundary */
 759        end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
 760
 761        /* Do not cross the free scanner or scan within a memory hole */
 762        if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
 763                cc->migrate_pfn = end_pfn;
 764                return ISOLATE_NONE;
 765        }
 766
 767        /* Perform the isolation */
 768        low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn, false);
 769        if (!low_pfn || cc->contended)
 770                return ISOLATE_ABORT;
 771
 772        cc->migrate_pfn = low_pfn;
 773
 774        return ISOLATE_SUCCESS;
 775}
 776
 777static int compact_finished(struct zone *zone,
 778                            struct compact_control *cc)
 779{
 780        unsigned int order;
 781        unsigned long watermark;
 782
 783        if (fatal_signal_pending(current))
 784                return COMPACT_PARTIAL;
 785
 786        /* Compaction run completes if the migrate and free scanner meet */
 787        if (cc->free_pfn <= cc->migrate_pfn) {
 788                /*
 789                 * Mark that the PG_migrate_skip information should be cleared
 790                 * by kswapd when it goes to sleep. kswapd does not set the
 791                 * flag itself as the decision to be clear should be directly
 792                 * based on an allocation request.
 793                 */
 794                if (!current_is_kswapd())
 795                        zone->compact_blockskip_flush = true;
 796
 797                return COMPACT_COMPLETE;
 798        }
 799
 800        /*
 801         * order == -1 is expected when compacting via
 802         * /proc/sys/vm/compact_memory
 803         */
 804        if (cc->order == -1)
 805                return COMPACT_CONTINUE;
 806
 807        /* Compaction run is not finished if the watermark is not met */
 808        watermark = low_wmark_pages(zone);
 809        watermark += (1 << cc->order);
 810
 811        if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
 812                return COMPACT_CONTINUE;
 813
 814        /* Direct compactor: Is a suitable page free? */
 815        for (order = cc->order; order < MAX_ORDER; order++) {
 816                struct free_area *area = &zone->free_area[order];
 817
 818                /* Job done if page is free of the right migratetype */
 819                if (!list_empty(&area->free_list[cc->migratetype]))
 820                        return COMPACT_PARTIAL;
 821
 822                /* Job done if allocation would set block type */
 823                if (cc->order >= pageblock_order && area->nr_free)
 824                        return COMPACT_PARTIAL;
 825        }
 826
 827        return COMPACT_CONTINUE;
 828}
 829
 830/*
 831 * compaction_suitable: Is this suitable to run compaction on this zone now?
 832 * Returns
 833 *   COMPACT_SKIPPED  - If there are too few free pages for compaction
 834 *   COMPACT_PARTIAL  - If the allocation would succeed without compaction
 835 *   COMPACT_CONTINUE - If compaction should run now
 836 */
 837unsigned long compaction_suitable(struct zone *zone, int order)
 838{
 839        int fragindex;
 840        unsigned long watermark;
 841
 842        /*
 843         * order == -1 is expected when compacting via
 844         * /proc/sys/vm/compact_memory
 845         */
 846        if (order == -1)
 847                return COMPACT_CONTINUE;
 848
 849        /*
 850         * Watermarks for order-0 must be met for compaction. Note the 2UL.
 851         * This is because during migration, copies of pages need to be
 852         * allocated and for a short time, the footprint is higher
 853         */
 854        watermark = low_wmark_pages(zone) + (2UL << order);
 855        if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
 856                return COMPACT_SKIPPED;
 857
 858        /*
 859         * fragmentation index determines if allocation failures are due to
 860         * low memory or external fragmentation
 861         *
 862         * index of -1000 implies allocations might succeed depending on
 863         * watermarks
 864         * index towards 0 implies failure is due to lack of memory
 865         * index towards 1000 implies failure is due to fragmentation
 866         *
 867         * Only compact if a failure would be due to fragmentation.
 868         */
 869        fragindex = fragmentation_index(zone, order);
 870        if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
 871                return COMPACT_SKIPPED;
 872
 873        if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
 874            0, 0))
 875                return COMPACT_PARTIAL;
 876
 877        return COMPACT_CONTINUE;
 878}
 879
 880static int compact_zone(struct zone *zone, struct compact_control *cc)
 881{
 882        int ret;
 883        unsigned long start_pfn = zone->zone_start_pfn;
 884        unsigned long end_pfn = zone->zone_start_pfn + zone->spanned_pages;
 885
 886        ret = compaction_suitable(zone, cc->order);
 887        switch (ret) {
 888        case COMPACT_PARTIAL:
 889        case COMPACT_SKIPPED:
 890                /* Compaction is likely to fail */
 891                return ret;
 892        case COMPACT_CONTINUE:
 893                /* Fall through to compaction */
 894                ;
 895        }
 896
 897        /*
 898         * Setup to move all movable pages to the end of the zone. Used cached
 899         * information on where the scanners should start but check that it
 900         * is initialised by ensuring the values are within zone boundaries.
 901         */
 902        cc->migrate_pfn = zone->compact_cached_migrate_pfn;
 903        cc->free_pfn = zone->compact_cached_free_pfn;
 904        if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
 905                cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
 906                zone->compact_cached_free_pfn = cc->free_pfn;
 907        }
 908        if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
 909                cc->migrate_pfn = start_pfn;
 910                zone->compact_cached_migrate_pfn = cc->migrate_pfn;
 911        }
 912
 913        /*
 914         * Clear pageblock skip if there were failures recently and compaction
 915         * is about to be retried after being deferred. kswapd does not do
 916         * this reset as it'll reset the cached information when going to sleep.
 917         */
 918        if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
 919                __reset_isolation_suitable(zone);
 920
 921        migrate_prep_local();
 922
 923        while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
 924                unsigned long nr_migrate, nr_remaining;
 925                int err;
 926
 927                switch (isolate_migratepages(zone, cc)) {
 928                case ISOLATE_ABORT:
 929                        ret = COMPACT_PARTIAL;
 930                        putback_lru_pages(&cc->migratepages);
 931                        cc->nr_migratepages = 0;
 932                        goto out;
 933                case ISOLATE_NONE:
 934                        continue;
 935                case ISOLATE_SUCCESS:
 936                        ;
 937                }
 938
 939                nr_migrate = cc->nr_migratepages;
 940                err = migrate_pages(&cc->migratepages, compaction_alloc,
 941                                (unsigned long)cc, false,
 942                                cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
 943                update_nr_listpages(cc);
 944                nr_remaining = cc->nr_migratepages;
 945
 946                count_vm_event(COMPACTBLOCKS);
 947                count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
 948                if (nr_remaining)
 949                        count_vm_events(COMPACTPAGEFAILED, nr_remaining);
 950                trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
 951                                                nr_remaining);
 952
 953                /* Release LRU pages not migrated */
 954                if (err) {
 955                        putback_lru_pages(&cc->migratepages);
 956                        cc->nr_migratepages = 0;
 957                        if (err == -ENOMEM) {
 958                                ret = COMPACT_PARTIAL;
 959                                goto out;
 960                        }
 961                }
 962        }
 963
 964out:
 965        /* Release free pages and check accounting */
 966        cc->nr_freepages -= release_freepages(&cc->freepages);
 967        VM_BUG_ON(cc->nr_freepages != 0);
 968
 969        return ret;
 970}
 971
 972static unsigned long compact_zone_order(struct zone *zone,
 973                                 int order, gfp_t gfp_mask,
 974                                 bool sync, bool *contended)
 975{
 976        unsigned long ret;
 977        struct compact_control cc = {
 978                .nr_freepages = 0,
 979                .nr_migratepages = 0,
 980                .order = order,
 981                .migratetype = allocflags_to_migratetype(gfp_mask),
 982                .zone = zone,
 983                .sync = sync,
 984        };
 985        INIT_LIST_HEAD(&cc.freepages);
 986        INIT_LIST_HEAD(&cc.migratepages);
 987
 988        ret = compact_zone(zone, &cc);
 989
 990        VM_BUG_ON(!list_empty(&cc.freepages));
 991        VM_BUG_ON(!list_empty(&cc.migratepages));
 992
 993        *contended = cc.contended;
 994        return ret;
 995}
 996
 997int sysctl_extfrag_threshold = 500;
 998
 999/**
1000 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
1001 * @zonelist: The zonelist used for the current allocation
1002 * @order: The order of the current allocation
1003 * @gfp_mask: The GFP mask of the current allocation
1004 * @nodemask: The allowed nodes to allocate from
1005 * @sync: Whether migration is synchronous or not
1006 * @contended: Return value that is true if compaction was aborted due to lock contention
1007 * @page: Optionally capture a free page of the requested order during compaction
1008 *
1009 * This is the main entry point for direct page compaction.
1010 */
1011unsigned long try_to_compact_pages(struct zonelist *zonelist,
1012                        int order, gfp_t gfp_mask, nodemask_t *nodemask,
1013                        bool sync, bool *contended)
1014{
1015        enum zone_type high_zoneidx = gfp_zone(gfp_mask);
1016        int may_enter_fs = gfp_mask & __GFP_FS;
1017        int may_perform_io = gfp_mask & __GFP_IO;
1018        struct zoneref *z;
1019        struct zone *zone;
1020        int rc = COMPACT_SKIPPED;
1021        int alloc_flags = 0;
1022
1023        /* Check if the GFP flags allow compaction */
1024        if (!order || !may_enter_fs || !may_perform_io)
1025                return rc;
1026
1027        count_vm_event(COMPACTSTALL);
1028
1029#ifdef CONFIG_CMA
1030        if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
1031                alloc_flags |= ALLOC_CMA;
1032#endif
1033        /* Compact each zone in the list */
1034        for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
1035                                                                nodemask) {
1036                int status;
1037
1038                status = compact_zone_order(zone, order, gfp_mask, sync,
1039                                                contended);
1040                rc = max(status, rc);
1041
1042                /* If a normal allocation would succeed, stop compacting */
1043                if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
1044                                      alloc_flags))
1045                        break;
1046        }
1047
1048        return rc;
1049}
1050
1051
1052/* Compact all zones within a node */
1053static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
1054{
1055        int zoneid;
1056        struct zone *zone;
1057
1058        for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
1059
1060                zone = &pgdat->node_zones[zoneid];
1061                if (!populated_zone(zone))
1062                        continue;
1063
1064                cc->nr_freepages = 0;
1065                cc->nr_migratepages = 0;
1066                cc->zone = zone;
1067                INIT_LIST_HEAD(&cc->freepages);
1068                INIT_LIST_HEAD(&cc->migratepages);
1069
1070                if (cc->order == -1 || !compaction_deferred(zone, cc->order))
1071                        compact_zone(zone, cc);
1072
1073                if (cc->order > 0) {
1074                        int ok = zone_watermark_ok(zone, cc->order,
1075                                                low_wmark_pages(zone), 0, 0);
1076                        if (ok && cc->order >= zone->compact_order_failed)
1077                                zone->compact_order_failed = cc->order + 1;
1078                        /* Currently async compaction is never deferred. */
1079                        else if (!ok && cc->sync)
1080                                defer_compaction(zone, cc->order);
1081                }
1082
1083                VM_BUG_ON(!list_empty(&cc->freepages));
1084                VM_BUG_ON(!list_empty(&cc->migratepages));
1085        }
1086
1087        return 0;
1088}
1089
1090int compact_pgdat(pg_data_t *pgdat, int order)
1091{
1092        struct compact_control cc = {
1093                .order = order,
1094                .sync = false,
1095        };
1096
1097        return __compact_pgdat(pgdat, &cc);
1098}
1099
1100static int compact_node(int nid)
1101{
1102        struct compact_control cc = {
1103                .order = -1,
1104                .sync = true,
1105        };
1106
1107        return __compact_pgdat(NODE_DATA(nid), &cc);
1108}
1109
1110/* Compact all nodes in the system */
1111static void compact_nodes(void)
1112{
1113        int nid;
1114
1115        /* Flush pending updates to the LRU lists */
1116        lru_add_drain_all();
1117
1118        for_each_online_node(nid)
1119                compact_node(nid);
1120}
1121
1122/* The written value is actually unused, all memory is compacted */
1123int sysctl_compact_memory;
1124
1125/* This is the entry point for compacting all nodes via /proc/sys/vm */
1126int sysctl_compaction_handler(struct ctl_table *table, int write,
1127                        void __user *buffer, size_t *length, loff_t *ppos)
1128{
1129        if (write)
1130                compact_nodes();
1131
1132        return 0;
1133}
1134
1135int sysctl_extfrag_handler(struct ctl_table *table, int write,
1136                        void __user *buffer, size_t *length, loff_t *ppos)
1137{
1138        proc_dointvec_minmax(table, write, buffer, length, ppos);
1139
1140        return 0;
1141}
1142
1143#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
1144ssize_t sysfs_compact_node(struct device *dev,
1145                        struct device_attribute *attr,
1146                        const char *buf, size_t count)
1147{
1148        int nid = dev->id;
1149
1150        if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1151                /* Flush pending updates to the LRU lists */
1152                lru_add_drain_all();
1153
1154                compact_node(nid);
1155        }
1156
1157        return count;
1158}
1159static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
1160
1161int compaction_register_node(struct node *node)
1162{
1163        return device_create_file(&node->dev, &dev_attr_compact);
1164}
1165
1166void compaction_unregister_node(struct node *node)
1167{
1168        return device_remove_file(&node->dev, &dev_attr_compact);
1169}
1170#endif /* CONFIG_SYSFS && CONFIG_NUMA */
1171
1172#endif /* CONFIG_COMPACTION */
1173
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.