2007-07-18 09:37:06 +08:00
|
|
|
/******************************************************************************
|
|
|
|
* grant_table.c
|
|
|
|
*
|
|
|
|
* Granting foreign access to our memory reservation.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005-2006, Christopher Clark
|
|
|
|
* Copyright (c) 2004-2005, K A Fraser
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation; or, when distributed
|
|
|
|
* separately from the Linux kernel or incorporated into other
|
|
|
|
* software packages, subject to the following license:
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this source file (the "Software"), to deal in the Software without
|
|
|
|
* restriction, including without limitation the rights to use, copy, modify,
|
|
|
|
* merge, publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
* and to permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2013-06-28 18:21:41 +08:00
|
|
|
#define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
|
|
|
|
|
2007-07-18 09:37:06 +08:00
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/mm.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2007-07-18 09:37:06 +08:00
|
|
|
#include <linux/vmalloc.h>
|
|
|
|
#include <linux/uaccess.h>
|
2010-05-18 00:08:21 +08:00
|
|
|
#include <linux/io.h>
|
2012-09-14 22:26:59 +08:00
|
|
|
#include <linux/delay.h>
|
2012-04-24 18:55:43 +08:00
|
|
|
#include <linux/hardirq.h>
|
2014-12-10 02:28:37 +08:00
|
|
|
#include <linux/workqueue.h>
|
2017-07-18 15:40:35 +08:00
|
|
|
#include <linux/ratelimit.h>
|
2007-07-18 09:37:06 +08:00
|
|
|
|
2009-10-07 06:11:14 +08:00
|
|
|
#include <xen/xen.h>
|
2007-07-18 09:37:06 +08:00
|
|
|
#include <xen/interface/xen.h>
|
|
|
|
#include <xen/page.h>
|
|
|
|
#include <xen/grant_table.h>
|
2010-05-18 00:08:21 +08:00
|
|
|
#include <xen/interface/memory.h>
|
xen/granttable: Grant tables V2 implementation
Receiver-side copying of packets is based on this implementation, it gives
better performance and better CPU accounting. It totally supports three types:
full-page, sub-page and transitive grants.
However this patch does not cover sub-page and transitive grants, it mainly
focus on Full-page part and implements grant table V2 interfaces corresponding
to what already exists in grant table V1, such as: grant table V2
initialization, mapping, releasing and exported interfaces.
Each guest can only supports one type of grant table type, every entry in grant
table should be the same version. It is necessary to set V1 or V2 version before
initializing the grant table.
Grant table exported interfaces of V2 are same with those of V1, Xen is
responsible to judge what grant table version guests are using in every grant
operation.
V2 fulfills the same role of V1, and it is totally backwards compitable with V1.
If dom0 support grant table V2, the guests runing on it can run with either V1
or V2.
Acked-by: Ian Campbell <ian.campbell@citrix.com>
Signed-off-by: Annie Li <annie.li@oracle.com>
[v1: Modified alloc_vm_area call (new parameters), indentation, and cleanpatch
warnings]
Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
2011-11-22 09:59:21 +08:00
|
|
|
#include <xen/hvc-console.h>
|
2013-10-25 18:41:44 +08:00
|
|
|
#include <xen/swiotlb-xen.h>
|
2015-01-09 02:06:01 +08:00
|
|
|
#include <xen/balloon.h>
|
2008-12-17 04:37:07 +08:00
|
|
|
#include <asm/xen/hypercall.h>
|
2012-08-06 22:27:09 +08:00
|
|
|
#include <asm/xen/interface.h>
|
2007-07-18 09:37:06 +08:00
|
|
|
|
|
|
|
#include <asm/pgtable.h>
|
|
|
|
#include <asm/sync_bitops.h>
|
|
|
|
|
|
|
|
/* External tools reserve first few grant table entries. */
|
|
|
|
#define NR_RESERVED_ENTRIES 8
|
|
|
|
#define GNTTAB_LIST_END 0xffffffff
|
|
|
|
|
|
|
|
static grant_ref_t **gnttab_list;
|
|
|
|
static unsigned int nr_grant_frames;
|
|
|
|
static int gnttab_free_count;
|
|
|
|
static grant_ref_t gnttab_free_head;
|
|
|
|
static DEFINE_SPINLOCK(gnttab_list_lock);
|
2014-01-06 23:40:36 +08:00
|
|
|
struct grant_frames xen_auto_xlat_grant_frames;
|
2007-07-18 09:37:06 +08:00
|
|
|
|
2011-11-22 09:58:06 +08:00
|
|
|
static union {
|
|
|
|
struct grant_entry_v1 *v1;
|
2017-11-02 17:19:17 +08:00
|
|
|
union grant_entry_v2 *v2;
|
2011-11-22 09:58:06 +08:00
|
|
|
void *addr;
|
|
|
|
} gnttab_shared;
|
|
|
|
|
|
|
|
/*This is a structure of function pointers for grant table*/
|
|
|
|
struct gnttab_ops {
|
|
|
|
/*
|
2011-12-12 18:13:57 +08:00
|
|
|
* Mapping a list of frames for storing grant entries. Frames parameter
|
|
|
|
* is used to store grant table address when grant table being setup,
|
|
|
|
* nr_gframes is the number of frames to map grant table. Returning
|
|
|
|
* GNTST_okay means success and negative value means failure.
|
2011-11-22 09:58:06 +08:00
|
|
|
*/
|
2012-10-17 16:39:14 +08:00
|
|
|
int (*map_frames)(xen_pfn_t *frames, unsigned int nr_gframes);
|
2011-11-22 09:58:06 +08:00
|
|
|
/*
|
|
|
|
* Release a list of frames which are mapped in map_frames for grant
|
|
|
|
* entry status.
|
|
|
|
*/
|
|
|
|
void (*unmap_frames)(void);
|
|
|
|
/*
|
2011-12-12 18:13:57 +08:00
|
|
|
* Introducing a valid entry into the grant table, granting the frame of
|
|
|
|
* this grant entry to domain for accessing or transfering. Ref
|
|
|
|
* parameter is reference of this introduced grant entry, domid is id of
|
|
|
|
* granted domain, frame is the page frame to be granted, and flags is
|
|
|
|
* status of the grant entry to be updated.
|
2011-11-22 09:58:06 +08:00
|
|
|
*/
|
2011-12-12 18:13:57 +08:00
|
|
|
void (*update_entry)(grant_ref_t ref, domid_t domid,
|
|
|
|
unsigned long frame, unsigned flags);
|
2011-11-22 09:58:06 +08:00
|
|
|
/*
|
2011-12-12 18:13:57 +08:00
|
|
|
* Stop granting a grant entry to domain for accessing. Ref parameter is
|
|
|
|
* reference of a grant entry whose grant access will be stopped,
|
|
|
|
* readonly is not in use in this function. If the grant entry is
|
2011-11-22 09:58:06 +08:00
|
|
|
* currently mapped for reading or writing, just return failure(==0)
|
|
|
|
* directly and don't tear down the grant access. Otherwise, stop grant
|
|
|
|
* access for this entry and return success(==1).
|
|
|
|
*/
|
2011-12-12 18:13:57 +08:00
|
|
|
int (*end_foreign_access_ref)(grant_ref_t ref, int readonly);
|
2011-11-22 09:58:06 +08:00
|
|
|
/*
|
2011-12-12 18:13:57 +08:00
|
|
|
* Stop granting a grant entry to domain for transfer. Ref parameter is
|
|
|
|
* reference of a grant entry whose grant transfer will be stopped. If
|
|
|
|
* tranfer has not started, just reclaim the grant entry and return
|
|
|
|
* failure(==0). Otherwise, wait for the transfer to complete and then
|
|
|
|
* return the frame.
|
2011-11-22 09:58:06 +08:00
|
|
|
*/
|
2011-12-12 18:13:57 +08:00
|
|
|
unsigned long (*end_foreign_transfer_ref)(grant_ref_t ref);
|
2011-11-22 09:58:06 +08:00
|
|
|
/*
|
2011-12-12 18:13:57 +08:00
|
|
|
* Query the status of a grant entry. Ref parameter is reference of
|
2011-11-22 09:58:06 +08:00
|
|
|
* queried grant entry, return value is the status of queried entry.
|
|
|
|
* Detailed status(writing/reading) can be gotten from the return value
|
|
|
|
* by bit operations.
|
|
|
|
*/
|
2011-12-12 18:13:57 +08:00
|
|
|
int (*query_foreign_access)(grant_ref_t ref);
|
2011-11-22 09:58:06 +08:00
|
|
|
};
|
|
|
|
|
2015-04-03 14:42:59 +08:00
|
|
|
struct unmap_refs_callback_data {
|
|
|
|
struct completion completion;
|
|
|
|
int result;
|
|
|
|
};
|
|
|
|
|
2015-11-28 22:28:40 +08:00
|
|
|
static const struct gnttab_ops *gnttab_interface;
|
2011-11-22 09:58:06 +08:00
|
|
|
|
2017-11-02 17:19:17 +08:00
|
|
|
/* This reflects status of grant entries, so act as a global value. */
|
|
|
|
static grant_status_t *grstatus;
|
|
|
|
|
2011-11-22 09:58:06 +08:00
|
|
|
static int grant_table_version;
|
2013-01-15 21:21:27 +08:00
|
|
|
static int grefs_per_grant_frame;
|
2007-07-18 09:37:06 +08:00
|
|
|
|
|
|
|
static struct gnttab_free_callback *gnttab_free_callback_list;
|
|
|
|
|
|
|
|
static int gnttab_expand(unsigned int req_entries);
|
|
|
|
|
|
|
|
#define RPP (PAGE_SIZE / sizeof(grant_ref_t))
|
2017-11-02 17:19:17 +08:00
|
|
|
#define SPP (PAGE_SIZE / sizeof(grant_status_t))
|
2007-07-18 09:37:06 +08:00
|
|
|
|
|
|
|
static inline grant_ref_t *__gnttab_entry(grant_ref_t entry)
|
|
|
|
{
|
|
|
|
return &gnttab_list[(entry) / RPP][(entry) % RPP];
|
|
|
|
}
|
|
|
|
/* This can be used as an l-value */
|
|
|
|
#define gnttab_entry(entry) (*__gnttab_entry(entry))
|
|
|
|
|
|
|
|
static int get_free_entries(unsigned count)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
2011-07-23 02:00:06 +08:00
|
|
|
int ref, rc = 0;
|
2007-07-18 09:37:06 +08:00
|
|
|
grant_ref_t head;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&gnttab_list_lock, flags);
|
|
|
|
|
|
|
|
if ((gnttab_free_count < count) &&
|
|
|
|
((rc = gnttab_expand(count - gnttab_free_count)) < 0)) {
|
|
|
|
spin_unlock_irqrestore(&gnttab_list_lock, flags);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
ref = head = gnttab_free_head;
|
|
|
|
gnttab_free_count -= count;
|
|
|
|
while (count-- > 1)
|
|
|
|
head = gnttab_entry(head);
|
|
|
|
gnttab_free_head = gnttab_entry(head);
|
|
|
|
gnttab_entry(head) = GNTTAB_LIST_END;
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&gnttab_list_lock, flags);
|
|
|
|
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void do_free_callbacks(void)
|
|
|
|
{
|
|
|
|
struct gnttab_free_callback *callback, *next;
|
|
|
|
|
|
|
|
callback = gnttab_free_callback_list;
|
|
|
|
gnttab_free_callback_list = NULL;
|
|
|
|
|
|
|
|
while (callback != NULL) {
|
|
|
|
next = callback->next;
|
|
|
|
if (gnttab_free_count >= callback->count) {
|
|
|
|
callback->next = NULL;
|
|
|
|
callback->fn(callback->arg);
|
|
|
|
} else {
|
|
|
|
callback->next = gnttab_free_callback_list;
|
|
|
|
gnttab_free_callback_list = callback;
|
|
|
|
}
|
|
|
|
callback = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void check_free_callbacks(void)
|
|
|
|
{
|
|
|
|
if (unlikely(gnttab_free_callback_list))
|
|
|
|
do_free_callbacks();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void put_free_entry(grant_ref_t ref)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
spin_lock_irqsave(&gnttab_list_lock, flags);
|
|
|
|
gnttab_entry(ref) = gnttab_free_head;
|
|
|
|
gnttab_free_head = ref;
|
|
|
|
gnttab_free_count++;
|
|
|
|
check_free_callbacks();
|
|
|
|
spin_unlock_irqrestore(&gnttab_list_lock, flags);
|
|
|
|
}
|
|
|
|
|
2011-11-22 09:58:06 +08:00
|
|
|
/*
|
2017-11-02 17:19:17 +08:00
|
|
|
* Following applies to gnttab_update_entry_v1 and gnttab_update_entry_v2.
|
2011-11-22 09:58:06 +08:00
|
|
|
* Introducing a valid entry into the grant table:
|
|
|
|
* 1. Write ent->domid.
|
|
|
|
* 2. Write ent->frame:
|
|
|
|
* GTF_permit_access: Frame to which access is permitted.
|
|
|
|
* GTF_accept_transfer: Pseudo-phys frame slot being filled by new
|
|
|
|
* frame, or zero if none.
|
|
|
|
* 3. Write memory barrier (WMB).
|
|
|
|
* 4. Write ent->flags, inc. valid type.
|
|
|
|
*/
|
|
|
|
static void gnttab_update_entry_v1(grant_ref_t ref, domid_t domid,
|
|
|
|
unsigned long frame, unsigned flags)
|
2007-07-18 09:37:06 +08:00
|
|
|
{
|
2011-11-22 09:58:06 +08:00
|
|
|
gnttab_shared.v1[ref].domid = domid;
|
|
|
|
gnttab_shared.v1[ref].frame = frame;
|
2007-07-18 09:37:06 +08:00
|
|
|
wmb();
|
2011-11-22 09:58:06 +08:00
|
|
|
gnttab_shared.v1[ref].flags = flags;
|
2007-07-18 09:37:06 +08:00
|
|
|
}
|
|
|
|
|
2017-11-02 17:19:17 +08:00
|
|
|
static void gnttab_update_entry_v2(grant_ref_t ref, domid_t domid,
|
|
|
|
unsigned long frame, unsigned int flags)
|
|
|
|
{
|
|
|
|
gnttab_shared.v2[ref].hdr.domid = domid;
|
|
|
|
gnttab_shared.v2[ref].full_page.frame = frame;
|
|
|
|
wmb(); /* Hypervisor concurrent accesses. */
|
|
|
|
gnttab_shared.v2[ref].hdr.flags = GTF_permit_access | flags;
|
|
|
|
}
|
|
|
|
|
2007-07-18 09:37:06 +08:00
|
|
|
/*
|
|
|
|
* Public grant-issuing interface functions
|
|
|
|
*/
|
|
|
|
void gnttab_grant_foreign_access_ref(grant_ref_t ref, domid_t domid,
|
|
|
|
unsigned long frame, int readonly)
|
|
|
|
{
|
2011-11-22 09:58:06 +08:00
|
|
|
gnttab_interface->update_entry(ref, domid, frame,
|
2007-07-18 09:37:06 +08:00
|
|
|
GTF_permit_access | (readonly ? GTF_readonly : 0));
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access_ref);
|
|
|
|
|
|
|
|
int gnttab_grant_foreign_access(domid_t domid, unsigned long frame,
|
|
|
|
int readonly)
|
|
|
|
{
|
|
|
|
int ref;
|
|
|
|
|
|
|
|
ref = get_free_entries(1);
|
|
|
|
if (unlikely(ref < 0))
|
|
|
|
return -ENOSPC;
|
|
|
|
|
|
|
|
gnttab_grant_foreign_access_ref(ref, domid, frame, readonly);
|
|
|
|
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access);
|
|
|
|
|
2011-11-22 09:58:06 +08:00
|
|
|
static int gnttab_query_foreign_access_v1(grant_ref_t ref)
|
2007-07-18 09:37:06 +08:00
|
|
|
{
|
2011-11-22 09:58:06 +08:00
|
|
|
return gnttab_shared.v1[ref].flags & (GTF_reading|GTF_writing);
|
|
|
|
}
|
2007-07-18 09:37:06 +08:00
|
|
|
|
2017-11-02 17:19:17 +08:00
|
|
|
static int gnttab_query_foreign_access_v2(grant_ref_t ref)
|
|
|
|
{
|
|
|
|
return grstatus[ref] & (GTF_reading|GTF_writing);
|
|
|
|
}
|
|
|
|
|
2011-11-22 09:58:06 +08:00
|
|
|
int gnttab_query_foreign_access(grant_ref_t ref)
|
|
|
|
{
|
|
|
|
return gnttab_interface->query_foreign_access(ref);
|
2007-07-18 09:37:06 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gnttab_query_foreign_access);
|
|
|
|
|
2011-11-22 09:58:06 +08:00
|
|
|
static int gnttab_end_foreign_access_ref_v1(grant_ref_t ref, int readonly)
|
2007-07-18 09:37:06 +08:00
|
|
|
{
|
|
|
|
u16 flags, nflags;
|
2011-11-22 09:58:47 +08:00
|
|
|
u16 *pflags;
|
2007-07-18 09:37:06 +08:00
|
|
|
|
2011-11-22 09:58:47 +08:00
|
|
|
pflags = &gnttab_shared.v1[ref].flags;
|
|
|
|
nflags = *pflags;
|
2007-07-18 09:37:06 +08:00
|
|
|
do {
|
|
|
|
flags = nflags;
|
2012-04-05 23:10:07 +08:00
|
|
|
if (flags & (GTF_reading|GTF_writing))
|
2007-07-18 09:37:06 +08:00
|
|
|
return 0;
|
2011-11-22 09:58:47 +08:00
|
|
|
} while ((nflags = sync_cmpxchg(pflags, flags, 0)) != flags);
|
2007-07-18 09:37:06 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2011-11-22 09:58:06 +08:00
|
|
|
|
2017-11-02 17:19:17 +08:00
|
|
|
static int gnttab_end_foreign_access_ref_v2(grant_ref_t ref, int readonly)
|
|
|
|
{
|
|
|
|
gnttab_shared.v2[ref].hdr.flags = 0;
|
|
|
|
mb(); /* Concurrent access by hypervisor. */
|
|
|
|
if (grstatus[ref] & (GTF_reading|GTF_writing)) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* The read of grstatus needs to have acquire semantics.
|
|
|
|
* On x86, reads already have that, and we just need to
|
|
|
|
* protect against compiler reorderings.
|
|
|
|
* On other architectures we may need a full barrier.
|
|
|
|
*/
|
|
|
|
#ifdef CONFIG_X86
|
|
|
|
barrier();
|
|
|
|
#else
|
|
|
|
mb();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-04-05 23:10:07 +08:00
|
|
|
static inline int _gnttab_end_foreign_access_ref(grant_ref_t ref, int readonly)
|
2011-11-22 09:58:06 +08:00
|
|
|
{
|
|
|
|
return gnttab_interface->end_foreign_access_ref(ref, readonly);
|
|
|
|
}
|
2012-04-05 23:10:07 +08:00
|
|
|
|
|
|
|
int gnttab_end_foreign_access_ref(grant_ref_t ref, int readonly)
|
|
|
|
{
|
|
|
|
if (_gnttab_end_foreign_access_ref(ref, readonly))
|
|
|
|
return 1;
|
|
|
|
pr_warn("WARNING: g.e. %#x still in use!\n", ref);
|
|
|
|
return 0;
|
|
|
|
}
|
2007-07-18 09:37:06 +08:00
|
|
|
EXPORT_SYMBOL_GPL(gnttab_end_foreign_access_ref);
|
|
|
|
|
2012-04-05 23:10:07 +08:00
|
|
|
struct deferred_entry {
|
|
|
|
struct list_head list;
|
|
|
|
grant_ref_t ref;
|
|
|
|
bool ro;
|
|
|
|
uint16_t warn_delay;
|
|
|
|
struct page *page;
|
|
|
|
};
|
|
|
|
static LIST_HEAD(deferred_list);
|
|
|
|
static void gnttab_handle_deferred(unsigned long);
|
|
|
|
static DEFINE_TIMER(deferred_timer, gnttab_handle_deferred, 0, 0);
|
|
|
|
|
|
|
|
static void gnttab_handle_deferred(unsigned long unused)
|
|
|
|
{
|
|
|
|
unsigned int nr = 10;
|
|
|
|
struct deferred_entry *first = NULL;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&gnttab_list_lock, flags);
|
|
|
|
while (nr--) {
|
|
|
|
struct deferred_entry *entry
|
|
|
|
= list_first_entry(&deferred_list,
|
|
|
|
struct deferred_entry, list);
|
|
|
|
|
|
|
|
if (entry == first)
|
|
|
|
break;
|
|
|
|
list_del(&entry->list);
|
|
|
|
spin_unlock_irqrestore(&gnttab_list_lock, flags);
|
|
|
|
if (_gnttab_end_foreign_access_ref(entry->ref, entry->ro)) {
|
|
|
|
put_free_entry(entry->ref);
|
|
|
|
if (entry->page) {
|
|
|
|
pr_debug("freeing g.e. %#x (pfn %#lx)\n",
|
|
|
|
entry->ref, page_to_pfn(entry->page));
|
|
|
|
__free_page(entry->page);
|
|
|
|
} else
|
|
|
|
pr_info("freeing g.e. %#x\n", entry->ref);
|
|
|
|
kfree(entry);
|
|
|
|
entry = NULL;
|
|
|
|
} else {
|
|
|
|
if (!--entry->warn_delay)
|
2013-06-28 18:21:41 +08:00
|
|
|
pr_info("g.e. %#x still pending\n", entry->ref);
|
2012-04-05 23:10:07 +08:00
|
|
|
if (!first)
|
|
|
|
first = entry;
|
|
|
|
}
|
|
|
|
spin_lock_irqsave(&gnttab_list_lock, flags);
|
|
|
|
if (entry)
|
|
|
|
list_add_tail(&entry->list, &deferred_list);
|
|
|
|
else if (list_empty(&deferred_list))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!list_empty(&deferred_list) && !timer_pending(&deferred_timer)) {
|
|
|
|
deferred_timer.expires = jiffies + HZ;
|
|
|
|
add_timer(&deferred_timer);
|
|
|
|
}
|
|
|
|
spin_unlock_irqrestore(&gnttab_list_lock, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gnttab_add_deferred(grant_ref_t ref, bool readonly,
|
|
|
|
struct page *page)
|
|
|
|
{
|
|
|
|
struct deferred_entry *entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
|
|
|
|
const char *what = KERN_WARNING "leaking";
|
|
|
|
|
|
|
|
if (entry) {
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
entry->ref = ref;
|
|
|
|
entry->ro = readonly;
|
|
|
|
entry->page = page;
|
|
|
|
entry->warn_delay = 60;
|
|
|
|
spin_lock_irqsave(&gnttab_list_lock, flags);
|
|
|
|
list_add_tail(&entry->list, &deferred_list);
|
|
|
|
if (!timer_pending(&deferred_timer)) {
|
|
|
|
deferred_timer.expires = jiffies + HZ;
|
|
|
|
add_timer(&deferred_timer);
|
|
|
|
}
|
|
|
|
spin_unlock_irqrestore(&gnttab_list_lock, flags);
|
|
|
|
what = KERN_DEBUG "deferring";
|
|
|
|
}
|
|
|
|
printk("%s g.e. %#x (pfn %#lx)\n",
|
|
|
|
what, ref, page ? page_to_pfn(page) : -1);
|
|
|
|
}
|
|
|
|
|
2007-07-18 09:37:06 +08:00
|
|
|
void gnttab_end_foreign_access(grant_ref_t ref, int readonly,
|
|
|
|
unsigned long page)
|
|
|
|
{
|
|
|
|
if (gnttab_end_foreign_access_ref(ref, readonly)) {
|
|
|
|
put_free_entry(ref);
|
|
|
|
if (page != 0)
|
|
|
|
free_page(page);
|
2012-04-05 23:10:07 +08:00
|
|
|
} else
|
|
|
|
gnttab_add_deferred(ref, readonly,
|
|
|
|
page ? virt_to_page(page) : NULL);
|
2007-07-18 09:37:06 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gnttab_end_foreign_access);
|
|
|
|
|
|
|
|
int gnttab_grant_foreign_transfer(domid_t domid, unsigned long pfn)
|
|
|
|
{
|
|
|
|
int ref;
|
|
|
|
|
|
|
|
ref = get_free_entries(1);
|
|
|
|
if (unlikely(ref < 0))
|
|
|
|
return -ENOSPC;
|
|
|
|
gnttab_grant_foreign_transfer_ref(ref, domid, pfn);
|
|
|
|
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer);
|
|
|
|
|
|
|
|
void gnttab_grant_foreign_transfer_ref(grant_ref_t ref, domid_t domid,
|
|
|
|
unsigned long pfn)
|
|
|
|
{
|
2011-11-22 09:58:06 +08:00
|
|
|
gnttab_interface->update_entry(ref, domid, pfn, GTF_accept_transfer);
|
2007-07-18 09:37:06 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer_ref);
|
|
|
|
|
2011-11-22 09:58:06 +08:00
|
|
|
static unsigned long gnttab_end_foreign_transfer_ref_v1(grant_ref_t ref)
|
2007-07-18 09:37:06 +08:00
|
|
|
{
|
|
|
|
unsigned long frame;
|
|
|
|
u16 flags;
|
2011-11-22 09:58:47 +08:00
|
|
|
u16 *pflags;
|
|
|
|
|
|
|
|
pflags = &gnttab_shared.v1[ref].flags;
|
2007-07-18 09:37:06 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If a transfer is not even yet started, try to reclaim the grant
|
|
|
|
* reference and return failure (== 0).
|
|
|
|
*/
|
2011-11-22 09:58:47 +08:00
|
|
|
while (!((flags = *pflags) & GTF_transfer_committed)) {
|
|
|
|
if (sync_cmpxchg(pflags, flags, 0) == flags)
|
2007-07-18 09:37:06 +08:00
|
|
|
return 0;
|
|
|
|
cpu_relax();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If a transfer is in progress then wait until it is completed. */
|
|
|
|
while (!(flags & GTF_transfer_completed)) {
|
2011-11-22 09:58:47 +08:00
|
|
|
flags = *pflags;
|
2007-07-18 09:37:06 +08:00
|
|
|
cpu_relax();
|
|
|
|
}
|
|
|
|
|
|
|
|
rmb(); /* Read the frame number /after/ reading completion status. */
|
2011-11-22 09:58:06 +08:00
|
|
|
frame = gnttab_shared.v1[ref].frame;
|
2007-07-18 09:37:06 +08:00
|
|
|
BUG_ON(frame == 0);
|
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
2011-11-22 09:58:06 +08:00
|
|
|
|
2017-11-02 17:19:17 +08:00
|
|
|
static unsigned long gnttab_end_foreign_transfer_ref_v2(grant_ref_t ref)
|
|
|
|
{
|
|
|
|
unsigned long frame;
|
|
|
|
u16 flags;
|
|
|
|
u16 *pflags;
|
|
|
|
|
|
|
|
pflags = &gnttab_shared.v2[ref].hdr.flags;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If a transfer is not even yet started, try to reclaim the grant
|
|
|
|
* reference and return failure (== 0).
|
|
|
|
*/
|
|
|
|
while (!((flags = *pflags) & GTF_transfer_committed)) {
|
|
|
|
if (sync_cmpxchg(pflags, flags, 0) == flags)
|
|
|
|
return 0;
|
|
|
|
cpu_relax();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If a transfer is in progress then wait until it is completed. */
|
|
|
|
while (!(flags & GTF_transfer_completed)) {
|
|
|
|
flags = *pflags;
|
|
|
|
cpu_relax();
|
|
|
|
}
|
|
|
|
|
|
|
|
rmb(); /* Read the frame number /after/ reading completion status. */
|
|
|
|
frame = gnttab_shared.v2[ref].full_page.frame;
|
|
|
|
BUG_ON(frame == 0);
|
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2011-11-22 09:58:06 +08:00
|
|
|
unsigned long gnttab_end_foreign_transfer_ref(grant_ref_t ref)
|
|
|
|
{
|
|
|
|
return gnttab_interface->end_foreign_transfer_ref(ref);
|
|
|
|
}
|
2007-07-18 09:37:06 +08:00
|
|
|
EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer_ref);
|
|
|
|
|
|
|
|
unsigned long gnttab_end_foreign_transfer(grant_ref_t ref)
|
|
|
|
{
|
|
|
|
unsigned long frame = gnttab_end_foreign_transfer_ref(ref);
|
|
|
|
put_free_entry(ref);
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer);
|
|
|
|
|
|
|
|
void gnttab_free_grant_reference(grant_ref_t ref)
|
|
|
|
{
|
|
|
|
put_free_entry(ref);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gnttab_free_grant_reference);
|
|
|
|
|
|
|
|
void gnttab_free_grant_references(grant_ref_t head)
|
|
|
|
{
|
|
|
|
grant_ref_t ref;
|
|
|
|
unsigned long flags;
|
|
|
|
int count = 1;
|
|
|
|
if (head == GNTTAB_LIST_END)
|
|
|
|
return;
|
|
|
|
spin_lock_irqsave(&gnttab_list_lock, flags);
|
|
|
|
ref = head;
|
|
|
|
while (gnttab_entry(ref) != GNTTAB_LIST_END) {
|
|
|
|
ref = gnttab_entry(ref);
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
gnttab_entry(ref) = gnttab_free_head;
|
|
|
|
gnttab_free_head = head;
|
|
|
|
gnttab_free_count += count;
|
|
|
|
check_free_callbacks();
|
|
|
|
spin_unlock_irqrestore(&gnttab_list_lock, flags);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gnttab_free_grant_references);
|
|
|
|
|
|
|
|
int gnttab_alloc_grant_references(u16 count, grant_ref_t *head)
|
|
|
|
{
|
|
|
|
int h = get_free_entries(count);
|
|
|
|
|
|
|
|
if (h < 0)
|
|
|
|
return -ENOSPC;
|
|
|
|
|
|
|
|
*head = h;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gnttab_alloc_grant_references);
|
|
|
|
|
|
|
|
int gnttab_empty_grant_references(const grant_ref_t *private_head)
|
|
|
|
{
|
|
|
|
return (*private_head == GNTTAB_LIST_END);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gnttab_empty_grant_references);
|
|
|
|
|
|
|
|
int gnttab_claim_grant_reference(grant_ref_t *private_head)
|
|
|
|
{
|
|
|
|
grant_ref_t g = *private_head;
|
|
|
|
if (unlikely(g == GNTTAB_LIST_END))
|
|
|
|
return -ENOSPC;
|
|
|
|
*private_head = gnttab_entry(g);
|
|
|
|
return g;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gnttab_claim_grant_reference);
|
|
|
|
|
|
|
|
void gnttab_release_grant_reference(grant_ref_t *private_head,
|
|
|
|
grant_ref_t release)
|
|
|
|
{
|
|
|
|
gnttab_entry(release) = *private_head;
|
|
|
|
*private_head = release;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gnttab_release_grant_reference);
|
|
|
|
|
|
|
|
void gnttab_request_free_callback(struct gnttab_free_callback *callback,
|
|
|
|
void (*fn)(void *), void *arg, u16 count)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
2013-07-31 23:00:42 +08:00
|
|
|
struct gnttab_free_callback *cb;
|
|
|
|
|
2007-07-18 09:37:06 +08:00
|
|
|
spin_lock_irqsave(&gnttab_list_lock, flags);
|
2013-07-31 23:00:42 +08:00
|
|
|
|
|
|
|
/* Check if the callback is already on the list */
|
|
|
|
cb = gnttab_free_callback_list;
|
|
|
|
while (cb) {
|
|
|
|
if (cb == callback)
|
|
|
|
goto out;
|
|
|
|
cb = cb->next;
|
|
|
|
}
|
|
|
|
|
2007-07-18 09:37:06 +08:00
|
|
|
callback->fn = fn;
|
|
|
|
callback->arg = arg;
|
|
|
|
callback->count = count;
|
|
|
|
callback->next = gnttab_free_callback_list;
|
|
|
|
gnttab_free_callback_list = callback;
|
|
|
|
check_free_callbacks();
|
|
|
|
out:
|
|
|
|
spin_unlock_irqrestore(&gnttab_list_lock, flags);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gnttab_request_free_callback);
|
|
|
|
|
|
|
|
void gnttab_cancel_free_callback(struct gnttab_free_callback *callback)
|
|
|
|
{
|
|
|
|
struct gnttab_free_callback **pcb;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&gnttab_list_lock, flags);
|
|
|
|
for (pcb = &gnttab_free_callback_list; *pcb; pcb = &(*pcb)->next) {
|
|
|
|
if (*pcb == callback) {
|
|
|
|
*pcb = callback->next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spin_unlock_irqrestore(&gnttab_list_lock, flags);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gnttab_cancel_free_callback);
|
|
|
|
|
|
|
|
static int grow_gnttab_list(unsigned int more_frames)
|
|
|
|
{
|
|
|
|
unsigned int new_nr_grant_frames, extra_entries, i;
|
2008-04-04 17:33:48 +08:00
|
|
|
unsigned int nr_glist_frames, new_nr_glist_frames;
|
2007-07-18 09:37:06 +08:00
|
|
|
|
2013-01-15 21:21:27 +08:00
|
|
|
BUG_ON(grefs_per_grant_frame == 0);
|
|
|
|
|
2007-07-18 09:37:06 +08:00
|
|
|
new_nr_grant_frames = nr_grant_frames + more_frames;
|
2013-01-15 21:21:27 +08:00
|
|
|
extra_entries = more_frames * grefs_per_grant_frame;
|
2007-07-18 09:37:06 +08:00
|
|
|
|
2013-01-15 21:21:27 +08:00
|
|
|
nr_glist_frames = (nr_grant_frames * grefs_per_grant_frame + RPP - 1) / RPP;
|
2008-04-04 17:33:48 +08:00
|
|
|
new_nr_glist_frames =
|
2013-01-15 21:21:27 +08:00
|
|
|
(new_nr_grant_frames * grefs_per_grant_frame + RPP - 1) / RPP;
|
2008-04-04 17:33:48 +08:00
|
|
|
for (i = nr_glist_frames; i < new_nr_glist_frames; i++) {
|
2007-07-18 09:37:06 +08:00
|
|
|
gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_ATOMIC);
|
|
|
|
if (!gnttab_list[i])
|
|
|
|
goto grow_nomem;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-15 21:21:27 +08:00
|
|
|
for (i = grefs_per_grant_frame * nr_grant_frames;
|
|
|
|
i < grefs_per_grant_frame * new_nr_grant_frames - 1; i++)
|
2007-07-18 09:37:06 +08:00
|
|
|
gnttab_entry(i) = i + 1;
|
|
|
|
|
|
|
|
gnttab_entry(i) = gnttab_free_head;
|
2013-01-15 21:21:27 +08:00
|
|
|
gnttab_free_head = grefs_per_grant_frame * nr_grant_frames;
|
2007-07-18 09:37:06 +08:00
|
|
|
gnttab_free_count += extra_entries;
|
|
|
|
|
|
|
|
nr_grant_frames = new_nr_grant_frames;
|
|
|
|
|
|
|
|
check_free_callbacks();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
grow_nomem:
|
2014-08-26 23:38:44 +08:00
|
|
|
while (i-- > nr_glist_frames)
|
2007-07-18 09:37:06 +08:00
|
|
|
free_page((unsigned long) gnttab_list[i]);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int __max_nr_grant_frames(void)
|
|
|
|
{
|
|
|
|
struct gnttab_query_size query;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
query.dom = DOMID_SELF;
|
|
|
|
|
|
|
|
rc = HYPERVISOR_grant_table_op(GNTTABOP_query_size, &query, 1);
|
|
|
|
if ((rc < 0) || (query.status != GNTST_okay))
|
|
|
|
return 4; /* Legacy max supported number of frames */
|
|
|
|
|
|
|
|
return query.max_nr_frames;
|
|
|
|
}
|
|
|
|
|
2010-05-18 00:08:21 +08:00
|
|
|
unsigned int gnttab_max_grant_frames(void)
|
2007-07-18 09:37:06 +08:00
|
|
|
{
|
|
|
|
unsigned int xen_max = __max_nr_grant_frames();
|
2014-01-01 04:55:39 +08:00
|
|
|
static unsigned int boot_max_nr_grant_frames;
|
|
|
|
|
|
|
|
/* First time, initialize it properly. */
|
|
|
|
if (!boot_max_nr_grant_frames)
|
|
|
|
boot_max_nr_grant_frames = __max_nr_grant_frames();
|
2007-07-18 09:37:06 +08:00
|
|
|
|
|
|
|
if (xen_max > boot_max_nr_grant_frames)
|
|
|
|
return boot_max_nr_grant_frames;
|
|
|
|
return xen_max;
|
|
|
|
}
|
2010-05-18 00:08:21 +08:00
|
|
|
EXPORT_SYMBOL_GPL(gnttab_max_grant_frames);
|
2007-07-18 09:37:06 +08:00
|
|
|
|
2014-01-30 20:56:34 +08:00
|
|
|
int gnttab_setup_auto_xlat_frames(phys_addr_t addr)
|
2014-01-06 23:40:36 +08:00
|
|
|
{
|
|
|
|
xen_pfn_t *pfn;
|
|
|
|
unsigned int max_nr_gframes = __max_nr_grant_frames();
|
|
|
|
unsigned int i;
|
|
|
|
void *vaddr;
|
|
|
|
|
|
|
|
if (xen_auto_xlat_grant_frames.count)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2015-05-05 23:37:49 +08:00
|
|
|
vaddr = xen_remap(addr, XEN_PAGE_SIZE * max_nr_gframes);
|
2014-01-06 23:40:36 +08:00
|
|
|
if (vaddr == NULL) {
|
2014-01-30 20:56:34 +08:00
|
|
|
pr_warn("Failed to ioremap gnttab share frames (addr=%pa)!\n",
|
|
|
|
&addr);
|
2014-01-06 23:40:36 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
pfn = kcalloc(max_nr_gframes, sizeof(pfn[0]), GFP_KERNEL);
|
|
|
|
if (!pfn) {
|
|
|
|
xen_unmap(vaddr);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
for (i = 0; i < max_nr_gframes; i++)
|
2015-05-05 23:37:49 +08:00
|
|
|
pfn[i] = XEN_PFN_DOWN(addr) + i;
|
2014-01-06 23:40:36 +08:00
|
|
|
|
|
|
|
xen_auto_xlat_grant_frames.vaddr = vaddr;
|
|
|
|
xen_auto_xlat_grant_frames.pfn = pfn;
|
|
|
|
xen_auto_xlat_grant_frames.count = max_nr_gframes;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gnttab_setup_auto_xlat_frames);
|
|
|
|
|
|
|
|
void gnttab_free_auto_xlat_frames(void)
|
|
|
|
{
|
|
|
|
if (!xen_auto_xlat_grant_frames.count)
|
|
|
|
return;
|
|
|
|
kfree(xen_auto_xlat_grant_frames.pfn);
|
|
|
|
xen_unmap(xen_auto_xlat_grant_frames.vaddr);
|
|
|
|
|
|
|
|
xen_auto_xlat_grant_frames.pfn = NULL;
|
|
|
|
xen_auto_xlat_grant_frames.count = 0;
|
|
|
|
xen_auto_xlat_grant_frames.vaddr = NULL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gnttab_free_auto_xlat_frames);
|
|
|
|
|
2015-01-09 02:06:01 +08:00
|
|
|
/**
|
|
|
|
* gnttab_alloc_pages - alloc pages suitable for grant mapping into
|
|
|
|
* @nr_pages: number of pages to alloc
|
|
|
|
* @pages: returns the pages
|
|
|
|
*/
|
|
|
|
int gnttab_alloc_pages(int nr_pages, struct page **pages)
|
|
|
|
{
|
2014-12-24 22:17:06 +08:00
|
|
|
int i;
|
2015-01-09 02:06:01 +08:00
|
|
|
int ret;
|
|
|
|
|
2015-06-25 20:12:46 +08:00
|
|
|
ret = alloc_xenballooned_pages(nr_pages, pages);
|
2015-01-09 02:06:01 +08:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2014-12-24 22:17:06 +08:00
|
|
|
for (i = 0; i < nr_pages; i++) {
|
|
|
|
#if BITS_PER_LONG < 64
|
|
|
|
struct xen_page_foreign *foreign;
|
|
|
|
|
|
|
|
foreign = kzalloc(sizeof(*foreign), GFP_KERNEL);
|
|
|
|
if (!foreign) {
|
|
|
|
gnttab_free_pages(nr_pages, pages);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
set_page_private(pages[i], (unsigned long)foreign);
|
|
|
|
#endif
|
|
|
|
SetPagePrivate(pages[i]);
|
|
|
|
}
|
|
|
|
|
2015-01-09 02:06:01 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(gnttab_alloc_pages);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gnttab_free_pages - free pages allocated by gnttab_alloc_pages()
|
|
|
|
* @nr_pages; number of pages to free
|
|
|
|
* @pages: the pages
|
|
|
|
*/
|
|
|
|
void gnttab_free_pages(int nr_pages, struct page **pages)
|
|
|
|
{
|
2014-12-24 22:17:06 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nr_pages; i++) {
|
|
|
|
if (PagePrivate(pages[i])) {
|
|
|
|
#if BITS_PER_LONG < 64
|
|
|
|
kfree((void *)page_private(pages[i]));
|
|
|
|
#endif
|
|
|
|
ClearPagePrivate(pages[i]);
|
|
|
|
}
|
|
|
|
}
|
2015-01-09 02:06:01 +08:00
|
|
|
free_xenballooned_pages(nr_pages, pages);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(gnttab_free_pages);
|
|
|
|
|
2012-09-14 22:26:59 +08:00
|
|
|
/* Handling of paged out grant targets (GNTST_eagain) */
|
|
|
|
#define MAX_DELAY 256
|
|
|
|
static inline void
|
|
|
|
gnttab_retry_eagain_gop(unsigned int cmd, void *gop, int16_t *status,
|
|
|
|
const char *func)
|
|
|
|
{
|
|
|
|
unsigned delay = 1;
|
|
|
|
|
|
|
|
do {
|
|
|
|
BUG_ON(HYPERVISOR_grant_table_op(cmd, gop, 1));
|
|
|
|
if (*status == GNTST_eagain)
|
|
|
|
msleep(delay++);
|
|
|
|
} while ((*status == GNTST_eagain) && (delay < MAX_DELAY));
|
|
|
|
|
|
|
|
if (delay >= MAX_DELAY) {
|
2013-06-28 18:21:41 +08:00
|
|
|
pr_err("%s: %s eagain grant\n", func, current->comm);
|
2012-09-14 22:26:59 +08:00
|
|
|
*status = GNTST_bad_page;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void gnttab_batch_map(struct gnttab_map_grant_ref *batch, unsigned count)
|
|
|
|
{
|
|
|
|
struct gnttab_map_grant_ref *op;
|
|
|
|
|
|
|
|
if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, batch, count))
|
|
|
|
BUG();
|
|
|
|
for (op = batch; op < batch + count; op++)
|
|
|
|
if (op->status == GNTST_eagain)
|
|
|
|
gnttab_retry_eagain_gop(GNTTABOP_map_grant_ref, op,
|
|
|
|
&op->status, __func__);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gnttab_batch_map);
|
|
|
|
|
|
|
|
void gnttab_batch_copy(struct gnttab_copy *batch, unsigned count)
|
|
|
|
{
|
|
|
|
struct gnttab_copy *op;
|
|
|
|
|
|
|
|
if (HYPERVISOR_grant_table_op(GNTTABOP_copy, batch, count))
|
|
|
|
BUG();
|
|
|
|
for (op = batch; op < batch + count; op++)
|
|
|
|
if (op->status == GNTST_eagain)
|
|
|
|
gnttab_retry_eagain_gop(GNTTABOP_copy, op,
|
|
|
|
&op->status, __func__);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gnttab_batch_copy);
|
|
|
|
|
2015-06-20 00:49:03 +08:00
|
|
|
void gnttab_foreach_grant_in_range(struct page *page,
|
|
|
|
unsigned int offset,
|
|
|
|
unsigned int len,
|
|
|
|
xen_grant_fn_t fn,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
unsigned int goffset;
|
|
|
|
unsigned int glen;
|
|
|
|
unsigned long xen_pfn;
|
|
|
|
|
|
|
|
len = min_t(unsigned int, PAGE_SIZE - offset, len);
|
|
|
|
goffset = xen_offset_in_page(offset);
|
|
|
|
|
|
|
|
xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(offset);
|
|
|
|
|
|
|
|
while (len) {
|
|
|
|
glen = min_t(unsigned int, XEN_PAGE_SIZE - goffset, len);
|
|
|
|
fn(pfn_to_gfn(xen_pfn), goffset, glen, data);
|
|
|
|
|
|
|
|
goffset = 0;
|
|
|
|
xen_pfn++;
|
|
|
|
len -= glen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gnttab_foreach_grant_in_range);
|
|
|
|
|
2015-10-14 00:50:12 +08:00
|
|
|
void gnttab_foreach_grant(struct page **pages,
|
|
|
|
unsigned int nr_grefs,
|
|
|
|
xen_grant_fn_t fn,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
unsigned int goffset = 0;
|
|
|
|
unsigned long xen_pfn = 0;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nr_grefs; i++) {
|
|
|
|
if ((i % XEN_PFN_PER_PAGE) == 0) {
|
|
|
|
xen_pfn = page_to_xen_pfn(pages[i / XEN_PFN_PER_PAGE]);
|
|
|
|
goffset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn(pfn_to_gfn(xen_pfn), goffset, XEN_PAGE_SIZE, data);
|
|
|
|
|
|
|
|
goffset += XEN_PAGE_SIZE;
|
|
|
|
xen_pfn++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-03 19:43:59 +08:00
|
|
|
int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
|
2011-11-22 09:59:56 +08:00
|
|
|
struct gnttab_map_grant_ref *kmap_ops,
|
2014-02-03 19:43:59 +08:00
|
|
|
struct page **pages, unsigned int count)
|
2010-12-10 22:54:44 +08:00
|
|
|
{
|
|
|
|
int i, ret;
|
|
|
|
|
|
|
|
ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map_ops, count);
|
2010-12-13 22:42:30 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2010-12-10 22:54:44 +08:00
|
|
|
|
2014-12-24 22:17:06 +08:00
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
/* Retry eagain maps */
|
2012-09-14 22:26:59 +08:00
|
|
|
if (map_ops[i].status == GNTST_eagain)
|
|
|
|
gnttab_retry_eagain_gop(GNTTABOP_map_grant_ref, map_ops + i,
|
|
|
|
&map_ops[i].status, __func__);
|
|
|
|
|
2014-12-24 22:17:06 +08:00
|
|
|
if (map_ops[i].status == GNTST_okay) {
|
|
|
|
struct xen_page_foreign *foreign;
|
|
|
|
|
|
|
|
SetPageForeign(pages[i]);
|
|
|
|
foreign = xen_page_foreign(pages[i]);
|
|
|
|
foreign->domid = map_ops[i].dom;
|
|
|
|
foreign->gref = map_ops[i].ref;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-27 23:55:30 +08:00
|
|
|
return set_foreign_p2m_mapping(map_ops, kmap_ops, pages, count);
|
2010-12-10 22:54:44 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gnttab_map_refs);
|
|
|
|
|
2014-02-03 19:43:59 +08:00
|
|
|
int gnttab_unmap_refs(struct gnttab_unmap_grant_ref *unmap_ops,
|
2015-01-05 22:13:41 +08:00
|
|
|
struct gnttab_unmap_grant_ref *kunmap_ops,
|
2014-02-03 19:43:59 +08:00
|
|
|
struct page **pages, unsigned int count)
|
2010-12-10 22:54:44 +08:00
|
|
|
{
|
2014-12-24 22:17:06 +08:00
|
|
|
unsigned int i;
|
2014-02-27 23:55:30 +08:00
|
|
|
int ret;
|
2010-12-10 22:54:44 +08:00
|
|
|
|
|
|
|
ret = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap_ops, count);
|
2010-12-13 22:42:30 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2014-12-24 22:17:06 +08:00
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
ClearPageForeign(pages[i]);
|
|
|
|
|
2015-01-05 22:13:41 +08:00
|
|
|
return clear_foreign_p2m_mapping(unmap_ops, kunmap_ops, pages, count);
|
2010-12-10 22:54:44 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gnttab_unmap_refs);
|
|
|
|
|
2014-12-10 02:28:37 +08:00
|
|
|
#define GNTTAB_UNMAP_REFS_DELAY 5
|
|
|
|
|
|
|
|
static void __gnttab_unmap_refs_async(struct gntab_unmap_queue_data* item);
|
|
|
|
|
|
|
|
static void gnttab_unmap_work(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct gntab_unmap_queue_data
|
|
|
|
*unmap_data = container_of(work,
|
|
|
|
struct gntab_unmap_queue_data,
|
|
|
|
gnttab_work.work);
|
|
|
|
if (unmap_data->age != UINT_MAX)
|
|
|
|
unmap_data->age++;
|
|
|
|
__gnttab_unmap_refs_async(unmap_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __gnttab_unmap_refs_async(struct gntab_unmap_queue_data* item)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int pc;
|
|
|
|
|
|
|
|
for (pc = 0; pc < item->count; pc++) {
|
|
|
|
if (page_count(item->pages[pc]) > 1) {
|
|
|
|
unsigned long delay = GNTTAB_UNMAP_REFS_DELAY * (item->age + 1);
|
|
|
|
schedule_delayed_work(&item->gnttab_work,
|
|
|
|
msecs_to_jiffies(delay));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = gnttab_unmap_refs(item->unmap_ops, item->kunmap_ops,
|
|
|
|
item->pages, item->count);
|
|
|
|
item->done(ret, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
void gnttab_unmap_refs_async(struct gntab_unmap_queue_data* item)
|
|
|
|
{
|
|
|
|
INIT_DELAYED_WORK(&item->gnttab_work, gnttab_unmap_work);
|
|
|
|
item->age = 0;
|
|
|
|
|
|
|
|
__gnttab_unmap_refs_async(item);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gnttab_unmap_refs_async);
|
|
|
|
|
2015-04-03 14:42:59 +08:00
|
|
|
static void unmap_refs_callback(int result,
|
|
|
|
struct gntab_unmap_queue_data *data)
|
|
|
|
{
|
|
|
|
struct unmap_refs_callback_data *d = data->data;
|
|
|
|
|
|
|
|
d->result = result;
|
|
|
|
complete(&d->completion);
|
|
|
|
}
|
|
|
|
|
|
|
|
int gnttab_unmap_refs_sync(struct gntab_unmap_queue_data *item)
|
|
|
|
{
|
|
|
|
struct unmap_refs_callback_data data;
|
|
|
|
|
|
|
|
init_completion(&data.completion);
|
|
|
|
item->data = &data;
|
|
|
|
item->done = &unmap_refs_callback;
|
|
|
|
gnttab_unmap_refs_async(item);
|
|
|
|
wait_for_completion(&data.completion);
|
|
|
|
|
|
|
|
return data.result;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gnttab_unmap_refs_sync);
|
|
|
|
|
2017-11-02 17:19:17 +08:00
|
|
|
static unsigned int nr_status_frames(unsigned int nr_grant_frames)
|
|
|
|
{
|
|
|
|
BUG_ON(grefs_per_grant_frame == 0);
|
|
|
|
return (nr_grant_frames * grefs_per_grant_frame + SPP - 1) / SPP;
|
|
|
|
}
|
|
|
|
|
2012-10-17 16:39:14 +08:00
|
|
|
static int gnttab_map_frames_v1(xen_pfn_t *frames, unsigned int nr_gframes)
|
2011-11-22 09:58:06 +08:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = arch_gnttab_map_shared(frames, nr_gframes,
|
|
|
|
gnttab_max_grant_frames(),
|
|
|
|
&gnttab_shared.addr);
|
|
|
|
BUG_ON(rc);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gnttab_unmap_frames_v1(void)
|
|
|
|
{
|
xen/granttable: Grant tables V2 implementation
Receiver-side copying of packets is based on this implementation, it gives
better performance and better CPU accounting. It totally supports three types:
full-page, sub-page and transitive grants.
However this patch does not cover sub-page and transitive grants, it mainly
focus on Full-page part and implements grant table V2 interfaces corresponding
to what already exists in grant table V1, such as: grant table V2
initialization, mapping, releasing and exported interfaces.
Each guest can only supports one type of grant table type, every entry in grant
table should be the same version. It is necessary to set V1 or V2 version before
initializing the grant table.
Grant table exported interfaces of V2 are same with those of V1, Xen is
responsible to judge what grant table version guests are using in every grant
operation.
V2 fulfills the same role of V1, and it is totally backwards compitable with V1.
If dom0 support grant table V2, the guests runing on it can run with either V1
or V2.
Acked-by: Ian Campbell <ian.campbell@citrix.com>
Signed-off-by: Annie Li <annie.li@oracle.com>
[v1: Modified alloc_vm_area call (new parameters), indentation, and cleanpatch
warnings]
Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
2011-11-22 09:59:21 +08:00
|
|
|
arch_gnttab_unmap(gnttab_shared.addr, nr_grant_frames);
|
|
|
|
}
|
|
|
|
|
2017-11-02 17:19:17 +08:00
|
|
|
static int gnttab_map_frames_v2(xen_pfn_t *frames, unsigned int nr_gframes)
|
|
|
|
{
|
|
|
|
uint64_t *sframes;
|
|
|
|
unsigned int nr_sframes;
|
|
|
|
struct gnttab_get_status_frames getframes;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
nr_sframes = nr_status_frames(nr_gframes);
|
|
|
|
|
|
|
|
/* No need for kzalloc as it is initialized in following hypercall
|
|
|
|
* GNTTABOP_get_status_frames.
|
|
|
|
*/
|
|
|
|
sframes = kmalloc_array(nr_sframes, sizeof(uint64_t), GFP_ATOMIC);
|
|
|
|
if (!sframes)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
getframes.dom = DOMID_SELF;
|
|
|
|
getframes.nr_frames = nr_sframes;
|
|
|
|
set_xen_guest_handle(getframes.frame_list, sframes);
|
|
|
|
|
|
|
|
rc = HYPERVISOR_grant_table_op(GNTTABOP_get_status_frames,
|
|
|
|
&getframes, 1);
|
|
|
|
if (rc == -ENOSYS) {
|
|
|
|
kfree(sframes);
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
BUG_ON(rc || getframes.status);
|
|
|
|
|
|
|
|
rc = arch_gnttab_map_status(sframes, nr_sframes,
|
|
|
|
nr_status_frames(gnttab_max_grant_frames()),
|
|
|
|
&grstatus);
|
|
|
|
BUG_ON(rc);
|
|
|
|
kfree(sframes);
|
|
|
|
|
|
|
|
rc = arch_gnttab_map_shared(frames, nr_gframes,
|
|
|
|
gnttab_max_grant_frames(),
|
|
|
|
&gnttab_shared.addr);
|
|
|
|
BUG_ON(rc);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gnttab_unmap_frames_v2(void)
|
|
|
|
{
|
|
|
|
arch_gnttab_unmap(gnttab_shared.addr, nr_grant_frames);
|
|
|
|
arch_gnttab_unmap(grstatus, nr_status_frames(nr_grant_frames));
|
|
|
|
}
|
|
|
|
|
2007-07-18 09:37:06 +08:00
|
|
|
static int gnttab_map(unsigned int start_idx, unsigned int end_idx)
|
|
|
|
{
|
|
|
|
struct gnttab_setup_table setup;
|
2012-10-17 16:39:14 +08:00
|
|
|
xen_pfn_t *frames;
|
2007-07-18 09:37:06 +08:00
|
|
|
unsigned int nr_gframes = end_idx + 1;
|
|
|
|
int rc;
|
|
|
|
|
2014-01-03 23:20:18 +08:00
|
|
|
if (xen_feature(XENFEAT_auto_translated_physmap)) {
|
2010-05-18 00:08:21 +08:00
|
|
|
struct xen_add_to_physmap xatp;
|
|
|
|
unsigned int i = end_idx;
|
|
|
|
rc = 0;
|
2014-01-06 23:40:36 +08:00
|
|
|
BUG_ON(xen_auto_xlat_grant_frames.count < nr_gframes);
|
2010-05-18 00:08:21 +08:00
|
|
|
/*
|
|
|
|
* Loop backwards, so that the first hypercall has the largest
|
|
|
|
* index, ensuring that the table will grow only once.
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
xatp.domid = DOMID_SELF;
|
|
|
|
xatp.idx = i;
|
|
|
|
xatp.space = XENMAPSPACE_grant_table;
|
2014-01-06 23:40:36 +08:00
|
|
|
xatp.gpfn = xen_auto_xlat_grant_frames.pfn[i];
|
2010-05-18 00:08:21 +08:00
|
|
|
rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp);
|
|
|
|
if (rc != 0) {
|
2013-06-28 18:21:41 +08:00
|
|
|
pr_warn("grant table add_to_physmap failed, err=%d\n",
|
|
|
|
rc);
|
2010-05-18 00:08:21 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (i-- > start_idx);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
xen/granttable: Grant tables V2 implementation
Receiver-side copying of packets is based on this implementation, it gives
better performance and better CPU accounting. It totally supports three types:
full-page, sub-page and transitive grants.
However this patch does not cover sub-page and transitive grants, it mainly
focus on Full-page part and implements grant table V2 interfaces corresponding
to what already exists in grant table V1, such as: grant table V2
initialization, mapping, releasing and exported interfaces.
Each guest can only supports one type of grant table type, every entry in grant
table should be the same version. It is necessary to set V1 or V2 version before
initializing the grant table.
Grant table exported interfaces of V2 are same with those of V1, Xen is
responsible to judge what grant table version guests are using in every grant
operation.
V2 fulfills the same role of V1, and it is totally backwards compitable with V1.
If dom0 support grant table V2, the guests runing on it can run with either V1
or V2.
Acked-by: Ian Campbell <ian.campbell@citrix.com>
Signed-off-by: Annie Li <annie.li@oracle.com>
[v1: Modified alloc_vm_area call (new parameters), indentation, and cleanpatch
warnings]
Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
2011-11-22 09:59:21 +08:00
|
|
|
/* No need for kzalloc as it is initialized in following hypercall
|
|
|
|
* GNTTABOP_setup_table.
|
|
|
|
*/
|
2007-07-18 09:37:06 +08:00
|
|
|
frames = kmalloc(nr_gframes * sizeof(unsigned long), GFP_ATOMIC);
|
|
|
|
if (!frames)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
setup.dom = DOMID_SELF;
|
|
|
|
setup.nr_frames = nr_gframes;
|
2008-04-03 01:53:52 +08:00
|
|
|
set_xen_guest_handle(setup.frame_list, frames);
|
2007-07-18 09:37:06 +08:00
|
|
|
|
|
|
|
rc = HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1);
|
|
|
|
if (rc == -ENOSYS) {
|
|
|
|
kfree(frames);
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
BUG_ON(rc || setup.status);
|
|
|
|
|
2011-11-22 09:58:06 +08:00
|
|
|
rc = gnttab_interface->map_frames(frames, nr_gframes);
|
2007-07-18 09:37:06 +08:00
|
|
|
|
|
|
|
kfree(frames);
|
|
|
|
|
2011-11-22 09:58:06 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-11-28 22:28:40 +08:00
|
|
|
static const struct gnttab_ops gnttab_v1_ops = {
|
2011-11-22 09:58:06 +08:00
|
|
|
.map_frames = gnttab_map_frames_v1,
|
|
|
|
.unmap_frames = gnttab_unmap_frames_v1,
|
|
|
|
.update_entry = gnttab_update_entry_v1,
|
|
|
|
.end_foreign_access_ref = gnttab_end_foreign_access_ref_v1,
|
|
|
|
.end_foreign_transfer_ref = gnttab_end_foreign_transfer_ref_v1,
|
|
|
|
.query_foreign_access = gnttab_query_foreign_access_v1,
|
|
|
|
};
|
|
|
|
|
2017-11-02 17:19:17 +08:00
|
|
|
static const struct gnttab_ops gnttab_v2_ops = {
|
|
|
|
.map_frames = gnttab_map_frames_v2,
|
|
|
|
.unmap_frames = gnttab_unmap_frames_v2,
|
|
|
|
.update_entry = gnttab_update_entry_v2,
|
|
|
|
.end_foreign_access_ref = gnttab_end_foreign_access_ref_v2,
|
|
|
|
.end_foreign_transfer_ref = gnttab_end_foreign_transfer_ref_v2,
|
|
|
|
.query_foreign_access = gnttab_query_foreign_access_v2,
|
|
|
|
};
|
|
|
|
|
2011-11-22 09:58:06 +08:00
|
|
|
static void gnttab_request_version(void)
|
|
|
|
{
|
2017-11-02 17:19:17 +08:00
|
|
|
int rc;
|
|
|
|
struct gnttab_set_version gsv;
|
xen/granttable: Grant tables V2 implementation
Receiver-side copying of packets is based on this implementation, it gives
better performance and better CPU accounting. It totally supports three types:
full-page, sub-page and transitive grants.
However this patch does not cover sub-page and transitive grants, it mainly
focus on Full-page part and implements grant table V2 interfaces corresponding
to what already exists in grant table V1, such as: grant table V2
initialization, mapping, releasing and exported interfaces.
Each guest can only supports one type of grant table type, every entry in grant
table should be the same version. It is necessary to set V1 or V2 version before
initializing the grant table.
Grant table exported interfaces of V2 are same with those of V1, Xen is
responsible to judge what grant table version guests are using in every grant
operation.
V2 fulfills the same role of V1, and it is totally backwards compitable with V1.
If dom0 support grant table V2, the guests runing on it can run with either V1
or V2.
Acked-by: Ian Campbell <ian.campbell@citrix.com>
Signed-off-by: Annie Li <annie.li@oracle.com>
[v1: Modified alloc_vm_area call (new parameters), indentation, and cleanpatch
warnings]
Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
2011-11-22 09:59:21 +08:00
|
|
|
|
2017-11-02 17:19:17 +08:00
|
|
|
gsv.version = 1;
|
|
|
|
|
|
|
|
rc = HYPERVISOR_grant_table_op(GNTTABOP_set_version, &gsv, 1);
|
|
|
|
if (rc == 0 && gsv.version == 2) {
|
|
|
|
grant_table_version = 2;
|
|
|
|
grefs_per_grant_frame = XEN_PAGE_SIZE /
|
|
|
|
sizeof(union grant_entry_v2);
|
|
|
|
gnttab_interface = &gnttab_v2_ops;
|
|
|
|
} else {
|
|
|
|
grant_table_version = 1;
|
|
|
|
grefs_per_grant_frame = XEN_PAGE_SIZE /
|
|
|
|
sizeof(struct grant_entry_v1);
|
|
|
|
gnttab_interface = &gnttab_v1_ops;
|
|
|
|
}
|
2013-06-28 18:21:41 +08:00
|
|
|
pr_info("Grant tables using version %d layout\n", grant_table_version);
|
2007-07-18 09:37:06 +08:00
|
|
|
}
|
|
|
|
|
2013-01-15 21:21:27 +08:00
|
|
|
static int gnttab_setup(void)
|
2007-07-18 09:37:06 +08:00
|
|
|
{
|
2010-05-18 00:08:21 +08:00
|
|
|
unsigned int max_nr_gframes;
|
|
|
|
|
|
|
|
max_nr_gframes = gnttab_max_grant_frames();
|
|
|
|
if (max_nr_gframes < nr_grant_frames)
|
2007-07-18 09:37:06 +08:00
|
|
|
return -ENOSYS;
|
2010-05-18 00:08:21 +08:00
|
|
|
|
2014-01-01 05:33:31 +08:00
|
|
|
if (xen_feature(XENFEAT_auto_translated_physmap) && gnttab_shared.addr == NULL) {
|
2014-01-06 23:40:36 +08:00
|
|
|
gnttab_shared.addr = xen_auto_xlat_grant_frames.vaddr;
|
2011-11-22 09:58:06 +08:00
|
|
|
if (gnttab_shared.addr == NULL) {
|
2014-01-06 23:40:36 +08:00
|
|
|
pr_warn("gnttab share frames (addr=0x%08lx) is not mapped!\n",
|
|
|
|
(unsigned long)xen_auto_xlat_grant_frames.vaddr);
|
2010-05-18 00:08:21 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
2014-01-01 05:33:31 +08:00
|
|
|
return gnttab_map(0, nr_grant_frames - 1);
|
2007-07-18 09:37:06 +08:00
|
|
|
}
|
|
|
|
|
2013-01-15 21:21:27 +08:00
|
|
|
int gnttab_resume(void)
|
|
|
|
{
|
|
|
|
gnttab_request_version();
|
|
|
|
return gnttab_setup();
|
|
|
|
}
|
|
|
|
|
2008-05-27 06:31:27 +08:00
|
|
|
int gnttab_suspend(void)
|
2007-07-18 09:37:06 +08:00
|
|
|
{
|
2014-06-16 18:12:03 +08:00
|
|
|
if (!xen_feature(XENFEAT_auto_translated_physmap))
|
|
|
|
gnttab_interface->unmap_frames();
|
2007-07-18 09:37:06 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gnttab_expand(unsigned int req_entries)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
unsigned int cur, extra;
|
|
|
|
|
2013-01-15 21:21:27 +08:00
|
|
|
BUG_ON(grefs_per_grant_frame == 0);
|
2007-07-18 09:37:06 +08:00
|
|
|
cur = nr_grant_frames;
|
2013-01-15 21:21:27 +08:00
|
|
|
extra = ((req_entries + (grefs_per_grant_frame-1)) /
|
|
|
|
grefs_per_grant_frame);
|
2017-07-18 15:40:35 +08:00
|
|
|
if (cur + extra > gnttab_max_grant_frames()) {
|
|
|
|
pr_warn_ratelimited("xen/grant-table: max_grant_frames reached"
|
|
|
|
" cur=%u extra=%u limit=%u"
|
|
|
|
" gnttab_free_count=%u req_entries=%u\n",
|
|
|
|
cur, extra, gnttab_max_grant_frames(),
|
|
|
|
gnttab_free_count, req_entries);
|
2007-07-18 09:37:06 +08:00
|
|
|
return -ENOSPC;
|
2017-07-18 15:40:35 +08:00
|
|
|
}
|
2007-07-18 09:37:06 +08:00
|
|
|
|
|
|
|
rc = gnttab_map(cur, cur + extra - 1);
|
|
|
|
if (rc == 0)
|
|
|
|
rc = grow_gnttab_list(extra);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2010-05-18 00:08:21 +08:00
|
|
|
int gnttab_init(void)
|
2007-07-18 09:37:06 +08:00
|
|
|
{
|
|
|
|
int i;
|
2014-07-11 23:42:34 +08:00
|
|
|
unsigned long max_nr_grant_frames;
|
2008-04-04 17:33:48 +08:00
|
|
|
unsigned int max_nr_glist_frames, nr_glist_frames;
|
2007-07-18 09:37:06 +08:00
|
|
|
unsigned int nr_init_grefs;
|
2012-04-15 17:27:12 +08:00
|
|
|
int ret;
|
2007-07-18 09:37:06 +08:00
|
|
|
|
2013-01-15 21:21:27 +08:00
|
|
|
gnttab_request_version();
|
2014-07-11 23:42:34 +08:00
|
|
|
max_nr_grant_frames = gnttab_max_grant_frames();
|
2007-07-18 09:37:06 +08:00
|
|
|
nr_grant_frames = 1;
|
|
|
|
|
|
|
|
/* Determine the maximum number of frames required for the
|
|
|
|
* grant reference free list on the current hypervisor.
|
|
|
|
*/
|
2013-01-15 21:21:27 +08:00
|
|
|
BUG_ON(grefs_per_grant_frame == 0);
|
2014-07-11 23:42:34 +08:00
|
|
|
max_nr_glist_frames = (max_nr_grant_frames *
|
2013-01-15 21:21:27 +08:00
|
|
|
grefs_per_grant_frame / RPP);
|
2007-07-18 09:37:06 +08:00
|
|
|
|
|
|
|
gnttab_list = kmalloc(max_nr_glist_frames * sizeof(grant_ref_t *),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (gnttab_list == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2013-01-15 21:21:27 +08:00
|
|
|
nr_glist_frames = (nr_grant_frames * grefs_per_grant_frame + RPP - 1) / RPP;
|
2008-04-04 17:33:48 +08:00
|
|
|
for (i = 0; i < nr_glist_frames; i++) {
|
2007-07-18 09:37:06 +08:00
|
|
|
gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_KERNEL);
|
2012-04-15 17:27:12 +08:00
|
|
|
if (gnttab_list[i] == NULL) {
|
|
|
|
ret = -ENOMEM;
|
2007-07-18 09:37:06 +08:00
|
|
|
goto ini_nomem;
|
2012-04-15 17:27:12 +08:00
|
|
|
}
|
2007-07-18 09:37:06 +08:00
|
|
|
}
|
|
|
|
|
2017-11-02 17:19:17 +08:00
|
|
|
ret = arch_gnttab_init(max_nr_grant_frames,
|
|
|
|
nr_status_frames(max_nr_grant_frames));
|
2014-07-11 23:42:34 +08:00
|
|
|
if (ret < 0)
|
|
|
|
goto ini_nomem;
|
|
|
|
|
2013-01-15 21:21:27 +08:00
|
|
|
if (gnttab_setup() < 0) {
|
2012-04-15 17:27:12 +08:00
|
|
|
ret = -ENODEV;
|
|
|
|
goto ini_nomem;
|
|
|
|
}
|
2007-07-18 09:37:06 +08:00
|
|
|
|
2013-01-15 21:21:27 +08:00
|
|
|
nr_init_grefs = nr_grant_frames * grefs_per_grant_frame;
|
2007-07-18 09:37:06 +08:00
|
|
|
|
|
|
|
for (i = NR_RESERVED_ENTRIES; i < nr_init_grefs - 1; i++)
|
|
|
|
gnttab_entry(i) = i + 1;
|
|
|
|
|
|
|
|
gnttab_entry(nr_init_grefs - 1) = GNTTAB_LIST_END;
|
|
|
|
gnttab_free_count = nr_init_grefs - NR_RESERVED_ENTRIES;
|
|
|
|
gnttab_free_head = NR_RESERVED_ENTRIES;
|
|
|
|
|
|
|
|
printk("Grant table initialized\n");
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ini_nomem:
|
|
|
|
for (i--; i >= 0; i--)
|
|
|
|
free_page((unsigned long)gnttab_list[i]);
|
|
|
|
kfree(gnttab_list);
|
2012-04-15 17:27:12 +08:00
|
|
|
return ret;
|
2007-07-18 09:37:06 +08:00
|
|
|
}
|
2010-05-18 00:08:21 +08:00
|
|
|
EXPORT_SYMBOL_GPL(gnttab_init);
|
|
|
|
|
2012-12-22 05:00:00 +08:00
|
|
|
static int __gnttab_init(void)
|
2010-05-18 00:08:21 +08:00
|
|
|
{
|
2017-02-06 23:56:15 +08:00
|
|
|
if (!xen_domain())
|
|
|
|
return -ENODEV;
|
|
|
|
|
2010-05-18 00:08:21 +08:00
|
|
|
/* Delay grant-table initialization in the PV on HVM case */
|
2017-02-06 23:56:15 +08:00
|
|
|
if (xen_hvm_domain() && !xen_pvh_domain())
|
2010-05-18 00:08:21 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return gnttab_init();
|
|
|
|
}
|
2014-01-03 23:20:18 +08:00
|
|
|
/* Starts after core_initcall so that xen_pvh_gnttab_setup can be called
|
|
|
|
* beforehand to initialize xen_auto_xlat_grant_frames. */
|
|
|
|
core_initcall_sync(__gnttab_init);
|