hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/xen/grant-table.c
....@@ -33,7 +33,7 @@
3333
3434 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
3535
36
-#include <linux/bootmem.h>
36
+#include <linux/memblock.h>
3737 #include <linux/sched.h>
3838 #include <linux/mm.h>
3939 #include <linux/slab.h>
....@@ -64,7 +64,6 @@
6464 #include <asm/xen/hypercall.h>
6565 #include <asm/xen/interface.h>
6666
67
-#include <asm/pgtable.h>
6867 #include <asm/sync_bitops.h>
6968
7069 /* External tools reserve first few grant table entries. */
....@@ -135,12 +134,9 @@
135134 */
136135 unsigned long (*end_foreign_transfer_ref)(grant_ref_t ref);
137136 /*
138
- * Query the status of a grant entry. Ref parameter is reference of
139
- * queried grant entry, return value is the status of queried entry.
140
- * Detailed status(writing/reading) can be gotten from the return value
141
- * by bit operations.
137
+ * Read the frame number related to a given grant reference.
142138 */
143
- int (*query_foreign_access)(grant_ref_t ref);
139
+ unsigned long (*read_frame)(grant_ref_t ref);
144140 };
145141
146142 struct unmap_refs_callback_data {
....@@ -285,22 +281,6 @@
285281 }
286282 EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access);
287283
288
-static int gnttab_query_foreign_access_v1(grant_ref_t ref)
289
-{
290
- return gnttab_shared.v1[ref].flags & (GTF_reading|GTF_writing);
291
-}
292
-
293
-static int gnttab_query_foreign_access_v2(grant_ref_t ref)
294
-{
295
- return grstatus[ref] & (GTF_reading|GTF_writing);
296
-}
297
-
298
-int gnttab_query_foreign_access(grant_ref_t ref)
299
-{
300
- return gnttab_interface->query_foreign_access(ref);
301
-}
302
-EXPORT_SYMBOL_GPL(gnttab_query_foreign_access);
303
-
304284 static int gnttab_end_foreign_access_ref_v1(grant_ref_t ref, int readonly)
305285 {
306286 u16 flags, nflags;
....@@ -354,6 +334,16 @@
354334 }
355335 EXPORT_SYMBOL_GPL(gnttab_end_foreign_access_ref);
356336
337
+static unsigned long gnttab_read_frame_v1(grant_ref_t ref)
338
+{
339
+ return gnttab_shared.v1[ref].frame;
340
+}
341
+
342
+static unsigned long gnttab_read_frame_v2(grant_ref_t ref)
343
+{
344
+ return gnttab_shared.v2[ref].full_page.frame;
345
+}
346
+
357347 struct deferred_entry {
358348 struct list_head list;
359349 grant_ref_t ref;
....@@ -383,12 +373,9 @@
383373 spin_unlock_irqrestore(&gnttab_list_lock, flags);
384374 if (_gnttab_end_foreign_access_ref(entry->ref, entry->ro)) {
385375 put_free_entry(entry->ref);
386
- if (entry->page) {
387
- pr_debug("freeing g.e. %#x (pfn %#lx)\n",
388
- entry->ref, page_to_pfn(entry->page));
389
- put_page(entry->page);
390
- } else
391
- pr_info("freeing g.e. %#x\n", entry->ref);
376
+ pr_debug("freeing g.e. %#x (pfn %#lx)\n",
377
+ entry->ref, page_to_pfn(entry->page));
378
+ put_page(entry->page);
392379 kfree(entry);
393380 entry = NULL;
394381 } else {
....@@ -413,8 +400,17 @@
413400 static void gnttab_add_deferred(grant_ref_t ref, bool readonly,
414401 struct page *page)
415402 {
416
- struct deferred_entry *entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
403
+ struct deferred_entry *entry;
404
+ gfp_t gfp = (in_atomic() || irqs_disabled()) ? GFP_ATOMIC : GFP_KERNEL;
417405 const char *what = KERN_WARNING "leaking";
406
+
407
+ entry = kmalloc(sizeof(*entry), gfp);
408
+ if (!page) {
409
+ unsigned long gfn = gnttab_interface->read_frame(ref);
410
+
411
+ page = pfn_to_page(gfn_to_pfn(gfn));
412
+ get_page(page);
413
+ }
418414
419415 if (entry) {
420416 unsigned long flags;
....@@ -436,11 +432,21 @@
436432 what, ref, page ? page_to_pfn(page) : -1);
437433 }
438434
435
+int gnttab_try_end_foreign_access(grant_ref_t ref)
436
+{
437
+ int ret = _gnttab_end_foreign_access_ref(ref, 0);
438
+
439
+ if (ret)
440
+ put_free_entry(ref);
441
+
442
+ return ret;
443
+}
444
+EXPORT_SYMBOL_GPL(gnttab_try_end_foreign_access);
445
+
439446 void gnttab_end_foreign_access(grant_ref_t ref, int readonly,
440447 unsigned long page)
441448 {
442
- if (gnttab_end_foreign_access_ref(ref, readonly)) {
443
- put_free_entry(ref);
449
+ if (gnttab_try_end_foreign_access(ref)) {
444450 if (page != 0)
445451 put_page(virt_to_page(page));
446452 } else
....@@ -664,7 +670,6 @@
664670 unsigned int nr_glist_frames, new_nr_glist_frames;
665671 unsigned int grefs_per_frame;
666672
667
- BUG_ON(gnttab_interface == NULL);
668673 grefs_per_frame = gnttab_interface->grefs_per_grant_frame;
669674
670675 new_nr_grant_frames = nr_grant_frames + more_frames;
....@@ -803,7 +808,7 @@
803808 {
804809 int ret;
805810
806
- ret = alloc_xenballooned_pages(nr_pages, pages);
811
+ ret = xen_alloc_unpopulated_pages(nr_pages, pages);
807812 if (ret < 0)
808813 return ret;
809814
....@@ -814,6 +819,129 @@
814819 return ret;
815820 }
816821 EXPORT_SYMBOL_GPL(gnttab_alloc_pages);
822
+
823
+#ifdef CONFIG_XEN_UNPOPULATED_ALLOC
824
+static inline void cache_init(struct gnttab_page_cache *cache)
825
+{
826
+ cache->pages = NULL;
827
+}
828
+
829
+static inline bool cache_empty(struct gnttab_page_cache *cache)
830
+{
831
+ return !cache->pages;
832
+}
833
+
834
+static inline struct page *cache_deq(struct gnttab_page_cache *cache)
835
+{
836
+ struct page *page;
837
+
838
+ page = cache->pages;
839
+ cache->pages = page->zone_device_data;
840
+
841
+ return page;
842
+}
843
+
844
+static inline void cache_enq(struct gnttab_page_cache *cache, struct page *page)
845
+{
846
+ page->zone_device_data = cache->pages;
847
+ cache->pages = page;
848
+}
849
+#else
850
+static inline void cache_init(struct gnttab_page_cache *cache)
851
+{
852
+ INIT_LIST_HEAD(&cache->pages);
853
+}
854
+
855
+static inline bool cache_empty(struct gnttab_page_cache *cache)
856
+{
857
+ return list_empty(&cache->pages);
858
+}
859
+
860
+static inline struct page *cache_deq(struct gnttab_page_cache *cache)
861
+{
862
+ struct page *page;
863
+
864
+ page = list_first_entry(&cache->pages, struct page, lru);
865
+ list_del(&page->lru);
866
+
867
+ return page;
868
+}
869
+
870
+static inline void cache_enq(struct gnttab_page_cache *cache, struct page *page)
871
+{
872
+ list_add(&page->lru, &cache->pages);
873
+}
874
+#endif
875
+
876
+void gnttab_page_cache_init(struct gnttab_page_cache *cache)
877
+{
878
+ spin_lock_init(&cache->lock);
879
+ cache_init(cache);
880
+ cache->num_pages = 0;
881
+}
882
+EXPORT_SYMBOL_GPL(gnttab_page_cache_init);
883
+
884
+int gnttab_page_cache_get(struct gnttab_page_cache *cache, struct page **page)
885
+{
886
+ unsigned long flags;
887
+
888
+ spin_lock_irqsave(&cache->lock, flags);
889
+
890
+ if (cache_empty(cache)) {
891
+ spin_unlock_irqrestore(&cache->lock, flags);
892
+ return gnttab_alloc_pages(1, page);
893
+ }
894
+
895
+ page[0] = cache_deq(cache);
896
+ cache->num_pages--;
897
+
898
+ spin_unlock_irqrestore(&cache->lock, flags);
899
+
900
+ return 0;
901
+}
902
+EXPORT_SYMBOL_GPL(gnttab_page_cache_get);
903
+
904
+void gnttab_page_cache_put(struct gnttab_page_cache *cache, struct page **page,
905
+ unsigned int num)
906
+{
907
+ unsigned long flags;
908
+ unsigned int i;
909
+
910
+ spin_lock_irqsave(&cache->lock, flags);
911
+
912
+ for (i = 0; i < num; i++)
913
+ cache_enq(cache, page[i]);
914
+ cache->num_pages += num;
915
+
916
+ spin_unlock_irqrestore(&cache->lock, flags);
917
+}
918
+EXPORT_SYMBOL_GPL(gnttab_page_cache_put);
919
+
920
+void gnttab_page_cache_shrink(struct gnttab_page_cache *cache, unsigned int num)
921
+{
922
+ struct page *page[10];
923
+ unsigned int i = 0;
924
+ unsigned long flags;
925
+
926
+ spin_lock_irqsave(&cache->lock, flags);
927
+
928
+ while (cache->num_pages > num) {
929
+ page[i] = cache_deq(cache);
930
+ cache->num_pages--;
931
+ if (++i == ARRAY_SIZE(page)) {
932
+ spin_unlock_irqrestore(&cache->lock, flags);
933
+ gnttab_free_pages(i, page);
934
+ i = 0;
935
+ spin_lock_irqsave(&cache->lock, flags);
936
+ }
937
+ }
938
+
939
+ spin_unlock_irqrestore(&cache->lock, flags);
940
+
941
+ if (i != 0)
942
+ gnttab_free_pages(i, page);
943
+}
944
+EXPORT_SYMBOL_GPL(gnttab_page_cache_shrink);
817945
818946 void gnttab_pages_clear_private(int nr_pages, struct page **pages)
819947 {
....@@ -838,7 +966,7 @@
838966 void gnttab_free_pages(int nr_pages, struct page **pages)
839967 {
840968 gnttab_pages_clear_private(nr_pages, pages);
841
- free_xenballooned_pages(nr_pages, pages);
969
+ xen_free_unpopulated_pages(nr_pages, pages);
842970 }
843971 EXPORT_SYMBOL_GPL(gnttab_free_pages);
844972
....@@ -852,6 +980,9 @@
852980 unsigned long pfn, start_pfn;
853981 size_t size;
854982 int i, ret;
983
+
984
+ if (args->nr_pages < 0 || args->nr_pages > (INT_MAX >> PAGE_SHIFT))
985
+ return -ENOMEM;
855986
856987 size = args->nr_pages << PAGE_SHIFT;
857988 if (args->coherent)
....@@ -1160,7 +1291,6 @@
11601291
11611292 static unsigned int nr_status_frames(unsigned int nr_grant_frames)
11621293 {
1163
- BUG_ON(gnttab_interface == NULL);
11641294 return gnttab_frames(nr_grant_frames, SPP);
11651295 }
11661296
....@@ -1297,7 +1427,7 @@
12971427 .update_entry = gnttab_update_entry_v1,
12981428 .end_foreign_access_ref = gnttab_end_foreign_access_ref_v1,
12991429 .end_foreign_transfer_ref = gnttab_end_foreign_transfer_ref_v1,
1300
- .query_foreign_access = gnttab_query_foreign_access_v1,
1430
+ .read_frame = gnttab_read_frame_v1,
13011431 };
13021432
13031433 static const struct gnttab_ops gnttab_v2_ops = {
....@@ -1309,7 +1439,7 @@
13091439 .update_entry = gnttab_update_entry_v2,
13101440 .end_foreign_access_ref = gnttab_end_foreign_access_ref_v2,
13111441 .end_foreign_transfer_ref = gnttab_end_foreign_transfer_ref_v2,
1312
- .query_foreign_access = gnttab_query_foreign_access_v2,
1442
+ .read_frame = gnttab_read_frame_v2,
13131443 };
13141444
13151445 static bool gnttab_need_v2(void)
....@@ -1363,8 +1493,7 @@
13631493 if (xen_feature(XENFEAT_auto_translated_physmap) && gnttab_shared.addr == NULL) {
13641494 gnttab_shared.addr = xen_auto_xlat_grant_frames.vaddr;
13651495 if (gnttab_shared.addr == NULL) {
1366
- pr_warn("gnttab share frames (addr=0x%08lx) is not mapped!\n",
1367
- (unsigned long)xen_auto_xlat_grant_frames.vaddr);
1496
+ pr_warn("gnttab share frames is not mapped!\n");
13681497 return -ENOMEM;
13691498 }
13701499 }
....@@ -1389,7 +1518,6 @@
13891518 int rc;
13901519 unsigned int cur, extra;
13911520
1392
- BUG_ON(gnttab_interface == NULL);
13931521 cur = nr_grant_frames;
13941522 extra = ((req_entries + gnttab_interface->grefs_per_grant_frame - 1) /
13951523 gnttab_interface->grefs_per_grant_frame);
....@@ -1424,7 +1552,6 @@
14241552 /* Determine the maximum number of frames required for the
14251553 * grant reference free list on the current hypervisor.
14261554 */
1427
- BUG_ON(gnttab_interface == NULL);
14281555 max_nr_glist_frames = (max_nr_grant_frames *
14291556 gnttab_interface->grefs_per_grant_frame / RPP);
14301557