Skip to contentSkip to conversation

fs/cachefiles: replace simple_strtoul with kstrtoul in fcull

PATCH In review Akhmad 3 participants 8 messages Jul 26 – Jul 27

Subsystems (2)
lore
Share & tools
lore

Akhmad

author
#0 lore raw

PATCH 1/6 fs/cachefiles: replace simple_strtoul with kstrtoul in fcull

From: Akhmad Zubairugadzhiev <[email protected]>

Replace deprecated simple_strtoul() with kstrtoul() in cachefiles_daemon_fcull(). The kstrtoul() function provides strict overflow checking and is the recommended replacement.

---
 fs/cachefiles/daemon.c | 36 +++++++++++++++++++++++-------------
 1 file changed, 23 insertions(+), 13 deletions(-)
fs/cachefiles/daemon.c +23 -13
index 4117b145a..7feae63e6 100644
@@ -405,22 +405,27 @@ static int cachefiles_daemon_range_error(struct cachefiles_cache *cache,
  */
 static int cachefiles_daemon_frun(struct cachefiles_cache *cache, char *args)
 {
-	unsigned long frun;
+        unsigned long frun;
+        int ret;
 
-	_enter(",%s", args);
+        _enter(",%s", args);
 
-	if (!*args)
-		return -EINVAL;
+        if (!*args)
+                return -EINVAL;
 
-	frun = simple_strtoul(args, &args, 10);
-	if (args[0] != '%' || args[1] != '\0')
-		return -EINVAL;
+        ret = kstrtoul(args, 10, &frun);
+        if (ret < 0)
+                return ret;
 
-	if (frun <= cache->fcull_percent || frun >= 100)
-		return cachefiles_daemon_range_error(cache, args);
+        args = strchr(args, '%');
+        if (!args || args[1] != '\0')
+                return -EINVAL;
 
-	cache->frun_percent = frun;
-	return 0;
+        if (frun <= cache->fcull_percent || frun >= 100)
+                return cachefiles_daemon_range_error(cache, args);
+
+        cache->frun_percent = frun;
+        return 0;
 }
 
 /*
@@ -430,14 +435,19 @@ static int cachefiles_daemon_frun(struct cachefiles_cache *cache, char *args)
 static int cachefiles_daemon_fcull(struct cachefiles_cache *cache, char *args)
 {
 	unsigned long fcull;
+	int ret;
 
 	_enter(",%s", args);
 
 	if (!*args)
 		return -EINVAL;
 
-	fcull = simple_strtoul(args, &args, 10);
-	if (args[0] != '%' || args[1] != '\0')
+	ret = kstrtoul(args, 10, &fcull);
+	if (ret < 0)
+		return ret;
+
+	args = strchr(args, '%');
+	if (!args || args[1] != '\0')
 		return -EINVAL;
 
 	if (fcull <= cache->fstop_percent || fcull >= cache->frun_percent)
Signature · 1 line
2.37.1 (Apple Git-137.1)
author
#1 lore raw

PATCH 2/6 fs/cachefiles: replace simple_strtoul with kstrtoul in fstop

From: Akhmad Zubairugadzhiev <[email protected]>

Replace deprecated simple_strtoul() with kstrtoul() in cachefiles_daemon_fstop(). The kstrtoul() function provides strict overflow checking and is the recommended replacement.

---
 fs/cachefiles/daemon.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
fs/cachefiles/daemon.c +7 -2
index 7feae63e6..67c5b16bb 100644
@@ -464,14 +464,19 @@ static int cachefiles_daemon_fcull(struct cachefiles_cache *cache, char *args)
 static int cachefiles_daemon_fstop(struct cachefiles_cache *cache, char *args)
 {
 	unsigned long fstop;
+	int ret;
 
 	_enter(",%s", args);
 
 	if (!*args)
 		return -EINVAL;
 
-	fstop = simple_strtoul(args, &args, 10);
-	if (args[0] != '%' || args[1] != '\0')
+	ret = kstrtoul(args, 10, &fstop);
+	if (ret < 0)
+		return ret;
+
+	args = strchr(args, '%');
+	if (!args || args[1] != '\0')
 		return -EINVAL;
 
 	if (fstop >= cache->fcull_percent)
Signature · 1 line
2.37.1 (Apple Git-137.1)
author
#2 lore raw

PATCH 3/6 fs/cachefiles: replace simple_strtoul with kstrtoul in brun

In reply to Akhmad

From: Akhmad Zubairugadzhiev <[email protected]>

Replace deprecated simple_strtoul() with kstrtoul() in cachefiles_daemon_brun(). The kstrtoul() function provides strict overflow checking and is the recommended replacement.

---
 fs/cachefiles/daemon.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
fs/cachefiles/daemon.c +7 -2
index 67c5b16bb..fd97195ec 100644
@@ -493,14 +493,19 @@ static int cachefiles_daemon_fstop(struct cachefiles_cache *cache, char *args)
 static int cachefiles_daemon_brun(struct cachefiles_cache *cache, char *args)
 {
 	unsigned long brun;
+	int ret;
 
 	_enter(",%s", args);
 
 	if (!*args)
 		return -EINVAL;
 
-	brun = simple_strtoul(args, &args, 10);
-	if (args[0] != '%' || args[1] != '\0')
+	ret = kstrtoul(args, 10, &brun);
+	if (ret < 0)
+		return ret;
+
+	args = strchr(args, '%');
+	if (!args || args[1] != '\0')
 		return -EINVAL;
 
 	if (brun <= cache->bcull_percent || brun >= 100)
Signature · 1 line
2.37.1 (Apple Git-137.1)
author
#3 lore raw

PATCH 4/6 fs/cachefiles: replace simple_strtoul with kstrtoul in bcull

In reply to Akhmad

From: Akhmad Zubairugadzhiev <[email protected]>

Replace deprecated simple_strtoul() with kstrtoul() in cachefiles_daemon_bcull(). The kstrtoul() function provides strict overflow checking and is the recommended replacement.

---
 fs/cachefiles/daemon.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
fs/cachefiles/daemon.c +7 -2
index fd97195ec..7cb1a2b19 100644
@@ -522,14 +522,19 @@ static int cachefiles_daemon_brun(struct cachefiles_cache *cache, char *args)
 static int cachefiles_daemon_bcull(struct cachefiles_cache *cache, char *args)
 {
 	unsigned long bcull;
+	int ret;
 
 	_enter(",%s", args);
 
 	if (!*args)
 		return -EINVAL;
 
-	bcull = simple_strtoul(args, &args, 10);
-	if (args[0] != '%' || args[1] != '\0')
+	ret = kstrtoul(args, 10, &bcull);
+	if (ret < 0)
+		return ret;
+
+	args = strchr(args, '%');
+	if (!args || args[1] != '\0')
 		return -EINVAL;
 
 	if (bcull <= cache->bstop_percent || bcull >= cache->brun_percent)
Signature · 1 line
2.37.1 (Apple Git-137.1)
maintainer
#4 lore raw

Re: [PATCH 4/6] fs/cachefiles: replace simple_strtoul with kstrtoul in bcull

fs/cachefiles/daemon.c static int cachefiles_daemon_brun(struct cachefiles_cache *cache, char *args) · line 534

Quoted 5 lines · Akhmad
fs/cachefiles/daemon.c +3 -2
-	bcull = simple_strtoul(args, &args, 10);
-	if (args[0] != '%' || args[1] != '\0')
+	ret = kstrtoul(args, 10, &bcull);
+	if (ret < 0)
+		return ret;

Nak. This is not an equivalent piece of code, but will cause EINVAL failures.

	static int _kstrtoull(const char *s, unsigned int base, unsigned long long *res)
	{
		unsigned long long _res;
		unsigned int rv;

		s = _parse_integer_fixup_radix(s, &base);
		rv = _parse_integer(s, base, &_res);
		if (rv & KSTRTOX_OVERFLOW)
			return -ERANGE;
		if (rv == 0)
			return -EINVAL;
		s += rv;
		if (*s == '\n')
			s++;
		if (*s)
			return -EINVAL;  <---- here
		*res = _res;
		return 0;
	}

kstrtoul() is *not* a drop-in replacement for simple_strtoul(). You should alter _kstrtoull() to return an end parameter too if you're going to do this. The end parameter is quite important.

David

author
#5 lore raw

PATCH 5/6 fs/cachefiles: replace simple_strtoul with kstrtoul in bstop

In reply to Akhmad

From: Akhmad Zubairugadzhiev <[email protected]>

Replace deprecated simple_strtoul() with kstrtoul() in cachefiles_daemon_bstop(). The kstrtoul() function provides strict overflow checking and is the recommended replacement.

---
 fs/cachefiles/daemon.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
fs/cachefiles/daemon.c +7 -2
index 7cb1a2b19..0afe4288e 100644
@@ -551,14 +551,19 @@ static int cachefiles_daemon_bcull(struct cachefiles_cache *cache, char *args)
 static int cachefiles_daemon_bstop(struct cachefiles_cache *cache, char *args)
 {
 	unsigned long bstop;
+	int ret;
 
 	_enter(",%s", args);
 
 	if (!*args)
 		return -EINVAL;
 
-	bstop = simple_strtoul(args, &args, 10);
-	if (args[0] != '%' || args[1] != '\0')
+	ret = kstrtoul(args, 10, &bstop);
+	if (ret < 0)
+		return ret;
+
+	args = strchr(args, '%');
+	if (!args || args[1] != '\0')
 		return -EINVAL;
 
 	if (bstop >= cache->bcull_percent)
Signature · 1 line
2.37.1 (Apple Git-137.1)
author
#6 lore raw

PATCH 6/6 fs/cachefiles: replace simple_strtoul with kstrtoul in debug

In reply to Akhmad

From: Akhmad Zubairugadzhiev <[email protected]>

Replace deprecated simple_strtoul() with kstrtoul() in cachefiles_daemon_debug(). The kstrtoul() function provides strict overflow checking and is the recommended replacement. Removed unnecessary manual validation as kstrtoul already validates the entire input string.

---
 fs/cachefiles/daemon.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
fs/cachefiles/daemon.c +4 -7
index 0afe4288e..f971fbfa9 100644
@@ -710,20 +710,17 @@ static int cachefiles_daemon_cull(struct cachefiles_cache *cache, char *args)
 static int cachefiles_daemon_debug(struct cachefiles_cache *cache, char *args)
 {
 	unsigned long mask;
+	int ret;
 
 	_enter(",%s", args);
 
-	mask = simple_strtoul(args, &args, 0);
-	if (args[0] != '\0')
-		goto inval;
+	ret = kstrtoul(args, 0, &mask);
+	if (ret < 0)
+		return ret;
 
 	cachefiles_debug = mask;
 	_leave(" = 0");
 	return 0;
-
-inval:
-	pr_err("debug command requires mask\n");
-	return -EINVAL;
 }
 
 /*
Signature · 1 line
2.37.1 (Apple Git-137.1)
#7 lore raw
In reply to Akhmad

fs/cachefiles/daemon.c static int cachefiles_daemon_range_error(struct cachefiles_cache *cache, · line 408

Quoted 3 lines · Akhmad

{

fs/cachefiles/daemon.c +1 -1
-	unsigned long frun;
+        unsigned long frun;

You've changed all the tabs to spaces. Please don't do this.