Commit 8e51e414a3c6d92ef2cc41720c67342a8e2c0bf7

Authored by Kent Overstreet
Committed by Kent Overstreet
1 parent 47cd2eb0ee

bcache: Use standard utility code

Some of bcache's utility code has made it into the rest of the kernel,
so drop the bcache versions.

Bcache used to have a workaround for allocating from a bio set under
generic_make_request() (if you allocated more than once, the bios you
already allocated would get stuck on current->bio_list when you
submitted, and you'd risk deadlock) - bcache would mask out __GFP_WAIT
when allocating bios under generic_make_request() so that allocation
could fail and it could retry from workqueue. But bio_alloc_bioset() has
a workaround now, so we can drop this hack and the associated error
handling.

Signed-off-by: Kent Overstreet <koverstreet@google.com>

Showing 8 changed files with 51 additions and 144 deletions Side-by-side Diff

drivers/md/bcache/btree.c
... ... @@ -350,7 +350,7 @@
350 350 bkey_copy(&k.key, &b->key);
351 351 SET_PTR_OFFSET(&k.key, 0, PTR_OFFSET(&k.key, 0) + bset_offset(b, i));
352 352  
353   - if (!bch_bio_alloc_pages(b->bio, GFP_NOIO)) {
  353 + if (!bio_alloc_pages(b->bio, GFP_NOIO)) {
354 354 int j;
355 355 struct bio_vec *bv;
356 356 void *base = (void *) ((unsigned long) i & ~(PAGE_SIZE - 1));
... ... @@ -1865,7 +1865,7 @@
1865 1865 should_split(b))
1866 1866 goto out;
1867 1867  
1868   - op->replace = KEY(op->inode, bio_end(bio), bio_sectors(bio));
  1868 + op->replace = KEY(op->inode, bio_end_sector(bio), bio_sectors(bio));
1869 1869  
1870 1870 SET_KEY_PTRS(&op->replace, 1);
1871 1871 get_random_bytes(&op->replace.ptr[0], sizeof(uint64_t));
... ... @@ -2194,9 +2194,6 @@
2194 2194 KEY_OFFSET(k) - bio->bi_sector);
2195 2195  
2196 2196 n = bch_bio_split(bio, sectors, GFP_NOIO, s->d->bio_split);
2197   - if (!n)
2198   - return -EAGAIN;
2199   -
2200 2197 if (n == bio)
2201 2198 op->lookup_done = true;
2202 2199  
drivers/md/bcache/debug.c
... ... @@ -199,7 +199,7 @@
199 199 if (!check)
200 200 return;
201 201  
202   - if (bch_bio_alloc_pages(check, GFP_NOIO))
  202 + if (bio_alloc_pages(check, GFP_NOIO))
203 203 goto out_put;
204 204  
205 205 check->bi_rw = READ_SYNC;
drivers/md/bcache/io.c
... ... @@ -68,13 +68,6 @@
68 68 * The newly allocated bio will point to @bio's bi_io_vec, if the split was on a
69 69 * bvec boundry; it is the caller's responsibility to ensure that @bio is not
70 70 * freed before the split.
71   - *
72   - * If bch_bio_split() is running under generic_make_request(), it's not safe to
73   - * allocate more than one bio from the same bio set. Therefore, if it is running
74   - * under generic_make_request() it masks out __GFP_WAIT when doing the
75   - * allocation. The caller must check for failure if there's any possibility of
76   - * it being called from under generic_make_request(); it is then the caller's
77   - * responsibility to retry from a safe context (by e.g. punting to workqueue).
78 71 */
79 72 struct bio *bch_bio_split(struct bio *bio, int sectors,
80 73 gfp_t gfp, struct bio_set *bs)
... ... @@ -85,15 +78,6 @@
85 78  
86 79 BUG_ON(sectors <= 0);
87 80  
88   - /*
89   - * If we're being called from underneath generic_make_request() and we
90   - * already allocated any bios from this bio set, we risk deadlock if we
91   - * use the mempool. So instead, we possibly fail and let the caller punt
92   - * to workqueue or somesuch and retry in a safe context.
93   - */
94   - if (current->bio_list)
95   - gfp &= ~__GFP_WAIT;
96   -
97 81 if (sectors >= bio_sectors(bio))
98 82 return bio;
99 83  
100 84  
101 85  
... ... @@ -164,17 +148,18 @@
164 148 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
165 149 unsigned max_segments = min_t(unsigned, BIO_MAX_PAGES,
166 150 queue_max_segments(q));
167   - struct bio_vec *bv, *end = bio_iovec(bio) +
168   - min_t(int, bio_segments(bio), max_segments);
169 151  
170 152 if (bio->bi_rw & REQ_DISCARD)
171 153 return min(ret, q->limits.max_discard_sectors);
172 154  
173 155 if (bio_segments(bio) > max_segments ||
174 156 q->merge_bvec_fn) {
  157 + struct bio_vec *bv;
  158 + int i, seg = 0;
  159 +
175 160 ret = 0;
176 161  
177   - for (bv = bio_iovec(bio); bv < end; bv++) {
  162 + bio_for_each_segment(bv, bio, i) {
178 163 struct bvec_merge_data bvm = {
179 164 .bi_bdev = bio->bi_bdev,
180 165 .bi_sector = bio->bi_sector,
181 166  
... ... @@ -182,10 +167,14 @@
182 167 .bi_rw = bio->bi_rw,
183 168 };
184 169  
  170 + if (seg == max_segments)
  171 + break;
  172 +
185 173 if (q->merge_bvec_fn &&
186 174 q->merge_bvec_fn(q, &bvm, bv) < (int) bv->bv_len)
187 175 break;
188 176  
  177 + seg++;
189 178 ret += bv->bv_len >> 9;
190 179 }
191 180 }
192 181  
... ... @@ -222,30 +211,10 @@
222 211 closure_put(cl);
223 212 }
224 213  
225   -static void __bch_bio_submit_split(struct closure *cl)
226   -{
227   - struct bio_split_hook *s = container_of(cl, struct bio_split_hook, cl);
228   - struct bio *bio = s->bio, *n;
229   -
230   - do {
231   - n = bch_bio_split(bio, bch_bio_max_sectors(bio),
232   - GFP_NOIO, s->p->bio_split);
233   - if (!n)
234   - continue_at(cl, __bch_bio_submit_split, system_wq);
235   -
236   - n->bi_end_io = bch_bio_submit_split_endio;
237   - n->bi_private = cl;
238   -
239   - closure_get(cl);
240   - bch_generic_make_request_hack(n);
241   - } while (n != bio);
242   -
243   - continue_at(cl, bch_bio_submit_split_done, NULL);
244   -}
245   -
246 214 void bch_generic_make_request(struct bio *bio, struct bio_split_pool *p)
247 215 {
248 216 struct bio_split_hook *s;
  217 + struct bio *n;
249 218  
250 219 if (!bio_has_data(bio) && !(bio->bi_rw & REQ_DISCARD))
251 220 goto submit;
... ... @@ -254,6 +223,7 @@
254 223 goto submit;
255 224  
256 225 s = mempool_alloc(p->bio_split_hook, GFP_NOIO);
  226 + closure_init(&s->cl, NULL);
257 227  
258 228 s->bio = bio;
259 229 s->p = p;
... ... @@ -261,8 +231,18 @@
261 231 s->bi_private = bio->bi_private;
262 232 bio_get(bio);
263 233  
264   - closure_call(&s->cl, __bch_bio_submit_split, NULL, NULL);
265   - return;
  234 + do {
  235 + n = bch_bio_split(bio, bch_bio_max_sectors(bio),
  236 + GFP_NOIO, s->p->bio_split);
  237 +
  238 + n->bi_end_io = bch_bio_submit_split_endio;
  239 + n->bi_private = &s->cl;
  240 +
  241 + closure_get(&s->cl);
  242 + bch_generic_make_request_hack(n);
  243 + } while (n != bio);
  244 +
  245 + continue_at(&s->cl, bch_bio_submit_split_done, NULL);
266 246 submit:
267 247 bch_generic_make_request_hack(bio);
268 248 }
drivers/md/bcache/movinggc.c
... ... @@ -46,9 +46,10 @@
46 46 {
47 47 struct moving_io *io = container_of(cl, struct moving_io, s.cl);
48 48 struct bio *bio = &io->bio.bio;
49   - struct bio_vec *bv = bio_iovec_idx(bio, bio->bi_vcnt);
  49 + struct bio_vec *bv;
  50 + int i;
50 51  
51   - while (bv-- != bio->bi_io_vec)
  52 + bio_for_each_segment_all(bv, bio, i)
52 53 __free_page(bv->bv_page);
53 54  
54 55 if (io->s.op.insert_collision)
... ... @@ -158,7 +159,7 @@
158 159 bio->bi_rw = READ;
159 160 bio->bi_end_io = read_moving_endio;
160 161  
161   - if (bch_bio_alloc_pages(bio, GFP_KERNEL))
  162 + if (bio_alloc_pages(bio, GFP_KERNEL))
162 163 goto err;
163 164  
164 165 trace_bcache_gc_copy(&w->key);
drivers/md/bcache/request.c
... ... @@ -509,10 +509,6 @@
509 509 goto err;
510 510  
511 511 n = bch_bio_split(bio, KEY_SIZE(k), GFP_NOIO, split);
512   - if (!n) {
513   - __bkey_put(op->c, k);
514   - continue_at(cl, bch_insert_data_loop, bcache_wq);
515   - }
516 512  
517 513 n->bi_end_io = bch_insert_data_endio;
518 514 n->bi_private = cl;
519 515  
520 516  
... ... @@ -821,54 +817,14 @@
821 817 */
822 818  
823 819 if (s->op.cache_bio) {
824   - struct bio_vec *src, *dst;
825   - unsigned src_offset, dst_offset, bytes;
826   - void *dst_ptr;
827   -
828 820 bio_reset(s->op.cache_bio);
829 821 s->op.cache_bio->bi_sector = s->cache_miss->bi_sector;
830 822 s->op.cache_bio->bi_bdev = s->cache_miss->bi_bdev;
831 823 s->op.cache_bio->bi_size = s->cache_bio_sectors << 9;
832 824 bch_bio_map(s->op.cache_bio, NULL);
833 825  
834   - src = bio_iovec(s->op.cache_bio);
835   - dst = bio_iovec(s->cache_miss);
836   - src_offset = src->bv_offset;
837   - dst_offset = dst->bv_offset;
838   - dst_ptr = kmap(dst->bv_page);
  826 + bio_copy_data(s->cache_miss, s->op.cache_bio);
839 827  
840   - while (1) {
841   - if (dst_offset == dst->bv_offset + dst->bv_len) {
842   - kunmap(dst->bv_page);
843   - dst++;
844   - if (dst == bio_iovec_idx(s->cache_miss,
845   - s->cache_miss->bi_vcnt))
846   - break;
847   -
848   - dst_offset = dst->bv_offset;
849   - dst_ptr = kmap(dst->bv_page);
850   - }
851   -
852   - if (src_offset == src->bv_offset + src->bv_len) {
853   - src++;
854   - if (src == bio_iovec_idx(s->op.cache_bio,
855   - s->op.cache_bio->bi_vcnt))
856   - BUG();
857   -
858   - src_offset = src->bv_offset;
859   - }
860   -
861   - bytes = min(dst->bv_offset + dst->bv_len - dst_offset,
862   - src->bv_offset + src->bv_len - src_offset);
863   -
864   - memcpy(dst_ptr + dst_offset,
865   - page_address(src->bv_page) + src_offset,
866   - bytes);
867   -
868   - src_offset += bytes;
869   - dst_offset += bytes;
870   - }
871   -
872 828 bio_put(s->cache_miss);
873 829 s->cache_miss = NULL;
874 830 }
... ... @@ -912,9 +868,6 @@
912 868 struct bio *miss;
913 869  
914 870 miss = bch_bio_split(bio, sectors, GFP_NOIO, s->d->bio_split);
915   - if (!miss)
916   - return -EAGAIN;
917   -
918 871 if (miss == bio)
919 872 s->op.lookup_done = true;
920 873  
... ... @@ -933,8 +886,9 @@
933 886 reada = min(dc->readahead >> 9,
934 887 sectors - bio_sectors(miss));
935 888  
936   - if (bio_end(miss) + reada > bdev_sectors(miss->bi_bdev))
937   - reada = bdev_sectors(miss->bi_bdev) - bio_end(miss);
  889 + if (bio_end_sector(miss) + reada > bdev_sectors(miss->bi_bdev))
  890 + reada = bdev_sectors(miss->bi_bdev) -
  891 + bio_end_sector(miss);
938 892 }
939 893  
940 894 s->cache_bio_sectors = bio_sectors(miss) + reada;
... ... @@ -958,7 +912,7 @@
958 912 goto out_put;
959 913  
960 914 bch_bio_map(s->op.cache_bio, NULL);
961   - if (bch_bio_alloc_pages(s->op.cache_bio, __GFP_NOWARN|GFP_NOIO))
  915 + if (bio_alloc_pages(s->op.cache_bio, __GFP_NOWARN|GFP_NOIO))
962 916 goto out_put;
963 917  
964 918 s->cache_miss = miss;
... ... @@ -1002,7 +956,7 @@
1002 956 struct bio *bio = &s->bio.bio;
1003 957 struct bkey start, end;
1004 958 start = KEY(dc->disk.id, bio->bi_sector, 0);
1005   - end = KEY(dc->disk.id, bio_end(bio), 0);
  959 + end = KEY(dc->disk.id, bio_end_sector(bio), 0);
1006 960  
1007 961 bch_keybuf_check_overlapping(&s->op.c->moving_gc_keys, &start, &end);
1008 962  
... ... @@ -1176,7 +1130,7 @@
1176 1130 if (i->sequential + bio->bi_size > i->sequential)
1177 1131 i->sequential += bio->bi_size;
1178 1132  
1179   - i->last = bio_end(bio);
  1133 + i->last = bio_end_sector(bio);
1180 1134 i->jiffies = jiffies + msecs_to_jiffies(5000);
1181 1135 s->task->sequential_io = i->sequential;
1182 1136  
1183 1137  
1184 1138  
1185 1139  
1186 1140  
... ... @@ -1294,31 +1248,26 @@
1294 1248 static int flash_dev_cache_miss(struct btree *b, struct search *s,
1295 1249 struct bio *bio, unsigned sectors)
1296 1250 {
  1251 + struct bio_vec *bv;
  1252 + int i;
  1253 +
1297 1254 /* Zero fill bio */
1298 1255  
1299   - while (bio->bi_idx != bio->bi_vcnt) {
1300   - struct bio_vec *bv = bio_iovec(bio);
  1256 + bio_for_each_segment(bv, bio, i) {
1301 1257 unsigned j = min(bv->bv_len >> 9, sectors);
1302 1258  
1303 1259 void *p = kmap(bv->bv_page);
1304 1260 memset(p + bv->bv_offset, 0, j << 9);
1305 1261 kunmap(bv->bv_page);
1306 1262  
1307   - bv->bv_len -= j << 9;
1308   - bv->bv_offset += j << 9;
1309   -
1310   - if (bv->bv_len)
1311   - return 0;
1312   -
1313   - bio->bi_sector += j;
1314   - bio->bi_size -= j << 9;
1315   -
1316   - bio->bi_idx++;
1317   - sectors -= j;
  1263 + sectors -= j;
1318 1264 }
1319 1265  
1320   - s->op.lookup_done = true;
  1266 + bio_advance(bio, min(sectors << 9, bio->bi_size));
1321 1267  
  1268 + if (!bio->bi_size)
  1269 + s->op.lookup_done = true;
  1270 +
1322 1271 return 0;
1323 1272 }
1324 1273  
... ... @@ -1344,8 +1293,8 @@
1344 1293 closure_call(&s->op.cl, btree_read_async, NULL, cl);
1345 1294 } else if (bio_has_data(bio) || s->op.skip) {
1346 1295 bch_keybuf_check_overlapping(&s->op.c->moving_gc_keys,
1347   - &KEY(d->id, bio->bi_sector, 0),
1348   - &KEY(d->id, bio_end(bio), 0));
  1296 + &KEY(d->id, bio->bi_sector, 0),
  1297 + &KEY(d->id, bio_end_sector(bio), 0));
1349 1298  
1350 1299 s->writeback = true;
1351 1300 s->op.cache_bio = bio;
drivers/md/bcache/util.c
... ... @@ -228,23 +228,6 @@
228 228 }
229 229 }
230 230  
231   -int bch_bio_alloc_pages(struct bio *bio, gfp_t gfp)
232   -{
233   - int i;
234   - struct bio_vec *bv;
235   -
236   - bio_for_each_segment(bv, bio, i) {
237   - bv->bv_page = alloc_page(gfp);
238   - if (!bv->bv_page) {
239   - while (bv-- != bio->bi_io_vec + bio->bi_idx)
240   - __free_page(bv->bv_page);
241   - return -ENOMEM;
242   - }
243   - }
244   -
245   - return 0;
246   -}
247   -
248 231 /*
249 232 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group (Any
250 233 * use permitted, subject to terms of PostgreSQL license; see.)
drivers/md/bcache/util.h
... ... @@ -564,11 +564,7 @@
564 564 return x;
565 565 }
566 566  
567   -#define bio_end(bio) ((bio)->bi_sector + bio_sectors(bio))
568   -
569 567 void bch_bio_map(struct bio *bio, void *base);
570   -
571   -int bch_bio_alloc_pages(struct bio *bio, gfp_t gfp);
572 568  
573 569 static inline sector_t bdev_sectors(struct block_device *bdev)
574 570 {
drivers/md/bcache/writeback.c
... ... @@ -285,9 +285,10 @@
285 285 struct dirty_io *io = container_of(cl, struct dirty_io, cl);
286 286 struct keybuf_key *w = io->bio.bi_private;
287 287 struct cached_dev *dc = io->dc;
288   - struct bio_vec *bv = bio_iovec_idx(&io->bio, io->bio.bi_vcnt);
  288 + struct bio_vec *bv;
  289 + int i;
289 290  
290   - while (bv-- != io->bio.bi_io_vec)
  291 + bio_for_each_segment_all(bv, &io->bio, i)
291 292 __free_page(bv->bv_page);
292 293  
293 294 /* This is kind of a dumb way of signalling errors. */
... ... @@ -418,7 +419,7 @@
418 419 io->bio.bi_rw = READ;
419 420 io->bio.bi_end_io = read_dirty_endio;
420 421  
421   - if (bch_bio_alloc_pages(&io->bio, GFP_KERNEL))
  422 + if (bio_alloc_pages(&io->bio, GFP_KERNEL))
422 423 goto err_free;
423 424  
424 425 trace_bcache_writeback(&w->key);