Skip to content

Commit 676ce6d

Browse files
Hugh Dickinsaxboe
authored andcommitted
block: replace __getblk_slow misfix by grow_dev_page fix
Commit 91f68c8 ("block: fix infinite loop in __getblk_slow") is not good: a successful call to grow_buffers() cannot guarantee that the page won't be reclaimed before the immediate next call to __find_get_block(), which is why there was always a loop there. Yesterday I got "EXT4-fs error (device loop0): __ext4_get_inode_loc:3595: inode #19278: block 664: comm cc1: unable to read itable block" on console, which pointed to this commit. I've been trying to bisect for weeks, why kbuild-on-ext4-on-loop-on-tmpfs sometimes fails from a missing header file, under memory pressure on ppc G5. I've never seen this on x86, and I've never seen it on 3.5-rc7 itself, despite that commit being in there: bisection pointed to an irrelevant pinctrl merge, but hard to tell when failure takes between 18 minutes and 38 hours (but so far it's happened quicker on 3.6-rc2). (I've since found such __ext4_get_inode_loc errors in /var/log/messages from previous weeks: why the message never appeared on console until yesterday morning is a mystery for another day.) Revert 91f68c8, restoring __getblk_slow() to how it was (plus a checkpatch nitfix). Simplify the interface between grow_buffers() and grow_dev_page(), and avoid the infinite loop beyond end of device by instead checking init_page_buffers()'s end_block there (I presume that's more efficient than a repeated call to blkdev_max_block()), returning -ENXIO to __getblk_slow() in that case. And remove akpm's ten-year-old "__getblk() cannot fail ... weird" comment, but that is worrying: are all users of __getblk() really now prepared for a NULL bh beyond end of device, or will some oops?? Signed-off-by: Hugh Dickins <hughd@google.com> Cc: stable@vger.kernel.org # 3.0 3.2 3.4 3.5 Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 79df9b4 commit 676ce6d

1 file changed

Lines changed: 30 additions & 36 deletions

File tree

‎fs/buffer.c‎

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ link_dev_buffers(struct page *page, struct buffer_head *head)
914914
/*
915915
* Initialise the state of a blockdev page's buffers.
916916
*/
917-
static void
917+
static sector_t
918918
init_page_buffers(struct page *page, struct block_device *bdev,
919919
sector_t block, int size)
920920
{
@@ -936,33 +936,41 @@ init_page_buffers(struct page *page, struct block_device *bdev,
936936
block++;
937937
bh = bh->b_this_page;
938938
} while (bh != head);
939+
940+
/*
941+
* Caller needs to validate requested block against end of device.
942+
*/
943+
return end_block;
939944
}
940945

941946
/*
942947
* Create the page-cache page that contains the requested block.
943948
*
944-
* This is user purely for blockdev mappings.
949+
* This is used purely for blockdev mappings.
945950
*/
946-
static struct page *
951+
static int
947952
grow_dev_page(struct block_device *bdev, sector_t block,
948-
pgoff_t index, int size)
953+
pgoff_t index, int size, int sizebits)
949954
{
950955
struct inode *inode = bdev->bd_inode;
951956
struct page *page;
952957
struct buffer_head *bh;
958+
sector_t end_block;
959+
int ret = 0; /* Will call free_more_memory() */
953960

954961
page = find_or_create_page(inode->i_mapping, index,
955962
(mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS)|__GFP_MOVABLE);
956963
if (!page)
957-
return NULL;
964+
return ret;
958965

959966
BUG_ON(!PageLocked(page));
960967

961968
if (page_has_buffers(page)) {
962969
bh = page_buffers(page);
963970
if (bh->b_size == size) {
964-
init_page_buffers(page, bdev, block, size);
965-
return page;
971+
end_block = init_page_buffers(page, bdev,
972+
index << sizebits, size);
973+
goto done;
966974
}
967975
if (!try_to_free_buffers(page))
968976
goto failed;
@@ -982,14 +990,14 @@ grow_dev_page(struct block_device *bdev, sector_t block,
982990
*/
983991
spin_lock(&inode->i_mapping->private_lock);
984992
link_dev_buffers(page, bh);
985-
init_page_buffers(page, bdev, block, size);
993+
end_block = init_page_buffers(page, bdev, index << sizebits, size);
986994
spin_unlock(&inode->i_mapping->private_lock);
987-
return page;
988-
995+
done:
996+
ret = (block < end_block) ? 1 : -ENXIO;
989997
failed:
990998
unlock_page(page);
991999
page_cache_release(page);
992-
return NULL;
1000+
return ret;
9931001
}
9941002

9951003
/*
@@ -999,7 +1007,6 @@ grow_dev_page(struct block_device *bdev, sector_t block,
9991007
static int
10001008
grow_buffers(struct block_device *bdev, sector_t block, int size)
10011009
{
1002-
struct page *page;
10031010
pgoff_t index;
10041011
int sizebits;
10051012

@@ -1023,22 +1030,14 @@ grow_buffers(struct block_device *bdev, sector_t block, int size)
10231030
bdevname(bdev, b));
10241031
return -EIO;
10251032
}
1026-
block = index << sizebits;
1033+
10271034
/* Create a page with the proper size buffers.. */
1028-
page = grow_dev_page(bdev, block, index, size);
1029-
if (!page)
1030-
return 0;
1031-
unlock_page(page);
1032-
page_cache_release(page);
1033-
return 1;
1035+
return grow_dev_page(bdev, block, index, size, sizebits);
10341036
}
10351037

10361038
static struct buffer_head *
10371039
__getblk_slow(struct block_device *bdev, sector_t block, int size)
10381040
{
1039-
int ret;
1040-
struct buffer_head *bh;
1041-
10421041
/* Size must be multiple of hard sectorsize */
10431042
if (unlikely(size & (bdev_logical_block_size(bdev)-1) ||
10441043
(size < 512 || size > PAGE_SIZE))) {
@@ -1051,21 +1050,20 @@ __getblk_slow(struct block_device *bdev, sector_t block, int size)
10511050
return NULL;
10521051
}
10531052

1054-
retry:
1055-
bh = __find_get_block(bdev, block, size);
1056-
if (bh)
1057-
return bh;
1053+
for (;;) {
1054+
struct buffer_head *bh;
1055+
int ret;
10581056

1059-
ret = grow_buffers(bdev, block, size);
1060-
if (ret == 0) {
1061-
free_more_memory();
1062-
goto retry;
1063-
} else if (ret > 0) {
10641057
bh = __find_get_block(bdev, block, size);
10651058
if (bh)
10661059
return bh;
1060+
1061+
ret = grow_buffers(bdev, block, size);
1062+
if (ret < 0)
1063+
return NULL;
1064+
if (ret == 0)
1065+
free_more_memory();
10671066
}
1068-
return NULL;
10691067
}
10701068

10711069
/*
@@ -1321,10 +1319,6 @@ EXPORT_SYMBOL(__find_get_block);
13211319
* which corresponds to the passed block_device, block and size. The
13221320
* returned buffer has its reference count incremented.
13231321
*
1324-
* __getblk() cannot fail - it just keeps trying. If you pass it an
1325-
* illegal block number, __getblk() will happily return a buffer_head
1326-
* which represents the non-existent block. Very weird.
1327-
*
13281322
* __getblk() will lock up the machine if grow_dev_page's try_to_free_buffers()
13291323
* attempt is failing. FIXME, perhaps?
13301324
*/

0 commit comments

Comments
 (0)