Skip to contentSkip to conversation

staging: lustre: lustre: Use kstrtoull instead of simple_strtoull

PATCH In review Georgiana Chelu 2 participants 3 messages Sep 17, 2017 – Sep 18, 2017 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: lustre: lustre: Use kstrtoull instead of simple_strtoull

Because simple_strtoull function is obsolete, replace it with kstrtoull. To make the change, some code refactoring was necessary.

The lprocfs_write_frac_helper() convert a string to a float. To keep the functionality, split the string into begin (the digits before '.') and end (the digits after '.'). Then, apply kstrtoull both on begin and end, and build the number.

Issue found by checkpatch.pl.

---
 .../lustre/lustre/obdclass/lprocfs_status.c        | 39 +++++++++++++++-------
 1 file changed, 27 insertions(+), 12 deletions(-)
drivers/staging/lustre/lustre/obdclass/lprocfs_status.c +27 -12
index e79485b4bf7f..e01ec4dcc6cc 100644
@@ -239,7 +239,9 @@ EXPORT_SYMBOL(lprocfs_read_frac_helper);
 int lprocfs_write_frac_helper(const char __user *buffer, unsigned long count,
 			      int *val, int mult)
 {
-	char kernbuf[20], *end, *pbuf;
+	int rc, counter;
+	unsigned int long temp_val;
+	char kernbuf[20], *begin, *end, *pbuf;
 
 	if (count > (sizeof(kernbuf) - 1))
 		return -EINVAL;
@@ -254,26 +256,39 @@ int lprocfs_write_frac_helper(const char __user *buffer, unsigned long count,
 		pbuf++;
 	}
 
-	*val = (int)simple_strtoul(pbuf, &end, 10) * mult;
-	if (pbuf == end)
-		return -EINVAL;
+	begin = pbuf;
+	counter = 0;
+	while (pbuf && *pbuf != '.') {
+		pbuf++;
+		counter++;
+	}
+	begin[counter] = '\0';
+	end = pbuf;
+
+	pbuf = begin;
+	rc = kstrtoul(pbuf, 10, &temp_val);
+	if (rc != 0)
+		return rc;
+
+	*val = (int)temp_val * mult;
 
 	if (end && *end == '.') {
-		int temp_val, pow = 1;
-		int i;
+		int pow = 1;
 
 		pbuf = end + 1;
 		if (strlen(pbuf) > 5)
 			pbuf[5] = '\0'; /*only allow 5bits fractional*/
 
-		temp_val = (int)simple_strtoul(pbuf, &end, 10) * mult;
-
-		if (pbuf < end) {
-			for (i = 0; i < (end - pbuf); i++)
-				pow *= 10;
+		rc = kstrtoul(pbuf, 10, &temp_val);
+		if (rc != 0)
+			return rc;
 
-			*val += temp_val / pow;
+		temp_val = temp_val * mult;
+		while (pbuf) {
+			pbuf++;
+			pow *= 10;
 		}
+		*val += (int)temp_val / pow;
 	}
 	return 0;
 }
Signature · 1 line
2.11.0
maintainer
#1 lore raw
Quoted 9 lines · Georgiana Chelu

Because simple_strtoull function is obsolete, replace it with kstrtoull. To make the change, some code refactoring was necessary.

The lprocfs_write_frac_helper() convert a string to a float. To keep the functionality, split the string into begin (the digits before '.') and end (the digits after '.'). Then, apply kstrtoull both on begin and end, and build the number.

Doesn't this seem a lot more complex and error-prone? I recommend just leaving it as-is, there is a reason that simple_strtoull is still in the kernel tree :)

thanks,

greg k-h

#2 lore raw
Quoted 11 lines · Greg KH
Quoted 9 lines · Georgiana Chelu

Because simple_strtoull function is obsolete, replace it with kstrtoull. To make the change, some code refactoring was necessary.

The lprocfs_write_frac_helper() convert a string to a float. To keep the functionality, split the string into begin (the digits before '.') and end (the digits after '.'). Then, apply kstrtoull both on begin and end, and build the number.

Quoted 7 lines

Doesn't this seem a lot more complex and error-prone? I recommend just leaving it as-is, there is a reason that simple_strtoull is still in the kernel tree :)

thanks,

greg k-h

I understand your point. I felt that my implementation was more complex than I expected, but I wanted a feedback.

Thank you,
Georgiana