// SPDX-License-Identifier: GPL-2.0-only1/*2* mm/balloon_compaction.c3*4* Common interface for making balloon pages movable by compaction.5*6* Copyright (C) 2012, Red Hat, Inc. Rafael Aquini <[email protected]>7*/8#include <linux/mm.h>9#include <linux/slab.h>10#include <linux/export.h>11#include <linux/balloon_compaction.h>1213static void balloon_page_enqueue_one(struct balloon_dev_info *b_dev_info,14struct page *page)15{16/*17* Block others from accessing the 'page' when we get around to18* establishing additional references. We should be the only one19* holding a reference to the 'page' at this point. If we are not, then20* memory corruption is possible and we should stop execution.21*/22BUG_ON(!trylock_page(page));23balloon_page_insert(b_dev_info, page);24unlock_page(page);25__count_vm_event(BALLOON_INFLATE);26inc_node_page_state(page, NR_BALLOON_PAGES);27}2829/**30* balloon_page_list_enqueue() - inserts a list of pages into the balloon page31* list.32* @b_dev_info: balloon device descriptor where we will insert a new page to33* @pages: pages to enqueue - allocated using balloon_page_alloc.34*35* Driver must call this function to properly enqueue balloon pages before36* definitively removing them from the guest system.37*38* Return: number of pages that were enqueued.39*/40size_t balloon_page_list_enqueue(struct balloon_dev_info *b_dev_info,41struct list_head *pages)42{43struct page *page, *tmp;44unsigned long flags;45size_t n_pages = 0;4647spin_lock_irqsave(&b_dev_info->pages_lock, flags);48list_for_each_entry_safe(page, tmp, pages, lru) {49list_del(&page->lru);50balloon_page_enqueue_one(b_dev_info, page);51n_pages++;52}53spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);54return n_pages;55}56EXPORT_SYMBOL_GPL(balloon_page_list_enqueue);5758/**59* balloon_page_list_dequeue() - removes pages from balloon's page list and60* returns a list of the pages.61* @b_dev_info: balloon device descriptor where we will grab a page from.62* @pages: pointer to the list of pages that would be returned to the caller.63* @n_req_pages: number of requested pages.64*65* Driver must call this function to properly de-allocate a previous enlisted66* balloon pages before definitively releasing it back to the guest system.67* This function tries to remove @n_req_pages from the ballooned pages and68* return them to the caller in the @pages list.69*70* Note that this function may fail to dequeue some pages even if the balloon71* isn't empty - since the page list can be temporarily empty due to compaction72* of isolated pages.73*74* Return: number of pages that were added to the @pages list.75*/76size_t balloon_page_list_dequeue(struct balloon_dev_info *b_dev_info,77struct list_head *pages, size_t n_req_pages)78{79struct page *page, *tmp;80unsigned long flags;81size_t n_pages = 0;8283spin_lock_irqsave(&b_dev_info->pages_lock, flags);84list_for_each_entry_safe(page, tmp, &b_dev_info->pages, lru) {85if (n_pages == n_req_pages)86break;8788/*89* Block others from accessing the 'page' while we get around to90* establishing additional references and preparing the 'page'91* to be released by the balloon driver.92*/93if (!trylock_page(page))94continue;9596list_del(&page->lru);97balloon_page_finalize(page);98__count_vm_event(BALLOON_DEFLATE);99list_add(&page->lru, pages);100unlock_page(page);101dec_node_page_state(page, NR_BALLOON_PAGES);102n_pages++;103}104spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);105106return n_pages;107}108EXPORT_SYMBOL_GPL(balloon_page_list_dequeue);109110/*111* balloon_page_alloc - allocates a new page for insertion into the balloon112* page list.113*114* Driver must call this function to properly allocate a new balloon page.115* Driver must call balloon_page_enqueue before definitively removing the page116* from the guest system.117*118* Return: struct page for the allocated page or NULL on allocation failure.119*/120struct page *balloon_page_alloc(void)121{122struct page *page = alloc_page(balloon_mapping_gfp_mask() |123__GFP_NOMEMALLOC | __GFP_NORETRY |124__GFP_NOWARN);125return page;126}127EXPORT_SYMBOL_GPL(balloon_page_alloc);128129/*130* balloon_page_enqueue - inserts a new page into the balloon page list.131*132* @b_dev_info: balloon device descriptor where we will insert a new page133* @page: new page to enqueue - allocated using balloon_page_alloc.134*135* Drivers must call this function to properly enqueue a new allocated balloon136* page before definitively removing the page from the guest system.137*138* Drivers must not call balloon_page_enqueue on pages that have been pushed to139* a list with balloon_page_push before removing them with balloon_page_pop. To140* enqueue a list of pages, use balloon_page_list_enqueue instead.141*/142void balloon_page_enqueue(struct balloon_dev_info *b_dev_info,143struct page *page)144{145unsigned long flags;146147spin_lock_irqsave(&b_dev_info->pages_lock, flags);148balloon_page_enqueue_one(b_dev_info, page);149spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);150}151EXPORT_SYMBOL_GPL(balloon_page_enqueue);152153/*154* balloon_page_dequeue - removes a page from balloon's page list and returns155* its address to allow the driver to release the page.156* @b_dev_info: balloon device descriptor where we will grab a page from.157*158* Driver must call this function to properly dequeue a previously enqueued page159* before definitively releasing it back to the guest system.160*161* Caller must perform its own accounting to ensure that this162* function is called only if some pages are actually enqueued.163*164* Note that this function may fail to dequeue some pages even if there are165* some enqueued pages - since the page list can be temporarily empty due to166* the compaction of isolated pages.167*168* TODO: remove the caller accounting requirements, and allow caller to wait169* until all pages can be dequeued.170*171* Return: struct page for the dequeued page, or NULL if no page was dequeued.172*/173struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info)174{175unsigned long flags;176LIST_HEAD(pages);177int n_pages;178179n_pages = balloon_page_list_dequeue(b_dev_info, &pages, 1);180181if (n_pages != 1) {182/*183* If we are unable to dequeue a balloon page because the page184* list is empty and there are no isolated pages, then something185* went out of track and some balloon pages are lost.186* BUG() here, otherwise the balloon driver may get stuck in187* an infinite loop while attempting to release all its pages.188*/189spin_lock_irqsave(&b_dev_info->pages_lock, flags);190if (unlikely(list_empty(&b_dev_info->pages) &&191!b_dev_info->isolated_pages))192BUG();193spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);194return NULL;195}196return list_first_entry(&pages, struct page, lru);197}198EXPORT_SYMBOL_GPL(balloon_page_dequeue);199200#ifdef CONFIG_BALLOON_COMPACTION201202static bool balloon_page_isolate(struct page *page, isolate_mode_t mode)203204{205struct balloon_dev_info *b_dev_info = balloon_page_device(page);206unsigned long flags;207208if (!b_dev_info)209return false;210211spin_lock_irqsave(&b_dev_info->pages_lock, flags);212list_del(&page->lru);213b_dev_info->isolated_pages++;214spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);215216return true;217}218219static void balloon_page_putback(struct page *page)220{221struct balloon_dev_info *b_dev_info = balloon_page_device(page);222unsigned long flags;223224/* Isolated balloon pages cannot get deflated. */225if (WARN_ON_ONCE(!b_dev_info))226return;227228spin_lock_irqsave(&b_dev_info->pages_lock, flags);229list_add(&page->lru, &b_dev_info->pages);230b_dev_info->isolated_pages--;231spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);232}233234/* move_to_new_page() counterpart for a ballooned page */235static int balloon_page_migrate(struct page *newpage, struct page *page,236enum migrate_mode mode)237{238struct balloon_dev_info *balloon = balloon_page_device(page);239240VM_BUG_ON_PAGE(!PageLocked(page), page);241VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);242243/* Isolated balloon pages cannot get deflated. */244if (WARN_ON_ONCE(!balloon))245return -EAGAIN;246247return balloon->migratepage(balloon, newpage, page, mode);248}249250const struct movable_operations balloon_mops = {251.migrate_page = balloon_page_migrate,252.isolate_page = balloon_page_isolate,253.putback_page = balloon_page_putback,254};255256static int __init balloon_init(void)257{258return set_movable_ops(&balloon_mops, PGTY_offline);259}260core_initcall(balloon_init);261262#endif /* CONFIG_BALLOON_COMPACTION */263264265