public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] 9p: Use hashtable.h for hash_errmap
@ 2025-03-20 14:52 Sasha Levin
  2025-03-22 21:26 ` asmadeus
  0 siblings, 1 reply; 3+ messages in thread
From: Sasha Levin @ 2025-03-20 14:52 UTC (permalink / raw)
  To: ericvh, lucho, asmadeus; +Cc: linux_oss, v9fs, linux-kernel, Sasha Levin

Convert hash_errmap in error.c to use the generic hashtable
implementation from hashtable.h instead of the manual hlist_head array
implementation.

This simplifies the code and makes it more maintainable by using the
standard hashtable API and removes the need for manual hash table
management.

Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 net/9p/error.c | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)

diff --git a/net/9p/error.c b/net/9p/error.c
index 8da744494b683..8ba8afc91482d 100644
--- a/net/9p/error.c
+++ b/net/9p/error.c
@@ -16,6 +16,7 @@
 #include <linux/list.h>
 #include <linux/jhash.h>
 #include <linux/errno.h>
+#include <linux/hashtable.h>
 #include <net/9p/9p.h>
 
 /**
@@ -33,8 +34,8 @@ struct errormap {
 	struct hlist_node list;
 };
 
-#define ERRHASHSZ		32
-static struct hlist_head hash_errmap[ERRHASHSZ];
+#define ERRHASH_BITS 5
+static DEFINE_HASHTABLE(hash_errmap, ERRHASH_BITS);
 
 /* FixMe - reduce to a reasonable size */
 static struct errormap errmap[] = {
@@ -176,18 +177,14 @@ static struct errormap errmap[] = {
 int p9_error_init(void)
 {
 	struct errormap *c;
-	int bucket;
-
-	/* initialize hash table */
-	for (bucket = 0; bucket < ERRHASHSZ; bucket++)
-		INIT_HLIST_HEAD(&hash_errmap[bucket]);
+	u32 hash;
 
 	/* load initial error map into hash table */
 	for (c = errmap; c->name; c++) {
 		c->namelen = strlen(c->name);
-		bucket = jhash(c->name, c->namelen, 0) % ERRHASHSZ;
+		hash = jhash(c->name, c->namelen, 0);
 		INIT_HLIST_NODE(&c->list);
-		hlist_add_head(&c->list, &hash_errmap[bucket]);
+		hash_add(hash_errmap, &c->list, hash);
 	}
 
 	return 1;
@@ -205,12 +202,12 @@ int p9_errstr2errno(char *errstr, int len)
 {
 	int errno;
 	struct errormap *c;
-	int bucket;
+	u32 hash;
 
 	errno = 0;
 	c = NULL;
-	bucket = jhash(errstr, len, 0) % ERRHASHSZ;
-	hlist_for_each_entry(c, &hash_errmap[bucket], list) {
+	hash = jhash(errstr, len, 0);
+	hash_for_each_possible(hash_errmap, c, list, hash) {
 		if (c->namelen == len && !memcmp(c->name, errstr, len)) {
 			errno = c->val;
 			break;
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] 9p: Use hashtable.h for hash_errmap
  2025-03-20 14:52 [PATCH] 9p: Use hashtable.h for hash_errmap Sasha Levin
@ 2025-03-22 21:26 ` asmadeus
  2025-03-23 13:05   ` Christian Schoenebeck
  0 siblings, 1 reply; 3+ messages in thread
From: asmadeus @ 2025-03-22 21:26 UTC (permalink / raw)
  To: Sasha Levin; +Cc: ericvh, lucho, linux_oss, v9fs, linux-kernel

Sasha Levin wrote on Thu, Mar 20, 2025 at 10:52:00AM -0400:
> Convert hash_errmap in error.c to use the generic hashtable
> implementation from hashtable.h instead of the manual hlist_head array
> implementation.
> 
> This simplifies the code and makes it more maintainable by using the
> standard hashtable API and removes the need for manual hash table
> management.

I'm not entierly convinced this is simpler from my little island (that's
one more API to learn about vs code that doesn't look all that
different), but I guess I can see the point from an overall perspective.

I see no problem with this, I'll pick it up for -next
Note this code is not used for 9p2000l iirc so I currently don't have
any test for it :/
(but it's simple enough that I'm willing to take the risk)

Thanks,
Dominique
> 
> Signed-off-by: Sasha Levin <sashal@kernel.org>
> ---
>  net/9p/error.c | 21 +++++++++------------
>  1 file changed, 9 insertions(+), 12 deletions(-)
> 
> diff --git a/net/9p/error.c b/net/9p/error.c
> index 8da744494b683..8ba8afc91482d 100644
> --- a/net/9p/error.c
> +++ b/net/9p/error.c
> @@ -16,6 +16,7 @@
>  #include <linux/list.h>
>  #include <linux/jhash.h>
>  #include <linux/errno.h>
> +#include <linux/hashtable.h>
>  #include <net/9p/9p.h>
>  
>  /**
> @@ -33,8 +34,8 @@ struct errormap {
>  	struct hlist_node list;
>  };
>  
> -#define ERRHASHSZ		32
> -static struct hlist_head hash_errmap[ERRHASHSZ];
> +#define ERRHASH_BITS 5
> +static DEFINE_HASHTABLE(hash_errmap, ERRHASH_BITS);
>  
>  /* FixMe - reduce to a reasonable size */
>  static struct errormap errmap[] = {
> @@ -176,18 +177,14 @@ static struct errormap errmap[] = {
>  int p9_error_init(void)
>  {
>  	struct errormap *c;
> -	int bucket;
> -
> -	/* initialize hash table */
> -	for (bucket = 0; bucket < ERRHASHSZ; bucket++)
> -		INIT_HLIST_HEAD(&hash_errmap[bucket]);
> +	u32 hash;
>  
>  	/* load initial error map into hash table */
>  	for (c = errmap; c->name; c++) {
>  		c->namelen = strlen(c->name);
> -		bucket = jhash(c->name, c->namelen, 0) % ERRHASHSZ;
> +		hash = jhash(c->name, c->namelen, 0);
>  		INIT_HLIST_NODE(&c->list);
> -		hlist_add_head(&c->list, &hash_errmap[bucket]);
> +		hash_add(hash_errmap, &c->list, hash);
>  	}
>  
>  	return 1;
> @@ -205,12 +202,12 @@ int p9_errstr2errno(char *errstr, int len)
>  {
>  	int errno;
>  	struct errormap *c;
> -	int bucket;
> +	u32 hash;
>  
>  	errno = 0;
>  	c = NULL;
> -	bucket = jhash(errstr, len, 0) % ERRHASHSZ;
> -	hlist_for_each_entry(c, &hash_errmap[bucket], list) {
> +	hash = jhash(errstr, len, 0);
> +	hash_for_each_possible(hash_errmap, c, list, hash) {
>  		if (c->namelen == len && !memcmp(c->name, errstr, len)) {
>  			errno = c->val;
>  			break;

-- 
Dominique Martinet | Asmadeus

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] 9p: Use hashtable.h for hash_errmap
  2025-03-22 21:26 ` asmadeus
