Skip to content

Commit c77162d

Browse files
paulusmackavikivity
authored andcommitted
KVM: PPC: Only get pages when actually needed, not in prepare_memory_region()
This removes the code from kvmppc_core_prepare_memory_region() that looked up the VMA for the region being added and called hva_to_page to get the pfns for the memory. We have no guarantee that there will be anything mapped there at the time of the KVM_SET_USER_MEMORY_REGION ioctl call; userspace can do that ioctl and then map memory into the region later. Instead we defer looking up the pfn for each memory page until it is needed, which generally means when the guest does an H_ENTER hcall on the page. Since we can't call get_user_pages in real mode, if we don't already have the pfn for the page, kvmppc_h_enter() will return H_TOO_HARD and we then call kvmppc_virtmode_h_enter() once we get back to kernel context. That calls kvmppc_get_guest_page() to get the pfn for the page, and then calls back to kvmppc_h_enter() to redo the HPTE insertion. When the first vcpu starts executing, we need to have the RMO or VRMA region mapped so that the guest's real mode accesses will work. Thus we now have a check in kvmppc_vcpu_run() to see if the RMO/VRMA is set up and if not, call kvmppc_hv_setup_rma(). It checks if the memslot starting at guest physical 0 now has RMO memory mapped there; if so it sets it up for the guest, otherwise on POWER7 it sets up the VRMA. The function that does that, kvmppc_map_vrma, is now a bit simpler, as it calls kvmppc_virtmode_h_enter instead of creating the HPTE itself. Since we are now potentially updating entries in the slot_phys[] arrays from multiple vcpu threads, we now have a spinlock protecting those updates to ensure that we don't lose track of any references to pages. Signed-off-by: Paul Mackerras <paulus@samba.org> Signed-off-by: Alexander Graf <agraf@suse.de> Signed-off-by: Avi Kivity <avi@redhat.com>
1 parent 075295d commit c77162d

7 files changed

Lines changed: 290 additions & 160 deletions

File tree

‎arch/powerpc/include/asm/kvm_book3s.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ extern pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn);
141141
extern void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long addr,
142142
unsigned long *nb_ret);
143143
extern void kvmppc_unpin_guest_page(struct kvm *kvm, void *addr);
144+
extern long kvmppc_virtmode_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
145+
long pte_index, unsigned long pteh, unsigned long ptel);
146+
extern long kvmppc_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
147+
long pte_index, unsigned long pteh, unsigned long ptel);
144148

145149
extern void kvmppc_entry_trampoline(void);
146150
extern void kvmppc_hv_entry_trampoline(void);

‎arch/powerpc/include/asm/kvm_book3s_64.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,16 @@ static inline unsigned long compute_tlbie_rb(unsigned long v, unsigned long r,
101101
return rb;
102102
}
103103

104+
static inline unsigned long hpte_page_size(unsigned long h, unsigned long l)
105+
{
106+
/* only handle 4k, 64k and 16M pages for now */
107+
if (!(h & HPTE_V_LARGE))
108+
return 1ul << 12; /* 4k page */
109+
if ((l & 0xf000) == 0x1000 && cpu_has_feature(CPU_FTR_ARCH_206))
110+
return 1ul << 16; /* 64k page */
111+
if ((l & 0xff000) == 0)
112+
return 1ul << 24; /* 16M page */
113+
return 0; /* error */
114+
}
115+
104116
#endif /* __ASM_KVM_BOOK3S_64_H__ */

‎arch/powerpc/include/asm/kvm_host.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,9 @@ struct kvm_arch {
194194
unsigned long lpcr;
195195
unsigned long rmor;
196196
struct kvmppc_rma_info *rma;
197+
int rma_setup_done;
197198
struct list_head spapr_tce_tables;
199+
spinlock_t slot_phys_lock;
198200
unsigned long *slot_phys[KVM_MEM_SLOTS_NUM];
199201
int slot_npages[KVM_MEM_SLOTS_NUM];
200202
unsigned short last_vcpu[NR_CPUS];

‎arch/powerpc/include/asm/kvm_ppc.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ extern long kvmppc_alloc_hpt(struct kvm *kvm);
121121
extern void kvmppc_free_hpt(struct kvm *kvm);
122122
extern long kvmppc_prepare_vrma(struct kvm *kvm,
123123
struct kvm_userspace_memory_region *mem);
124-
extern void kvmppc_map_vrma(struct kvm *kvm,
125-
struct kvm_userspace_memory_region *mem);
124+
extern void kvmppc_map_vrma(struct kvm_vcpu *vcpu,
125+
struct kvm_memory_slot *memslot);
126126
extern int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu);
127127
extern long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
128128
struct kvm_create_spapr_tce *args);

