Skip to contentSkip to conversation

CIFS: Fix a bug with re-sending wdata when transport returning -EAGAIN

PATCH Superseded Long Li 3 participants 4 messages Mar 2, 2019 – Mar 5, 2019

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Long Li

author
#0 lore raw

PATCH 1/2 CIFS: Fix a bug with re-sending wdata when transport returning -EAGAIN

From: Long Li <[email protected]>

When sending a wdata, transport may return -EAGAIN. In this case we should re-obtain credits because the session may have been reconnected.

---
 fs/cifs/file.c | 61 +++++++++++++++++++++++++-------------------------
 1 file changed, 31 insertions(+), 30 deletions(-)
fs/cifs/file.c +31 -30
index 9b53f33137b3..08e73759d6ec 100644
@@ -2620,43 +2620,44 @@ cifs_resend_wdata(struct cifs_writedata *wdata, struct list_head *wdata_list,
 	struct TCP_Server_Info *server =
 		tlink_tcon(wdata->cfile->tlink)->ses->server;
 
-	/*
-	 * Wait for credits to resend this wdata.
-	 * Note: we are attempting to resend the whole wdata not in segments
-	 */
 	do {
-		rc = server->ops->wait_mtu_credits(server, wdata->bytes, &wsize,
-						   &credits);
-
-		if (rc)
-			goto out;
-
-		if (wsize < wdata->bytes) {
-			add_credits_and_wake_if(server, &credits, 0);
-			msleep(1000);
-		}
-	} while (wsize < wdata->bytes);
+		/*
+		 * Wait for credits to resend this wdata.
+		 * Note: we are attempting to resend the whole wdata not in
+		 * segments
+		 */
+		do {
+			rc = server->ops->wait_mtu_credits(server, wdata->bytes,
+						&wsize, &credits);
+			if (rc)
+				goto fail;
+
+			if (wsize < wdata->bytes) {
+				add_credits_and_wake_if(server, &credits, 0);
+				msleep(1000);
+			}
+		} while (wsize < wdata->bytes);
 
-	wdata->credits = credits;
-	rc = -EAGAIN;
-	while (rc == -EAGAIN) {
+		wdata->credits = credits;
 		rc = 0;
 		if (wdata->cfile->invalidHandle)
 			rc = cifs_reopen_file(wdata->cfile, false);
 		if (!rc)
 			rc = server->ops->async_writev(wdata,
-					cifs_uncached_writedata_release);
-	}
+				cifs_uncached_writedata_release);
 
-	if (!rc) {
-		list_add_tail(&wdata->list, wdata_list);
-		return 0;
-	}
+		/* If the write was successfully sent, we are done */
+		if (!rc) {
+			list_add_tail(&wdata->list, wdata_list);
+			return 0;
+		}
 
-	add_credits_and_wake_if(server, &wdata->credits, 0);
-out:
-	kref_put(&wdata->refcount, cifs_uncached_writedata_release);
+		/* Roll back credits and retry if needed */
+		add_credits_and_wake_if(server, &wdata->credits, 0);
+	} while (rc == -EAGAIN);
 
+fail:
+	kref_put(&wdata->refcount, cifs_uncached_writedata_release);
 	return rc;
 }
 
@@ -2884,12 +2885,12 @@ static void collect_uncached_write_data(struct cifs_aio_ctx *ctx)
 						wdata->bytes, &tmp_from,
 						ctx->cfile, cifs_sb, &tmp_list,
 						ctx);
+
+					kref_put(&wdata->refcount,
+						cifs_uncached_writedata_release);
 				}
 
 				list_splice(&tmp_list, &ctx->list);
-
-				kref_put(&wdata->refcount,
-					 cifs_uncached_writedata_release);
 				goto restart_loop;
 			}
 		}
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH 2/2 CIFS: Fix a bug with re-sending rdata when transport returning -EAGAIN

From: Long Li <[email protected]>

When sending a rdata, transport may return -EAGAIN. In this case we should re-obtain credits because the session may have been reconnected.

