scsi: esp: check TI buffer index before read/write
[qemu/kevin.git] / page_cache.c
blob37a66e497b06cddbfafb25bbab22c8008484c9dc
1 /*
2 * Page cache for QEMU
3 * The cache is base on a hash of the page address
5 * Copyright 2012 Red Hat, Inc. and/or its affiliates
7 * Authors:
8 * Orit Wasserman <owasserm@redhat.com>
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
15 #include "qemu/osdep.h"
16 #include <glib.h>
18 #include "qemu-common.h"
19 #include "qemu/host-utils.h"
20 #include "migration/page_cache.h"
22 #ifdef DEBUG_CACHE
23 #define DPRINTF(fmt, ...) \
24 do { fprintf(stdout, "cache: " fmt, ## __VA_ARGS__); } while (0)
25 #else
26 #define DPRINTF(fmt, ...) \
27 do { } while (0)
28 #endif
30 /* the page in cache will not be replaced in two cycles */
31 #define CACHED_PAGE_LIFETIME 2
33 typedef struct CacheItem CacheItem;
35 struct CacheItem {
36 uint64_t it_addr;
37 uint64_t it_age;
38 uint8_t *it_data;
41 struct PageCache {
42 CacheItem *page_cache;
43 unsigned int page_size;
44 int64_t max_num_items;
45 uint64_t max_item_age;
46 int64_t num_items;
49 PageCache *cache_init(int64_t num_pages, unsigned int page_size)
51 int64_t i;
53 PageCache *cache;
55 if (num_pages <= 0) {
56 DPRINTF("invalid number of pages\n");
57 return NULL;
60 /* We prefer not to abort if there is no memory */
61 cache = g_try_malloc(sizeof(*cache));
62 if (!cache) {
63 DPRINTF("Failed to allocate cache\n");
64 return NULL;
66 /* round down to the nearest power of 2 */
67 if (!is_power_of_2(num_pages)) {
68 num_pages = pow2floor(num_pages);
69 DPRINTF("rounding down to %" PRId64 "\n", num_pages);
71 cache->page_size = page_size;
72 cache->num_items = 0;
73 cache->max_item_age = 0;
74 cache->max_num_items = num_pages;
76 DPRINTF("Setting cache buckets to %" PRId64 "\n", cache->max_num_items);
78 /* We prefer not to abort if there is no memory */
79 cache->page_cache = g_try_malloc((cache->max_num_items) *
80 sizeof(*cache->page_cache));
81 if (!cache->page_cache) {
82 DPRINTF("Failed to allocate cache->page_cache\n");
83 g_free(cache);
84 return NULL;
87 for (i = 0; i < cache->max_num_items; i++) {
88 cache->page_cache[i].it_data = NULL;
89 cache->page_cache[i].it_age = 0;
90 cache->page_cache[i].it_addr = -1;
93 return cache;
96 void cache_fini(PageCache *cache)
98 int64_t i;
100 g_assert(cache);
101 g_assert(cache->page_cache);
103 for (i = 0; i < cache->max_num_items; i++) {
104 g_free(cache->page_cache[i].it_data);
107 g_free(cache->page_cache);
108 cache->page_cache = NULL;
109 g_free(cache);
112 static size_t cache_get_cache_pos(const PageCache *cache,
113 uint64_t address)
115 size_t pos;
117 g_assert(cache->max_num_items);
118 pos = (address / cache->page_size) & (cache->max_num_items - 1);
119 return pos;
122 static CacheItem *cache_get_by_addr(const PageCache *cache, uint64_t addr)
124 size_t pos;
126 g_assert(cache);
127 g_assert(cache->page_cache);
129 pos = cache_get_cache_pos(cache, addr);
131 return &cache->page_cache[pos];
134 uint8_t *get_cached_data(const PageCache *cache, uint64_t addr)
136 return cache_get_by_addr(cache, addr)->it_data;
139 bool cache_is_cached(const PageCache *cache, uint64_t addr,
140 uint64_t current_age)
142 CacheItem *it;
144 it = cache_get_by_addr(cache, addr);
146 if (it->it_addr == addr) {
147 /* update the it_age when the cache hit */
148 it->it_age = current_age;
149 return true;
151 return false;
154 int cache_insert(PageCache *cache, uint64_t addr, const uint8_t *pdata,
155 uint64_t current_age)
158 CacheItem *it;
160 /* actual update of entry */
161 it = cache_get_by_addr(cache, addr);
163 if (it->it_data && it->it_addr != addr &&
164 it->it_age + CACHED_PAGE_LIFETIME > current_age) {
165 /* the cache page is fresh, don't replace it */
166 return -1;
168 /* allocate page */
169 if (!it->it_data) {
170 it->it_data = g_try_malloc(cache->page_size);
171 if (!it->it_data) {
172 DPRINTF("Error allocating page\n");
173 return -1;
175 cache->num_items++;
178 memcpy(it->it_data, pdata, cache->page_size);
180 it->it_age = current_age;
181 it->it_addr = addr;
183 return 0;
186 int64_t cache_resize(PageCache *cache, int64_t new_num_pages)
188 PageCache *new_cache;
189 int64_t i;
191 CacheItem *old_it, *new_it;
193 g_assert(cache);
195 /* cache was not inited */
196 if (cache->page_cache == NULL) {
197 return -1;
200 /* same size */
201 if (pow2floor(new_num_pages) == cache->max_num_items) {
202 return cache->max_num_items;
205 new_cache = cache_init(new_num_pages, cache->page_size);
206 if (!(new_cache)) {
207 DPRINTF("Error creating new cache\n");
208 return -1;
211 /* move all data from old cache */
212 for (i = 0; i < cache->max_num_items; i++) {
213 old_it = &cache->page_cache[i];
214 if (old_it->it_addr != -1) {
215 /* check for collision, if there is, keep MRU page */
216 new_it = cache_get_by_addr(new_cache, old_it->it_addr);
217 if (new_it->it_data && new_it->it_age >= old_it->it_age) {
218 /* keep the MRU page */
219 g_free(old_it->it_data);
220 } else {
221 if (!new_it->it_data) {
222 new_cache->num_items++;
224 g_free(new_it->it_data);
225 new_it->it_data = old_it->it_data;
226 new_it->it_age = old_it->it_age;
227 new_it->it_addr = old_it->it_addr;
232 g_free(cache->page_cache);
233 cache->page_cache = new_cache->page_cache;
234 cache->max_num_items = new_cache->max_num_items;
235 cache->num_items = new_cache->num_items;
237 g_free(new_cache);
239 return cache->max_num_items;