hc
2024-05-14 bedbef8ad3e75a304af6361af235302bcc61d06b
kernel/drivers/block/xen-blkback/blkback.c
....@@ -62,8 +62,8 @@
6262 * IO workloads.
6363 */
6464
65
-static int xen_blkif_max_buffer_pages = 1024;
66
-module_param_named(max_buffer_pages, xen_blkif_max_buffer_pages, int, 0644);
65
+static int max_buffer_pages = 1024;
66
+module_param_named(max_buffer_pages, max_buffer_pages, int, 0644);
6767 MODULE_PARM_DESC(max_buffer_pages,
6868 "Maximum number of free pages to keep in each block backend buffer");
6969
....@@ -78,8 +78,8 @@
7878 * algorithm.
7979 */
8080
81
-static int xen_blkif_max_pgrants = 1056;
82
-module_param_named(max_persistent_grants, xen_blkif_max_pgrants, int, 0644);
81
+static int max_pgrants = 1056;
82
+module_param_named(max_persistent_grants, max_pgrants, int, 0644);
8383 MODULE_PARM_DESC(max_persistent_grants,
8484 "Maximum number of grants to map persistently");
8585
....@@ -88,8 +88,8 @@
8888 * use. The time is in seconds, 0 means indefinitely long.
8989 */
9090
91
-static unsigned int xen_blkif_pgrant_timeout = 60;
92
-module_param_named(persistent_grant_unused_seconds, xen_blkif_pgrant_timeout,
91
+static unsigned int pgrant_timeout = 60;
92
+module_param_named(persistent_grant_unused_seconds, pgrant_timeout,
9393 uint, 0644);
9494 MODULE_PARM_DESC(persistent_grant_unused_seconds,
9595 "Time in seconds an unused persistent grant is allowed to "
....@@ -132,72 +132,10 @@
132132
133133 #define BLKBACK_INVALID_HANDLE (~0)
134134
135
-/* Number of free pages to remove on each call to gnttab_free_pages */
136
-#define NUM_BATCH_FREE_PAGES 10
137
-
138135 static inline bool persistent_gnt_timeout(struct persistent_gnt *persistent_gnt)
139136 {
140
- return xen_blkif_pgrant_timeout &&
141
- (jiffies - persistent_gnt->last_used >=
142
- HZ * xen_blkif_pgrant_timeout);
143
-}
144
-
145
-static inline int get_free_page(struct xen_blkif_ring *ring, struct page **page)
146
-{
147
- unsigned long flags;
148
-
149
- spin_lock_irqsave(&ring->free_pages_lock, flags);
150
- if (list_empty(&ring->free_pages)) {
151
- BUG_ON(ring->free_pages_num != 0);
152
- spin_unlock_irqrestore(&ring->free_pages_lock, flags);
153
- return gnttab_alloc_pages(1, page);
154
- }
155
- BUG_ON(ring->free_pages_num == 0);
156
- page[0] = list_first_entry(&ring->free_pages, struct page, lru);
157
- list_del(&page[0]->lru);
158
- ring->free_pages_num--;
159
- spin_unlock_irqrestore(&ring->free_pages_lock, flags);
160
-
161
- return 0;
162
-}
163
-
164
-static inline void put_free_pages(struct xen_blkif_ring *ring, struct page **page,
165
- int num)
166
-{
167
- unsigned long flags;
168
- int i;
169
-
170
- spin_lock_irqsave(&ring->free_pages_lock, flags);
171
- for (i = 0; i < num; i++)
172
- list_add(&page[i]->lru, &ring->free_pages);
173
- ring->free_pages_num += num;
174
- spin_unlock_irqrestore(&ring->free_pages_lock, flags);
175
-}
176
-
177
-static inline void shrink_free_pagepool(struct xen_blkif_ring *ring, int num)
178
-{
179
- /* Remove requested pages in batches of NUM_BATCH_FREE_PAGES */
180
- struct page *page[NUM_BATCH_FREE_PAGES];
181
- unsigned int num_pages = 0;
182
- unsigned long flags;
183
-
184
- spin_lock_irqsave(&ring->free_pages_lock, flags);
185
- while (ring->free_pages_num > num) {
186
- BUG_ON(list_empty(&ring->free_pages));
187
- page[num_pages] = list_first_entry(&ring->free_pages,
188
- struct page, lru);
189
- list_del(&page[num_pages]->lru);
190
- ring->free_pages_num--;
191
- if (++num_pages == NUM_BATCH_FREE_PAGES) {
192
- spin_unlock_irqrestore(&ring->free_pages_lock, flags);
193
- gnttab_free_pages(num_pages, page);
194
- spin_lock_irqsave(&ring->free_pages_lock, flags);
195
- num_pages = 0;
196
- }
197
- }
198
- spin_unlock_irqrestore(&ring->free_pages_lock, flags);
199
- if (num_pages != 0)
200
- gnttab_free_pages(num_pages, page);
137
+ return pgrant_timeout && (jiffies - persistent_gnt->last_used >=
138
+ HZ * pgrant_timeout);
201139 }
202140
203141 #define vaddr(page) ((unsigned long)pfn_to_kaddr(page_to_pfn(page)))
....@@ -234,7 +172,7 @@
234172 struct persistent_gnt *this;
235173 struct xen_blkif *blkif = ring->blkif;
236174
237
- if (ring->persistent_gnt_c >= xen_blkif_max_pgrants) {
175
+ if (ring->persistent_gnt_c >= max_pgrants) {
238176 if (!blkif->vbd.overflow_max_grants)
239177 blkif->vbd.overflow_max_grants = 1;
240178 return -EBUSY;
....@@ -332,7 +270,8 @@
332270 unmap_data.count = segs_to_unmap;
333271 BUG_ON(gnttab_unmap_refs_sync(&unmap_data));
334272
335
- put_free_pages(ring, pages, segs_to_unmap);
273
+ gnttab_page_cache_put(&ring->free_pages, pages,
274
+ segs_to_unmap);
336275 segs_to_unmap = 0;
337276 }
338277
....@@ -372,7 +311,8 @@
372311 if (++segs_to_unmap == BLKIF_MAX_SEGMENTS_PER_REQUEST) {
373312 unmap_data.count = segs_to_unmap;
374313 BUG_ON(gnttab_unmap_refs_sync(&unmap_data));
375
- put_free_pages(ring, pages, segs_to_unmap);
314
+ gnttab_page_cache_put(&ring->free_pages, pages,
315
+ segs_to_unmap);
376316 segs_to_unmap = 0;
377317 }
378318 kfree(persistent_gnt);
....@@ -380,7 +320,7 @@
380320 if (segs_to_unmap > 0) {
381321 unmap_data.count = segs_to_unmap;
382322 BUG_ON(gnttab_unmap_refs_sync(&unmap_data));
383
- put_free_pages(ring, pages, segs_to_unmap);
323
+ gnttab_page_cache_put(&ring->free_pages, pages, segs_to_unmap);
384324 }
385325 }
386326
....@@ -397,14 +337,13 @@
397337 goto out;
398338 }
399339
400
- if (ring->persistent_gnt_c < xen_blkif_max_pgrants ||
401
- (ring->persistent_gnt_c == xen_blkif_max_pgrants &&
340
+ if (ring->persistent_gnt_c < max_pgrants ||
341
+ (ring->persistent_gnt_c == max_pgrants &&
402342 !ring->blkif->vbd.overflow_max_grants)) {
403343 num_clean = 0;
404344 } else {
405
- num_clean = (xen_blkif_max_pgrants / 100) * LRU_PERCENT_CLEAN;
406
- num_clean = ring->persistent_gnt_c - xen_blkif_max_pgrants +
407
- num_clean;
345
+ num_clean = (max_pgrants / 100) * LRU_PERCENT_CLEAN;
346
+ num_clean = ring->persistent_gnt_c - max_pgrants + num_clean;
408347 num_clean = min(ring->persistent_gnt_c, num_clean);
409348 pr_debug("Going to purge at least %u persistent grants\n",
410349 num_clean);
....@@ -599,8 +538,7 @@
599538 current->comm, ring->st_oo_req,
600539 ring->st_rd_req, ring->st_wr_req,
601540 ring->st_f_req, ring->st_ds_req,
602
- ring->persistent_gnt_c,
603
- xen_blkif_max_pgrants);
541
+ ring->persistent_gnt_c, max_pgrants);
604542 ring->st_print = jiffies + msecs_to_jiffies(10 * 1000);
605543 ring->st_rd_req = 0;
606544 ring->st_wr_req = 0;
....@@ -665,8 +603,12 @@
665603 ring->next_lru = jiffies + msecs_to_jiffies(LRU_INTERVAL);
666604 }
667605
668
- /* Shrink if we have more than xen_blkif_max_buffer_pages */
669
- shrink_free_pagepool(ring, xen_blkif_max_buffer_pages);
606
+ /* Shrink the free pages pool if it is too large. */
607
+ if (time_before(jiffies, blkif->buffer_squeeze_end))
608
+ gnttab_page_cache_shrink(&ring->free_pages, 0);
609
+ else
610
+ gnttab_page_cache_shrink(&ring->free_pages,
611
+ max_buffer_pages);
670612
671613 if (log_stats && time_after(jiffies, ring->st_print))
672614 print_stats(ring);
....@@ -697,7 +639,7 @@
697639 ring->persistent_gnt_c = 0;
698640
699641 /* Since we are shutting down remove all pages from the buffer */
700
- shrink_free_pagepool(ring, 0 /* All */);
642
+ gnttab_page_cache_shrink(&ring->free_pages, 0 /* All */);
701643 }
702644
703645 static unsigned int xen_blkbk_unmap_prepare(
....@@ -736,7 +678,7 @@
736678 but is this the best way to deal with this? */
737679 BUG_ON(result);
738680
739
- put_free_pages(ring, data->pages, data->count);
681
+ gnttab_page_cache_put(&ring->free_pages, data->pages, data->count);
740682 make_response(ring, pending_req->id,
741683 pending_req->operation, pending_req->status);
742684 free_req(ring, pending_req);
....@@ -803,7 +745,8 @@
803745 if (invcount) {
804746 ret = gnttab_unmap_refs(unmap, NULL, unmap_pages, invcount);
805747 BUG_ON(ret);
806
- put_free_pages(ring, unmap_pages, invcount);
748
+ gnttab_page_cache_put(&ring->free_pages, unmap_pages,
749
+ invcount);
807750 }
808751 pages += batch;
809752 num -= batch;
....@@ -850,8 +793,11 @@
850793 pages[i]->page = persistent_gnt->page;
851794 pages[i]->persistent_gnt = persistent_gnt;
852795 } else {
853
- if (get_free_page(ring, &pages[i]->page)) {
854
- put_free_pages(ring, pages_to_gnt, segs_to_map);
796
+ if (gnttab_page_cache_get(&ring->free_pages,
797
+ &pages[i]->page)) {
798
+ gnttab_page_cache_put(&ring->free_pages,
799
+ pages_to_gnt,
800
+ segs_to_map);
855801 ret = -ENOMEM;
856802 goto out;
857803 }
....@@ -884,7 +830,8 @@
884830 BUG_ON(new_map_idx >= segs_to_map);
885831 if (unlikely(map[new_map_idx].status != 0)) {
886832 pr_debug("invalid buffer -- could not remap it\n");
887
- put_free_pages(ring, &pages[seg_idx]->page, 1);
833
+ gnttab_page_cache_put(&ring->free_pages,
834
+ &pages[seg_idx]->page, 1);
888835 pages[seg_idx]->handle = BLKBACK_INVALID_HANDLE;
889836 ret |= !ret;
890837 goto next;
....@@ -894,7 +841,7 @@
894841 continue;
895842 }
896843 if (use_persistent_gnts &&
897
- ring->persistent_gnt_c < xen_blkif_max_pgrants) {
844
+ ring->persistent_gnt_c < max_pgrants) {
898845 /*
899846 * We are using persistent grants, the grant is
900847 * not mapped but we might have room for it.
....@@ -921,7 +868,7 @@
921868 pages[seg_idx]->persistent_gnt = persistent_gnt;
922869 pr_debug("grant %u added to the tree of persistent grants, using %u/%u\n",
923870 persistent_gnt->gnt, ring->persistent_gnt_c,
924
- xen_blkif_max_pgrants);
871
+ max_pgrants);
925872 goto next;
926873 }
927874 if (use_persistent_gnts && !blkif->vbd.overflow_max_grants) {
....@@ -1274,7 +1221,7 @@
12741221 break;
12751222 case BLKIF_OP_WRITE_BARRIER:
12761223 drain = true;
1277
- /* fall through */
1224
+ fallthrough;
12781225 case BLKIF_OP_FLUSH_DISKCACHE:
12791226 ring->st_f_req++;
12801227 operation = REQ_OP_WRITE;
....@@ -1520,5 +1467,13 @@
15201467
15211468 module_init(xen_blkif_init);
15221469
1470
+static void __exit xen_blkif_fini(void)
1471
+{
1472
+ xen_blkif_xenbus_fini();
1473
+ xen_blkif_interface_fini();
1474
+}
1475
+
1476
+module_exit(xen_blkif_fini);
1477
+
15231478 MODULE_LICENSE("Dual BSD/GPL");
15241479 MODULE_ALIAS("xen-backend:vbd");