add patch move-error-report-out-of-atomic-context
[ext4-patch-queue.git] / support-buffer-cache-allocations-with-gfp-modifiers
blob2c6f4bb0d517adb16f3ac2500007de2a18e2c5e2
1 fs/buffer.c: support buffer cache allocations with gfp modifiers
3 From: Gioh Kim <gioh.kim@lge.com>
5 A buffer cache is allocated from movable area because it is referred
6 for a while and released soon.  But some filesystems are taking buffer
7 cache for a long time and it can disturb page migration.
9 New APIs are introduced to allocate buffer cache with user specific
10 flag.  *_gfp APIs are for user want to set page allocation flag for
11 page cache allocation.  And *_unmovable APIs are for the user wants to
12 allocate page cache from non-movable area.
14 Signed-off-by: Gioh Kim <gioh.kim@lge.com>
15 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
16 Reviewed-by: Jan Kara <jack@suse.cz>
17 ---
18  fs/buffer.c                 |   45 ++++++++++++++++++++++++-----------------
19  include/linux/buffer_head.h |   47 ++++++++++++++++++++++++++++++++++++++-----
20  2 files changed, 68 insertions(+), 24 deletions(-)
22 diff --git a/fs/buffer.c b/fs/buffer.c
23 index 8f05111..9a6029e 100644
24 --- a/fs/buffer.c
25 +++ b/fs/buffer.c
26 @@ -993,7 +993,7 @@ init_page_buffers(struct page *page, struct block_device *bdev,
27   */
28  static int
29  grow_dev_page(struct block_device *bdev, sector_t block,
30 -               pgoff_t index, int size, int sizebits)
31 +             pgoff_t index, int size, int sizebits, gfp_t gfp)
32  {
33         struct inode *inode = bdev->bd_inode;
34         struct page *page;
35 @@ -1002,8 +1002,8 @@ grow_dev_page(struct block_device *bdev, sector_t block,
36         int ret = 0;            /* Will call free_more_memory() */
37         gfp_t gfp_mask;
39 -       gfp_mask = mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS;
40 -       gfp_mask |= __GFP_MOVABLE;
41 +       gfp_mask = (mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS) | gfp;
43         /*
44          * XXX: __getblk_slow() can not really deal with failure and
45          * will endlessly loop on improvised global reclaim.  Prefer
46 @@ -1058,7 +1058,7 @@ failed:
47   * that page was dirty, the buffers are set dirty also.
48   */
49  static int
50 -grow_buffers(struct block_device *bdev, sector_t block, int size)
51 +grow_buffers(struct block_device *bdev, sector_t block, int size, gfp_t gfp)
52  {
53         pgoff_t index;
54         int sizebits;
55 @@ -1085,11 +1085,12 @@ grow_buffers(struct block_device *bdev, sector_t block, int size)
56         }
58         /* Create a page with the proper size buffers.. */
59 -       return grow_dev_page(bdev, block, index, size, sizebits);
60 +       return grow_dev_page(bdev, block, index, size, sizebits, gfp);
61  }
63 -static struct buffer_head *
64 -__getblk_slow(struct block_device *bdev, sector_t block, int size)
65 +struct buffer_head *
66 +__getblk_slow(struct block_device *bdev, sector_t block,
67 +            unsigned size, gfp_t gfp)
68  {
69         /* Size must be multiple of hard sectorsize */
70         if (unlikely(size & (bdev_logical_block_size(bdev)-1) ||
71 @@ -1111,13 +1112,14 @@ __getblk_slow(struct block_device *bdev, sector_t block, int size)
72                 if (bh)
73                         return bh;
75 -               ret = grow_buffers(bdev, block, size);
76 +               ret = grow_buffers(bdev, block, size, gfp);
77                 if (ret < 0)
78                         return NULL;
79                 if (ret == 0)
80                         free_more_memory();
81         }
82  }
83 +EXPORT_SYMBOL(__getblk_slow);
85  /*
86   * The relationship between dirty buffers and dirty pages:
87 @@ -1371,24 +1373,25 @@ __find_get_block(struct block_device *bdev, sector_t block, unsigned size)
88  EXPORT_SYMBOL(__find_get_block);
90  /*
91 - * __getblk will locate (and, if necessary, create) the buffer_head
92 + * __getblk_gfp() will locate (and, if necessary, create) the buffer_head
93   * which corresponds to the passed block_device, block and size. The
94   * returned buffer has its reference count incremented.
95   *
96 - * __getblk() will lock up the machine if grow_dev_page's try_to_free_buffers()
97 - * attempt is failing.  FIXME, perhaps?
98 + * __getblk_gfp() will lock up the machine if grow_dev_page's
99 + * try_to_free_buffers() attempt is failing.  FIXME, perhaps?
100   */
101  struct buffer_head *
102 -__getblk(struct block_device *bdev, sector_t block, unsigned size)
103 +__getblk_gfp(struct block_device *bdev, sector_t block,
104 +            unsigned size, gfp_t gfp)
106         struct buffer_head *bh = __find_get_block(bdev, block, size);
108         might_sleep();
109         if (bh == NULL)
110 -               bh = __getblk_slow(bdev, block, size);
111 +               bh = __getblk_slow(bdev, block, size, gfp);
112         return bh;
114 -EXPORT_SYMBOL(__getblk);
115 +EXPORT_SYMBOL(__getblk_gfp);
117  /*
118   * Do async read-ahead on a buffer..
119 @@ -1404,24 +1407,28 @@ void __breadahead(struct block_device *bdev, sector_t block, unsigned size)
120  EXPORT_SYMBOL(__breadahead);
122  /**
123 - *  __bread() - reads a specified block and returns the bh
124 + *  __bread_gfp() - reads a specified block and returns the bh
125   *  @bdev: the block_device to read from
126   *  @block: number of block
127   *  @size: size (in bytes) to read
128 - * 
129 + *  @gfp: page allocation flag
130 + *
131   *  Reads a specified block, and returns buffer head that contains it.
132 + *  The page cache can be allocated from non-movable area
133 + *  not to prevent page migration if you set gfp to zero.
134   *  It returns NULL if the block was unreadable.
135   */
136  struct buffer_head *
137 -__bread(struct block_device *bdev, sector_t block, unsigned size)
138 +__bread_gfp(struct block_device *bdev, sector_t block,
139 +                  unsigned size, gfp_t gfp)
141 -       struct buffer_head *bh = __getblk(bdev, block, size);
142 +       struct buffer_head *bh = __getblk_gfp(bdev, block, size, gfp);
144         if (likely(bh) && !buffer_uptodate(bh))
145                 bh = __bread_slow(bh);
146         return bh;
148 -EXPORT_SYMBOL(__bread);
149 +EXPORT_SYMBOL(__bread_gfp);
151  /*
152   * invalidate_bh_lrus() is called rarely - but not only at unmount.
153 diff --git a/include/linux/buffer_head.h b/include/linux/buffer_head.h
154 index 324329c..73b4522 100644
155 --- a/include/linux/buffer_head.h
156 +++ b/include/linux/buffer_head.h
157 @@ -175,12 +175,13 @@ void __wait_on_buffer(struct buffer_head *);
158  wait_queue_head_t *bh_waitq_head(struct buffer_head *bh);
159  struct buffer_head *__find_get_block(struct block_device *bdev, sector_t block,
160                         unsigned size);
161 -struct buffer_head *__getblk(struct block_device *bdev, sector_t block,
162 -                       unsigned size);
163 +struct buffer_head *__getblk_gfp(struct block_device *bdev, sector_t block,
164 +                                 unsigned size, gfp_t gfp);
165  void __brelse(struct buffer_head *);
166  void __bforget(struct buffer_head *);
167  void __breadahead(struct block_device *, sector_t block, unsigned int size);
168 -struct buffer_head *__bread(struct block_device *, sector_t block, unsigned size);
169 +struct buffer_head *__bread_gfp(struct block_device *,
170 +                               sector_t block, unsigned size, gfp_t gfp);
171  void invalidate_bh_lrus(void);
172  struct buffer_head *alloc_buffer_head(gfp_t gfp_flags);
173  void free_buffer_head(struct buffer_head * bh);
174 @@ -295,7 +296,13 @@ static inline void bforget(struct buffer_head *bh)
175  static inline struct buffer_head *
176  sb_bread(struct super_block *sb, sector_t block)
178 -       return __bread(sb->s_bdev, block, sb->s_blocksize);
179 +       return __bread_gfp(sb->s_bdev, block, sb->s_blocksize, __GFP_MOVABLE);
182 +static inline struct buffer_head *
183 +sb_bread_unmovable(struct super_block *sb, sector_t block)
185 +       return __bread_gfp(sb->s_bdev, block, sb->s_blocksize, 0);
188  static inline void
189 @@ -307,7 +314,7 @@ sb_breadahead(struct super_block *sb, sector_t block)
190  static inline struct buffer_head *
191  sb_getblk(struct super_block *sb, sector_t block)
193 -       return __getblk(sb->s_bdev, block, sb->s_blocksize);
194 +       return __getblk_gfp(sb->s_bdev, block, sb->s_blocksize, __GFP_MOVABLE);
197  static inline struct buffer_head *
198 @@ -344,6 +351,36 @@ static inline void lock_buffer(struct buffer_head *bh)
199                 __lock_buffer(bh);
202 +static inline struct buffer_head *getblk_unmovable(struct block_device *bdev,
203 +                                                  sector_t block,
204 +                                                  unsigned size)
206 +       return __getblk_gfp(bdev, block, size, 0);
209 +static inline struct buffer_head *__getblk(struct block_device *bdev,
210 +                                          sector_t block,
211 +                                          unsigned size)
213 +       return __getblk_gfp(bdev, block, size, __GFP_MOVABLE);
216 +/**
217 + *  __bread() - reads a specified block and returns the bh
218 + *  @bdev: the block_device to read from
219 + *  @block: number of block
220 + *  @size: size (in bytes) to read
221 + *
222 + *  Reads a specified block, and returns buffer head that contains it.
223 + *  The page cache is allocated from movable area so that it can be migrated.
224 + *  It returns NULL if the block was unreadable.
225 + */
226 +static inline struct buffer_head *
227 +__bread(struct block_device *bdev, sector_t block, unsigned size)
229 +       return __bread_gfp(bdev, block, size, __GFP_MOVABLE);
232  extern int __set_page_dirty_buffers(struct page *page);
234  #else /* CONFIG_BLOCK */
235 -- 
236 1.7.9.5