// SPDX-License-Identifier: GPL-2.0-only1/*2* Common interface for implementing a memory balloon, including support3* for migration of pages inflated in a memory balloon.4*5* Copyright (C) 2012, Red Hat, Inc. Rafael Aquini <[email protected]>6*/7#include <linux/mm.h>8#include <linux/slab.h>9#include <linux/export.h>10#include <linux/balloon.h>1112/*13* Lock protecting the balloon_dev_info of all devices. We don't really14* expect more than one device.15*/16static DEFINE_SPINLOCK(balloon_pages_lock);1718/**19* balloon_page_insert - insert a page into the balloon's page list and make20* the page->private assignment accordingly.21* @balloon : pointer to balloon device22* @page : page to be assigned as a 'balloon page'23*24* Caller must ensure the balloon_pages_lock is held.25*/26static void balloon_page_insert(struct balloon_dev_info *balloon,27struct page *page)28{29lockdep_assert_held(&balloon_pages_lock);30__SetPageOffline(page);31if (IS_ENABLED(CONFIG_BALLOON_MIGRATION)) {32SetPageMovableOps(page);33set_page_private(page, (unsigned long)balloon);34}35list_add(&page->lru, &balloon->pages);36}3738/**39* balloon_page_finalize - prepare a balloon page that was removed from the40* balloon list for release to the page allocator41* @page: page to be released to the page allocator42*43* Caller must ensure the balloon_pages_lock is held.44*/45static void balloon_page_finalize(struct page *page)46{47lockdep_assert_held(&balloon_pages_lock);48if (IS_ENABLED(CONFIG_BALLOON_MIGRATION))49set_page_private(page, 0);50/* PageOffline is sticky until the page is freed to the buddy. */51}5253static void balloon_page_enqueue_one(struct balloon_dev_info *b_dev_info,54struct page *page)55{56balloon_page_insert(b_dev_info, page);57if (b_dev_info->adjust_managed_page_count)58adjust_managed_page_count(page, -1);59__count_vm_event(BALLOON_INFLATE);60inc_node_page_state(page, NR_BALLOON_PAGES);61}6263/**64* balloon_page_list_enqueue() - inserts a list of pages into the balloon page65* list.66* @b_dev_info: balloon device descriptor where we will insert a new page to67* @pages: pages to enqueue - allocated using balloon_page_alloc.68*69* Driver must call this function to properly enqueue balloon pages before70* definitively removing them from the guest system.71*72* Return: number of pages that were enqueued.73*/74size_t balloon_page_list_enqueue(struct balloon_dev_info *b_dev_info,75struct list_head *pages)76{77struct page *page, *tmp;78unsigned long flags;79size_t n_pages = 0;8081spin_lock_irqsave(&balloon_pages_lock, flags);82list_for_each_entry_safe(page, tmp, pages, lru) {83list_del(&page->lru);84balloon_page_enqueue_one(b_dev_info, page);85n_pages++;86}87spin_unlock_irqrestore(&balloon_pages_lock, flags);88return n_pages;89}90EXPORT_SYMBOL_GPL(balloon_page_list_enqueue);9192/**93* balloon_page_list_dequeue() - removes pages from balloon's page list and94* returns a list of the pages.95* @b_dev_info: balloon device descriptor where we will grab a page from.96* @pages: pointer to the list of pages that would be returned to the caller.97* @n_req_pages: number of requested pages.98*99* Driver must call this function to properly de-allocate a previous enlisted100* balloon pages before definitively releasing it back to the guest system.101* This function tries to remove @n_req_pages from the ballooned pages and102* return them to the caller in the @pages list.103*104* Note that this function may fail to dequeue some pages even if the balloon105* isn't empty - since the page list can be temporarily empty due to compaction106* of isolated pages.107*108* Return: number of pages that were added to the @pages list.109*/110size_t balloon_page_list_dequeue(struct balloon_dev_info *b_dev_info,111struct list_head *pages, size_t n_req_pages)112{113struct page *page, *tmp;114unsigned long flags;115size_t n_pages = 0;116117spin_lock_irqsave(&balloon_pages_lock, flags);118list_for_each_entry_safe(page, tmp, &b_dev_info->pages, lru) {119if (n_pages == n_req_pages)120break;121list_del(&page->lru);122if (b_dev_info->adjust_managed_page_count)123adjust_managed_page_count(page, 1);124balloon_page_finalize(page);125__count_vm_event(BALLOON_DEFLATE);126list_add(&page->lru, pages);127dec_node_page_state(page, NR_BALLOON_PAGES);128n_pages++;129}130spin_unlock_irqrestore(&balloon_pages_lock, flags);131132return n_pages;133}134EXPORT_SYMBOL_GPL(balloon_page_list_dequeue);135136/**137* balloon_page_alloc - allocates a new page for insertion into the balloon138* page list.139*140* Driver must call this function to properly allocate a new balloon page.141* Driver must call balloon_page_enqueue before definitively removing the page142* from the guest system.143*144* Return: struct page for the allocated page or NULL on allocation failure.145*/146struct page *balloon_page_alloc(void)147{148gfp_t gfp_flags = __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;149150if (IS_ENABLED(CONFIG_BALLOON_MIGRATION))151gfp_flags |= GFP_HIGHUSER_MOVABLE;152else153gfp_flags |= GFP_HIGHUSER;154155return alloc_page(gfp_flags);156}157EXPORT_SYMBOL_GPL(balloon_page_alloc);158159/**160* balloon_page_enqueue - inserts a new page into the balloon page list.161*162* @b_dev_info: balloon device descriptor where we will insert a new page163* @page: new page to enqueue - allocated using balloon_page_alloc.164*165* Drivers must call this function to properly enqueue a new allocated balloon166* page before definitively removing the page from the guest system.167*168* Drivers must not enqueue pages while page->lru is still in169* use, and must not use page->lru until a page was unqueued again.170*/171void balloon_page_enqueue(struct balloon_dev_info *b_dev_info,172struct page *page)173{174unsigned long flags;175176spin_lock_irqsave(&balloon_pages_lock, flags);177balloon_page_enqueue_one(b_dev_info, page);178spin_unlock_irqrestore(&balloon_pages_lock, flags);179}180EXPORT_SYMBOL_GPL(balloon_page_enqueue);181182/**183* balloon_page_dequeue - removes a page from balloon's page list and returns184* its address to allow the driver to release the page.185* @b_dev_info: balloon device descriptor where we will grab a page from.186*187* Driver must call this function to properly dequeue a previously enqueued page188* before definitively releasing it back to the guest system.189*190* Caller must perform its own accounting to ensure that this191* function is called only if some pages are actually enqueued.192*193* Note that this function may fail to dequeue some pages even if there are194* some enqueued pages - since the page list can be temporarily empty due to195* the compaction of isolated pages.196*197* TODO: remove the caller accounting requirements, and allow caller to wait198* until all pages can be dequeued.199*200* Return: struct page for the dequeued page, or NULL if no page was dequeued.201*/202struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info)203{204unsigned long flags;205LIST_HEAD(pages);206int n_pages;207208n_pages = balloon_page_list_dequeue(b_dev_info, &pages, 1);209210if (n_pages != 1) {211/*212* If we are unable to dequeue a balloon page because the page213* list is empty and there are no isolated pages, then something214* went out of track and some balloon pages are lost.215* BUG() here, otherwise the balloon driver may get stuck in216* an infinite loop while attempting to release all its pages.217*/218spin_lock_irqsave(&balloon_pages_lock, flags);219if (unlikely(list_empty(&b_dev_info->pages) &&220!b_dev_info->isolated_pages))221BUG();222spin_unlock_irqrestore(&balloon_pages_lock, flags);223return NULL;224}225return list_first_entry(&pages, struct page, lru);226}227EXPORT_SYMBOL_GPL(balloon_page_dequeue);228229#ifdef CONFIG_BALLOON_MIGRATION230static struct balloon_dev_info *balloon_page_device(struct page *page)231{232return (struct balloon_dev_info *)page_private(page);233}234235static bool balloon_page_isolate(struct page *page, isolate_mode_t mode)236237{238struct balloon_dev_info *b_dev_info;239unsigned long flags;240241spin_lock_irqsave(&balloon_pages_lock, flags);242b_dev_info = balloon_page_device(page);243if (!b_dev_info) {244/*245* The page already got deflated and removed from the246* balloon list.247*/248spin_unlock_irqrestore(&balloon_pages_lock, flags);249return false;250}251list_del(&page->lru);252b_dev_info->isolated_pages++;253spin_unlock_irqrestore(&balloon_pages_lock, flags);254255return true;256}257258static void balloon_page_putback(struct page *page)259{260struct balloon_dev_info *b_dev_info = balloon_page_device(page);261unsigned long flags;262263/*264* When we isolated the page, the page was still inflated in a balloon265* device. As isolated balloon pages cannot get deflated, we still have266* a balloon device here.267*/268if (WARN_ON_ONCE(!b_dev_info))269return;270271spin_lock_irqsave(&balloon_pages_lock, flags);272list_add(&page->lru, &b_dev_info->pages);273b_dev_info->isolated_pages--;274spin_unlock_irqrestore(&balloon_pages_lock, flags);275}276277static int balloon_page_migrate(struct page *newpage, struct page *page,278enum migrate_mode mode)279{280struct balloon_dev_info *b_dev_info = balloon_page_device(page);281unsigned long flags;282int rc;283284/*285* When we isolated the page, the page was still inflated in a balloon286* device. As isolated balloon pages cannot get deflated, we still have287* a balloon device here.288*/289if (WARN_ON_ONCE(!b_dev_info))290return -EAGAIN;291292rc = b_dev_info->migratepage(b_dev_info, newpage, page, mode);293if (rc < 0 && rc != -ENOENT)294return rc;295296spin_lock_irqsave(&balloon_pages_lock, flags);297if (!rc) {298/* Insert the new page into the balloon list. */299get_page(newpage);300balloon_page_insert(b_dev_info, newpage);301__count_vm_event(BALLOON_MIGRATE);302303if (b_dev_info->adjust_managed_page_count &&304page_zone(page) != page_zone(newpage)) {305/*306* When we migrate a page to a different zone we307* have to fixup the count of both involved zones.308*/309adjust_managed_page_count(page, 1);310adjust_managed_page_count(newpage, -1);311}312} else {313/* Old page was deflated but new page not inflated. */314__count_vm_event(BALLOON_DEFLATE);315316if (b_dev_info->adjust_managed_page_count)317adjust_managed_page_count(page, 1);318}319320b_dev_info->isolated_pages--;321322/* Free the now-deflated page we isolated in balloon_page_isolate(). */323balloon_page_finalize(page);324spin_unlock_irqrestore(&balloon_pages_lock, flags);325326put_page(page);327328return 0;329}330331static const struct movable_operations balloon_mops = {332.migrate_page = balloon_page_migrate,333.isolate_page = balloon_page_isolate,334.putback_page = balloon_page_putback,335};336337static int __init balloon_init(void)338{339return set_movable_ops(&balloon_mops, PGTY_offline);340}341core_initcall(balloon_init);342343#endif /* CONFIG_BALLOON_MIGRATION */344345346