memcg: show memcg information during OOM
[safe/jmp/linux-2.6] / mm / readahead.c
index 3d262bb..9ce303d 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (C) 2002, Linus Torvalds
  *
- * 09Apr2002   akpm@zip.com.au
+ * 09Apr2002   Andrew Morton
  *             Initial version.
  */
 
 #include <linux/backing-dev.h>
 #include <linux/task_io_accounting_ops.h>
 #include <linux/pagevec.h>
-
-void default_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
-{
-}
-EXPORT_SYMBOL(default_unplug_io_fn);
-
-/*
- * Convienent macros for min/max read-ahead pages.
- * Note that MAX_RA_PAGES is rounded down, while MIN_RA_PAGES is rounded up.
- * The latter is necessary for systems with large page size(i.e. 64k).
- */
-#define MAX_RA_PAGES   (VM_MAX_READAHEAD*1024 / PAGE_CACHE_SIZE)
-#define MIN_RA_PAGES   DIV_ROUND_UP(VM_MIN_READAHEAD*1024, PAGE_CACHE_SIZE)
-
-struct backing_dev_info default_backing_dev_info = {
-       .ra_pages       = MAX_RA_PAGES,
-       .state          = 0,
-       .capabilities   = BDI_CAP_MAP_COPY,
-       .unplug_io_fn   = default_unplug_io_fn,
-};
-EXPORT_SYMBOL_GPL(default_backing_dev_info);
+#include <linux/pagemap.h>
 
 /*
  * Initialise a struct file's readahead state.  Assumes that the caller has
@@ -45,7 +25,7 @@ void
 file_ra_state_init(struct file_ra_state *ra, struct address_space *mapping)
 {
        ra->ra_pages = mapping->backing_dev_info->ra_pages;
-       ra->prev_index = -1;
+       ra->prev_pos = -1;
 }
 EXPORT_SYMBOL_GPL(file_ra_state_init);
 
@@ -65,28 +45,25 @@ int read_cache_pages(struct address_space *mapping, struct list_head *pages,
                        int (*filler)(void *, struct page *), void *data)
 {
        struct page *page;
-       struct pagevec lru_pvec;
        int ret = 0;
 
-       pagevec_init(&lru_pvec, 0);
-
        while (!list_empty(pages)) {
                page = list_to_page(pages);
                list_del(&page->lru);
-               if (add_to_page_cache(page, mapping, page->index, GFP_KERNEL)) {
+               if (add_to_page_cache_lru(page, mapping,
+                                       page->index, GFP_KERNEL)) {
                        page_cache_release(page);
                        continue;
                }
+               page_cache_release(page);
+
                ret = filler(data, page);
-               if (!pagevec_add(&lru_pvec, page))
-                       __pagevec_lru_add(&lru_pvec);
-               if (ret) {
+               if (unlikely(ret)) {
                        put_pages_list(pages);
                        break;
                }
                task_io_account_read(PAGE_CACHE_SIZE);
        }
-       pagevec_lru_add(&lru_pvec);
        return ret;
 }
 
@@ -96,7 +73,6 @@ static int read_pages(struct address_space *mapping, struct file *filp,
                struct list_head *pages, unsigned nr_pages)
 {
        unsigned page_idx;
-       struct pagevec lru_pvec;
        int ret;
 
        if (mapping->a_ops->readpages) {
@@ -106,19 +82,15 @@ static int read_pages(struct address_space *mapping, struct file *filp,
                goto out;
        }
 
-       pagevec_init(&lru_pvec, 0);
        for (page_idx = 0; page_idx < nr_pages; page_idx++) {
                struct page *page = list_to_page(pages);
                list_del(&page->lru);
-               if (!add_to_page_cache(page, mapping,
+               if (!add_to_page_cache_lru(page, mapping,
                                        page->index, GFP_KERNEL)) {
                        mapping->a_ops->readpage(filp, page);
-                       if (!pagevec_add(&lru_pvec, page))
-                               __pagevec_lru_add(&lru_pvec);
-               } else
-                       page_cache_release(page);
+               }
+               page_cache_release(page);
        }
-       pagevec_lru_add(&lru_pvec);
        ret = 0;
 out:
        return ret;
@@ -156,20 +128,19 @@ __do_page_cache_readahead(struct address_space *mapping, struct file *filp,
        /*
         * Preallocate as many pages as we will need.
         */
-       read_lock_irq(&mapping->tree_lock);
        for (page_idx = 0; page_idx < nr_to_read; page_idx++) {
                pgoff_t page_offset = offset + page_idx;
 
                if (page_offset > end_index)
                        break;
 
+               rcu_read_lock();
                page = radix_tree_lookup(&mapping->page_tree, page_offset);
+               rcu_read_unlock();
                if (page)
                        continue;
 
-               read_unlock_irq(&mapping->tree_lock);
                page = page_cache_alloc_cold(mapping);
-               read_lock_irq(&mapping->tree_lock);
                if (!page)
                        break;
                page->index = page_offset;
@@ -178,7 +149,6 @@ __do_page_cache_readahead(struct address_space *mapping, struct file *filp,
                        SetPageReadahead(page);
                ret++;
        }
-       read_unlock_irq(&mapping->tree_lock);
 
        /*
         * Now start the IO.  We ignore I/O errors - if the page is not
@@ -246,28 +216,23 @@ int do_page_cache_readahead(struct address_space *mapping, struct file *filp,
  */
 unsigned long max_sane_readahead(unsigned long nr)
 {
-       return min(nr, (node_page_state(numa_node_id(), NR_INACTIVE)
+       return min(nr, (node_page_state(numa_node_id(), NR_INACTIVE_FILE)
                + node_page_state(numa_node_id(), NR_FREE_PAGES)) / 2);
 }
 
 /*
  * Submit IO for the read-ahead request in file_ra_state.
  */
-unsigned long ra_submit(struct file_ra_state *ra,
+static unsigned long ra_submit(struct file_ra_state *ra,
                       struct address_space *mapping, struct file *filp)
 {
-       unsigned long ra_size;
-       unsigned long la_size;
        int actual;
 
-       ra_size = ra_readahead_size(ra);
-       la_size = ra_lookahead_size(ra);
        actual = __do_page_cache_readahead(mapping, filp,
-                                       ra->ra_index, ra_size, la_size);
+                                       ra->start, ra->size, ra->async_size);
 
        return actual;
 }
-EXPORT_SYMBOL_GPL(ra_submit);
 
 /*
  * Set the initial window size, round to next power of 2 and square
@@ -296,7 +261,7 @@ static unsigned long get_init_ra_size(unsigned long size, unsigned long max)
 static unsigned long get_next_ra_size(struct file_ra_state *ra,
                                                unsigned long max)
 {
-       unsigned long cur = ra->readahead_index - ra->ra_index;
+       unsigned long cur = ra->size;
        unsigned long newsize;
 
        if (cur < max / 16)
@@ -313,32 +278,25 @@ static unsigned long get_next_ra_size(struct file_ra_state *ra,
  * The fields in struct file_ra_state represent the most-recently-executed
  * readahead attempt:
  *
- *                    |-------- last readahead window -------->|
- *       |-- application walking here -->|
- * ======#============|==================#=====================|
- *       ^la_index    ^ra_index          ^lookahead_index      ^readahead_index
- *
- * [ra_index, readahead_index) represents the last readahead window.
- *
- * [la_index, lookahead_index] is where the application would be walking(in
- * the common case of cache-cold sequential reads): the last window was
- * established when the application was at la_index, and the next window will
- * be bring in when the application reaches lookahead_index.
+ *                        |<----- async_size ---------|
+ *     |------------------- size -------------------->|
+ *     |==================#===========================|
+ *     ^start             ^page marked with PG_readahead
  *
  * To overlap application thinking time and disk I/O time, we do
  * `readahead pipelining': Do not wait until the application consumed all
  * readahead pages and stalled on the missing page at readahead_index;
- * Instead, submit an asynchronous readahead I/O as early as the application
- * reads on the page at lookahead_index. Normally lookahead_index will be
- * equal to ra_index, for maximum pipelining.
+ * Instead, submit an asynchronous readahead I/O as soon as there are
+ * only async_size pages left in the readahead window. Normally async_size
+ * will be equal to size, for maximum pipelining.
  *
  * In interleaved sequential reads, concurrent streams on the same fd can
  * be invalidating each other's readahead state. So we flag the new readahead
- * page at lookahead_index with PG_readahead, and use it as readahead
+ * page at (start+size-async_size) with PG_readahead, and use it as readahead
  * indicator. The flag won't be set on already cached pages, to avoid the
  * readahead-for-nothing fuss, saving pointless page cache lookups.
  *
- * prev_index tracks the last visited page in the _previous_ read request.
+ * prev_pos tracks the last visited byte in the _previous_ read request.
  * It should be maintained by the caller, and will be used for detecting
  * small random reads. Note that the readahead algorithm checks loosely
  * for sequential patterns. Hence interleaved reads might be served as
@@ -362,27 +320,25 @@ ondemand_readahead(struct address_space *mapping,
                   bool hit_readahead_marker, pgoff_t offset,
                   unsigned long req_size)
 {
-       unsigned long max;      /* max readahead pages */
-       pgoff_t ra_index;       /* readahead index */
-       unsigned long ra_size;  /* readahead size */
-       unsigned long la_size;  /* lookahead size */
-       int sequential;
-
-       max = ra->ra_pages;
-       sequential = (offset - ra->prev_index <= 1UL) || (req_size > max);
+       int     max = ra->ra_pages;     /* max readahead pages */
+       pgoff_t prev_offset;
+       int     sequential;
 
        /*
-        * Lookahead/readahead hit, assume sequential access.
+        * It's the expected callback offset, assume sequential access.
         * Ramp up sizes, and push forward the readahead window.
         */
-       if (offset && (offset == ra->lookahead_index ||
-                       offset == ra->readahead_index)) {
-               ra_index = ra->readahead_index;
-               ra_size = get_next_ra_size(ra, max);
-               la_size = ra_size;
-               goto fill_ra;
+       if (offset && (offset == (ra->start + ra->size - ra->async_size) ||
+                       offset == (ra->start + ra->size))) {
+               ra->start += ra->size;
+               ra->size = get_next_ra_size(ra, max);
+               ra->async_size = ra->size;
+               goto readit;
        }
 
+       prev_offset = ra->prev_pos >> PAGE_CACHE_SHIFT;
+       sequential = offset - prev_offset <= 1UL || req_size > max;
+
        /*
         * Standalone, small read.
         * Read as is, and do not pollute the readahead state.
@@ -393,30 +349,40 @@ ondemand_readahead(struct address_space *mapping,
        }
 
        /*
+        * Hit a marked page without valid readahead state.
+        * E.g. interleaved reads.
+        * Query the pagecache for async_size, which normally equals to
+        * readahead size. Ramp it up and use it as the new readahead size.
+        */
+       if (hit_readahead_marker) {
+               pgoff_t start;
+
+               rcu_read_lock();
+               start = radix_tree_next_hole(&mapping->page_tree, offset,max+1);
+               rcu_read_unlock();
+
+               if (!start || start - offset > max)
+                       return 0;
+
+               ra->start = start;
+               ra->size = start - offset;      /* old async_size */
+               ra->size = get_next_ra_size(ra, max);
+               ra->async_size = ra->size;
+               goto readit;
+       }
+
+       /*
         * It may be one of
         *      - first read on start of file
         *      - sequential cache miss
         *      - oversize random read
         * Start readahead for it.
         */
-       ra_index = offset;
-       ra_size = get_init_ra_size(req_size, max);
-       la_size = ra_size > req_size ? ra_size - req_size : ra_size;
-
-       /*
-        * Hit on a lookahead page without valid readahead state.
-        * E.g. interleaved reads.
-        * Not knowing its readahead pos/size, bet on the minimal possible one.
-        */
-       if (hit_readahead_marker) {
-               ra_index++;
-               ra_size = min(4 * ra_size, max);
-       }
-
-fill_ra:
-       ra_set_index(ra, offset, ra_index);
-       ra_set_size(ra, ra_size, la_size);
+       ra->start = offset;
+       ra->size = get_init_ra_size(req_size, max);
+       ra->async_size = ra->size > req_size ? ra->size - req_size : ra->size;
 
+readit:
        return ra_submit(ra, mapping, filp);
 }
 
@@ -458,9 +424,10 @@ EXPORT_SYMBOL_GPL(page_cache_sync_readahead);
  *            pagecache pages
  *
  * page_cache_async_ondemand() should be called when a page is used which
- * has the PG_readahead flag: this is a marker to suggest that the application
+ * has the PG_readahead flag; this is a marker to suggest that the application
  * has used up enough of the readahead window that we should start pulling in
- * more pages. */
+ * more pages.
+ */
 void
 page_cache_async_readahead(struct address_space *mapping,
                           struct file_ra_state *ra, struct file *filp,