| @@ -8,6 +8,105 @@ · 7 unmodified lines |
| 8 | * Initial version. | 8 | * Initial version. |
| 9 | */ | 9 | */ |
| 10 | | 10 | |
| | 11 | /** |
| | 12 | * DOC: Readahead Overview |
| | 13 | * |
| | 14 | * Readahead is used to read content into the page cache before it is |
| | 15 | * explicitly requested by the application. Readahead only ever |
| | 16 | * attempts to read pages that are not yet in the page cache. If a |
| | 17 | * page is present but not up-to-date, readahead will not try to read |
| | 18 | * it. In that case a simple ->readpage() will be requested. |
| | 19 | * |
| | 20 | * Readahead is triggered when an application read request (whether a |
| | 21 | * systemcall or a page fault) finds that the requested page is not in |
| | 22 | * the page cache, or that it is in the page cache and has the |
| | 23 | * %PG_readahead flag set. This flag indicates that the page was loaded |
| | 24 | * as part of a previous read-ahead request and now that it has been |
| | 25 | * accessed, it is time for the next read-ahead. |
| | 26 | * |
| | 27 | * Each readahead request is partly synchronous read, and partly async |
| | 28 | * read-ahead. This is reflected in the struct file_ra_state which |
| | 29 | * contains ->size being to total number of pages, and ->async_size |
| | 30 | * which is the number of pages in the async section. The first page in |
| | 31 | * this async section will have %PG_readahead set as a trigger for a |
| | 32 | * subsequent read ahead. Once a series of sequential reads has been |
| | 33 | * established, there should be no need for a synchronous component and |
| | 34 | * all read ahead request will be fully asynchronous. |
| | 35 | * |
| | 36 | * When either of the triggers causes a readahead, three numbers need to |
| | 37 | * be determined: the start of the region, the size of the region, and |
| | 38 | * the size of the async tail. |
| | 39 | * |
| | 40 | * The start of the region is simply the first page address at or after |
| | 41 | * the accessed address, which is not currently populated in the page |
| | 42 | * cache. This is found with a simple search in the page cache. |
| | 43 | * |
| | 44 | * The size of the async tail is determined by subtracting the size that |
| | 45 | * was explicitly requested from the determined request size, unless |
| | 46 | * this would be less than zero - then zero is used. NOTE THIS |
| | 47 | * CALCULATION IS WRONG WHEN THE START OF THE REGION IS NOT THE ACCESSED |
| | 48 | * PAGE. |
| | 49 | * |
| | 50 | * The size of the region is normally determined from the size of the |
| | 51 | * previous readahead which loaded the preceding pages. This may be |
| | 52 | * discovered from the struct file_ra_state for simple sequential reads, |
| | 53 | * or from examining the state of the page cache when multiple |
| | 54 | * sequential reads are interleaved. Specifically: where the readahead |
| | 55 | * was triggered by the %PG_readahead flag, the size of the previous |
| | 56 | * readahead is assumed to be the number of pages from the triggering |
| | 57 | * page to the start of the new readahead. In these cases, the size of |
| | 58 | * the previous readahead is scaled, often doubled, for the new |
| | 59 | * readahead, though see get_next_ra_size() for details. |
| | 60 | * |
| | 61 | * If the size of the previous read cannot be determined, the number of |
| | 62 | * preceding pages in the page cache is used to estimate the size of |
| | 63 | * a previous read. This estimate could easily be misled by random |
| | 64 | * reads being coincidentally adjacent, so it is ignored unless it is |
| | 65 | * larger than the current request, and it is not scaled up, unless it |
| | 66 | * is at the start of file. |
| | 67 | * |
| | 68 | * In general read ahead is accelerated at the start of the file, as |
| | 69 | * reads from there are often sequential. There are other minor |
| | 70 | * adjustments to the read ahead size in various special cases and these |
| | 71 | * are best discovered by reading the code. |
| | 72 | * |
| | 73 | * The above calculation determines the readahead, to which any requested |
| | 74 | * read size may be added. |
| | 75 | * |
| | 76 | * Readahead requests are sent to the filesystem using the ->readahead() |
| | 77 | * address space operation, for which mpage_readahead() is a canonical |
| | 78 | * implementation. ->readahead() should normally initiate reads on all |
| | 79 | * pages, but may fail to read any or all pages without causing an IO |
| | 80 | * error. The page cache reading code will issue a ->readpage() request |
| | 81 | * for any page which ->readahead() does not provided, and only an error |
| | 82 | * from this will be final. |
| | 83 | * |
| | 84 | * ->readahead() will generally call readahead_page() repeatedly to get |
| | 85 | * each page from those prepared for read ahead. It may fail to read a |
| | 86 | * page by: |
| | 87 | * |
| | 88 | * * not calling readahead_page() sufficiently many times, effectively |
| | 89 | * ignoring some pages, as might be appropriate if the path to |
| | 90 | * storage is congested. |
| | 91 | * |
| | 92 | * * failing to actually submit a read request for a given page, |
| | 93 | * possibly due to insufficient resources, or |
| | 94 | * |
| | 95 | * * getting an error during subsequent processing of a request. |
| | 96 | * |
| | 97 | * In the last two cases, the page should be unlocked to indicate that |
| | 98 | * the read attempt has failed. In the first case the page will be |
| | 99 | * unlocked by the caller. |
| | 100 | * |
| | 101 | * Those pages not in the final ``async_size`` of the request should be |
| | 102 | * considered to be important and ->readahead() should not fail them due |
| | 103 | * to congestion or temporary resource unavailability, but should wait |
| | 104 | * for necessary resources (e.g. memory or indexing information) to |
| | 105 | * become available. Pages in the final ``async_size`` may be |
| | 106 | * considered less urgent and failure to read them is more acceptable. |
| | 107 | * They will eventually be read individually using ->readpage(). |
| | 108 | */ |
| | 109 | |
| 11 | #include <linux/kernel.h> | 110 | #include <linux/kernel.h> |
| 12 | #include <linux/dax.h> | 111 | #include <linux/dax.h> |
| 13 | #include <linux/gfp.h> | 112 | #include <linux/gfp.h> |