‎arch/powerpc/kvm/book3s_64_mmu_hv.c‎

Lines changed: 106 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,17 @@ void kvmppc_free_hpt(struct kvm *kvm)
9595
free_pages(kvm->arch.hpt_virt, HPT_ORDER - PAGE_SHIFT);
9696
}
9797

98-
void kvmppc_map_vrma(struct kvm *kvm, struct kvm_userspace_memory_region *mem)
98+
void kvmppc_map_vrma(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot)
9999
{
100+
struct kvm *kvm = vcpu->kvm;
100101
unsigned long i;
101102
unsigned long npages;
102-
unsigned long pa;
103-
unsigned long *hpte;
104-
unsigned long hash;
103+
unsigned long hp_v, hp_r;
104+
unsigned long addr, hash;
105105
unsigned long porder = kvm->arch.ram_porder;
106-
struct revmap_entry *rev;
107-
unsigned long *physp;
106+
long ret;
108107

109-
physp = kvm->arch.slot_phys[mem->slot];
110-
npages = kvm->arch.slot_npages[mem->slot];
108+
npages = kvm->arch.slot_npages[memslot->id];
111109

112110
/* VRMA can't be > 1TB */
113111
if (npages > 1ul << (40 - porder))
@@ -117,10 +115,7 @@ void kvmppc_map_vrma(struct kvm *kvm, struct kvm_userspace_memory_region *mem)
117115
npages = HPT_NPTEG;
118116

119117
for (i = 0; i < npages; ++i) {
120-
pa = physp[i];
121-
if (!pa)
122-
break;
123-
pa &= PAGE_MASK;
118+
addr = i << porder;
124119
/* can't use hpt_hash since va > 64 bits */
125120
hash = (i ^ (VRMA_VSID ^ (VRMA_VSID << 25))) & HPT_HASH_MASK;
126121
/*
@@ -130,18 +125,16 @@ void kvmppc_map_vrma(struct kvm *kvm, struct kvm_userspace_memory_region *mem)
130125
* is available and use it.
131126
*/
132127
hash = (hash << 3) + 7;
133-
hpte = (unsigned long *) (kvm->arch.hpt_virt + (hash << 4));
134-
/* HPTE low word - RPN, protection, etc. */
135-
hpte[1] = pa | HPTE_R_R | HPTE_R_C | HPTE_R_M | PP_RWXX;
136-
smp_wmb();
137-
hpte[0] = HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)) |
128+
hp_v = HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)) |
138129
(i << (VRMA_PAGE_ORDER - 16)) | HPTE_V_BOLTED |
139130
HPTE_V_LARGE | HPTE_V_VALID;
140-
141-
/* Reverse map info */
142-
rev = &kvm->arch.revmap[hash];
143-
rev->guest_rpte = (i << porder) | HPTE_R_R | HPTE_R_C |
144-
HPTE_R_M | PP_RWXX;
131+
hp_r = addr | HPTE_R_R | HPTE_R_C | HPTE_R_M | PP_RWXX;
132+
ret = kvmppc_virtmode_h_enter(vcpu, H_EXACT, hash, hp_v, hp_r);
133+
if (ret != H_SUCCESS) {
134+
pr_err("KVM: map_vrma at %lx failed, ret=%ld\n",
135+
addr, ret);
136+
break;
137+
}
145138
}
146139
}
147140

@@ -178,6 +171,92 @@ static void kvmppc_mmu_book3s_64_hv_reset_msr(struct kvm_vcpu *vcpu)
178171
kvmppc_set_msr(vcpu, MSR_SF | MSR_ME);
179172
}
180173

