Skip to contentSkip to conversation

btrfs: add comment on why we can return 0 if we failed to atomically lock the page in read_extent_buffer_pages()

PATCH In review Qu Wenruo 4 participants 4 messages Jan 28, 2021 – Feb 3, 2021 Btrfs file system

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

PATCH 1/1 btrfs: add comment on why we can return 0 if we failed to atomically lock the page in read_extent_buffer_pages()

In read_extent_buffer_pages(), if we failed to lock the page atomically, we just exit with return value 0.

This is pretty counter-intuitive, as normally if we can't lock what we need, we would return something like -EAGAIN.

But the that return hides under (wait == WAIT_NONE) branch, which only get triggered for readahead.

And for readahead, if we failed to lock the page, it means the extent buffer is either being read by other thread, or has been read and is under modification.
Either way the eb will or has been cached, thus readahead has no need to wait for it.

This patch will add extra comment on this counter-intuitive behavior.

---
 fs/btrfs/extent_io.c | 7 +++++++
 1 file changed, 7 insertions(+)
fs/btrfs/extent_io.c +7 -0
index 7f689ad7709c..038adc423454 100644
@@ -5577,6 +5577,13 @@ int read_extent_buffer_pages(struct extent_buffer *eb, int wait, int mirror_num)
 	for (i = 0; i < num_pages; i++) {
 		page = eb->pages[i];
 		if (wait == WAIT_NONE) {
+			/*
+			 * WAIT_NONE is only utilized by readahead. If we can't
+			 * acquire the lock atomically it means either the eb
+			 * is being read out or under modification.
+			 * Either way the eb will be or has been cached,
+			 * readahead can exit safely.
+			 */
 			if (!trylock_page(page))
 				goto unlock_exit;
 		} else {
Signature · 1 line
2.30.0
#1 lore raw

Yeah. Once you know it's only for readahead then that makes perfect sense. Thanks!

regards,
dan carpenter

Reviewed-by on v1 1/1
#2 lore raw
In reply to Qu Wenruo
Quoted 16 lines · Qu Wenruo

In read_extent_buffer_pages(), if we failed to lock the page atomically, we just exit with return value 0.

This is pretty counter-intuitive, as normally if we can't lock what we need, we would return something like -EAGAIN.

But the that return hides under (wait == WAIT_NONE) branch, which only get triggered for readahead.

And for readahead, if we failed to lock the page, it means the extent buffer is either being read by other thread, or has been read and is under modification.
Either way the eb will or has been cached, thus readahead has no need to wait for it.

Quoted 4 lines

This patch will add extra comment on this counter-intuitive behavior.

Looks good, thanks.

Quoted 25 lines
---
 fs/btrfs/extent_io.c | 7 +++++++
 1 file changed, 7 insertions(+)
fs/btrfs/extent_io.c +7 -1
index 7f689ad7709c..038adc423454 100644
@@ -5577,6 +5577,13 @@ int read_extent_buffer_pages(struct extent_buffer *eb, int wait, int mirror_num)
        for (i = 0; i < num_pages; i++) {
                page = eb->pages[i];
                if (wait == WAIT_NONE) {
+                       /*
+                        * WAIT_NONE is only utilized by readahead. If we can't
+                        * acquire the lock atomically it means either the eb
+                        * is being read out or under modification.
+                        * Either way the eb will be or has been cached,
+                        * readahead can exit safely.
+                        */
                        if (!trylock_page(page))
                                goto unlock_exit;
                } else {
--

2.30.0

Signature · 3 lines
Filipe David Manana,

“Whether you think you can, or you think you can't — you're right.”
#3 lore raw
In reply to Qu Wenruo
Quoted 15 lines · Qu Wenruo

In read_extent_buffer_pages(), if we failed to lock the page atomically, we just exit with return value 0.

This is pretty counter-intuitive, as normally if we can't lock what we need, we would return something like -EAGAIN.

But the that return hides under (wait == WAIT_NONE) branch, which only get triggered for readahead.

And for readahead, if we failed to lock the page, it means the extent buffer is either being read by other thread, or has been read and is under modification.
Either way the eb will or has been cached, thus readahead has no need to wait for it.

Quoted 4 lines

This patch will add extra comment on this counter-intuitive behavior.

Added to misc-next, thanks, I've slightly rephrased the subject.