Merge tag 'drm-next-2018-12-27' of git://anongit.freedesktop.org/drm/drm
[sfrench/cifs-2.6.git] / Documentation / block / biodoc.txt
1         Notes on the Generic Block Layer Rewrite in Linux 2.5
2         =====================================================
3
4 Notes Written on Jan 15, 2002:
5         Jens Axboe <jens.axboe@oracle.com>
6         Suparna Bhattacharya <suparna@in.ibm.com>
7
8 Last Updated May 2, 2002
9 September 2003: Updated I/O Scheduler portions
10         Nick Piggin <npiggin@kernel.dk>
11
12 Introduction:
13
14 These are some notes describing some aspects of the 2.5 block layer in the
15 context of the bio rewrite. The idea is to bring out some of the key
16 changes and a glimpse of the rationale behind those changes.
17
18 Please mail corrections & suggestions to suparna@in.ibm.com.
19
20 Credits:
21 ---------
22
23 2.5 bio rewrite:
24         Jens Axboe <jens.axboe@oracle.com>
25
26 Many aspects of the generic block layer redesign were driven by and evolved
27 over discussions, prior patches and the collective experience of several
28 people. See sections 8 and 9 for a list of some related references.
29
30 The following people helped with review comments and inputs for this
31 document:
32         Christoph Hellwig <hch@infradead.org>
33         Arjan van de Ven <arjanv@redhat.com>
34         Randy Dunlap <rdunlap@xenotime.net>
35         Andre Hedrick <andre@linux-ide.org>
36
37 The following people helped with fixes/contributions to the bio patches
38 while it was still work-in-progress:
39         David S. Miller <davem@redhat.com>
40
41
42 Description of Contents:
43 ------------------------
44
45 1. Scope for tuning of logic to various needs
46   1.1 Tuning based on device or low level driver capabilities
47         - Per-queue parameters
48         - Highmem I/O support
49         - I/O scheduler modularization
50   1.2 Tuning based on high level requirements/capabilities
51         1.2.1 Request Priority/Latency
52   1.3 Direct access/bypass to lower layers for diagnostics and special
53       device operations
54         1.3.1 Pre-built commands
55 2. New flexible and generic but minimalist i/o structure or descriptor
56    (instead of using buffer heads at the i/o layer)
57   2.1 Requirements/Goals addressed
58   2.2 The bio struct in detail (multi-page io unit)
59   2.3 Changes in the request structure
60 3. Using bios
61   3.1 Setup/teardown (allocation, splitting)
62   3.2 Generic bio helper routines
63     3.2.1 Traversing segments and completion units in a request
64     3.2.2 Setting up DMA scatterlists
65     3.2.3 I/O completion
66     3.2.4 Implications for drivers that do not interpret bios (don't handle
67           multiple segments)
68   3.3 I/O submission
69 4. The I/O scheduler
70 5. Scalability related changes
71   5.1 Granular locking: Removal of io_request_lock
72   5.2 Prepare for transition to 64 bit sector_t
73 6. Other Changes/Implications
74   6.1 Partition re-mapping handled by the generic block layer
75 7. A few tips on migration of older drivers
76 8. A list of prior/related/impacted patches/ideas
77 9. Other References/Discussion Threads
78
79 ---------------------------------------------------------------------------
80
81 Bio Notes
82 --------
83
84 Let us discuss the changes in the context of how some overall goals for the
85 block layer are addressed.
86
87 1. Scope for tuning the generic logic to satisfy various requirements
88
89 The block layer design supports adaptable abstractions to handle common
90 processing with the ability to tune the logic to an appropriate extent
91 depending on the nature of the device and the requirements of the caller.
92 One of the objectives of the rewrite was to increase the degree of tunability
93 and to enable higher level code to utilize underlying device/driver
94 capabilities to the maximum extent for better i/o performance. This is
95 important especially in the light of ever improving hardware capabilities
96 and application/middleware software designed to take advantage of these
97 capabilities.
98
99 1.1 Tuning based on low level device / driver capabilities
100
101 Sophisticated devices with large built-in caches, intelligent i/o scheduling
102 optimizations, high memory DMA support, etc may find some of the
103 generic processing an overhead, while for less capable devices the
104 generic functionality is essential for performance or correctness reasons.
105 Knowledge of some of the capabilities or parameters of the device should be
106 used at the generic block layer to take the right decisions on
107 behalf of the driver.
108
109 How is this achieved ?
110
111 Tuning at a per-queue level:
112
113 i. Per-queue limits/values exported to the generic layer by the driver
114
115 Various parameters that the generic i/o scheduler logic uses are set at
116 a per-queue level (e.g maximum request size, maximum number of segments in
117 a scatter-gather list, logical block size)
118
119 Some parameters that were earlier available as global arrays indexed by
120 major/minor are now directly associated with the queue. Some of these may
121 move into the block device structure in the future. Some characteristics
122 have been incorporated into a queue flags field rather than separate fields
123 in themselves.  There are blk_queue_xxx functions to set the parameters,
124 rather than update the fields directly
125
126 Some new queue property settings:
127
128         blk_queue_bounce_limit(q, u64 dma_address)
129                 Enable I/O to highmem pages, dma_address being the
130                 limit. No highmem default.
131
132         blk_queue_max_sectors(q, max_sectors)
133                 Sets two variables that limit the size of the request.
134
135                 - The request queue's max_sectors, which is a soft size in
136                 units of 512 byte sectors, and could be dynamically varied
137                 by the core kernel.
138
139                 - The request queue's max_hw_sectors, which is a hard limit
140                 and reflects the maximum size request a driver can handle
141                 in units of 512 byte sectors.
142
143                 The default for both max_sectors and max_hw_sectors is
144                 255. The upper limit of max_sectors is 1024.
145
146         blk_queue_max_phys_segments(q, max_segments)
147                 Maximum physical segments you can handle in a request. 128
148                 default (driver limit). (See 3.2.2)
149
150         blk_queue_max_hw_segments(q, max_segments)
151                 Maximum dma segments the hardware can handle in a request. 128
152                 default (host adapter limit, after dma remapping).
153                 (See 3.2.2)
154
155         blk_queue_max_segment_size(q, max_seg_size)
156                 Maximum size of a clustered segment, 64kB default.
157
158         blk_queue_logical_block_size(q, logical_block_size)
159                 Lowest possible sector size that the hardware can operate
160                 on, 512 bytes default.
161
162 New queue flags:
163
164         QUEUE_FLAG_CLUSTER (see 3.2.2)
165         QUEUE_FLAG_QUEUED (see 3.2.4)
166
167
168 ii. High-mem i/o capabilities are now considered the default
169
170 The generic bounce buffer logic, present in 2.4, where the block layer would
171 by default copyin/out i/o requests on high-memory buffers to low-memory buffers
172 assuming that the driver wouldn't be able to handle it directly, has been
173 changed in 2.5. The bounce logic is now applied only for memory ranges
174 for which the device cannot handle i/o. A driver can specify this by
175 setting the queue bounce limit for the request queue for the device
176 (blk_queue_bounce_limit()). This avoids the inefficiencies of the copyin/out
177 where a device is capable of handling high memory i/o.
178
179 In order to enable high-memory i/o where the device is capable of supporting
180 it, the pci dma mapping routines and associated data structures have now been
181 modified to accomplish a direct page -> bus translation, without requiring
182 a virtual address mapping (unlike the earlier scheme of virtual address
183 -> bus translation). So this works uniformly for high-memory pages (which
184 do not have a corresponding kernel virtual address space mapping) and
185 low-memory pages.
186
187 Note: Please refer to Documentation/DMA-API-HOWTO.txt for a discussion
188 on PCI high mem DMA aspects and mapping of scatter gather lists, and support
189 for 64 bit PCI.
190
191 Special handling is required only for cases where i/o needs to happen on
192 pages at physical memory addresses beyond what the device can support. In these
193 cases, a bounce bio representing a buffer from the supported memory range
194 is used for performing the i/o with copyin/copyout as needed depending on
195 the type of the operation.  For example, in case of a read operation, the
196 data read has to be copied to the original buffer on i/o completion, so a
197 callback routine is set up to do this, while for write, the data is copied
198 from the original buffer to the bounce buffer prior to issuing the
199 operation. Since an original buffer may be in a high memory area that's not
200 mapped in kernel virtual addr, a kmap operation may be required for
201 performing the copy, and special care may be needed in the completion path
202 as it may not be in irq context. Special care is also required (by way of
203 GFP flags) when allocating bounce buffers, to avoid certain highmem
204 deadlock possibilities.
205
206 It is also possible that a bounce buffer may be allocated from high-memory
207 area that's not mapped in kernel virtual addr, but within the range that the
208 device can use directly; so the bounce page may need to be kmapped during
209 copy operations. [Note: This does not hold in the current implementation,
210 though]
211
212 There are some situations when pages from high memory may need to
213 be kmapped, even if bounce buffers are not necessary. For example a device
214 may need to abort DMA operations and revert to PIO for the transfer, in
215 which case a virtual mapping of the page is required. For SCSI it is also
216 done in some scenarios where the low level driver cannot be trusted to
217 handle a single sg entry correctly. The driver is expected to perform the
218 kmaps as needed on such occasions as appropriate. A driver could also use
219 the blk_queue_bounce() routine on its own to bounce highmem i/o to low
220 memory for specific requests if so desired.
221
222 iii. The i/o scheduler algorithm itself can be replaced/set as appropriate
223
224 As in 2.4, it is possible to plugin a brand new i/o scheduler for a particular
225 queue or pick from (copy) existing generic schedulers and replace/override
226 certain portions of it. The 2.5 rewrite provides improved modularization
227 of the i/o scheduler. There are more pluggable callbacks, e.g for init,
228 add request, extract request, which makes it possible to abstract specific
229 i/o scheduling algorithm aspects and details outside of the generic loop.
230 It also makes it possible to completely hide the implementation details of
231 the i/o scheduler from block drivers.
232
233 I/O scheduler wrappers are to be used instead of accessing the queue directly.
234 See section 4. The I/O scheduler for details.
235
236 1.2 Tuning Based on High level code capabilities
237
238 i. Application capabilities for raw i/o
239
240 This comes from some of the high-performance database/middleware
241 requirements where an application prefers to make its own i/o scheduling
242 decisions based on an understanding of the access patterns and i/o
243 characteristics
244
245 ii. High performance filesystems or other higher level kernel code's
246 capabilities
247
248 Kernel components like filesystems could also take their own i/o scheduling
249 decisions for optimizing performance. Journalling filesystems may need
250 some control over i/o ordering.
251
252 What kind of support exists at the generic block layer for this ?
253
254 The flags and rw fields in the bio structure can be used for some tuning
255 from above e.g indicating that an i/o is just a readahead request, or priority
256 settings (currently unused). As far as user applications are concerned they
257 would need an additional mechanism either via open flags or ioctls, or some
258 other upper level mechanism to communicate such settings to block.
259
260 1.2.1 Request Priority/Latency
261
262 Todo/Under discussion:
263 Arjan's proposed request priority scheme allows higher levels some broad
264   control (high/med/low) over the priority  of an i/o request vs other pending
265   requests in the queue. For example it allows reads for bringing in an
266   executable page on demand to be given a higher priority over pending write
267   requests which haven't aged too much on the queue. Potentially this priority
268   could even be exposed to applications in some manner, providing higher level
269   tunability. Time based aging avoids starvation of lower priority
270   requests. Some bits in the bi_opf flags field in the bio structure are
271   intended to be used for this priority information.
272
273
274 1.3 Direct Access to Low level Device/Driver Capabilities (Bypass mode)
275     (e.g Diagnostics, Systems Management)
276
277 There are situations where high-level code needs to have direct access to
278 the low level device capabilities or requires the ability to issue commands
279 to the device bypassing some of the intermediate i/o layers.
280 These could, for example, be special control commands issued through ioctl
281 interfaces, or could be raw read/write commands that stress the drive's
282 capabilities for certain kinds of fitness tests. Having direct interfaces at
283 multiple levels without having to pass through upper layers makes
284 it possible to perform bottom up validation of the i/o path, layer by
285 layer, starting from the media.
286
287 The normal i/o submission interfaces, e.g submit_bio, could be bypassed
288 for specially crafted requests which such ioctl or diagnostics
289 interfaces would typically use, and the elevator add_request routine
290 can instead be used to directly insert such requests in the queue or preferably
291 the blk_do_rq routine can be used to place the request on the queue and
292 wait for completion. Alternatively, sometimes the caller might just
293 invoke a lower level driver specific interface with the request as a
294 parameter.
295
296 If the request is a means for passing on special information associated with
297 the command, then such information is associated with the request->special
298 field (rather than misuse the request->buffer field which is meant for the
299 request data buffer's virtual mapping).
300
301 For passing request data, the caller must build up a bio descriptor
302 representing the concerned memory buffer if the underlying driver interprets
303 bio segments or uses the block layer end*request* functions for i/o
304 completion. Alternatively one could directly use the request->buffer field to
305 specify the virtual address of the buffer, if the driver expects buffer
306 addresses passed in this way and ignores bio entries for the request type
307 involved. In the latter case, the driver would modify and manage the
308 request->buffer, request->sector and request->nr_sectors or
309 request->current_nr_sectors fields itself rather than using the block layer
310 end_request or end_that_request_first completion interfaces.
311 (See 2.3 or Documentation/block/request.txt for a brief explanation of
312 the request structure fields)
313
314 [TBD: end_that_request_last should be usable even in this case;
315 Perhaps an end_that_direct_request_first routine could be implemented to make
316 handling direct requests easier for such drivers; Also for drivers that
317 expect bios, a helper function could be provided for setting up a bio
318 corresponding to a data buffer]
319
320 <JENS: I dont understand the above, why is end_that_request_first() not
321 usable? Or _last for that matter. I must be missing something>
322 <SUP: What I meant here was that if the request doesn't have a bio, then
323  end_that_request_first doesn't modify nr_sectors or current_nr_sectors,
324  and hence can't be used for advancing request state settings on the
325  completion of partial transfers. The driver has to modify these fields 
326  directly by hand.
327  This is because end_that_request_first only iterates over the bio list,
328  and always returns 0 if there are none associated with the request.
329  _last works OK in this case, and is not a problem, as I mentioned earlier
330 >
331
332 1.3.1 Pre-built Commands
333
334 A request can be created with a pre-built custom command  to be sent directly
335 to the device. The cmd block in the request structure has room for filling
336 in the command bytes. (i.e rq->cmd is now 16 bytes in size, and meant for
337 command pre-building, and the type of the request is now indicated
338 through rq->flags instead of via rq->cmd)
339
340 The request structure flags can be set up to indicate the type of request
341 in such cases (REQ_PC: direct packet command passed to driver, REQ_BLOCK_PC:
342 packet command issued via blk_do_rq, REQ_SPECIAL: special request).
343
344 It can help to pre-build device commands for requests in advance.
345 Drivers can now specify a request prepare function (q->prep_rq_fn) that the
346 block layer would invoke to pre-build device commands for a given request,
347 or perform other preparatory processing for the request. This is routine is
348 called by elv_next_request(), i.e. typically just before servicing a request.
349 (The prepare function would not be called for requests that have RQF_DONTPREP
350 enabled)
351
352 Aside:
353   Pre-building could possibly even be done early, i.e before placing the
354   request on the queue, rather than construct the command on the fly in the
355   driver while servicing the request queue when it may affect latencies in
356   interrupt context or responsiveness in general. One way to add early
357   pre-building would be to do it whenever we fail to merge on a request.
358   Now REQ_NOMERGE is set in the request flags to skip this one in the future,
359   which means that it will not change before we feed it to the device. So
360   the pre-builder hook can be invoked there.
361
362
363 2. Flexible and generic but minimalist i/o structure/descriptor.
364
365 2.1 Reason for a new structure and requirements addressed
366
367 Prior to 2.5, buffer heads were used as the unit of i/o at the generic block
368 layer, and the low level request structure was associated with a chain of
369 buffer heads for a contiguous i/o request. This led to certain inefficiencies
370 when it came to large i/o requests and readv/writev style operations, as it
371 forced such requests to be broken up into small chunks before being passed
372 on to the generic block layer, only to be merged by the i/o scheduler
373 when the underlying device was capable of handling the i/o in one shot.
374 Also, using the buffer head as an i/o structure for i/os that didn't originate
375 from the buffer cache unnecessarily added to the weight of the descriptors
376 which were generated for each such chunk.
377
378 The following were some of the goals and expectations considered in the
379 redesign of the block i/o data structure in 2.5.
380
381 i.  Should be appropriate as a descriptor for both raw and buffered i/o  -
382     avoid cache related fields which are irrelevant in the direct/page i/o path,
383     or filesystem block size alignment restrictions which may not be relevant
384     for raw i/o.
385 ii. Ability to represent high-memory buffers (which do not have a virtual
386     address mapping in kernel address space).
387 iii.Ability to represent large i/os w/o unnecessarily breaking them up (i.e
388     greater than PAGE_SIZE chunks in one shot)
389 iv. At the same time, ability to retain independent identity of i/os from
390     different sources or i/o units requiring individual completion (e.g. for
391     latency reasons)
392 v.  Ability to represent an i/o involving multiple physical memory segments
393     (including non-page aligned page fragments, as specified via readv/writev)
394     without unnecessarily breaking it up, if the underlying device is capable of
395     handling it.
396 vi. Preferably should be based on a memory descriptor structure that can be
397     passed around different types of subsystems or layers, maybe even
398     networking, without duplication or extra copies of data/descriptor fields
399     themselves in the process
400 vii.Ability to handle the possibility of splits/merges as the structure passes
401     through layered drivers (lvm, md, evms), with minimal overhead.
402
403 The solution was to define a new structure (bio)  for the block layer,
404 instead of using the buffer head structure (bh) directly, the idea being
405 avoidance of some associated baggage and limitations. The bio structure
406 is uniformly used for all i/o at the block layer ; it forms a part of the
407 bh structure for buffered i/o, and in the case of raw/direct i/o kiobufs are
408 mapped to bio structures.
409
410 2.2 The bio struct
411
412 The bio structure uses a vector representation pointing to an array of tuples
413 of <page, offset, len> to describe the i/o buffer, and has various other
414 fields describing i/o parameters and state that needs to be maintained for
415 performing the i/o.
416
417 Notice that this representation means that a bio has no virtual address
418 mapping at all (unlike buffer heads).
419
420 struct bio_vec {
421        struct page     *bv_page;
422        unsigned short  bv_len;
423        unsigned short  bv_offset;
424 };
425
426 /*
427  * main unit of I/O for the block layer and lower layers (ie drivers)
428  */
429 struct bio {
430        struct bio          *bi_next;    /* request queue link */
431        struct block_device *bi_bdev;    /* target device */
432        unsigned long       bi_flags;    /* status, command, etc */
433        unsigned long       bi_opf;       /* low bits: r/w, high: priority */
434
435        unsigned int     bi_vcnt;     /* how may bio_vec's */
436        struct bvec_iter bi_iter;        /* current index into bio_vec array */
437
438        unsigned int     bi_size;     /* total size in bytes */
439        unsigned short   bi_phys_segments; /* segments after physaddr coalesce*/
440        unsigned short   bi_hw_segments; /* segments after DMA remapping */
441        unsigned int     bi_max;      /* max bio_vecs we can hold
442                                         used as index into pool */
443        struct bio_vec   *bi_io_vec;  /* the actual vec list */
444        bio_end_io_t     *bi_end_io;  /* bi_end_io (bio) */
445        atomic_t         bi_cnt;      /* pin count: free when it hits zero */
446        void             *bi_private;
447 };
448
449 With this multipage bio design:
450
451 - Large i/os can be sent down in one go using a bio_vec list consisting
452   of an array of <page, offset, len> fragments (similar to the way fragments
453   are represented in the zero-copy network code)
454 - Splitting of an i/o request across multiple devices (as in the case of
455   lvm or raid) is achieved by cloning the bio (where the clone points to
456   the same bi_io_vec array, but with the index and size accordingly modified)
457 - A linked list of bios is used as before for unrelated merges (*) - this
458   avoids reallocs and makes independent completions easier to handle.
459 - Code that traverses the req list can find all the segments of a bio
460   by using rq_for_each_segment.  This handles the fact that a request
461   has multiple bios, each of which can have multiple segments.
462 - Drivers which can't process a large bio in one shot can use the bi_iter
463   field to keep track of the next bio_vec entry to process.
464   (e.g a 1MB bio_vec needs to be handled in max 128kB chunks for IDE)
465   [TBD: Should preferably also have a bi_voffset and bi_vlen to avoid modifying
466    bi_offset an len fields]
467
468 (*) unrelated merges -- a request ends up containing two or more bios that
469     didn't originate from the same place.
470
471 bi_end_io() i/o callback gets called on i/o completion of the entire bio.
472
473 At a lower level, drivers build a scatter gather list from the merged bios.
474 The scatter gather list is in the form of an array of <page, offset, len>
475 entries with their corresponding dma address mappings filled in at the
476 appropriate time. As an optimization, contiguous physical pages can be
477 covered by a single entry where <page> refers to the first page and <len>
478 covers the range of pages (up to 16 contiguous pages could be covered this
479 way). There is a helper routine (blk_rq_map_sg) which drivers can use to build
480 the sg list.
481
482 Note: Right now the only user of bios with more than one page is ll_rw_kio,
483 which in turn means that only raw I/O uses it (direct i/o may not work
484 right now). The intent however is to enable clustering of pages etc to
485 become possible. The pagebuf abstraction layer from SGI also uses multi-page
486 bios, but that is currently not included in the stock development kernels.
487 The same is true of Andrew Morton's work-in-progress multipage bio writeout 
488 and readahead patches.
489
490 2.3 Changes in the Request Structure
491
492 The request structure is the structure that gets passed down to low level
493 drivers. The block layer make_request function builds up a request structure,
494 places it on the queue and invokes the drivers request_fn. The driver makes
495 use of block layer helper routine elv_next_request to pull the next request
496 off the queue. Control or diagnostic functions might bypass block and directly
497 invoke underlying driver entry points passing in a specially constructed
498 request structure.
499
500 Only some relevant fields (mainly those which changed or may be referred
501 to in some of the discussion here) are listed below, not necessarily in
502 the order in which they occur in the structure (see include/linux/blkdev.h)
503 Refer to Documentation/block/request.txt for details about all the request
504 structure fields and a quick reference about the layers which are
505 supposed to use or modify those fields.
506
507 struct request {
508         struct list_head queuelist;  /* Not meant to be directly accessed by
509                                         the driver.
510                                         Used by q->elv_next_request_fn
511                                         rq->queue is gone
512                                         */
513         .
514         .
515         unsigned char cmd[16]; /* prebuilt command data block */
516         unsigned long flags;   /* also includes earlier rq->cmd settings */
517         .
518         .
519         sector_t sector; /* this field is now of type sector_t instead of int
520                             preparation for 64 bit sectors */
521         .
522         .
523
524         /* Number of scatter-gather DMA addr+len pairs after
525          * physical address coalescing is performed.
526          */
527         unsigned short nr_phys_segments;
528
529         /* Number of scatter-gather addr+len pairs after
530          * physical and DMA remapping hardware coalescing is performed.
531          * This is the number of scatter-gather entries the driver
532          * will actually have to deal with after DMA mapping is done.
533          */
534         unsigned short nr_hw_segments;
535
536         /* Various sector counts */
537         unsigned long nr_sectors;  /* no. of sectors left: driver modifiable */
538         unsigned long hard_nr_sectors;  /* block internal copy of above */
539         unsigned int current_nr_sectors; /* no. of sectors left in the
540                                            current segment:driver modifiable */
541         unsigned long hard_cur_sectors; /* block internal copy of the above */
542         .
543         .
544         int tag;        /* command tag associated with request */
545         void *special;  /* same as before */
546         char *buffer;   /* valid only for low memory buffers up to
547                          current_nr_sectors */
548         .
549         .
550         struct bio *bio, *biotail;  /* bio list instead of bh */
551         struct request_list *rl;
552 }
553         
554 See the req_ops and req_flag_bits definitions for an explanation of the various
555 flags available. Some bits are used by the block layer or i/o scheduler.
556         
557 The behaviour of the various sector counts are almost the same as before,
558 except that since we have multi-segment bios, current_nr_sectors refers
559 to the numbers of sectors in the current segment being processed which could
560 be one of the many segments in the current bio (i.e i/o completion unit).
561 The nr_sectors value refers to the total number of sectors in the whole
562 request that remain to be transferred (no change). The purpose of the
563 hard_xxx values is for block to remember these counts every time it hands
564 over the request to the driver. These values are updated by block on
565 end_that_request_first, i.e. every time the driver completes a part of the
566 transfer and invokes block end*request helpers to mark this. The
567 driver should not modify these values. The block layer sets up the
568 nr_sectors and current_nr_sectors fields (based on the corresponding
569 hard_xxx values and the number of bytes transferred) and updates it on
570 every transfer that invokes end_that_request_first. It does the same for the
571 buffer, bio, bio->bi_iter fields too.
572
573 The buffer field is just a virtual address mapping of the current segment
574 of the i/o buffer in cases where the buffer resides in low-memory. For high
575 memory i/o, this field is not valid and must not be used by drivers.
576
577 Code that sets up its own request structures and passes them down to
578 a driver needs to be careful about interoperation with the block layer helper
579 functions which the driver uses. (Section 1.3)
580
581 3. Using bios
582
583 3.1 Setup/Teardown
584
585 There are routines for managing the allocation, and reference counting, and
586 freeing of bios (bio_alloc, bio_get, bio_put).
587
588 This makes use of Ingo Molnar's mempool implementation, which enables
589 subsystems like bio to maintain their own reserve memory pools for guaranteed
590 deadlock-free allocations during extreme VM load. For example, the VM
591 subsystem makes use of the block layer to writeout dirty pages in order to be
592 able to free up memory space, a case which needs careful handling. The
593 allocation logic draws from the preallocated emergency reserve in situations
594 where it cannot allocate through normal means. If the pool is empty and it
595 can wait, then it would trigger action that would help free up memory or
596 replenish the pool (without deadlocking) and wait for availability in the pool.
597 If it is in IRQ context, and hence not in a position to do this, allocation
598 could fail if the pool is empty. In general mempool always first tries to
599 perform allocation without having to wait, even if it means digging into the
600 pool as long it is not less that 50% full.
601
602 On a free, memory is released to the pool or directly freed depending on
603 the current availability in the pool. The mempool interface lets the
604 subsystem specify the routines to be used for normal alloc and free. In the
605 case of bio, these routines make use of the standard slab allocator.
606
607 The caller of bio_alloc is expected to taken certain steps to avoid
608 deadlocks, e.g. avoid trying to allocate more memory from the pool while
609 already holding memory obtained from the pool.
610 [TBD: This is a potential issue, though a rare possibility
611  in the bounce bio allocation that happens in the current code, since
612  it ends up allocating a second bio from the same pool while
613  holding the original bio ]
614
615 Memory allocated from the pool should be released back within a limited
616 amount of time (in the case of bio, that would be after the i/o is completed).
617 This ensures that if part of the pool has been used up, some work (in this
618 case i/o) must already be in progress and memory would be available when it
619 is over. If allocating from multiple pools in the same code path, the order
620 or hierarchy of allocation needs to be consistent, just the way one deals
621 with multiple locks.
622
623 The bio_alloc routine also needs to allocate the bio_vec_list (bvec_alloc())
624 for a non-clone bio. There are the 6 pools setup for different size biovecs,
625 so bio_alloc(gfp_mask, nr_iovecs) will allocate a vec_list of the
626 given size from these slabs.
627
628 The bio_get() routine may be used to hold an extra reference on a bio prior
629 to i/o submission, if the bio fields are likely to be accessed after the
630 i/o is issued (since the bio may otherwise get freed in case i/o completion
631 happens in the meantime).
632
633 The bio_clone_fast() routine may be used to duplicate a bio, where the clone
634 shares the bio_vec_list with the original bio (i.e. both point to the
635 same bio_vec_list). This would typically be used for splitting i/o requests
636 in lvm or md.
637
638 3.2 Generic bio helper Routines
639
640 3.2.1 Traversing segments and completion units in a request
641
642 The macro rq_for_each_segment() should be used for traversing the bios
643 in the request list (drivers should avoid directly trying to do it
644 themselves). Using these helpers should also make it easier to cope
645 with block changes in the future.
646
647         struct req_iterator iter;
648         rq_for_each_segment(bio_vec, rq, iter)
649                 /* bio_vec is now current segment */
650
651 I/O completion callbacks are per-bio rather than per-segment, so drivers
652 that traverse bio chains on completion need to keep that in mind. Drivers
653 which don't make a distinction between segments and completion units would
654 need to be reorganized to support multi-segment bios.
655
656 3.2.2 Setting up DMA scatterlists
657
658 The blk_rq_map_sg() helper routine would be used for setting up scatter
659 gather lists from a request, so a driver need not do it on its own.
660
661         nr_segments = blk_rq_map_sg(q, rq, scatterlist);
662
663 The helper routine provides a level of abstraction which makes it easier
664 to modify the internals of request to scatterlist conversion down the line
665 without breaking drivers. The blk_rq_map_sg routine takes care of several
666 things like collapsing physically contiguous segments (if QUEUE_FLAG_CLUSTER
667 is set) and correct segment accounting to avoid exceeding the limits which
668 the i/o hardware can handle, based on various queue properties.
669
670 - Prevents a clustered segment from crossing a 4GB mem boundary
671 - Avoids building segments that would exceed the number of physical
672   memory segments that the driver can handle (phys_segments) and the
673   number that the underlying hardware can handle at once, accounting for
674   DMA remapping (hw_segments)  (i.e. IOMMU aware limits).
675
676 Routines which the low level driver can use to set up the segment limits:
677
678 blk_queue_max_hw_segments() : Sets an upper limit of the maximum number of
679 hw data segments in a request (i.e. the maximum number of address/length
680 pairs the host adapter can actually hand to the device at once)
681
682 blk_queue_max_phys_segments() : Sets an upper limit on the maximum number
683 of physical data segments in a request (i.e. the largest sized scatter list
684 a driver could handle)
685
686 3.2.3 I/O completion
687
688 The existing generic block layer helper routines end_request,
689 end_that_request_first and end_that_request_last can be used for i/o
690 completion (and setting things up so the rest of the i/o or the next
691 request can be kicked of) as before. With the introduction of multi-page
692 bio support, end_that_request_first requires an additional argument indicating
693 the number of sectors completed.
694
695 3.2.4 Implications for drivers that do not interpret bios (don't handle
696  multiple segments)
697
698 Drivers that do not interpret bios e.g those which do not handle multiple
699 segments and do not support i/o into high memory addresses (require bounce
700 buffers) and expect only virtually mapped buffers, can access the rq->buffer
701 field. As before the driver should use current_nr_sectors to determine the
702 size of remaining data in the current segment (that is the maximum it can
703 transfer in one go unless it interprets segments), and rely on the block layer
704 end_request, or end_that_request_first/last to take care of all accounting
705 and transparent mapping of the next bio segment when a segment boundary
706 is crossed on completion of a transfer. (The end*request* functions should
707 be used if only if the request has come down from block/bio path, not for
708 direct access requests which only specify rq->buffer without a valid rq->bio)
709
710 3.3 I/O Submission
711
712 The routine submit_bio() is used to submit a single io. Higher level i/o
713 routines make use of this:
714
715 (a) Buffered i/o:
716 The routine submit_bh() invokes submit_bio() on a bio corresponding to the
717 bh, allocating the bio if required. ll_rw_block() uses submit_bh() as before.
718
719 (b) Kiobuf i/o (for raw/direct i/o):
720 The ll_rw_kio() routine breaks up the kiobuf into page sized chunks and
721 maps the array to one or more multi-page bios, issuing submit_bio() to
722 perform the i/o on each of these.
723
724 The embedded bh array in the kiobuf structure has been removed and no
725 preallocation of bios is done for kiobufs. [The intent is to remove the
726 blocks array as well, but it's currently in there to kludge around direct i/o.]
727 Thus kiobuf allocation has switched back to using kmalloc rather than vmalloc.
728
729 Todo/Observation:
730
731  A single kiobuf structure is assumed to correspond to a contiguous range
732  of data, so brw_kiovec() invokes ll_rw_kio for each kiobuf in a kiovec.
733  So right now it wouldn't work for direct i/o on non-contiguous blocks.
734  This is to be resolved.  The eventual direction is to replace kiobuf
735  by kvec's.
736
737  Badari Pulavarty has a patch to implement direct i/o correctly using
738  bio and kvec.
739
740
741 (c) Page i/o:
742 Todo/Under discussion:
743
744  Andrew Morton's multi-page bio patches attempt to issue multi-page
745  writeouts (and reads) from the page cache, by directly building up
746  large bios for submission completely bypassing the usage of buffer
747  heads. This work is still in progress.
748
749  Christoph Hellwig had some code that uses bios for page-io (rather than
750  bh). This isn't included in bio as yet. Christoph was also working on a
751  design for representing virtual/real extents as an entity and modifying
752  some of the address space ops interfaces to utilize this abstraction rather
753  than buffer_heads. (This is somewhat along the lines of the SGI XFS pagebuf
754  abstraction, but intended to be as lightweight as possible).
755
756 (d) Direct access i/o:
757 Direct access requests that do not contain bios would be submitted differently
758 as discussed earlier in section 1.3.
759
760 Aside:
761
762   Kvec i/o:
763
764   Ben LaHaise's aio code uses a slightly different structure instead
765   of kiobufs, called a kvec_cb. This contains an array of <page, offset, len>
766   tuples (very much like the networking code), together with a callback function
767   and data pointer. This is embedded into a brw_cb structure when passed
768   to brw_kvec_async().
769
770   Now it should be possible to directly map these kvecs to a bio. Just as while
771   cloning, in this case rather than PRE_BUILT bio_vecs, we set the bi_io_vec
772   array pointer to point to the veclet array in kvecs.
773
774   TBD: In order for this to work, some changes are needed in the way multi-page
775   bios are handled today. The values of the tuples in such a vector passed in
776   from higher level code should not be modified by the block layer in the course
777   of its request processing, since that would make it hard for the higher layer
778   to continue to use the vector descriptor (kvec) after i/o completes. Instead,
779   all such transient state should either be maintained in the request structure,
780   and passed on in some way to the endio completion routine.
781
782
783 4. The I/O scheduler
784 I/O scheduler, a.k.a. elevator, is implemented in two layers.  Generic dispatch
785 queue and specific I/O schedulers.  Unless stated otherwise, elevator is used
786 to refer to both parts and I/O scheduler to specific I/O schedulers.
787
788 Block layer implements generic dispatch queue in block/*.c.
789 The generic dispatch queue is responsible for requeueing, handling non-fs
790 requests and all other subtleties.
791
792 Specific I/O schedulers are responsible for ordering normal filesystem
793 requests.  They can also choose to delay certain requests to improve
794 throughput or whatever purpose.  As the plural form indicates, there are
795 multiple I/O schedulers.  They can be built as modules but at least one should
796 be built inside the kernel.  Each queue can choose different one and can also
797 change to another one dynamically.
798
799 A block layer call to the i/o scheduler follows the convention elv_xxx(). This
800 calls elevator_xxx_fn in the elevator switch (block/elevator.c). Oh, xxx
801 and xxx might not match exactly, but use your imagination. If an elevator
802 doesn't implement a function, the switch does nothing or some minimal house
803 keeping work.
804
805 4.1. I/O scheduler API
806
807 The functions an elevator may implement are: (* are mandatory)
808 elevator_merge_fn               called to query requests for merge with a bio
809
810 elevator_merge_req_fn           called when two requests get merged. the one
811                                 which gets merged into the other one will be
812                                 never seen by I/O scheduler again. IOW, after
813                                 being merged, the request is gone.
814
815 elevator_merged_fn              called when a request in the scheduler has been
816                                 involved in a merge. It is used in the deadline
817                                 scheduler for example, to reposition the request
818                                 if its sorting order has changed.
819
820 elevator_allow_merge_fn         called whenever the block layer determines
821                                 that a bio can be merged into an existing
822                                 request safely. The io scheduler may still
823                                 want to stop a merge at this point if it
824                                 results in some sort of conflict internally,
825                                 this hook allows it to do that. Note however
826                                 that two *requests* can still be merged at later
827                                 time. Currently the io scheduler has no way to
828                                 prevent that. It can only learn about the fact
829                                 from elevator_merge_req_fn callback.
830
831 elevator_dispatch_fn*           fills the dispatch queue with ready requests.
832                                 I/O schedulers are free to postpone requests by
833                                 not filling the dispatch queue unless @force
834                                 is non-zero.  Once dispatched, I/O schedulers
835                                 are not allowed to manipulate the requests -
836                                 they belong to generic dispatch queue.
837
838 elevator_add_req_fn*            called to add a new request into the scheduler
839
840 elevator_former_req_fn
841 elevator_latter_req_fn          These return the request before or after the
842                                 one specified in disk sort order. Used by the
843                                 block layer to find merge possibilities.
844
845 elevator_completed_req_fn       called when a request is completed.
846
847 elevator_may_queue_fn           returns true if the scheduler wants to allow the
848                                 current context to queue a new request even if
849                                 it is over the queue limit. This must be used
850                                 very carefully!!
851
852 elevator_set_req_fn
853 elevator_put_req_fn             Must be used to allocate and free any elevator
854                                 specific storage for a request.
855
856 elevator_activate_req_fn        Called when device driver first sees a request.
857                                 I/O schedulers can use this callback to
858                                 determine when actual execution of a request
859                                 starts.
860 elevator_deactivate_req_fn      Called when device driver decides to delay
861                                 a request by requeueing it.
862
863 elevator_init_fn*
864 elevator_exit_fn                Allocate and free any elevator specific storage
865                                 for a queue.
866
867 4.2 Request flows seen by I/O schedulers
868 All requests seen by I/O schedulers strictly follow one of the following three
869 flows.
870
871  set_req_fn ->
872
873  i.   add_req_fn -> (merged_fn ->)* -> dispatch_fn -> activate_req_fn ->
874       (deactivate_req_fn -> activate_req_fn ->)* -> completed_req_fn
875  ii.  add_req_fn -> (merged_fn ->)* -> merge_req_fn
876  iii. [none]
877
878  -> put_req_fn
879
880 4.3 I/O scheduler implementation
881 The generic i/o scheduler algorithm attempts to sort/merge/batch requests for
882 optimal disk scan and request servicing performance (based on generic
883 principles and device capabilities), optimized for:
884 i.   improved throughput
885 ii.  improved latency
886 iii. better utilization of h/w & CPU time
887
888 Characteristics:
889
890 i. Binary tree
891 AS and deadline i/o schedulers use red black binary trees for disk position
892 sorting and searching, and a fifo linked list for time-based searching. This
893 gives good scalability and good availability of information. Requests are
894 almost always dispatched in disk sort order, so a cache is kept of the next
895 request in sort order to prevent binary tree lookups.
896
897 This arrangement is not a generic block layer characteristic however, so
898 elevators may implement queues as they please.
899
900 ii. Merge hash
901 AS and deadline use a hash table indexed by the last sector of a request. This
902 enables merging code to quickly look up "back merge" candidates, even when
903 multiple I/O streams are being performed at once on one disk.
904
905 "Front merges", a new request being merged at the front of an existing request,
906 are far less common than "back merges" due to the nature of most I/O patterns.
907 Front merges are handled by the binary trees in AS and deadline schedulers.
908
909 iii. Plugging the queue to batch requests in anticipation of opportunities for
910      merge/sort optimizations
911
912 Plugging is an approach that the current i/o scheduling algorithm resorts to so
913 that it collects up enough requests in the queue to be able to take
914 advantage of the sorting/merging logic in the elevator. If the
915 queue is empty when a request comes in, then it plugs the request queue
916 (sort of like plugging the bath tub of a vessel to get fluid to build up)
917 till it fills up with a few more requests, before starting to service
918 the requests. This provides an opportunity to merge/sort the requests before
919 passing them down to the device. There are various conditions when the queue is
920 unplugged (to open up the flow again), either through a scheduled task or
921 could be on demand. For example wait_on_buffer sets the unplugging going
922 through sync_buffer() running blk_run_address_space(mapping). Or the caller
923 can do it explicity through blk_unplug(bdev). So in the read case,
924 the queue gets explicitly unplugged as part of waiting for completion on that
925 buffer.
926
927 Aside:
928   This is kind of controversial territory, as it's not clear if plugging is
929   always the right thing to do. Devices typically have their own queues,
930   and allowing a big queue to build up in software, while letting the device be
931   idle for a while may not always make sense. The trick is to handle the fine
932   balance between when to plug and when to open up. Also now that we have
933   multi-page bios being queued in one shot, we may not need to wait to merge
934   a big request from the broken up pieces coming by.
935
936 4.4 I/O contexts
937 I/O contexts provide a dynamically allocated per process data area. They may
938 be used in I/O schedulers, and in the block layer (could be used for IO statis,
939 priorities for example). See *io_context in block/ll_rw_blk.c, and as-iosched.c
940 for an example of usage in an i/o scheduler.
941
942
943 5. Scalability related changes
944
945 5.1 Granular Locking: io_request_lock replaced by a per-queue lock
946
947 The global io_request_lock has been removed as of 2.5, to avoid
948 the scalability bottleneck it was causing, and has been replaced by more
949 granular locking. The request queue structure has a pointer to the
950 lock to be used for that queue. As a result, locking can now be
951 per-queue, with a provision for sharing a lock across queues if
952 necessary (e.g the scsi layer sets the queue lock pointers to the
953 corresponding adapter lock, which results in a per host locking
954 granularity). The locking semantics are the same, i.e. locking is
955 still imposed by the block layer, grabbing the lock before
956 request_fn execution which it means that lots of older drivers
957 should still be SMP safe. Drivers are free to drop the queue
958 lock themselves, if required. Drivers that explicitly used the
959 io_request_lock for serialization need to be modified accordingly.
960 Usually it's as easy as adding a global lock:
961
962         static DEFINE_SPINLOCK(my_driver_lock);
963
964 and passing the address to that lock to blk_init_queue().
965
966 5.2 64 bit sector numbers (sector_t prepares for 64 bit support)
967
968 The sector number used in the bio structure has been changed to sector_t,
969 which could be defined as 64 bit in preparation for 64 bit sector support.
970
971 6. Other Changes/Implications
972
973 6.1 Partition re-mapping handled by the generic block layer
974
975 In 2.5 some of the gendisk/partition related code has been reorganized.
976 Now the generic block layer performs partition-remapping early and thus
977 provides drivers with a sector number relative to whole device, rather than
978 having to take partition number into account in order to arrive at the true
979 sector number. The routine blk_partition_remap() is invoked by
980 generic_make_request even before invoking the queue specific make_request_fn,
981 so the i/o scheduler also gets to operate on whole disk sector numbers. This
982 should typically not require changes to block drivers, it just never gets
983 to invoke its own partition sector offset calculations since all bios
984 sent are offset from the beginning of the device.
985
986
987 7. A Few Tips on Migration of older drivers
988
989 Old-style drivers that just use CURRENT and ignores clustered requests,
990 may not need much change.  The generic layer will automatically handle
991 clustered requests, multi-page bios, etc for the driver.
992
993 For a low performance driver or hardware that is PIO driven or just doesn't
994 support scatter-gather changes should be minimal too.
995
996 The following are some points to keep in mind when converting old drivers
997 to bio.
998
999 Drivers should use elv_next_request to pick up requests and are no longer
1000 supposed to handle looping directly over the request list.
1001 (struct request->queue has been removed)
1002
1003 Now end_that_request_first takes an additional number_of_sectors argument.
1004 It used to handle always just the first buffer_head in a request, now
1005 it will loop and handle as many sectors (on a bio-segment granularity)
1006 as specified.
1007
1008 Now bh->b_end_io is replaced by bio->bi_end_io, but most of the time the
1009 right thing to use is bio_endio(bio) instead.
1010
1011 If the driver is dropping the io_request_lock from its request_fn strategy,
1012 then it just needs to replace that with q->queue_lock instead.
1013
1014 As described in Sec 1.1, drivers can set max sector size, max segment size
1015 etc per queue now. Drivers that used to define their own merge functions i
1016 to handle things like this can now just use the blk_queue_* functions at
1017 blk_init_queue time.
1018
1019 Drivers no longer have to map a {partition, sector offset} into the
1020 correct absolute location anymore, this is done by the block layer, so
1021 where a driver received a request ala this before:
1022
1023         rq->rq_dev = mk_kdev(3, 5);     /* /dev/hda5 */
1024         rq->sector = 0;                 /* first sector on hda5 */
1025
1026   it will now see
1027
1028         rq->rq_dev = mk_kdev(3, 0);     /* /dev/hda */
1029         rq->sector = 123128;            /* offset from start of disk */
1030
1031 As mentioned, there is no virtual mapping of a bio. For DMA, this is
1032 not a problem as the driver probably never will need a virtual mapping.
1033 Instead it needs a bus mapping (dma_map_page for a single segment or
1034 use dma_map_sg for scatter gather) to be able to ship it to the driver. For
1035 PIO drivers (or drivers that need to revert to PIO transfer once in a
1036 while (IDE for example)), where the CPU is doing the actual data
1037 transfer a virtual mapping is needed. If the driver supports highmem I/O,
1038 (Sec 1.1, (ii) ) it needs to use kmap_atomic or similar to temporarily map
1039 a bio into the virtual address space.
1040
1041
1042 8. Prior/Related/Impacted patches
1043
1044 8.1. Earlier kiobuf patches (sct/axboe/chait/hch/mkp)
1045 - orig kiobuf & raw i/o patches (now in 2.4 tree)
1046 - direct kiobuf based i/o to devices (no intermediate bh's)
1047 - page i/o using kiobuf
1048 - kiobuf splitting for lvm (mkp)
1049 - elevator support for kiobuf request merging (axboe)
1050 8.2. Zero-copy networking (Dave Miller)
1051 8.3. SGI XFS - pagebuf patches - use of kiobufs
1052 8.4. Multi-page pioent patch for bio (Christoph Hellwig)
1053 8.5. Direct i/o implementation (Andrea Arcangeli) since 2.4.10-pre11
1054 8.6. Async i/o implementation patch (Ben LaHaise)
1055 8.7. EVMS layering design (IBM EVMS team)
1056 8.8. Larger page cache size patch (Ben LaHaise) and
1057      Large page size (Daniel Phillips)
1058     => larger contiguous physical memory buffers
1059 8.9. VM reservations patch (Ben LaHaise)
1060 8.10. Write clustering patches ? (Marcelo/Quintela/Riel ?)
1061 8.11. Block device in page cache patch (Andrea Archangeli) - now in 2.4.10+
1062 8.12. Multiple block-size transfers for faster raw i/o (Shailabh Nagar,
1063       Badari)
1064 8.13  Priority based i/o scheduler - prepatches (Arjan van de Ven)
1065 8.14  IDE Taskfile i/o patch (Andre Hedrick)
1066 8.15  Multi-page writeout and readahead patches (Andrew Morton)
1067 8.16  Direct i/o patches for 2.5 using kvec and bio (Badari Pulavarthy)
1068
1069 9. Other References:
1070
1071 9.1 The Splice I/O Model - Larry McVoy (and subsequent discussions on lkml,
1072 and Linus' comments - Jan 2001)
1073 9.2 Discussions about kiobuf and bh design on lkml between sct, linus, alan
1074 et al - Feb-March 2001 (many of the initial thoughts that led to bio were
1075 brought up in this discussion thread)
1076 9.3 Discussions on mempool on lkml - Dec 2001.
1077