2002-12-06 09:27:59

by Miles Bader

[permalink] [raw]
Subject: [PATCH] Make `hash_long' function work if bits parameter is 0.

If the bits parameter of hash_long (in <linux/hash.h>) is 0, then it
ends up right-shifting by BITS_PER_LONG, which is undefined in C (and
often is a nop).

This patch just handles that case explicitly.

diff -ruN -X../cludes ../orig/linux-2.5.50-uc0/include/linux/hash.h include/linux/hash.h
--- ../orig/linux-2.5.50-uc0/include/linux/hash.h 2002-09-18 09:59:18.000000000 +0900
+++ include/linux/hash.h 2002-12-06 13:22:00.000000000 +0900
@@ -27,6 +27,9 @@
{
unsigned long hash = val;

+ if (bits == 0)
+ return 0;
+
#if BITS_PER_LONG == 64
/* Sigh, gcc can't optimise this alone like it does for 32 bits. */
unsigned long n = hash;


2002-12-06 16:28:46

by Linus Torvalds

[permalink] [raw]
Subject: Re: [PATCH] Make `hash_long' function work if bits parameter is 0.




On Fri, 6 Dec 2002, Miles Bader wrote:
>
> If the bits parameter of hash_long (in <linux/hash.h>) is 0, then it
> ends up right-shifting by BITS_PER_LONG, which is undefined in C (and
> often is a nop).

I would much rather just add a comment saying that "bits" had better be in
a valid range. There are no valid uses for a 0-bit hash table that I can
see, and undefined behaviour for undefined operations is fine with me.

Linus

2002-12-06 17:50:44

by Miles Bader

[permalink] [raw]
Subject: Re: [PATCH] Make `hash_long' function work if bits parameter is 0.

On Fri, Dec 06, 2002 at 08:37:26AM -0800, Linus Torvalds wrote:
> > If the bits parameter of hash_long (in <linux/hash.h>) is 0, then it
> > ends up right-shifting by BITS_PER_LONG, which is undefined in C (and
> > often is a nop).
>
> I would much rather just add a comment saying that "bits" had better be in
> a valid range. There are no valid uses for a 0-bit hash table that I can
> see, and undefined behaviour for undefined operations is fine with me.

The reason I sent the patch is because I ran into a case where the return
value _should_ be zero -- on a machine with very little memory (1MB), the
page wait-queue hash-table ends up having only one bucket (it has 256 pages,
and the code tries to make a wait-queue for every 256 pages....). The 0 is
returned by the `wait_table_bits' function in mm/page_alloc.c.

I suppose an alternative in this case is to special-case above calculation to
peg the minimum at 1.

-Miles
--
`...the Soviet Union was sliding in to an economic collapse so comprehensive
that in the end its factories produced not goods but bads: finished products
less valuable than the raw materials they were made from.' [The Economist]

2002-12-06 18:08:16

by Linus Torvalds

[permalink] [raw]
Subject: Re: [PATCH] Make `hash_long' function work if bits parameter is 0.




On Fri, 6 Dec 2002, Miles Bader wrote:
>
> The reason I sent the patch is because I ran into a case where the return
> value _should_ be zero -- on a machine with very little memory (1MB), the
> page wait-queue hash-table ends up having only one bucket (it has 256 pages,
> and the code tries to make a wait-queue for every 256 pages....). The 0 is
> returned by the `wait_table_bits' function in mm/page_alloc.c.

Ahh, ok. Fair enough, but I do have to say that I consider a hash of size
1 to be kind of useless, so even then I think the right approach is:

> I suppose an alternative in this case is to special-case above
> calculation to peg the minimum at 1.

Yup. It needs a special case _somewhere_, and I suspect it's clearer to
just make the special case be where the issue is.

Linus

2002-12-08 17:15:41

by Rogier Wolff

[permalink] [raw]
Subject: Re: [PATCH] Make `hash_long' function work if bits parameter is 0.

On Fri, Dec 06, 2002 at 08:37:26AM -0800, Linus Torvalds wrote:
> On Fri, 6 Dec 2002, Miles Bader wrote:
> >
> > If the bits parameter of hash_long (in <linux/hash.h>) is 0, then it
> > ends up right-shifting by BITS_PER_LONG, which is undefined in C (and
> > often is a nop).
>
> I would much rather just add a comment saying that "bits" had better be in
> a valid range. There are no valid uses for a 0-bit hash table that I can
> see, and undefined behaviour for undefined operations is fine with me.

Wouldn't it be nice to have a memory-for-speed tradeoff by being able
to set the bits-in-the-hash-table to zero?

Roger.

--
** [email protected] ** http://www.BitWizard.nl/ ** +31-15-2600998 **
*-- BitWizard writes Linux device drivers for any device you may have! --*
* The Worlds Ecosystem is a stable system. Stable systems may experience *
* excursions from the stable situation. We are currently in such an *
* excursion: The stable situation does not include humans. ***************