---
 fs/cifs/file.c | 51 +++++++++++++++++++++++++-------------------------
 1 file changed, 26 insertions(+), 25 deletions(-)
fs/cifs/file.c +26 -25
index 08e73759d6ec..c83ca96f883b 100644
@@ -3335,44 +3335,45 @@ static int cifs_resend_rdata(struct cifs_readdata *rdata,
 	struct TCP_Server_Info *server =
 		tlink_tcon(rdata->cfile->tlink)->ses->server;
 
-	/*
-	 * Wait for credits to resend this rdata.
-	 * Note: we are attempting to resend the whole rdata not in segments
-	 */
 	do {
-		rc = server->ops->wait_mtu_credits(server, rdata->bytes,
+		/*
+		 * Wait for credits to resend this rdata.
+		 * Note: we are attempting to resend the whole rdata not in
+		 * segments
+		 */
+		do {
+			rc = server->ops->wait_mtu_credits(server, rdata->bytes,
 						&rsize, &credits);
 
-		if (rc)
-			goto out;
+			if (rc)
+				goto fail;
 
-		if (rsize < rdata->bytes) {
-			add_credits_and_wake_if(server, &credits, 0);
-			msleep(1000);
-		}
-	} while (rsize < rdata->bytes);
+			if (rsize < rdata->bytes) {
+				add_credits_and_wake_if(server, &credits, 0);
+				msleep(1000);
+			}
+		} while (rsize < rdata->bytes);
 
-	rdata->credits = credits;
-	rc = -EAGAIN;
-	while (rc == -EAGAIN) {
+		rdata->credits = credits;
 		rc = 0;
 		if (rdata->cfile->invalidHandle)
 			rc = cifs_reopen_file(rdata->cfile, true);
 		if (!rc)
 			rc = server->ops->async_readv(rdata);
-	}
 
-	if (!rc) {
-		/* Add to aio pending list */
-		list_add_tail(&rdata->list, rdata_list);
-		return 0;
-	}
+		/* If the read was successfully sent, we are done */
+		if (!rc) {
+			/* Add to aio pending list */
+			list_add_tail(&rdata->list, rdata_list);
+			return 0;
+		}
 
-	add_credits_and_wake_if(server, &rdata->credits, 0);
-out:
-	kref_put(&rdata->refcount,
-		cifs_uncached_readdata_release);
+		/* Roll back credits and retry if needed */
+		add_credits_and_wake_if(server, &rdata->credits, 0);
+	} while (rc == -EAGAIN);
 
+fail:
+	kref_put(&rdata->refcount, cifs_uncached_readdata_release);
 	return rc;
 }
 
Signature · 1 line
2.17.1
#2 lore raw
In reply to Long Li

I tested this vs. Samba a few minutes ago with test 208 - looks like
it works. Thank you!

Now just need some additional reviews as this can be a complex area of code.

Quoted 107 lines · Long Li

From: Long Li <[email protected]>

When sending a wdata, transport may return -EAGAIN. In this case we should re-obtain credits because the session may have been reconnected.

---
 fs/cifs/file.c | 61 +++++++++++++++++++++++++-------------------------
 1 file changed, 31 insertions(+), 30 deletions(-)
