ram: Split host_from_stream_offset() into two helper functions

Split host_from_stream_offset() into two parts:
One is to get ram block, which the block idstr may be get from migration
stream, the other is to get hva (host) address from block and the offset.
Besides, we will do the check working in a new helper offset_in_ramblock().

Signed-off-by: zhanghailiang <zhang.zhanghailiang@huawei.com>
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Reviewed-by: Amit Shah <amit.shah@redhat.com>
Message-Id: <1452829066-9764-2-git-send-email-zhang.zhanghailiang@huawei.com>
Signed-off-by: Amit Shah <amit.shah@redhat.com>
This commit is contained in:
zhanghailiang 2016-01-15 11:37:41 +08:00 committed by Amit Shah
parent d38ea87ac5
commit 4c4bad4861
2 changed files with 31 additions and 17 deletions

View File

@ -38,10 +38,14 @@ struct RAMBlock {
int fd; int fd;
}; };
static inline bool offset_in_ramblock(RAMBlock *b, ram_addr_t offset)
{
return (b && b->host && offset < b->used_length) ? true : false;
}
static inline void *ramblock_ptr(RAMBlock *block, ram_addr_t offset) static inline void *ramblock_ptr(RAMBlock *block, ram_addr_t offset)
{ {
assert(offset < block->used_length); assert(offset_in_ramblock(block, offset));
assert(block->host);
return (char *)block->host + offset; return (char *)block->host + offset;
} }

View File

@ -2124,28 +2124,24 @@ static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)
* Returns a pointer from within the RCU-protected ram_list. * Returns a pointer from within the RCU-protected ram_list.
*/ */
/* /*
* Read a RAMBlock ID from the stream f, find the host address of the * Read a RAMBlock ID from the stream f.
* start of that block and add on 'offset'
* *
* f: Stream to read from * f: Stream to read from
* offset: Offset within the block
* flags: Page flags (mostly to see if it's a continuation of previous block) * flags: Page flags (mostly to see if it's a continuation of previous block)
*/ */
static inline void *host_from_stream_offset(QEMUFile *f, static inline RAMBlock *ram_block_from_stream(QEMUFile *f,
ram_addr_t offset, int flags)
int flags)
{ {
static RAMBlock *block = NULL; static RAMBlock *block = NULL;
char id[256]; char id[256];
uint8_t len; uint8_t len;
if (flags & RAM_SAVE_FLAG_CONTINUE) { if (flags & RAM_SAVE_FLAG_CONTINUE) {
if (!block || block->max_length <= offset) { if (!block) {
error_report("Ack, bad migration stream!"); error_report("Ack, bad migration stream!");
return NULL; return NULL;
} }
return block;
return block->host + offset;
} }
len = qemu_get_byte(f); len = qemu_get_byte(f);
@ -2153,12 +2149,22 @@ static inline void *host_from_stream_offset(QEMUFile *f,
id[len] = 0; id[len] = 0;
block = qemu_ram_block_by_name(id); block = qemu_ram_block_by_name(id);
if (block && block->max_length > offset) { if (!block) {
return block->host + offset; error_report("Can't find block %s", id);
return NULL;
} }
error_report("Can't find block %s", id); return block;
return NULL; }
static inline void *host_from_ram_block_offset(RAMBlock *block,
ram_addr_t offset)
{
if (!offset_in_ramblock(block, offset)) {
return NULL;
}
return block->host + offset;
} }
/* /*
@ -2302,7 +2308,9 @@ static int ram_load_postcopy(QEMUFile *f)
trace_ram_load_postcopy_loop((uint64_t)addr, flags); trace_ram_load_postcopy_loop((uint64_t)addr, flags);
place_needed = false; place_needed = false;
if (flags & (RAM_SAVE_FLAG_COMPRESS | RAM_SAVE_FLAG_PAGE)) { if (flags & (RAM_SAVE_FLAG_COMPRESS | RAM_SAVE_FLAG_PAGE)) {
host = host_from_stream_offset(f, addr, flags); RAMBlock *block = ram_block_from_stream(f, flags);
host = host_from_ram_block_offset(block, addr);
if (!host) { if (!host) {
error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); error_report("Illegal RAM offset " RAM_ADDR_FMT, addr);
ret = -EINVAL; ret = -EINVAL;
@ -2433,7 +2441,9 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)
if (flags & (RAM_SAVE_FLAG_COMPRESS | RAM_SAVE_FLAG_PAGE | if (flags & (RAM_SAVE_FLAG_COMPRESS | RAM_SAVE_FLAG_PAGE |
RAM_SAVE_FLAG_COMPRESS_PAGE | RAM_SAVE_FLAG_XBZRLE)) { RAM_SAVE_FLAG_COMPRESS_PAGE | RAM_SAVE_FLAG_XBZRLE)) {
host = host_from_stream_offset(f, addr, flags); RAMBlock *block = ram_block_from_stream(f, flags);
host = host_from_ram_block_offset(block, addr);
if (!host) { if (!host) {
error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); error_report("Illegal RAM offset " RAM_ADDR_FMT, addr);
ret = -EINVAL; ret = -EINVAL;