2012-03-16 05:58:34 +08:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* Copyright 2010 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
|
|
|
|
* Copyright 2011 David Gibson, IBM Corporation <dwg@au1.ibm.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/kvm.h>
|
|
|
|
#include <linux/kvm_host.h>
|
|
|
|
#include <linux/highmem.h>
|
|
|
|
#include <linux/gfp.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/hugetlb.h>
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/anon_inodes.h>
|
|
|
|
|
|
|
|
#include <asm/tlbflush.h>
|
|
|
|
#include <asm/kvm_ppc.h>
|
|
|
|
#include <asm/kvm_book3s.h>
|
|
|
|
#include <asm/mmu-hash64.h>
|
|
|
|
#include <asm/hvcall.h>
|
|
|
|
#include <asm/synch.h>
|
|
|
|
#include <asm/ppc-opcode.h>
|
|
|
|
#include <asm/kvm_host.h>
|
|
|
|
#include <asm/udbg.h>
|
2016-02-15 09:55:07 +08:00
|
|
|
#include <asm/iommu.h>
|
2012-03-16 05:58:34 +08:00
|
|
|
|
|
|
|
#define TCES_PER_PAGE (PAGE_SIZE / sizeof(u64))
|
|
|
|
|
2016-02-15 09:55:06 +08:00
|
|
|
static unsigned long kvmppc_tce_pages(unsigned long window_size)
|
2012-03-16 05:58:34 +08:00
|
|
|
{
|
2016-02-15 09:55:07 +08:00
|
|
|
return ALIGN((window_size >> IOMMU_PAGE_SHIFT_4K)
|
2012-03-16 05:58:34 +08:00
|
|
|
* sizeof(u64), PAGE_SIZE) / PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
2016-02-15 09:55:06 +08:00
|
|
|
static unsigned long kvmppc_stt_pages(unsigned long tce_pages)
|
|
|
|
{
|
|
|
|
unsigned long stt_bytes = sizeof(struct kvmppc_spapr_tce_table) +
|
|
|
|
(tce_pages * sizeof(struct page *));
|
|
|
|
|
|
|
|
return tce_pages + ALIGN(stt_bytes, PAGE_SIZE) / PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long kvmppc_account_memlimit(unsigned long stt_pages, bool inc)
|
|
|
|
{
|
|
|
|
long ret = 0;
|
|
|
|
|
|
|
|
if (!current || !current->mm)
|
|
|
|
return ret; /* process exited */
|
|
|
|
|
|
|
|
down_write(¤t->mm->mmap_sem);
|
|
|
|
|
|
|
|
if (inc) {
|
|
|
|
unsigned long locked, lock_limit;
|
|
|
|
|
|
|
|
locked = current->mm->locked_vm + stt_pages;
|
|
|
|
lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
|
|
|
|
if (locked > lock_limit && !capable(CAP_IPC_LOCK))
|
|
|
|
ret = -ENOMEM;
|
|
|
|
else
|
|
|
|
current->mm->locked_vm += stt_pages;
|
|
|
|
} else {
|
|
|
|
if (WARN_ON_ONCE(stt_pages > current->mm->locked_vm))
|
|
|
|
stt_pages = current->mm->locked_vm;
|
|
|
|
|
|
|
|
current->mm->locked_vm -= stt_pages;
|
|
|
|
}
|
|
|
|
|
|
|
|
pr_debug("[%d] RLIMIT_MEMLOCK KVM %c%ld %ld/%ld%s\n", current->pid,
|
|
|
|
inc ? '+' : '-',
|
|
|
|
stt_pages << PAGE_SHIFT,
|
|
|
|
current->mm->locked_vm << PAGE_SHIFT,
|
|
|
|
rlimit(RLIMIT_MEMLOCK),
|
|
|
|
ret ? " - exceeded" : "");
|
|
|
|
|
|
|
|
up_write(¤t->mm->mmap_sem);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-02-15 09:55:05 +08:00
|
|
|
static void release_spapr_tce_table(struct rcu_head *head)
|
2012-03-16 05:58:34 +08:00
|
|
|
{
|
2016-02-15 09:55:05 +08:00
|
|
|
struct kvmppc_spapr_tce_table *stt = container_of(head,
|
|
|
|
struct kvmppc_spapr_tce_table, rcu);
|
2012-03-16 05:58:34 +08:00
|
|
|
int i;
|
2016-02-15 09:55:06 +08:00
|
|
|
unsigned long npages = kvmppc_tce_pages(stt->window_size);
|
2012-03-16 05:58:34 +08:00
|
|
|
|
2016-02-15 09:55:06 +08:00
|
|
|
for (i = 0; i < npages; i++)
|
2012-03-16 05:58:34 +08:00
|
|
|
__free_page(stt->pages[i]);
|
|
|
|
|
2016-02-15 09:55:05 +08:00
|
|
|
kfree(stt);
|
2012-03-16 05:58:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int kvm_spapr_tce_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
|
|
|
|
{
|
|
|
|
struct kvmppc_spapr_tce_table *stt = vma->vm_file->private_data;
|
|
|
|
struct page *page;
|
|
|
|
|
2016-02-15 09:55:06 +08:00
|
|
|
if (vmf->pgoff >= kvmppc_tce_pages(stt->window_size))
|
2012-03-16 05:58:34 +08:00
|
|
|
return VM_FAULT_SIGBUS;
|
|
|
|
|
|
|
|
page = stt->pages[vmf->pgoff];
|
|
|
|
get_page(page);
|
|
|
|
vmf->page = page;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct vm_operations_struct kvm_spapr_tce_vm_ops = {
|
|
|
|
.fault = kvm_spapr_tce_fault,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int kvm_spapr_tce_mmap(struct file *file, struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
vma->vm_ops = &kvm_spapr_tce_vm_ops;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int kvm_spapr_tce_release(struct inode *inode, struct file *filp)
|
|
|
|
{
|
|
|
|
struct kvmppc_spapr_tce_table *stt = filp->private_data;
|
|
|
|
|
2016-02-15 09:55:05 +08:00
|
|
|
list_del_rcu(&stt->list);
|
|
|
|
|
|
|
|
kvm_put_kvm(stt->kvm);
|
|
|
|
|
2016-02-15 09:55:06 +08:00
|
|
|
kvmppc_account_memlimit(
|
|
|
|
kvmppc_stt_pages(kvmppc_tce_pages(stt->window_size)), false);
|
2016-02-15 09:55:05 +08:00
|
|
|
call_rcu(&stt->rcu, release_spapr_tce_table);
|
|
|
|
|
2012-03-16 05:58:34 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-05 07:09:41 +08:00
|
|
|
static const struct file_operations kvm_spapr_tce_fops = {
|
2012-03-16 05:58:34 +08:00
|
|
|
.mmap = kvm_spapr_tce_mmap,
|
|
|
|
.release = kvm_spapr_tce_release,
|
|
|
|
};
|
|
|
|
|
|
|
|
long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
|
|
|
|
struct kvm_create_spapr_tce *args)
|
|
|
|
{
|
|
|
|
struct kvmppc_spapr_tce_table *stt = NULL;
|
2016-02-15 09:55:06 +08:00
|
|
|
unsigned long npages;
|
2012-03-16 05:58:34 +08:00
|
|
|
int ret = -ENOMEM;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Check this LIOBN hasn't been previously allocated */
|
|
|
|
list_for_each_entry(stt, &kvm->arch.spapr_tce_tables, list) {
|
|
|
|
if (stt->liobn == args->liobn)
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2016-02-15 09:55:06 +08:00
|
|
|
npages = kvmppc_tce_pages(args->window_size);
|
|
|
|
ret = kvmppc_account_memlimit(kvmppc_stt_pages(npages), true);
|
|
|
|
if (ret) {
|
|
|
|
stt = NULL;
|
|
|
|
goto fail;
|
|
|
|
}
|
2012-03-16 05:58:34 +08:00
|
|
|
|
|
|
|
stt = kzalloc(sizeof(*stt) + npages * sizeof(struct page *),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!stt)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
stt->liobn = args->liobn;
|
|
|
|
stt->window_size = args->window_size;
|
|
|
|
stt->kvm = kvm;
|
|
|
|
|
|
|
|
for (i = 0; i < npages; i++) {
|
|
|
|
stt->pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO);
|
|
|
|
if (!stt->pages[i])
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
kvm_get_kvm(kvm);
|
|
|
|
|
|
|
|
mutex_lock(&kvm->lock);
|
2016-02-15 09:55:05 +08:00
|
|
|
list_add_rcu(&stt->list, &kvm->arch.spapr_tce_tables);
|
2012-03-16 05:58:34 +08:00
|
|
|
|
|
|
|
mutex_unlock(&kvm->lock);
|
|
|
|
|
|
|
|
return anon_inode_getfd("kvm-spapr-tce", &kvm_spapr_tce_fops,
|
2013-08-25 04:14:08 +08:00
|
|
|
stt, O_RDWR | O_CLOEXEC);
|
2012-03-16 05:58:34 +08:00
|
|
|
|
|
|
|
fail:
|
|
|
|
if (stt) {
|
|
|
|
for (i = 0; i < npages; i++)
|
|
|
|
if (stt->pages[i])
|
|
|
|
__free_page(stt->pages[i]);
|
|
|
|
|
|
|
|
kfree(stt);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|