Skip to contentSkip to conversation

block: Do not write past block device boundary during a flush

PATCH Applied Andrey Smirnov 2 participants 3 messages Jan 22, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 block: Do not write past block device boundary during a flush

When calling I/O functions of underlying block device driver we always need to make sure that its size is small enough to not go past device's boundary. Not only in get_chunk() and block_cache(), but in writebuffer_flush() as well. Since the same code is used in three different places, move it into a subroutine and adjust all of the calls to ->write()/->read() accordingly.

---
 common/block.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)
common/block.c +13 -9
index 35173c65f..3a031a4fc 100644
@@ -38,6 +38,11 @@ struct chunk {
 
 #define BUFSIZE (PAGE_SIZE * 4)
 
+static int writebuffer_io_len(struct block_device *blk, struct chunk *chunk)
+{
+	return min(blk->rdbufsize, blk->num_blocks - chunk->block_start);
+}
+
 /*
  * Write all dirty chunks back to the device
  */
@@ -51,7 +56,9 @@ static int writebuffer_flush(struct block_device *blk)
 
 	list_for_each_entry(chunk, &blk->buffered_blocks, list) {
 		if (chunk->dirty) {
-			ret = blk->ops->write(blk, chunk->data, chunk->block_start, blk->rdbufsize);
+			ret = blk->ops->write(blk, chunk->data,
+					      chunk->block_start,
+					      writebuffer_io_len(blk, chunk));
 			if (ret < 0)
 				return ret;
 
@@ -118,10 +125,9 @@ static struct chunk *get_chunk(struct block_device *blk)
 		/* use last entry which is the most unused */
 		chunk = list_last_entry(&blk->buffered_blocks, struct chunk, list);
 		if (chunk->dirty) {
-			size_t num_blocks = min(blk->rdbufsize,
-					blk->num_blocks - chunk->block_start);
-			ret = blk->ops->write(blk, chunk->data, chunk->block_start,
-					      num_blocks);
+			ret = blk->ops->write(blk, chunk->data,
+					      chunk->block_start,
+					      writebuffer_io_len(blk, chunk));
 			if (ret < 0)
 				return ERR_PTR(ret);
 
@@ -145,7 +151,6 @@ static struct chunk *get_chunk(struct block_device *blk)
 static int block_cache(struct block_device *blk, int block)
 {
 	struct chunk *chunk;
-	size_t num_blocks;
 	int ret;
 
 	chunk = get_chunk(blk);
@@ -157,9 +162,8 @@ static int block_cache(struct block_device *blk, int block)
 	dev_dbg(blk->dev, "%s: %d to %d\n", __func__, chunk->block_start,
 		chunk->num);
 
-	num_blocks = min(blk->rdbufsize, blk->num_blocks - chunk->block_start);
-
-	ret = blk->ops->read(blk, chunk->data, chunk->block_start, num_blocks);
+	ret = blk->ops->read(blk, chunk->data, chunk->block_start,
+			     writebuffer_io_len(blk, chunk));
 	if (ret) {
 		list_add_tail(&chunk->list, &blk->idle_blocks);
 		return ret;
Signature · 7 lines
2.20.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#1 lore raw

PATCH 2/2 block: Move shared code in get_chunk() out of if statement

---
 common/block.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
common/block.c +2 -3
index 3a031a4fc..291721876 100644
@@ -133,13 +133,12 @@ static struct chunk *get_chunk(struct block_device *blk)
 
 			chunk->dirty = 0;
 		}
-
-		list_del(&chunk->list);
 	} else {
 		chunk = list_first_entry(&blk->idle_blocks, struct chunk, list);
-		list_del(&chunk->list);
 	}
 
+	list_del(&chunk->list);
+
 	return chunk;
 }
 
Signature · 7 lines
2.20.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Applied
#2 lore raw
In reply to Andrey Smirnov
Quoted 11 lines · Andrey Smirnov

When calling I/O functions of underlying block device driver we always need to make sure that its size is small enough to not go past device's boundary. Not only in get_chunk() and block_cache(), but in writebuffer_flush() as well. Since the same code is used in three different places, move it into a subroutine and adjust all of the calls to ->write()/->read() accordingly.

---
 common/block.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

Applied, thanks

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox