| 1 |
/****************************************************************************** |
| 2 |
* |
| 3 |
* Xen domain paging default policy. |
| 4 |
* |
| 5 |
* Copyright (c) 2009 Citrix Systems, Inc. (Patrick Colp) |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <errno.h> |
| 22 |
|
| 23 |
#include "policy.h" |
| 24 |
|
| 25 |
|
| 26 |
#define DEFAULT_MRU_SIZE (1024 * 16) |
| 27 |
|
| 28 |
|
| 29 |
static unsigned long *mru; |
| 30 |
static unsigned int i_mru; |
| 31 |
static unsigned int mru_size; |
| 32 |
static unsigned long *bitmap; |
| 33 |
static unsigned long *unconsumed; |
| 34 |
static unsigned int unconsumed_cleared; |
| 35 |
static unsigned long current_gfn; |
| 36 |
static unsigned long max_pages; |
| 37 |
|
| 38 |
|
| 39 |
int policy_init(struct xenpaging *paging) |
| 40 |
{ |
| 41 |
int i; |
| 42 |
int rc = -ENOMEM; |
| 43 |
|
| 44 |
max_pages = paging->max_pages; |
| 45 |
|
| 46 |
/* Allocate bitmap for pages not to page out */ |
| 47 |
bitmap = bitmap_alloc(max_pages); |
| 48 |
if ( !bitmap ) |
| 49 |
goto out; |
| 50 |
/* Allocate bitmap to track unusable pages */ |
| 51 |
unconsumed = bitmap_alloc(max_pages); |
| 52 |
if ( !unconsumed ) |
| 53 |
goto out; |
| 54 |
|
| 55 |
/* Initialise MRU list of paged in pages */ |
| 56 |
if ( paging->policy_mru_size > 0 ) |
| 57 |
mru_size = paging->policy_mru_size; |
| 58 |
else |
| 59 |
mru_size = paging->policy_mru_size = DEFAULT_MRU_SIZE; |
| 60 |
|
| 61 |
mru = malloc(sizeof(*mru) * mru_size); |
| 62 |
if ( mru == NULL ) |
| 63 |
goto out; |
| 64 |
|
| 65 |
for ( i = 0; i < mru_size; i++ ) |
| 66 |
mru[i] = INVALID_MFN; |
| 67 |
|
| 68 |
/* Don't page out page 0 */ |
| 69 |
set_bit(0, bitmap); |
| 70 |
|
| 71 |
/* Start in the middle to avoid paging during BIOS startup */ |
| 72 |
current_gfn = max_pages / 2; |
| 73 |
|
| 74 |
rc = 0; |
| 75 |
out: |
| 76 |
return rc; |
| 77 |
} |
| 78 |
|
| 79 |
unsigned long policy_choose_victim(struct xenpaging *paging) |
| 80 |
{ |
| 81 |
xc_interface *xch = paging->xc_handle; |
| 82 |
unsigned long i; |
| 83 |
|
| 84 |
/* One iteration over all possible gfns */ |
| 85 |
for ( i = 0; i < max_pages; i++ ) |
| 86 |
{ |
| 87 |
/* Try next gfn */ |
| 88 |
current_gfn++; |
| 89 |
|
| 90 |
/* Restart on wrap */ |
| 91 |
if ( current_gfn >= max_pages ) |
| 92 |
current_gfn = 0; |
| 93 |
|
| 94 |
if ( (current_gfn & (BITS_PER_LONG - 1)) == 0 ) |
| 95 |
{ |
| 96 |
/* All gfns busy */ |
| 97 |
if ( ~bitmap[current_gfn >> ORDER_LONG] == 0 || ~unconsumed[current_gfn >> ORDER_LONG] == 0 ) |
| 98 |
{ |
| 99 |
current_gfn += BITS_PER_LONG; |
| 100 |
i += BITS_PER_LONG; |
| 101 |
continue; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
/* gfn busy */ |
| 106 |
if ( test_bit(current_gfn, bitmap) ) |
| 107 |
continue; |
| 108 |
|
| 109 |
/* gfn already tested */ |
| 110 |
if ( test_bit(current_gfn, unconsumed) ) |
| 111 |
continue; |
| 112 |
|
| 113 |
/* gfn found */ |
| 114 |
break; |
| 115 |
} |
| 116 |
|
| 117 |
/* Could not nominate any gfn */ |
| 118 |
if ( i >= max_pages ) |
| 119 |
{ |
| 120 |
/* No more pages, wait in poll */ |
| 121 |
paging->use_poll_timeout = 1; |
| 122 |
/* Count wrap arounds */ |
| 123 |
unconsumed_cleared++; |
| 124 |
/* Force retry every few seconds (depends on poll() timeout) */ |
| 125 |
if ( unconsumed_cleared > 123) |
| 126 |
{ |
| 127 |
/* Force retry of unconsumed gfns on next call */ |
| 128 |
bitmap_clear(unconsumed, max_pages); |
| 129 |
unconsumed_cleared = 0; |
| 130 |
DPRINTF("clearing unconsumed, current_gfn %lx", current_gfn); |
| 131 |
} |
| 132 |
return INVALID_MFN; |
| 133 |
} |
| 134 |
|
| 135 |
set_bit(current_gfn, unconsumed); |
| 136 |
return current_gfn; |
| 137 |
} |
| 138 |
|
| 139 |
void policy_notify_paged_out(unsigned long gfn) |
| 140 |
{ |
| 141 |
set_bit(gfn, bitmap); |
| 142 |
clear_bit(gfn, unconsumed); |
| 143 |
} |
| 144 |
|
| 145 |
static void policy_handle_paged_in(unsigned long gfn, int do_mru) |
| 146 |
{ |
| 147 |
unsigned long old_gfn = mru[i_mru & (mru_size - 1)]; |
| 148 |
|
| 149 |
if ( old_gfn != INVALID_MFN ) |
| 150 |
clear_bit(old_gfn, bitmap); |
| 151 |
|
| 152 |
if (do_mru) { |
| 153 |
mru[i_mru & (mru_size - 1)] = gfn; |
| 154 |
} else { |
| 155 |
clear_bit(gfn, bitmap); |
| 156 |
mru[i_mru & (mru_size - 1)] = INVALID_MFN; |
| 157 |
} |
| 158 |
|
| 159 |
i_mru++; |
| 160 |
} |
| 161 |
|
| 162 |
void policy_notify_paged_in(unsigned long gfn) |
| 163 |
{ |
| 164 |
policy_handle_paged_in(gfn, 1); |
| 165 |
} |
| 166 |
|
| 167 |
void policy_notify_paged_in_nomru(unsigned long gfn) |
| 168 |
{ |
| 169 |
policy_handle_paged_in(gfn, 0); |
| 170 |
} |
| 171 |
|
| 172 |
void policy_notify_dropped(unsigned long gfn) |
| 173 |
{ |
| 174 |
clear_bit(gfn, bitmap); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
/* |
| 179 |
* Local variables: |
| 180 |
* mode: C |
| 181 |
* c-file-style: "BSD" |
| 182 |
* c-basic-offset: 4 |
| 183 |
* tab-width: 4 |
| 184 |
* indent-tabs-mode: nil |
| 185 |
* End: |
| 186 |
*/ |