blob: 5e98ac78e410de992c2792b85c1d0b085313da7c [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Christoph Lameter6225e932007-05-06 14:49:50 -07002/*
3 * Quicklist support.
4 *
5 * Quicklists are light weight lists of pages that have a defined state
6 * on alloc and free. Pages must be in the quicklist specific defined state
7 * (zero by default) when the page is freed. It seems that the initial idea
8 * for such lists first came from Dave Miller and then various other people
9 * improved on it.
10 *
11 * Copyright (C) 2007 SGI,
Christoph Lameter93e205a2016-03-17 14:21:15 -070012 * Christoph Lameter <cl@linux.com>
Christoph Lameter6225e932007-05-06 14:49:50 -070013 * Generalized, added support for multiple lists and
14 * constructors / destructors.
15 */
16#include <linux/kernel.h>
17
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/gfp.h>
Christoph Lameter6225e932007-05-06 14:49:50 -070019#include <linux/mm.h>
20#include <linux/mmzone.h>
Christoph Lameter6225e932007-05-06 14:49:50 -070021#include <linux/quicklist.h>
22
Tejun Heo204fba42009-06-24 15:13:45 +090023DEFINE_PER_CPU(struct quicklist [CONFIG_NR_QUICK], quicklist);
Christoph Lameter6225e932007-05-06 14:49:50 -070024
25#define FRACTION_OF_NODE_MEM 16
26
27static unsigned long max_pages(unsigned long min_pages)
28{
29 unsigned long node_free_pages, max;
KOSAKI Motohirob9541852008-09-02 14:35:58 -070030 int node = numa_node_id();
31 struct zone *zones = NODE_DATA(node)->node_zones;
32 int num_cpus_on_node;
Christoph Lameter6225e932007-05-06 14:49:50 -070033
Christoph Lameter96990a42008-01-14 00:55:14 -080034 node_free_pages =
35#ifdef CONFIG_ZONE_DMA
36 zone_page_state(&zones[ZONE_DMA], NR_FREE_PAGES) +
37#endif
38#ifdef CONFIG_ZONE_DMA32
39 zone_page_state(&zones[ZONE_DMA32], NR_FREE_PAGES) +
40#endif
41 zone_page_state(&zones[ZONE_NORMAL], NR_FREE_PAGES);
42
Christoph Lameter6225e932007-05-06 14:49:50 -070043 max = node_free_pages / FRACTION_OF_NODE_MEM;
KOSAKI Motohirob9541852008-09-02 14:35:58 -070044
Rusty Russelldb790782009-09-24 09:34:52 -060045 num_cpus_on_node = cpumask_weight(cpumask_of_node(node));
KOSAKI Motohirob9541852008-09-02 14:35:58 -070046 max /= num_cpus_on_node;
47
Christoph Lameter6225e932007-05-06 14:49:50 -070048 return max(max, min_pages);
49}
50
51static long min_pages_to_free(struct quicklist *q,
52 unsigned long min_pages, long max_free)
53{
54 long pages_to_free;
55
56 pages_to_free = q->nr_pages - max_pages(min_pages);
57
58 return min(pages_to_free, max_free);
59}
60
61/*
62 * Trim down the number of pages in the quicklist
63 */
64void quicklist_trim(int nr, void (*dtor)(void *),
65 unsigned long min_pages, unsigned long max_free)
66{
67 long pages_to_free;
68 struct quicklist *q;
69
70 q = &get_cpu_var(quicklist)[nr];
71 if (q->nr_pages > min_pages) {
72 pages_to_free = min_pages_to_free(q, min_pages, max_free);
73
74 while (pages_to_free > 0) {
75 /*
76 * We pass a gfp_t of 0 to quicklist_alloc here
77 * because we will never call into the page allocator.
78 */
79 void *p = quicklist_alloc(nr, 0, NULL);
80
81 if (dtor)
82 dtor(p);
83 free_page((unsigned long)p);
84 pages_to_free--;
85 }
86 }
87 put_cpu_var(quicklist);
88}
89
90unsigned long quicklist_total_size(void)
91{
92 unsigned long count = 0;
93 int cpu;
94 struct quicklist *ql, *q;
95
96 for_each_online_cpu(cpu) {
97 ql = per_cpu(quicklist, cpu);
98 for (q = ql; q < ql + CONFIG_NR_QUICK; q++)
99 count += q->nr_pages;
100 }
101 return count;
102}
103