Skip to contentSkip to conversation

lprocfs_write_frac_u64_helper cleanup

PATCH Superseded Chris Rorvick 3 participants 5 messages Dec 17, 2014 Staging

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover lprocfs_write_frac_u64_helper cleanup

Added a second patch to address Dan Carpenter's concern with the complexity of passing the sign through `mult'. Compile tested only.

Chris Rorvick (2):
drivers: staging: lustre: Use mult if units not specified
drivers: staging: lustre: Track sign separately

 drivers/staging/lustre/lustre/obdclass/lprocfs_status.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
Signature · 1 line
2.1.0
#1 lore raw

PATCH v2 1/2 drivers: staging: lustre: Use mult if units not specified

Units can be passed to lprocfs_write_frac_u64_helper() via a suffix (e.g., "...K", "...M", etc.) tacked onto the value. A comment states that "specified units override the multiplier," though the multiplier is overridden regardless. Update the conditional logic so that it only applies when units are specified.

---
 drivers/staging/lustre/lustre/obdclass/lprocfs_status.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/lustre/lustre/obdclass/lprocfs_status.c +1 -1
index 61e04af..92ed0a0 100644
@@ -1910,7 +1910,7 @@ int lprocfs_write_frac_u64_helper(const char *buffer, unsigned long count,
 		units <<= 10;
 	}
 	/* Specified units override the multiplier */
-	if (units)
+	if (units > 1)
 		mult = mult < 0 ? -units : units;
 
 	frac *= mult;
Signature · 1 line
2.1.0
#2 lore raw

PATCH v2 2/2 drivers: staging: lustre: Track sign separately

In reply to Chris Rorvick

The mult' parameter is negated if the user data begins with a '-' so that the final value has the appropriate sign. But mult' is only used if the user data does not include a "units" suffix. In this case, `mult' is overridden with the numeric scale conveyed by the units suffix, but retains the sign of the original value.

Having mult' serving double-duty works but is confusing. Use a new local variable to store the sign of the user data instead. This also fixes a pitfall of passing 0 to mult', expecting it to be ignored when a units suffix is specified, but having the effect of taking the absolute value of the user-provided data.

---
 drivers/staging/lustre/lustre/obdclass/lprocfs_status.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/staging/lustre/lustre/obdclass/lprocfs_status.c +4 -3
index 92ed0a0..b6c3352 100644
@@ -1864,6 +1864,7 @@ int lprocfs_write_frac_u64_helper(const char *buffer, unsigned long count,
 	char kernbuf[22], *end, *pbuf;
 	__u64 whole, frac = 0, units;
 	unsigned frac_d = 1;
+	int sign = 1;
 
 	if (count > (sizeof(kernbuf) - 1))
 		return -EINVAL;
@@ -1874,7 +1875,7 @@ int lprocfs_write_frac_u64_helper(const char *buffer, unsigned long count,
 	kernbuf[count] = '\0';
 	pbuf = kernbuf;
 	if (*pbuf == '-') {
-		mult = -mult;
+		sign = -1;
 		pbuf++;
 	}
 
@@ -1911,11 +1912,11 @@ int lprocfs_write_frac_u64_helper(const char *buffer, unsigned long count,
 	}
 	/* Specified units override the multiplier */
 	if (units > 1)
-		mult = mult < 0 ? -units : units;
+		mult = units;
 
 	frac *= mult;
 	do_div(frac, frac_d);
-	*val = whole * mult + frac;
+	*val = sign * (whole * mult + frac);
 	return 0;
 }
 EXPORT_SYMBOL(lprocfs_write_frac_u64_helper);
Signature · 1 line
2.1.0
Reviewed-by on v2 2/2
#3 lore raw

Re: [PATCH v2 2/2] drivers: staging: lustre: Track sign separately

Quoted 6 lines · Chris Rorvick

The mult' parameter is negated if the user data begins with a '-' so that the final value has the appropriate sign. But mult' is only used if the user data does not include a "units" suffix. In this case, `mult' is overridden with the numeric scale conveyed by the units suffix, but retains the sign of the original value.

Quoted 7 lines

Having mult' serving double-duty works but is confusing. Use a new local variable to store the sign of the user data instead. This also fixes a pitfall of passing 0 to mult', expecting it to be ignored when a units suffix is specified, but having the effect of taking the absolute value of the user-provided data.

Both patches look good to me.

Quoted 42 lines

---
drivers/staging/lustre/lustre/obdclass/lprocfs_status.c | 7 ++++---

1 file changed, 4 insertions(+), 3 deletions(-)
drivers/staging/lustre/lustre/obdclass/lprocfs_status.c +1 -0
index 92ed0a0..b6c3352 100644
@@ -1864,6 +1864,7 @@ int lprocfs_write_frac_u64_helper(const char
 *buffer, unsigned long count,
 	char kernbuf[22], *end, *pbuf;
 	__u64 whole, frac = 0, units;
 	unsigned frac_d = 1;
+	int sign = 1;
 
 	if (count > (sizeof(kernbuf) - 1))
		return -EINVAL;
drivers/staging/lustre/lustre/obdclass/lprocfs_status.c +3 -3
@@ -1874,7 +1875,7 @@ int lprocfs_write_frac_u64_helper(const char
 *buffer, unsigned long count,
 	kernbuf[count] = '\0';
 	pbuf = kernbuf;
 	if (*pbuf == '-') {
-		mult = -mult;
+		sign = -1;
 		pbuf++;
 	}
@@ -1911,11 +1912,11 @@ int lprocfs_write_frac_u64_helper(const char
 *buffer, unsigned long count,
 	}
 	/* Specified units override the multiplier */
 	if (units > 1)
-		mult = mult < 0 ? -units : units;
+		mult = units;
 
 	frac *= mult;
 	do_div(frac, frac_d);
-	*val = whole * mult + frac;
+	*val = sign * (whole * mult + frac);
 	return 0;
 }
Quoted 5 lines

EXPORT_SYMBOL(lprocfs_write_frac_u64_helper);

Signature · 1 line
2.1.0

Cheers, Andreas

Signature · 4 lines
Andreas Dilger

Lustre Software Architect
Intel High Performance Data Division
#4 lore raw
In reply to Chris Rorvick
Quoted 2 lines · Chris Rorvick

Added a second patch to address Dan Carpenter's concern with the complexity of passing the sign through `mult'. Compile tested only.

Thanks. That does look cleaner.

regards,
dan carpenter