Re: use hashfree on pcb hash tables

2018-03-28 Thread David Hill
round #2.

keep track of size (num of elements) in the inpcbtable struct.
passes regress tests.

OK?

Index: netinet/in_pcb.c
===
RCS file: /cvs/src/sys/netinet/in_pcb.c,v
retrieving revision 1.228
diff -u -p -r1.228 in_pcb.c
--- netinet/in_pcb.c19 Feb 2018 08:59:53 -  1.228
+++ netinet/in_pcb.c29 Mar 2018 02:21:34 -
@@ -206,6 +206,7 @@ in_pcbinit(struct inpcbtable *table, int
if (table->inpt_lhashtbl == NULL)
panic("in_pcbinit: hashinit failed for lport");
table->inpt_count = 0;
+   table->inpt_size = hashsize;
arc4random_buf(>inpt_key, sizeof(table->inpt_key));
 }
 
@@ -998,32 +999,34 @@ int
 in_pcbresize(struct inpcbtable *table, int hashsize)
 {
u_long nhash, nlhash;
+   int osize;
void *nhashtbl, *nlhashtbl, *ohashtbl, *olhashtbl;
struct inpcb *inp0, *inp1;
 
ohashtbl = table->inpt_hashtbl;
olhashtbl = table->inpt_lhashtbl;
+   osize = table->inpt_size;
 
nhashtbl = hashinit(hashsize, M_PCB, M_NOWAIT, );
+   if (nhashtbl == NULL)
+   return ENOBUFS;
nlhashtbl = hashinit(hashsize, M_PCB, M_NOWAIT, );
-   if (nhashtbl == NULL || nlhashtbl == NULL) {
-   if (nhashtbl != NULL)
-   free(nhashtbl, M_PCB, 0);
-   if (nlhashtbl != NULL)
-   free(nlhashtbl, M_PCB, 0);
-   return (ENOBUFS);
+   if (nlhashtbl == NULL) {
+   hashfree(nhashtbl, hashsize, M_PCB);
+   return ENOBUFS;
}
table->inpt_hashtbl = nhashtbl;
table->inpt_lhashtbl = nlhashtbl;
table->inpt_hash = nhash;
table->inpt_lhash = nlhash;
+   table->inpt_size = hashsize;
arc4random_buf(>inpt_key, sizeof(table->inpt_key));
 
TAILQ_FOREACH_SAFE(inp0, >inpt_queue, inp_queue, inp1) {
in_pcbrehash(inp0);
}
-   free(ohashtbl, M_PCB, 0);
-   free(olhashtbl, M_PCB, 0);
+   hashfree(ohashtbl, osize, M_PCB);
+   hashfree(olhashtbl, osize, M_PCB);
 
return (0);
 }
Index: netinet/in_pcb.h
===
RCS file: /cvs/src/sys/netinet/in_pcb.h,v
retrieving revision 1.106
diff -u -p -r1.106 in_pcb.h
--- netinet/in_pcb.h1 Dec 2017 10:33:33 -   1.106
+++ netinet/in_pcb.h29 Mar 2018 02:21:34 -
@@ -152,7 +152,7 @@ struct inpcbtable {
struct inpcbhead *inpt_hashtbl, *inpt_lhashtbl;
SIPHASH_KEY inpt_key;
u_longinpt_hash, inpt_lhash;
-   int   inpt_count;
+   int   inpt_count, inpt_size;
 };
 
 /* flags in inp_flags: */



Re: use hashfree on pcb hash tables

2018-03-27 Thread David Hill
Ignore for now.

A lovely regress/ test exposes a problem in my diff. :)

On Tue, Mar 27, 2018 at 01:11:19PM -0400, David Hill wrote:
> Hello -
> 
> The hash tables are allocated with hashinit, so free them with hashfree.
> This gives the bonus of calling free() with a size as well.
> 



use hashfree on pcb hash tables

2018-03-27 Thread David Hill
Hello -

The hash tables are allocated with hashinit, so free them with hashfree.
This gives the bonus of calling free() with a size as well.

OK?

Index: in_pcb.c
===
RCS file: /cvs/src/sys/netinet/in_pcb.c,v
retrieving revision 1.228
diff -u -p -r1.228 in_pcb.c
--- in_pcb.c19 Feb 2018 08:59:53 -  1.228
+++ in_pcb.c27 Mar 2018 17:06:51 -
@@ -997,20 +997,21 @@ in_pcbrehash(struct inpcb *inp)
 int
 in_pcbresize(struct inpcbtable *table, int hashsize)
 {
-   u_long nhash, nlhash;
+   u_long nhash, nlhash, ohash, olhash;
void *nhashtbl, *nlhashtbl, *ohashtbl, *olhashtbl;
struct inpcb *inp0, *inp1;
 
ohashtbl = table->inpt_hashtbl;
+   ohash = table->inpt_hash;
olhashtbl = table->inpt_lhashtbl;
+   olhash = table->inpt_lhash;
 
nhashtbl = hashinit(hashsize, M_PCB, M_NOWAIT, );
+   if (nhashtbl == NULL)
+   return (ENOBUFS);
nlhashtbl = hashinit(hashsize, M_PCB, M_NOWAIT, );
-   if (nhashtbl == NULL || nlhashtbl == NULL) {
-   if (nhashtbl != NULL)
-   free(nhashtbl, M_PCB, 0);
-   if (nlhashtbl != NULL)
-   free(nlhashtbl, M_PCB, 0);
+   if (nlhashtbl == NULL) {
+   hashfree(nhashtbl, nhash, M_PCB);
return (ENOBUFS);
}
table->inpt_hashtbl = nhashtbl;
@@ -1022,8 +1023,8 @@ in_pcbresize(struct inpcbtable *table, i
TAILQ_FOREACH_SAFE(inp0, >inpt_queue, inp_queue, inp1) {
in_pcbrehash(inp0);
}
-   free(ohashtbl, M_PCB, 0);
-   free(olhashtbl, M_PCB, 0);
+   hashfree(ohashtbl, ohash, M_PCB);
+   hashfree(olhashtbl, olhash, M_PCB);
 
return (0);
 }