cifs: fix leaked reference on requeued write
Subsystems (2)
PATCH 1/1 cifs: fix leaked reference on requeued write
Failed async writes that are requeued may not clean up a refcount on the file, which can result in a leaked open. This scenario arises very reliably when using persistent handles and a reconnect occurs while writing.
cifs_writev_requeue only releases the reference if the write fails (rc != 0). The server->ops->async_writev operation will take its own reference, so the initial reference can always be released.
--- fs/cifs/cifssmb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/cifs/cifssmb.c +1 -1
@@ -2152,8 +2152,8 @@ cifs_writev_requeue(struct cifs_writedata *wdata) } } +kref_put (&wdata2->refcount, cifs_writedata_release);if (rc) { -kref_put (&wdata2->refcount, cifs_writedata_release);if (is_retryable_error (rc))continue ; i += nr_pages;
Signature · 1 line
2.17.1
Shyam and Pavel noticed things which didn't make sense
e.g. in cifs_writepages weare putting the reference unconditionally
but in cifs_write_from_iter we are doing the same thing. So how was
this working before - should have resulted in a reference leak and direct i/o shouldn't have had a chance to complete??
and wouldn't there be an underrun if a retryable error with your patch with it getting called twice?
Jeff,
Any thoughts on this?
Quoted 30 lines · Adam McCoy
Failed async writes that are requeued may not clean up a refcount on the file, which can result in a leaked open. This scenario arises very reliably when using persistent handles and a reconnect occurs while writing.
cifs_writev_requeueonly releases the reference if the write fails (rc != 0). Theserver->ops->async_writevoperation will take its own reference, so the initial reference can always be released.Signed-off-by Adam McCoy <[email protected]>--- fs/cifs/cifssmb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/cifs/cifssmb.c +1
-2@@ -2152,8 +2152,8 @@ cifs_writev_requeue(struct cifs_writedata *wdata) } } +kref_put (&wdata2->refcount, cifs_writedata_release);if (rc) { -kref_put (&wdata2->refcount, cifs_writedata_release);if (is_retryable_error (rc))continue ; i += nr_pages; --
Quoted 2 lines
2.17.1
--
Thanks,
Steve
Part of this makes sense Pavel reminded me:
in cifs_writepages() we don't need to reference wdata because we
are leaving the function. in cifs_write_from_iter() we put all wdatas in the list and that's why we need an extra reference there
Quoted 55 lines · Steve French
Shyam and Pavel noticed things which didn't make sense
e.g. in
cifs_writepagesweare putting the reference unconditionally
but incifs_write_from_iterwe are doing the same thing. So how was
this working before - should have resulted in a reference leak and direct i/o shouldn't have had a chance to complete??and wouldn't there be an underrun if a retryable error with your patch with it getting called twice?
Jeff,
Any thoughts on this?Quoted 30 lines · Adam McCoy
Failed async writes that are requeued may not clean up a refcount on the file, which can result in a leaked open. This scenario arises very reliably when using persistent handles and a reconnect occurs while writing.
cifs_writev_requeueonly releases the reference if the write fails (rc != 0). Theserver->ops->async_writevoperation will take its own reference, so the initial reference can always be released.Signed-off-by Adam McCoy <[email protected]>--- fs/cifs/cifssmb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/cifs/cifssmb.c +1
-2@@ -2152,8 +2152,8 @@ cifs_writev_requeue(struct cifs_writedata *wdata) } } +kref_put (&wdata2->refcount, cifs_writedata_release);if (rc) { -kref_put (&wdata2->refcount, cifs_writedata_release);if (is_retryable_error (rc))continue ; i += nr_pages; --Quoted 2 lines
2.17.1
--
Thanks,Steve
Signature · 3 lines
Thanks, Steve
Quoted 4 lines
Part of this makes sense Pavel reminded me:
incifs_writepages()we don't need to reference wdata because we
are leaving the function. incifs_write_from_iter()we put all wdatas in the list and that's why we need an extra reference there
Yes, this looks right. cifs_writev_requeue() seems to work like cifs_writepages() in that the wdata2 reference disappears when the loop exits. If the loop iterates a new struct is created each time.
Quoted 2 lines
and wouldn't there be an underrun if a retryable error with your patch with it getting called twice?
There shouldn't be any difference if there is any kind of write error (rc != 0), since the put call is just moving. The only difference should be that the put call will happen if the write succeeds.
Quoted 69 lines · Steve French
Part of this makes sense Pavel reminded me:
incifs_writepages()we don't need to reference wdata because we
are leaving the function. incifs_write_from_iter()we put all wdatas in the list and that's why we need an extra reference thereQuoted 55 lines · Steve French
Shyam and Pavel noticed things which didn't make sense
e.g. in
cifs_writepagesweare putting the reference unconditionally
but incifs_write_from_iterwe are doing the same thing. So how was
this working before - should have resulted in a reference leak and direct i/o shouldn't have had a chance to complete??and wouldn't there be an underrun if a retryable error with your patch with it getting called twice?
Jeff,
Any thoughts on this?Quoted 30 lines · Adam McCoy
Failed async writes that are requeued may not clean up a refcount on the file, which can result in a leaked open. This scenario arises very reliably when using persistent handles and a reconnect occurs while writing.
cifs_writev_requeueonly releases the reference if the write fails (rc != 0). Theserver->ops->async_writevoperation will take its own reference, so the initial reference can always be released.Signed-off-by Adam McCoy <[email protected]>--- fs/cifs/cifssmb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/cifs/cifssmb.c +1
-2@@ -2152,8 +2152,8 @@ cifs_writev_requeue(struct cifs_writedata *wdata) } } +kref_put (&wdata2->refcount, cifs_writedata_release);if (rc) { -kref_put (&wdata2->refcount, cifs_writedata_release);if (is_retryable_error (rc))continue ; i += nr_pages; --Quoted 2 lines
2.17.1
--
Thanks,Steve
Signature · 3 lines
Thanks, Steve
Pavel Shilovsky
Reviewed-byср, 13 мая 2020 г. в 18:18, Adam McCoy <[email protected]>:
Quoted 10 lines
Quoted 5 lines
Part of this makes sense Pavel reminded me:
incifs_writepages()we don't need to reference wdata because we
are leaving the function. incifs_write_from_iter()we put all wdatas in the list and that's why we need an extra reference thereYes, this looks right.
cifs_writev_requeue()seems to work likecifs_writepages()in that the wdata2 reference disappears when the loop exits. If the loop iterates a new struct is created each time.
Quoted 7 lines
Quoted 2 lines
and wouldn't there be an underrun if a retryable error with your patch with it getting called twice?
There shouldn't be any difference if there is any kind of write error (rc != 0), since the put call is just moving. The only difference should be that the put call will happen if the write succeeds.
Thanks for the patch! Good catch!
This is the old code and the problem is important to fix, so, stable tag is needed.
--
Best regards,
Pavel Shilovsky