linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] USER_HZ & SG problem [timeouts]
@ 2002-09-13 19:42 Rolf Fokkens
  0 siblings, 0 replies; only message in thread
From: Rolf Fokkens @ 2002-09-13 19:42 UTC (permalink / raw)
  To: linux-kernel

Hi!

One of the places in the kernel where HZ is interfaced with userspace
is in the SG driver. With HZ different from USER_HZ this may result
in all kinds of unexpected timeouts when using SG.

This patch is an attempt to fix this by transforming USER_HZ based timing to
HZ based timing an v.v. A problem I know of is the fact that SG_GET_TIMEOUT
may return a different value than the value that was passed on with
SG_SET_TIMEOUT.  Depending on its impact this may need to be solved by
adding a field "user_timeout" to Sg_fd which contains the USER_HZ based value.

Cheers,

Rolf

--- linux-2.5.34.orig/drivers/scsi/sg.c	Sun Sep  8 09:00:32 2002
+++ linux-2.5.34/drivers/scsi/sg.c	Fri Sep 13 21:30:24 2002
@@ -731,6 +731,17 @@
 	return 0;
 }
 
+/*
+ * Suppose you want to calculate the formula muldiv(x,m,d)=int(x * m / d)
+ * Then when using 32 bit integers x * m may overflow during the calculation.
+ * Replacing muldiv(x) by muldiv(x)=((x % d) * m) / d + int(x / d) * m
+ * calculates the same, but prevents the overflow when both m and d
+ * are "small" numbers (like HZ and USER_HZ).
+ * Of course an overflow is inavoidable if the result of muldiv doesn't fit
+ * in 32 bits.
+ */
+#define MULDIV(X,MUL,DIV) ((((X % DIV) * MUL) / DIV) + ((X / DIV) * MUL))
+
 static int
 sg_ioctl(struct inode *inode, struct file *filp,
 	 unsigned int cmd_in, unsigned long arg)
@@ -790,10 +801,15 @@
 			return result;
 		if (val < 0)
 			return -EIO;
-		sfp->timeout = val;
+		if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
+		    val = MULDIV (INT_MAX, USER_HZ, HZ);
+		sfp->timeout = MULDIV (val, HZ, USER_HZ);
+
 		return 0;
 	case SG_GET_TIMEOUT:	/* N.B. User receives timeout as return value */
-		return sfp->timeout;	/* strange ..., for backward compatibility */
+		val = sfp->timeout;
+		val = MULDIV (val, USER_HZ, HZ);
+		return val;	/* strange ..., for backward compatibility */
 	case SG_SET_FORCE_LOW_DMA:
 		result = get_user(val, (int *) arg);
 		if (result)

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2002-09-13 19:39 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-09-13 19:42 [PATCH] USER_HZ & SG problem [timeouts] Rolf Fokkens

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).