@ 2025-03-23 13:05   ` Christian Schoenebeck
  0 siblings, 0 replies; 3+ messages in thread
From: Christian Schoenebeck @ 2025-03-23 13:05 UTC (permalink / raw)
  To: Sasha Levin, asmadeus; +Cc: ericvh, lucho, v9fs, linux-kernel

On Saturday, March 22, 2025 10:26:20 PM CET asmadeus@codewreck.org wrote:
> Sasha Levin wrote on Thu, Mar 20, 2025 at 10:52:00AM -0400:
> > Convert hash_errmap in error.c to use the generic hashtable
> > implementation from hashtable.h instead of the manual hlist_head array
> > implementation.
> > 
> > This simplifies the code and makes it more maintainable by using the
> > standard hashtable API and removes the need for manual hash table
> > management.
> 
> I'm not entierly convinced this is simpler from my little island (that's
> one more API to learn about vs code that doesn't look all that
> different), but I guess I can see the point from an overall perspective.
> 
> I see no problem with this, I'll pick it up for -next
> Note this code is not used for 9p2000l iirc so I currently don't have
> any test for it :/
> (but it's simple enough that I'm willing to take the risk)

Correct, that code is only used for legacy 9p2000 and 9p2000.u protocol
versions, but not for the default 9p2000.L version which is transmitting
numeric (Linux) error codes only (i.e. error strings are never transmitted
with 9p2000.L, hence no translation via hash map needed).

/Christian



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-03-23 13:05 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-20 14:52 [PATCH] 9p: Use hashtable.h for hash_errmap Sasha Levin
2025-03-22 21:26 ` asmadeus
2025-03-23 13:05   ` Christian Schoenebeck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox