biodoc.txt 54 KB

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