fs/cifs/file.c +31 -31
index 9b53f33137b3..08e73759d6ec 100644
@@ -2620,43 +2620,44 @@ cifs_resend_wdata(struct cifs_writedata *wdata, struct list_head *wdata_list,
        struct TCP_Server_Info *server =
                tlink_tcon(wdata->cfile->tlink)->ses->server;
 
-       /*
-        * Wait for credits to resend this wdata.
-        * Note: we are attempting to resend the whole wdata not in segments
-        */
        do {
-               rc = server->ops->wait_mtu_credits(server, wdata->bytes, &wsize,
-                                                  &credits);
-
-               if (rc)
-                       goto out;
-
-               if (wsize < wdata->bytes) {
-                       add_credits_and_wake_if(server, &credits, 0);
-                       msleep(1000);
-               }
-       } while (wsize < wdata->bytes);
+               /*
+                * Wait for credits to resend this wdata.
+                * Note: we are attempting to resend the whole wdata not in
+                * segments
+                */
+               do {
+                       rc = server->ops->wait_mtu_credits(server, wdata->bytes,
+                                               &wsize, &credits);
+                       if (rc)
+                               goto fail;
+
+                       if (wsize < wdata->bytes) {
+                               add_credits_and_wake_if(server, &credits, 0);
+                               msleep(1000);
+                       }
+               } while (wsize < wdata->bytes);
 
-       wdata->credits = credits;
-       rc = -EAGAIN;
-       while (rc == -EAGAIN) {
+               wdata->credits = credits;
                rc = 0;
                if (wdata->cfile->invalidHandle)
                        rc = cifs_reopen_file(wdata->cfile, false);
                if (!rc)
                        rc = server->ops->async_writev(wdata,
-                                       cifs_uncached_writedata_release);
-       }
+                               cifs_uncached_writedata_release);
 
-       if (!rc) {
-               list_add_tail(&wdata->list, wdata_list);
-               return 0;
-       }
+               /* If the write was successfully sent, we are done */
+               if (!rc) {
+                       list_add_tail(&wdata->list, wdata_list);
+                       return 0;
+               }
 
-       add_credits_and_wake_if(server, &wdata->credits, 0);
-out:
-       kref_put(&wdata->refcount, cifs_uncached_writedata_release);
+               /* Roll back credits and retry if needed */
+               add_credits_and_wake_if(server, &wdata->credits, 0);
+       } while (rc == -EAGAIN);
 
+fail:
+       kref_put(&wdata->refcount, cifs_uncached_writedata_release);
        return rc;
 }
 
@@ -2884,12 +2885,12 @@ static void collect_uncached_write_data(struct cifs_aio_ctx *ctx)
                                                wdata->bytes, &tmp_from,
                                                ctx->cfile, cifs_sb, &tmp_list,
                                                ctx);
+
+                                       kref_put(&wdata->refcount,
+                                               cifs_uncached_writedata_release);
                                }
 
                                list_splice(&tmp_list, &ctx->list);
-
-                               kref_put(&wdata->refcount,
-                                        cifs_uncached_writedata_release);
                                goto restart_loop;
                        }
                }
--

2.17.1

Signature · 3 lines
Thanks,

Steve
#3 lore raw
In reply to Long Li

пт, 1 мар. 2019 г. в 19:04, Long Li <[email protected]>:

Quoted 34 lines

From: Long Li <[email protected]>

When sending a wdata, transport may return -EAGAIN. In this case we should re-obtain credits because the session may have been reconnected.

---
 fs/cifs/file.c | 61 +++++++++++++++++++++++++-------------------------
 1 file changed, 31 insertions(+), 30 deletions(-)