174+
/*
175+
* This is called to get a reference to a guest page if there isn't
176+
* one already in the kvm->arch.slot_phys[][] arrays.
177+
*/
178+
static long kvmppc_get_guest_page(struct kvm *kvm, unsigned long gfn,
179+
struct kvm_memory_slot *memslot)
180+
{
181+
unsigned long start;
182+
long np;
183+
struct page *page, *pages[1];
184+
unsigned long *physp;
185+
unsigned long pfn, i;
186+
187+
physp = kvm->arch.slot_phys[memslot->id];
188+
if (!physp)
189+
return -EINVAL;
190+
i = (gfn - memslot->base_gfn) >> (kvm->arch.ram_porder - PAGE_SHIFT);
191+
if (physp[i])
192+
return 0;
193+
194+
page = NULL;
195+
start = gfn_to_hva_memslot(memslot, gfn);
196+
197+
/* Instantiate and get the page we want access to */
198+
np = get_user_pages_fast(start, 1, 1, pages);
199+
if (np != 1)
200+
return -EINVAL;
201+
page = pages[0];
202+
203+
/* Check it's a 16MB page */
204+
if (!PageHead(page) ||
205+
compound_order(page) != (kvm->arch.ram_porder - PAGE_SHIFT)) {
206+
pr_err("page at %lx isn't 16MB (o=%d)\n",
207+
start, compound_order(page));
208+
put_page(page);
209+
return -EINVAL;
210+
}
211+
pfn = page_to_pfn(page);
212+
213+
spin_lock(&kvm->arch.slot_phys_lock);
214+
if (!physp[i])
215+
physp[i] = (pfn << PAGE_SHIFT) | KVMPPC_GOT_PAGE;
216+
else
217+
put_page(page);
218+
spin_unlock(&kvm->arch.slot_phys_lock);
219+
220+
return 0;
221+
}
222+
223+
/*
224+
* We come here on a H_ENTER call from the guest when
225+
* we don't have the requested page pinned already.
226+
*/
227+
long kvmppc_virtmode_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
228+
long pte_index, unsigned long pteh, unsigned long ptel)
229+
{
230+
struct kvm *kvm = vcpu->kvm;
231+
unsigned long psize, gpa, gfn;
232+
struct kvm_memory_slot *memslot;
233+
long ret;
234+
235+
psize = hpte_page_size(pteh, ptel);
236+
if (!psize)
237+
return H_PARAMETER;
238+
239+
/* Find the memslot (if any) for this address */
240+
gpa = (ptel & HPTE_R_RPN) & ~(psize - 1);
241+
gfn = gpa >> PAGE_SHIFT;
242+
memslot = gfn_to_memslot(kvm, gfn);
243+
if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
244+
return H_PARAMETER;
245+
if (kvmppc_get_guest_page(kvm, gfn, memslot) < 0)
246+
return H_PARAMETER;
247+
248+
preempt_disable();
249+
ret = kvmppc_h_enter(vcpu, flags, pte_index, pteh, ptel);
250+
preempt_enable();
251+
if (ret == H_TOO_HARD) {
252+
/* this can't happen */
253+
pr_err("KVM: Oops, kvmppc_h_enter returned too hard!\n");
254+
ret = H_RESOURCE; /* or something */
255+
}
256+
return ret;
257+
258+
}
259+
181260
static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
182261
struct kvmppc_pte *gpte, bool data)
183262
{
@@ -203,8 +282,11 @@ void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long gpa,
203282
physp += (gfn - memslot->base_gfn) >>
204283
(kvm->arch.ram_porder - PAGE_SHIFT);
205284
pa = *physp;
206-
if (!pa)
207-
return NULL;
285+
if (!pa) {
286+
if (kvmppc_get_guest_page(kvm, gfn, memslot) < 0)
287+
return NULL;
288+
pa = *physp;
289+
}
208290
pfn = pa >> PAGE_SHIFT;
209291
page = pfn_to_page(pfn);
210292
get_page(page);

0 commit comments

Comments
 (0)