From: Neil Horman <nhor...@tuxdriver.com> Date: Thu, 18 Feb 2016 10:02:04 -0500
> Dmitry Vyukov noted recently that the sctp_port_hashtable had an error in > its size computation, observing that the current method never guaranteed > that the hashsize (measured in number of entries) would be a power of two, > which the input hash function for that table requires. The root cause of > the problem is that two values need to be computed (one, the allocation > order of the storage requries, as passed to __get_free_pages, and two the > number of entries for the hash table). Both need to be ^2, but for > different reasons, and the existing code is simply computing one order > value, and using it as the basis for both, which is wrong (i.e. it assumes > that ((1<<order)*PAGE_SIZE)/sizeof(bucket) is still ^2 when its not). > > To fix this, we change the logic slightly. We start by computing a goal > allocation order (which is limited by the maximum size hash table we want > to support. Then we attempt to allocate that size table, decreasing the > order until a successful allocation is made. Then, with the resultant > successful order we compute the number of buckets that hash table supports, > which we then round down to the nearest power of two, giving us the number > of entries the table actually supports. > > I've tested this locally here, using non-debug and spinlock-debug kernels, > and the number of entries in the hashtable consistently work out to be > powers of two in all cases. > > Signed-off-by: Neil Horman <nhor...@tuxdriver.com> > Reported-by: Dmitry Vyukov <dvyu...@google.com> This needs some work: In file included from include/linux/list.h:8:0, from include/linux/module.h:9, from net/sctp/protocol.c:44: net/sctp/protocol.c: In function ‘sctp_init’: include/linux/kernel.h:752:17: warning: comparison of distinct pointer types lacks a cast (void) (&_min1 == &_min2); \ ^ net/sctp/protocol.c:1431:10: note: in expansion of macro ‘min’ order = min(order, max_entry_order); ^ In file included from include/linux/printk.h:6:0, from include/linux/kernel.h:13, from include/linux/list.h:8, from include/linux/module.h:9, from net/sctp/protocol.c:44: include/linux/kern_levels.h:4:18: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘long unsigned int’ [-Wformat=] #define KERN_SOH "\001" /* ASCII Start Of Header */ ^ include/linux/kern_levels.h:13:19: note: in expansion of macro ‘KERN_SOH’ #define KERN_INFO KERN_SOH "6" /* informational */ ^ include/linux/printk.h:259:9: note: in expansion of macro ‘KERN_INFO’ printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__) ^ net/sctp/protocol.c:1484:2: note: in expansion of macro ‘pr_info’ pr_info("Hash tables configured (bind %d/%d)\n", sctp_port_hashsize, ^