fs/cifs/file.c +0 -13
index 9b53f33137b3..08e73759d6ec 100644
@@ -2620,43 +2620,44 @@ cifs_resend_wdata(struct cifs_writedata *wdata, struct list_head *wdata_list,
        struct TCP_Server_Info *server =
                tlink_tcon(wdata->cfile->tlink)->ses->server;
 
-       /*
-        * Wait for credits to resend this wdata.
-        * Note: we are attempting to resend the whole wdata not in segments
-        */
        do {
-               rc = server->ops->wait_mtu_credits(server, wdata->bytes, &wsize,
-                                                  &credits);
-
-               if (rc)
-                       goto out;
-
-               if (wsize < wdata->bytes) {
-                       add_credits_and_wake_if(server, &credits, 0);
-                       msleep(1000);

fs/cifs/file.c cifs_resend_wdata(struct cifs_writedata *wdata, struct list_head *wdata_list, · line 2631

Quoted 10 lines
fs/cifs/file.c +8 -2
-               }
-       } while (wsize < wdata->bytes);
+               /*
+                * Wait for credits to resend this wdata.
+                * Note: we are attempting to resend the whole wdata not in
+                * segments
+                */
+               do {
+                       rc = server->ops->wait_mtu_credits(server, wdata->bytes,
+                                               &wsize, &credits);

The steps in write/read codepaths have been recently re-ordered: now we need to first check the open handle, re-open it if needed and then attempt to get credits. This approach avoid a possibility to be stuck waiting for credits because generally cifs_reopen_file() may need a lot of credits to complete (up to 2 SMB2_CREATE commands and arbitrary number of SMB2_LOCK commands).

Please look at this loop as an example:
https://git.samba.org/?p=sfrench/cifs-2.6.git;a=blob;f=fs/cifs/file.c;h=bf864123093162c7ef5cd49ee08d8b40ad7938c8;hb=778d81b65e4d596251943002522d94a7c6fbcf69#l2690

fs/cifs/file.c cifs_resend_wdata(struct cifs_writedata *wdata, struct list_head *wdata_list, · line 2641

Quoted 13 lines
fs/cifs/file.c +9 -3
+                       if (rc)
+                               goto fail;
+
+                       if (wsize < wdata->bytes) {
+                               add_credits_and_wake_if(server, &credits, 0);
+                               msleep(1000);
+                       }
+               } while (wsize < wdata->bytes);
 
-       wdata->credits = credits;
-       rc = -EAGAIN;
-       while (rc == -EAGAIN) {
+               wdata->credits = credits;

let's call adjust_credits before calling async_writev() as in the example above.

Quoted 15 lines
fs/cifs/file.c +3 -6
                rc = 0;
                if (wdata->cfile->invalidHandle)
                        rc = cifs_reopen_file(wdata->cfile, false);
                if (!rc)
                        rc = server->ops->async_writev(wdata,
-                                       cifs_uncached_writedata_release);
-       }
+                               cifs_uncached_writedata_release);
 
-       if (!rc) {
-               list_add_tail(&wdata->list, wdata_list);
-               return 0;
-       }
+               /* If the write was successfully sent, we are done */
+               if (!rc) {

fs/cifs/file.c cifs_resend_wdata(struct cifs_writedata *wdata, struct list_head *wdata_list, · line 2657

Quoted 10 lines
fs/cifs/file.c +6 -3
+                       list_add_tail(&wdata->list, wdata_list);
+                       return 0;
+               }
 
-       add_credits_and_wake_if(server, &wdata->credits, 0);
-out:
-       kref_put(&wdata->refcount, cifs_uncached_writedata_release);
+               /* Roll back credits and retry if needed */
+               add_credits_and_wake_if(server, &wdata->credits, 0);
+       } while (rc == -EAGAIN);

I don't think there is a need for the nested loop here. How about just do:

do {
    if (wdata->cfile->invalidHandle) {
         rc = cifs_reopen_file(...);
         if (rc) break;
    }

    // get_credits
    // if not enough credits - wait and continue the loop.

    rc = adjust_credits(...);
    if (!rc) {
         if (wdata->cfile->invalidHandle)
             // set rc, put credits and continue the loop.
        else
            // send async write
    }
    // handle write errors
} while(rc == -EAGAIN);
Quoted 23 lines
fs/cifs/file.c +5 -4
+fail:
+       kref_put(&wdata->refcount, cifs_uncached_writedata_release);
        return rc;
 }
 
@@ -2884,12 +2885,12 @@ static void collect_uncached_write_data(struct cifs_aio_ctx *ctx)
                                                wdata->bytes, &tmp_from,
                                                ctx->cfile, cifs_sb, &tmp_list,
                                                ctx);
+
+                                       kref_put(&wdata->refcount,
+                                               cifs_uncached_writedata_release);
                                }
 
                                list_splice(&tmp_list, &ctx->list);
-
-                               kref_put(&wdata->refcount,
-                                        cifs_uncached_writedata_release);
                                goto restart_loop;
                        }
                }
--
Quoted 2 lines

2.17.1

The similar concepts applies for async reads as well.

--
Best regards,
Pavel Shilovsky