linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 01/23] slab: make kmalloc_index() return "unsigned int"
@ 2017-11-23 22:16 Alexey Dobriyan
  2017-11-23 22:16 ` [PATCH 02/23] slab: make kmalloc_size() " Alexey Dobriyan
                   ` (22 more replies)
  0 siblings, 23 replies; 31+ messages in thread
From: Alexey Dobriyan @ 2017-11-23 22:16 UTC (permalink / raw)
  To: akpm; +Cc: linux-mm, cl, penberg, rientjes, iamjoonsoo.kim, Alexey Dobriyan

kmalloc_index() return index into an array of kmalloc kmem caches,
therefore should unsigned.

Space savings:

	add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-6 (-6)
	Function                                     old     new   delta
	rtsx_scsi_handler                           9116    9114      -2
	vnic_rq_alloc                                424     420      -4

This patch start a series of converting SLUB (mostly) to "unsigned int".
1) Most integers in the code are in fact unsigned entities: array indexes,
   lengths, buffer sizes, allocation orders. It is therefore better to use
   unsigned variables

2) Some integers in the code are either "size_t" or "unsigned long" for no
   reason.
 
   size_t usually comes from people trying to "maintain" type correctness
   and figuring out that "sizeof" operator returns size_t or that
   memset/memcpy    takes size_t so should everything you pass to it.

   However the number of 4GB+ objects in the kernel is very small.
   Most, if not all, dynamically allocated objects with kmalloc() or
   kmem_cache_create() aren't actually big. Maintaining wide types
   doesn't do anything.

   64-bit ops are bigger than 32-bit on our beloved x86_64,
   so try to not use 64-bit where it isn't necessary
   (read: everywhere where integers are integers not pointers)

3) in case of SLAB allocators, there are additional limitations
   *) page->inuse, page->objects are only 16-/15-bit,
   *) cache size was always 32-bit
   *) slab orders are small, order 20 is needed to go 64-bit on x86_64
      (PAGE_SIZE << order)

Basically everything is 32-bit except kmalloc(1ULL<<32) which gets shortcut
through page allocator.

Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
---
 include/linux/slab.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/include/linux/slab.h b/include/linux/slab.h
index 50697a1d6621..e765800d7c9b 100644
--- a/include/linux/slab.h
+++ b/include/linux/slab.h
@@ -295,7 +295,7 @@ extern struct kmem_cache *kmalloc_dma_caches[KMALLOC_SHIFT_HIGH + 1];
  * 2 = 129 .. 192 bytes
  * n = 2^(n-1)+1 .. 2^n
  */
-static __always_inline int kmalloc_index(size_t size)
+static __always_inline unsigned int kmalloc_index(size_t size)
 {
 	if (!size)
 		return 0;
@@ -491,7 +491,7 @@ static __always_inline void *kmalloc(size_t size, gfp_t flags)
 			return kmalloc_large(size, flags);
 #ifndef CONFIG_SLOB
 		if (!(flags & GFP_DMA)) {
-			int index = kmalloc_index(size);
+			unsigned int index = kmalloc_index(size);
 
 			if (!index)
 				return ZERO_SIZE_PTR;
@@ -529,7 +529,7 @@ static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node)
 #ifndef CONFIG_SLOB
 	if (__builtin_constant_p(size) &&
 		size <= KMALLOC_MAX_CACHE_SIZE && !(flags & GFP_DMA)) {
-		int i = kmalloc_index(size);
+		unsigned int i = kmalloc_index(size);
 
 		if (!i)
 			return ZERO_SIZE_PTR;
-- 
2.13.6

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

end of thread, other threads:[~2017-11-28 18:47 UTC | newest]

Thread overview: 31+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-23 22:16 [PATCH 01/23] slab: make kmalloc_index() return "unsigned int" Alexey Dobriyan
2017-11-23 22:16 ` [PATCH 02/23] slab: make kmalloc_size() " Alexey Dobriyan
2017-11-23 22:16 ` [PATCH 03/23] slab: create_kmalloc_cache() works with 32-bit sizes Alexey Dobriyan
2017-11-24  1:06   ` Matthew Wilcox
2017-11-27 10:21     ` Alexey Dobriyan
2017-11-27 13:56       ` Matthew Wilcox
2017-11-23 22:16 ` [PATCH 04/23] slab: create_boot_cache() only " Alexey Dobriyan
2017-11-23 22:16 ` [PATCH 05/23] slab: kmem_cache_create() " Alexey Dobriyan
2017-11-23 22:16 ` [PATCH 06/23] slab: make size_index[] array u8 Alexey Dobriyan
2017-11-23 22:16 ` [PATCH 07/23] slab: make size_index_elem() unsigned int Alexey Dobriyan
2017-11-23 22:16 ` [PATCH 08/23] slub: make ->remote_node_defrag_ratio " Alexey Dobriyan
2017-11-23 22:16 ` [PATCH 09/23] slub: make ->max_attr_size " Alexey Dobriyan
2017-11-23 22:16 ` [PATCH 10/23] slub: make ->red_left_pad " Alexey Dobriyan
2017-11-23 22:16 ` [PATCH 11/23] slub: make ->reserved " Alexey Dobriyan
2017-11-23 22:16 ` [PATCH 12/23] slub: make ->align " Alexey Dobriyan
2017-11-23 22:16 ` [PATCH 13/23] slub: make ->inuse " Alexey Dobriyan
2017-11-23 22:16 ` [PATCH 14/23] slub: make ->cpu_partial " Alexey Dobriyan
2017-11-23 22:16 ` [PATCH 15/23] slub: make ->offset " Alexey Dobriyan
2017-11-23 22:16 ` [PATCH 16/23] slub: make ->object_size " Alexey Dobriyan
2017-11-23 22:16 ` [PATCH 17/23] slub: make ->size " Alexey Dobriyan
2017-11-23 22:16 ` [PATCH 18/23] slab: make kmem_cache_flags accept 32-bit object size Alexey Dobriyan
2017-11-23 22:16 ` [PATCH 19/23] kasan: make kasan_cache_create() work with 32-bit slab caches Alexey Dobriyan
2017-11-24 22:29   ` kbuild test robot
2017-11-23 22:16 ` [PATCH 20/23] slub: make slab_index() return unsigned int Alexey Dobriyan
2017-11-23 22:16 ` [PATCH 21/23] slub: make struct kmem_cache_order_objects::x " Alexey Dobriyan
2017-11-24 17:31   ` Christopher Lameter
2017-11-27 10:17     ` Alexey Dobriyan
2017-11-23 22:16 ` [PATCH 22/23] slub: make size_from_object() return " Alexey Dobriyan
2017-11-23 22:16 ` [PATCH 23/23] slab: use 32-bit arithmetic in freelist_randomize() Alexey Dobriyan
2017-11-28  0:36 ` [PATCH 01/23] slab: make kmalloc_index() return "unsigned int" Andrew Morton
2017-11-28 18:46   ` Christopher Lameter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).