Module Name:    src
Committed By:   riastradh
Date:           Sun Dec 19 12:35:37 UTC 2021

Modified Files:
        src/sys/external/bsd/common/include/linux: completion.h

Log Message:
linux: Timeout audit -- linux/completion.h.


To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 \
    src/sys/external/bsd/common/include/linux/completion.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/external/bsd/common/include/linux/completion.h
diff -u src/sys/external/bsd/common/include/linux/completion.h:1.11 src/sys/external/bsd/common/include/linux/completion.h:1.12
--- src/sys/external/bsd/common/include/linux/completion.h:1.11	Sun Dec 19 12:22:56 2021
+++ src/sys/external/bsd/common/include/linux/completion.h	Sun Dec 19 12:35:37 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: completion.h,v 1.11 2021/12/19 12:22:56 riastradh Exp $	*/
+/*	$NetBSD: completion.h,v 1.12 2021/12/19 12:35:37 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2013 The NetBSD Foundation, Inc.
@@ -198,18 +198,18 @@ wait_for_completion_interruptible_timeou
 
 	mutex_enter(&completion->c_lock);
 
-	/* Wait until c_done is nonzero.  */
+	/* Wait until c_done is nonzero, timeout, or signal.  */
 	while (completion->c_done == 0) {
-		error = cv_timedwait_sig(&completion->c_cv,
-		    &completion->c_lock, ticks);
-		if (error)
-			goto out;
-		now = getticks();
-		if (ticks < (now - start)) {
+		if (ticks == 0) {
 			error = EWOULDBLOCK;
 			goto out;
 		}
-		ticks -= (now - start);
+		error = cv_timedwait_sig(&completion->c_cv,
+		    &completion->c_lock, MIN(ticks, INT_MAX/2));
+		now = getticks();
+		if (error)
+			goto out;
+		ticks -= MIN(ticks, (now - start));
 		start = now;
 	}
 
@@ -224,7 +224,7 @@ out:	mutex_exit(&completion->c_lock);
 		return -ERESTARTSYS;
 	} else {
 		KASSERTMSG((error == 0), "error = %d", error);
-		return ticks;
+		return MAX(1, MIN(ticks, INT_MAX/2));
 	}
 }
 
@@ -232,23 +232,23 @@ static inline int
 wait_for_completion_timeout(struct completion *completion, unsigned long ticks)
 {
 	/* XXX Arithmetic overflow...?  */
-	unsigned int start = hardclock_ticks, now;
+	unsigned int start = getticks(), now;
 	int error;
 
 	mutex_enter(&completion->c_lock);
 
-	/* Wait until c_done is nonzero.  */
+	/* Wait until c_done is nonzero or timeout.  */
 	while (completion->c_done == 0) {
-		error = cv_timedwait(&completion->c_cv, &completion->c_lock,
-		    ticks);
-		if (error)
-			goto out;
-		now = hardclock_ticks;
-		if (ticks < (now - start)) {
+		if (ticks == 0) {
 			error = EWOULDBLOCK;
 			goto out;
 		}
-		ticks -= (now - start);
+		error = cv_timedwait(&completion->c_cv, &completion->c_lock,
+		    MIN(ticks, INT_MAX/2));
+		now = getticks();
+		if (error)
+			goto out;
+		ticks -= MIN(ticks, (now - start));
 		start = now;
 	}
 
@@ -261,7 +261,7 @@ out:	mutex_exit(&completion->c_lock);
 		return 0;
 	} else {
 		KASSERTMSG((error == 0), "error = %d", error);
-		return ticks;
+		return MAX(1, MIN(ticks, INT_MAX/2));
 	}
 }
 
@@ -275,7 +275,7 @@ wait_for_completion_interruptible(struct
 
 	mutex_enter(&completion->c_lock);
 
-	/* Wait until c_done is nonzero.  */
+	/* Wait until c_done is nonzero or signal.  */
 	while (completion->c_done == 0) {
 		error = cv_wait_sig(&completion->c_cv, &completion->c_lock);
 		if (error)
@@ -287,9 +287,12 @@ wait_for_completion_interruptible(struct
 	error = 0;
 
 out:	mutex_exit(&completion->c_lock);
-	if ((error == EINTR) || (error == ERESTART))
-		error = -ERESTARTSYS;
-	return error;
+	if ((error == EINTR) || (error == ERESTART)) {
+		return -ERESTARTSYS;
+	} else {
+		KASSERTMSG((error == 0), "error = %d", error);
+		return 0;
+	}
 }
 
 /*

Reply via email to