Skip to contentSkip to conversation

arm64: Increase the max granular size

PATCH Applied Robert Richter 11 participants 32 messages Sep 22, 2015 – Nov 10, 2015

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm64: Increase the max granular size

From: Tirumalesh Chalamarla <[email protected]>

Increase the standard cacheline size to avoid having locks in the same cacheline.

Cavium's ThunderX core implements cache lines of 128 byte size. With current granulare size of 64 bytes (L1_CACHE_SHIFT=6) two locks could share the same cache line leading a performance degradation. Increasing the size fixes that.

Increasing the size has no negative impact to cache invalidation on systems with a smaller cache line. There is an impact on memory usage, but that's not too important for arm64 use cases.

---
 arch/arm64/include/asm/cache.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/include/asm/cache.h +1 -1
index bde449936e2f..5082b30bc2c0 100644
@@ -18,7 +18,7 @@
 
 #include <asm/cachetype.h>
 
-#define L1_CACHE_SHIFT		6
+#define L1_CACHE_SHIFT		7
 #define L1_CACHE_BYTES		(1 << L1_CACHE_SHIFT)
 
 /*
Signature · 1 line
2.1.1
#1 lore raw
Quoted 9 lines · Robert Richter

From: Tirumalesh Chalamarla <[email protected]>

Increase the standard cacheline size to avoid having locks in the same cacheline.

Cavium's ThunderX core implements cache lines of 128 byte size. With current granulare size of 64 bytes (L1_CACHE_SHIFT=6) two locks could share the same cache line leading a performance degradation. Increasing the size fixes that.

Do you have an example of that happening?

Quoted 3 lines

Increasing the size has no negative impact to cache invalidation on systems with a smaller cache line. There is an impact on memory usage, but that's not too important for arm64 use cases.

Do you have any before/after numbers to show the impact of this change on other supported SoCs?

Will

#2 lore raw

Will,

Quoted 6 lines · Will Deacon
Quoted 5 lines · Robert Richter

From: Tirumalesh Chalamarla <[email protected]>

Increase the standard cacheline size to avoid having locks in the same cacheline.

Quoted 6 lines
Quoted 4 lines

Cavium's ThunderX core implements cache lines of 128 byte size. With current granulare size of 64 bytes (L1_CACHE_SHIFT=6) two locks could share the same cache line leading a performance degradation. Increasing the size fixes that.

Do you have an example of that happening?

I did some 'poor man's kernel build all modules benchmarking' and could not find significant performance improvements so far (second part with the patch reverted):

 build-allmodules-4.2.0-01404-g5818d6e89783.log:real       7m10.490s
 build-allmodules-4.2.0-01404-g5818d6e89783.log:real       6m59.747s
 build-allmodules-4.2.0-01404-g5818d6e89783.log:real       6m59.264s
 build-allmodules-4.2.0-01404-g5818d6e89783.log:real       7m0.435s
 build-allmodules-4.2.0-01404-g5818d6e89783.log:real       6m59.569s
 build-allmodules-4.2.0-01404-g5818d6e89783.log:real       6m59.274s
 build-allmodules-4.2.0-01404-g5818d6e89783.log:real       7m0.507s
 build-allmodules-4.2.0-01404-g5818d6e89783.log:real       7m1.551s
 build-allmodules-4.2.0-01404-g5818d6e89783.log:real       6m59.073s
 build-allmodules-4.2.0-01404-g5818d6e89783.log:real       7m1.738s
 build-allmodules-4.2.0-01406-g638c69fddc40.log:real       7m10.644s
 build-allmodules-4.2.0-01406-g638c69fddc40.log:real       6m59.814s
 build-allmodules-4.2.0-01406-g638c69fddc40.log:real       7m0.315s
 build-allmodules-4.2.0-01406-g638c69fddc40.log:real       6m59.610s
 build-allmodules-4.2.0-01406-g638c69fddc40.log:real       6m59.885s
 build-allmodules-4.2.0-01406-g638c69fddc40.log:real       6m59.281s
 build-allmodules-4.2.0-01406-g638c69fddc40.log:real       7m0.869s
 build-allmodules-4.2.0-01406-g638c69fddc40.log:real       7m0.953s
 build-allmodules-4.2.0-01406-g638c69fddc40.log:real       7m0.787s
 build-allmodules-4.2.0-01406-g638c69fddc40.log:real       7m0.656s

I will check what kind of workloads this patch was written for. Tirumalesh, any idea?

Thanks,

-Robert

Quoted 7 lines
Quoted 4 lines

Increasing the size has no negative impact to cache invalidation on systems with a smaller cache line. There is an impact on memory usage, but that's not too important for arm64 use cases.

Do you have any before/after numbers to show the impact of this change on other supported SoCs?

#3 lore raw
Quoted 40 lines · Robert Richter

Will,

Quoted 11 lines · Will Deacon
Quoted 9 lines · Robert Richter

From: Tirumalesh Chalamarla <[email protected]>

Increase the standard cacheline size to avoid having locks in the same cacheline.

Cavium's ThunderX core implements cache lines of 128 byte size. With current granulare size of 64 bytes (L1_CACHE_SHIFT=6) two locks could share the same cache line leading a performance degradation. Increasing the size fixes that.

Do you have an example of that happening?

I did some 'poor man's kernel build all modules benchmarking' and could not find significant performance improvements so far (second part with the patch reverted):

  build-allmodules-4.2.0-01404-g5818d6e89783.log:real       7m10.490s
  build-allmodules-4.2.0-01404-g5818d6e89783.log:real       6m59.747s
  build-allmodules-4.2.0-01404-g5818d6e89783.log:real       6m59.264s
  build-allmodules-4.2.0-01404-g5818d6e89783.log:real       7m0.435s
  build-allmodules-4.2.0-01404-g5818d6e89783.log:real       6m59.569s
  build-allmodules-4.2.0-01404-g5818d6e89783.log:real       6m59.274s
  build-allmodules-4.2.0-01404-g5818d6e89783.log:real       7m0.507s
  build-allmodules-4.2.0-01404-g5818d6e89783.log:real       7m1.551s
  build-allmodules-4.2.0-01404-g5818d6e89783.log:real       6m59.073s
  build-allmodules-4.2.0-01404-g5818d6e89783.log:real       7m1.738s
  build-allmodules-4.2.0-01406-g638c69fddc40.log:real       7m10.644s
  build-allmodules-4.2.0-01406-g638c69fddc40.log:real       6m59.814s
  build-allmodules-4.2.0-01406-g638c69fddc40.log:real       7m0.315s
  build-allmodules-4.2.0-01406-g638c69fddc40.log:real       6m59.610s
  build-allmodules-4.2.0-01406-g638c69fddc40.log:real       6m59.885s
  build-allmodules-4.2.0-01406-g638c69fddc40.log:real       6m59.281s
  build-allmodules-4.2.0-01406-g638c69fddc40.log:real       7m0.869s
  build-allmodules-4.2.0-01406-g638c69fddc40.log:real       7m0.953s
  build-allmodules-4.2.0-01406-g638c69fddc40.log:real       7m0.787s
  build-allmodules-4.2.0-01406-g638c69fddc40.log:real       7m0.656s
Quoted 2 lines

I will check what kind of workloads this patch was written for. Tirumalesh, any idea?

mainly for workloads where compiler optimizes based on cache line size, let me write a small bench mark

Quoted 9 lines

Thanks,

-Robert

Quoted 5 lines
Quoted 3 lines

Increasing the size has no negative impact to cache invalidation on systems with a smaller cache line. There is an impact on memory usage, but that's not too important for arm64 use cases.

Do you have any before/after numbers to show the impact of this change on other supported SoCs?

#4 lore raw
In reply to Robert Richter

arch/arm64/include/asm/cache.h line 22

Quoted 4 lines · Robert Richter
arch/arm64/include/asm/cache.h +1 -1
-#define L1_CACHE_SHIFT         6
+#define L1_CACHE_SHIFT         7
 #define L1_CACHE_BYTES         (1 << L1_CACHE_SHIFT)

Would it be better if this were a Kconfig option, like it is on ARM32?

http://lxr.free-electrons.com/source/arch/arm/include/asm/cache.h#L7

Signature · 3 lines
Qualcomm Innovation Center, Inc.
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project.
#5 lore raw
Quoted 9 lines · Timur Tabi

arch/arm64/include/asm/cache.h line 22

Quoted 4 lines · Robert Richter
arch/arm64/include/asm/cache.h +1 -1
-#define L1_CACHE_SHIFT         6
+#define L1_CACHE_SHIFT         7
 #define L1_CACHE_BYTES         (1 << L1_CACHE_SHIFT)

Would it be better if this were a Kconfig option, like it is on ARM32?

http://lxr.free-electrons.com/source/arch/arm/include/asm/cache.h#L7

I don't think it adds anything, to be honest. We really want one kernel that runs everywhere and we don't (yet) have the SoC variation that exists on arch/arm/, so we may as well just keep it as big as it needs to be. Of course, if we start to get significant divergence between the minimum and maximum value and that in turn shows a non-trivial impact on kernel size and/or performance, then we could consider a Kconfig option but at that point we'd probably also need to consider whether there are alternative ways of providing this information to the kernel.

If somebody really wants to change it for their particular kernel build, modifying the #define isn't exactly rocket science.

Will

Acked-by on v1 1/1
#6 lore raw
In reply to Robert Richter
Quoted 10 lines · Robert Richter

From: Tirumalesh Chalamarla <[email protected]>

Increase the standard cacheline size to avoid having locks in the same cacheline.

Cavium's ThunderX core implements cache lines of 128 byte size. With current granulare size of 64 bytes (L1_CACHE_SHIFT=6) two locks could share the same cache line leading a performance degradation. Increasing the size fixes that.

Quoted 6 lines

Increasing the size has no negative impact to cache invalidation on systems with a smaller cache line. There is an impact on memory usage, but that's not too important for arm64 use cases.

We need this patch, because on our silicon, CTR_EL0[CWG] set to 5, which means that setup_processor() complains with this warning:

cls = cache_line_size();

if (L1_CACHE_BYTES < cls)
pr_warn("L1_CACHE_BYTES smaller than the Cache Writeback Granule
(%d < %d)\n",

            L1_CACHE_BYTES, cls);
Signature · 3 lines
Qualcomm Innovation Center, Inc.
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project.
maintainer Applied
#7 lore raw
In reply to Robert Richter
Quoted 10 lines · Robert Richter

From: Tirumalesh Chalamarla <[email protected]>

Increase the standard cacheline size to avoid having locks in the same cacheline.

Cavium's ThunderX core implements cache lines of 128 byte size. With current granulare size of 64 bytes (L1_CACHE_SHIFT=6) two locks could share the same cache line leading a performance degradation. Increasing the size fixes that.

Quoted 6 lines

Increasing the size has no negative impact to cache invalidation on systems with a smaller cache line. There is an impact on memory usage, but that's not too important for arm64 use cases.

Applied. Thanks.

Signature · 1 line
Catalin
#8 lore raw
Quoted 11 lines · Catalin Marinas
Quoted 10 lines · Robert Richter

From: Tirumalesh Chalamarla <[email protected]>

Increase the standard cacheline size to avoid having locks in the same cacheline.

Cavium's ThunderX core implements cache lines of 128 byte size. With current granulare size of 64 bytes (L1_CACHE_SHIFT=6) two locks could share the same cache line leading a performance degradation. Increasing the size fixes that.

Quoted 8 lines
Quoted 6 lines

Increasing the size has no negative impact to cache invalidation on systems with a smaller cache line. There is an impact on memory usage, but that's not too important for arm64 use cases.

Applied. Thanks.

This patch causes a BUG() on r8a7795/salvator-x, for which support is not yet upstream.

My config (attached) uses SLAB. If I switch to SLUB, it works. The arm64 defconfig works, even if I switch from SLUB to SLAB.

Booting Linux on physical CPU 0x0
Linux version 4.3.0-salvator-x-08136-gce303d3c239f417e-dirty (geert@ramsan) (gcc version 4.9.0 (GCC) ) #264 SMP Tue Nov 3 11:40:59 CET 2015
Boot CPU: AArch64 Processor [411fd073]
bootconsole [uart0] enabled
debug: ignoring loglevel setting.
efi: Getting EFI parameters from FDT:
efi: UEFI not found.
On node 0 totalpages: 262144
DMA zone: 3584 pages used for memmap
DMA zone: 0 pages reserved
DMA zone: 262144 pages, LIFO batch:31
PERCPU: Embedded 16 pages/cpu @ffffffc03ffc4000 s27136 r8192 d30208 u65536
pcpu-alloc: s27136 r8192 d30208 u65536 alloc=16*4096
pcpu-alloc: [0] 0
Detected PIPT I-cache on CPU0
Built 1 zonelists in Zone order, mobility grouping on. Total pages: 258560 Kernel command line: earlycon ignore_loglevel ip=dhcp root=/dev/nfs rw nfsroot=192.168.97.21:/home/salvator-x/debian-arm64
PID hash table entries: 4096 (order: 3, 32768 bytes)
Dentry cache hash table entries: 131072 (order: 8, 1048576 bytes) Inode-cache hash table entries: 65536 (order: 7, 524288 bytes) software IO TLB [mem 0x7ae00000-0x7ee00000] (64MB) mapped at [ffffffc03ae00000-ffffffc03edfffff]
Memory: 946232K/1048576K available (4159K kernel code, 395K rwdat a, 1964K rodata, 244K init, 13383K bss, 102344K reserved, 0K cma-reserved) Virtual kernel memory layout:

    vmalloc : 0xffffff8000000000 - 0xffffffbdffff0000   (   247 GB)
    vmemmap : 0xffffffbe00000000 - 0xffffffbfc0000000   (     7 GB maximum)
              0xffffffbe00e00000 - 0xffffffbe01c00000   (    14 MB actual)
    fixed   : 0xffffffbffa7fd000 - 0xffffffbffac00000   (  4108 KB)
    PCI I/O : 0xffffffbffae00000 - 0xffffffbffbe00000   (    16 MB)
    modules : 0xffffffbffc000000 - 0xffffffc000000000   (    64 MB)
    memory  : 0xffffffc000000000 - 0xffffffc040000000   (  1024 MB)
      .init : 0xffffffc00067d000 - 0xffffffc0006ba000   (   244 KB)
      .text : 0xffffffc000080000 - 0xffffffc00067cdb4   (  6132 KB)
      .data : 0xffffffc0006c7000 - 0xffffffc000729e00   (   396 KB)
------------[ cut here ]------------
kernel BUG at mm/slab.c:2283!
Internal error: Oops - BUG: 0 [#1] SMP
Modules linked in:
CPU: 0 PID: 0 Comm: swapper Not tainted
4.3.0-salvator-x-08136-gce303d3c239f417e-dirty #264
Hardware name: Renesas Salvator-X board based on r8a7795 (DT)
task: ffffffc0006d22f0 ti: ffffffc0006c8000 task.ti: ffffffc0006c8000
PC is at __kmem_cache_create+0x21c/0x280
LR is at __kmem_cache_create+0x210/0x280
pc : [<ffffffc00014f9b4>] lr : [<ffffffc00014f9a8>] pstate: 800002c5

sp : ffffffc0006cbe50
x29: ffffffc0006cbe50 x28: 0000000000000000
x27: ffffffc000081198 x26: 000000004143f000
x25: 000000004143c000 x24: ffffffc03ffd6200
x23: 0000000080000000 x22: 0000000000000000
x21: 0000000080002000 x20: ffffffc03a800180
x19: 0000000000000020 x18: 000000007b73533d
x17: ffffffc00048c000 x16: 0000000000000000
x15: 0000000080000000 x14: 0000000000000081
x13: 0000000000001000 x12: 0000000000000001
x11: 0000000000000080 x10: ffffffc0006a7d58
x9 : 000000000000007f x8 : ffffffffffffff80
x7 : 0000000000000080 x6 : 0000000000001000
x5 : 0000000000000000 x4 : 0000000000000000
x3 : 0000000000000000 x2 : 0000000000000000
x1 : 0000000000000000 x0 : 0000000000000000

Process swapper (pid: 0, stack limit = 0xffffffc0006c8020)

Stack: (0xffffffc0006cbe50 to 0xffffffc0006cc000)

be40: ffffffc0006cbe90 ffffffc00068be50
be60: ffffffc03a800180 0000000000000080 ffffffc0005e90a4 0000000000002000
be80: ffffffc0013ed450 ffffffc0000d5890 ffffffc0006cbed0 ffffffc00068bed8
bea0: ffffffc03a800180 ffffffc0005e90a4 0000000000000080 ffffffc0005e90a4
bec0: 0000000000000080 0000000000002000 ffffffc0006cbf10 ffffffc00068bfc0
bee0: ffffffc0006ac170 ffffffc0013ed450 0000000000000000 ffffffc00014e8b4
bf00: ffffffc0006cbf30 0000000000002000 ffffffc0006cbf60 ffffffc00068db08
bf20: ffffffc0006a7c60 ffffffc0013ed000 ffffffc0006e3818 ffffffc0013ed450
bf40: ffffffc0013ed000 ffffffc03ffd6200 000000004143c000 ffffffc0013ed450
bf60: ffffffc0006cbfa0 ffffffc00067d7d8 ffffffc0006ce000 ffffffc00072d000
bf80: ffffffc00072d000 ffffffc0006ce000 ffffffc0006a6ed8 ffffffc00067d7d0
bfa0: 0000000000000000 0000000040488000 0000000000000400 0000000000000e11
bfc0: 0000000040079000 0000000000000003 000000007fe31b40 0000000040000000
bfe0: 0000000000000000 ffffffc0006a6ed8 0000000000000000 0000000000000000

Call trace:
[<ffffffc00014f9b4>] __kmem_cache_create+0x21c/0x280
[<ffffffc00068be50>] create_boot_cache+0x4c/0x80
[<ffffffc00068bed8>] create_kmalloc_cache+0x54/0x88
[<ffffffc00068bfc0>] create_kmalloc_caches+0x50/0xf4
[<ffffffc00068db08>] kmem_cache_init+0x104/0x118
[<ffffffc00067d7d8>] start_kernel+0x218/0x33c
[<0000000040488000>] 0x40488000
Code: 97ffa117 f9002280 f100401f 54000048 (d4210000)
---[ end trace cb88537fdc8fa200 ]---

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

maintainer
#9 lore raw
Quoted 27 lines · Geert Uytterhoeven
Quoted 11 lines · Catalin Marinas
Quoted 10 lines · Robert Richter

From: Tirumalesh Chalamarla <[email protected]>

Increase the standard cacheline size to avoid having locks in the same cacheline.

Cavium's ThunderX core implements cache lines of 128 byte size. With current granulare size of 64 bytes (L1_CACHE_SHIFT=6) two locks could share the same cache line leading a performance degradation. Increasing the size fixes that.

Quoted 8 lines
Quoted 6 lines

Increasing the size has no negative impact to cache invalidation on systems with a smaller cache line. There is an impact on memory usage, but that's not too important for arm64 use cases.

Applied. Thanks.

This patch causes a BUG() on r8a7795/salvator-x, for which support is not yet upstream.

My config (attached) uses SLAB. If I switch to SLUB, it works. The arm64 defconfig works, even if I switch from SLUB to SLAB.

[...]

Quoted 3 lines
------------[ cut here ]------------
kernel BUG at mm/slab.c:2283!
Internal error: Oops - BUG: 0 [#1] SMP

[...]

Quoted 7 lines
Call trace:
[<ffffffc00014f9b4>] __kmem_cache_create+0x21c/0x280
[<ffffffc00068be50>] create_boot_cache+0x4c/0x80
[<ffffffc00068bed8>] create_kmalloc_cache+0x54/0x88
[<ffffffc00068bfc0>] create_kmalloc_caches+0x50/0xf4
[<ffffffc00068db08>] kmem_cache_init+0x104/0x118
[<ffffffc00067d7d8>] start_kernel+0x218/0x33c

I haven't managed to reproduce this on a Juno kernel. Could you please add some debug printing to see what's being passed to kmalloc_slab()? The freelist_size getting out of bounds?

Signature · 1 line
Catalin
maintainer
#10 lore raw
Quoted 41 lines · Catalin Marinas
Quoted 27 lines · Geert Uytterhoeven
Quoted 11 lines · Catalin Marinas
Quoted 10 lines · Robert Richter

From: Tirumalesh Chalamarla <[email protected]>

Increase the standard cacheline size to avoid having locks in the same cacheline.

Cavium's ThunderX core implements cache lines of 128 byte size. With current granulare size of 64 bytes (L1_CACHE_SHIFT=6) two locks could share the same cache line leading a performance degradation. Increasing the size fixes that.

Quoted 8 lines
Quoted 6 lines

Increasing the size has no negative impact to cache invalidation on systems with a smaller cache line. There is an impact on memory usage, but that's not too important for arm64 use cases.

Applied. Thanks.

This patch causes a BUG() on r8a7795/salvator-x, for which support is not yet upstream.

My config (attached) uses SLAB. If I switch to SLUB, it works. The arm64 defconfig works, even if I switch from SLUB to SLAB.

[...]

Quoted 3 lines
------------[ cut here ]------------
kernel BUG at mm/slab.c:2283!
Internal error: Oops - BUG: 0 [#1] SMP

[...]

Quoted 7 lines
Call trace:
[<ffffffc00014f9b4>] __kmem_cache_create+0x21c/0x280
[<ffffffc00068be50>] create_boot_cache+0x4c/0x80
[<ffffffc00068bed8>] create_kmalloc_cache+0x54/0x88
[<ffffffc00068bfc0>] create_kmalloc_caches+0x50/0xf4
[<ffffffc00068db08>] kmem_cache_init+0x104/0x118
[<ffffffc00067d7d8>] start_kernel+0x218/0x33c
Quoted 1 line

I haven't managed to reproduce this on a Juno kernel.

I now managed to reproduce it with your config (slightly adapted to allow Juno). I'll look into it.

Signature · 1 line
Catalin
#11 lore raw

Hi Catalin,

Quoted 42 lines · Catalin Marinas
Quoted 41 lines · Catalin Marinas
Quoted 27 lines · Geert Uytterhoeven
Quoted 11 lines · Catalin Marinas
Quoted 10 lines · Robert Richter

From: Tirumalesh Chalamarla <[email protected]>

Increase the standard cacheline size to avoid having locks in the same cacheline.

Cavium's ThunderX core implements cache lines of 128 byte size. With current granulare size of 64 bytes (L1_CACHE_SHIFT=6) two locks could share the same cache line leading a performance degradation. Increasing the size fixes that.

Quoted 8 lines
Quoted 6 lines

Increasing the size has no negative impact to cache invalidation on systems with a smaller cache line. There is an impact on memory usage, but that's not too important for arm64 use cases.

Applied. Thanks.

This patch causes a BUG() on r8a7795/salvator-x, for which support is not yet upstream.

My config (attached) uses SLAB. If I switch to SLUB, it works. The arm64 defconfig works, even if I switch from SLUB to SLAB.

[...]

Quoted 3 lines
------------[ cut here ]------------
kernel BUG at mm/slab.c:2283!
Internal error: Oops - BUG: 0 [#1] SMP

[...]

Quoted 7 lines
Call trace:
[<ffffffc00014f9b4>] __kmem_cache_create+0x21c/0x280
[<ffffffc00068be50>] create_boot_cache+0x4c/0x80
[<ffffffc00068bed8>] create_kmalloc_cache+0x54/0x88
[<ffffffc00068bfc0>] create_kmalloc_caches+0x50/0xf4
[<ffffffc00068db08>] kmem_cache_init+0x104/0x118
[<ffffffc00067d7d8>] start_kernel+0x218/0x33c
Quoted 4 lines
Quoted 1 line

I haven't managed to reproduce this on a Juno kernel.

I now managed to reproduce it with your config (slightly adapted to allow Juno). I'll look into it.

Good to hear that!

BTW, I see this:

freelist_size = 32
cache_line_size() = 64

It seems like the value returned by cache_line_size() in arch/arm64/include/asm/cache.h disagrees with L1_CACHE_SHIFT == 7:

        static inline int cache_line_size(void)
        {
                u32 cwg = cache_type_cwg();
                return cwg ? 4 << cwg : L1_CACHE_BYTES;
        }

Making cache_line_size() always return L1_CACHE_BYTES doesn't help.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

maintainer
#12 lore raw
Quoted 58 lines · Geert Uytterhoeven
Quoted 42 lines · Catalin Marinas
Quoted 41 lines · Catalin Marinas
Quoted 27 lines · Geert Uytterhoeven
Quoted 11 lines · Catalin Marinas
Quoted 10 lines · Robert Richter

From: Tirumalesh Chalamarla <[email protected]>

Increase the standard cacheline size to avoid having locks in the same cacheline.

Cavium's ThunderX core implements cache lines of 128 byte size. With current granulare size of 64 bytes (L1_CACHE_SHIFT=6) two locks could share the same cache line leading a performance degradation. Increasing the size fixes that.

Quoted 8 lines
Quoted 6 lines

Increasing the size has no negative impact to cache invalidation on systems with a smaller cache line. There is an impact on memory usage, but that's not too important for arm64 use cases.

Applied. Thanks.

This patch causes a BUG() on r8a7795/salvator-x, for which support is not yet upstream.

My config (attached) uses SLAB. If I switch to SLUB, it works. The arm64 defconfig works, even if I switch from SLUB to SLAB.

[...]

Quoted 3 lines
------------[ cut here ]------------
kernel BUG at mm/slab.c:2283!
Internal error: Oops - BUG: 0 [#1] SMP

[...]

Quoted 7 lines
Call trace:
[<ffffffc00014f9b4>] __kmem_cache_create+0x21c/0x280
[<ffffffc00068be50>] create_boot_cache+0x4c/0x80
[<ffffffc00068bed8>] create_kmalloc_cache+0x54/0x88
[<ffffffc00068bfc0>] create_kmalloc_caches+0x50/0xf4
[<ffffffc00068db08>] kmem_cache_init+0x104/0x118
[<ffffffc00067d7d8>] start_kernel+0x218/0x33c
Quoted 4 lines
Quoted 1 line

I haven't managed to reproduce this on a Juno kernel.

I now managed to reproduce it with your config (slightly adapted to allow Juno). I'll look into it.

Good to hear that!

BTW, I see this:

freelist_size = 32
cache_line_size() = 64

It seems like the value returned by cache_line_size() in arch/arm64/include/asm/cache.h disagrees with L1_CACHE_SHIFT == 7:

Quoted 7 lines
        static inline int cache_line_size(void)
        {
                u32 cwg = cache_type_cwg();
                return cwg ? 4 << cwg : L1_CACHE_BYTES;
        }

Making cache_line_size() always return L1_CACHE_BYTES doesn't help.

(cc'ing Jonsoo and Christoph; summary: slab failure with L1_CACHE_BYTES of 128 and sizeof(kmem_cache_node) of 152)

If I revert commit 8fc9cf420b36 ("slab: make more slab management structure off the slab") it works but I still need to figure out how slab indices are calculated. The size_index[] array is overridden so that 0..15 are 7 and 16..23 are 8. But the kmalloc_caches[7] has never been populated, hence the BUG_ON. Another option may be to change kmalloc_size and kmalloc_index to cope with KMALLOC_MIN_SIZE of 128.

I'll do some more investigation tomorrow.

Signature · 1 line
Catalin
#13 lore raw
Quoted 2 lines · Catalin Marinas

(cc'ing Jonsoo and Christoph; summary: slab failure with L1_CACHE_BYTES of 128 and sizeof(kmem_cache_node) of 152)

Hmmm... Yes that would mean use the 196 sized kmalloc array which is not a power of two slab. But the code looks fine to me.

Quoted 8 lines

If I revert commit 8fc9cf420b36 ("slab: make more slab management structure off the slab") it works but I still need to figure out how slab indices are calculated. The size_index[] array is overridden so that 0..15 are 7 and 16..23 are 8. But the kmalloc_caches[7] has never been populated, hence the BUG_ON. Another option may be to change kmalloc_size and kmalloc_index to cope with KMALLOC_MIN_SIZE of 128.

I'll do some more investigation tomorrow.

The commit allows off slab management for PAGE_SIZE >> 5 that is 128.

After that commit kmem_cache_create would try to allocate an off slab management structure which is not available during early boot. But the slab_early_init is set which should prevent the use of an off slab management infrastructure in kmem_cache_create().

However, the failure in line 2283 shows that the OFF SLAB flag was mistakenly set anyways!!!! Something must havee cleared slab_early_init?

maintainer
#14 lore raw

(+ linux-mm)

Quoted 6 lines · Christoph Lameter
Quoted 2 lines · Catalin Marinas

(cc'ing Jonsoo and Christoph; summary: slab failure with L1_CACHE_BYTES of 128 and sizeof(kmem_cache_node) of 152)

Hmmm... Yes that would mean use the 196 sized kmalloc array which is not a power of two slab. But the code looks fine to me.

I'm not entirely sure that gets used (or even created). kmalloc_index(152) returns 8 (INDEX_NODE==8) since KMALLOC_MIN_SIZE==128 and the "kmalloc-node" cache size is 256.

Quoted 10 lines
Quoted 8 lines

If I revert commit 8fc9cf420b36 ("slab: make more slab management structure off the slab") it works but I still need to figure out how slab indices are calculated. The size_index[] array is overridden so that 0..15 are 7 and 16..23 are 8. But the kmalloc_caches[7] has never been populated, hence the BUG_ON. Another option may be to change kmalloc_size and kmalloc_index to cope with KMALLOC_MIN_SIZE of 128.

I'll do some more investigation tomorrow.

The commit allows off slab management for PAGE_SIZE >> 5 that is 128.

This means that the first kmalloc cache to be created, "kmalloc-128", is off slab.

Quoted 7 lines

After that commit kmem_cache_create would try to allocate an off slab management structure which is not available during early boot. But the slab_early_init is set which should prevent the use of an off slab management infrastructure in kmem_cache_create().

However, the failure in line 2283 shows that the OFF SLAB flag was mistakenly set anyways!!!! Something must havee cleared slab_early_init?

slab_early_init is cleared after "kmem_cache" and "kmalloc-node" caches are successfully created. Following this, the minimum kmalloc allocation goes off-slab when KMALLOC_MIN_SIZE == 128.

When trying to create "kmalloc-128" (via create_kmalloc_caches(), slab_early_init is already 0), __kmem_cache_create() requires an allocation of 32 bytes (freelist_size) which has index 7, hence exactly the kmalloc_caches[7] we are trying to create.

The simplest option would be to make sure that off slab isn't allowed for caches of KMALLOC_MIN_SIZE or smaller, with the drawback that not only "kmalloc-128" but any other such caches will be on slab.

I think a better option would be to first check that there is a kmalloc_caches[] entry for freelist_size before deciding to go off-slab. See below:

-----8<------------------------------

Quoted 1 line

From ce27c5c6d156522ceaff20de8a7af281cf079b6f Mon Sep 17 00:00:00 2001

From: Catalin Marinas <[email protected]>
Date: Wed, 4 Nov 2015 12:19:00 +0000
Subject: [PATCH] mm: slab: Avoid BUG when KMALLOC_MIN_SIZE == (PAGE_SIZE >> 5)

The slab allocator, following commit 8fc9cf420b36 ("slab: make more slab management structure off the slab"), tries to place slab management off-slab when the object size is PAGE_SIZE >> 5 or larger. On arm64 with KMALLOC_MIN_SIZE = L1_CACHE_BYTES = 128, "kmalloc-128" is the smallest cache to be created after slab_early_init = 0. The current __kmem_cache_create() implementation aims to place the management structure off-slab. However, the kmalloc_slab(freelist_size) has not been populated yet, triggering a bug on !cachep->freelist_cache.

This patch addresses the problem by keeping the management structure on-slab if the corresponding kmalloc_caches[] is not populated yet.

Fixes 8fc9cf420b36 ("slab: make more slab management structure off the slab")
Cc <[email protected]> # 3.15+
---
 mm/slab.c | 43 ++++++++++++++++++++++++-------------------
 1 file changed, 24 insertions(+), 19 deletions(-)
mm/slab.c +24 -19
index 4fcc5dd8d5a6..d4a21736eb5d 100644
@@ -2246,16 +2246,33 @@ __kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
 
 	if (flags & CFLGS_OFF_SLAB) {
 		/* really off slab. No need for manual alignment */
-		freelist_size = calculate_freelist_size(cachep->num, 0);
+		size_t off_freelist_size = calculate_freelist_size(cachep->num, 0);
+
+		cachep->freelist_cache = kmalloc_slab(off_freelist_size, 0u);
+		if (ZERO_OR_NULL_PTR(cachep->freelist_cache)) {
+			/*
+			 * We don't have kmalloc_caches[] populated for
+			 * off_freelist_size yet. This can happen during
+			 * create_kmalloc_caches() when KMALLOC_MIN_SIZE >=
+			 * (PAGE_SHIFT >> 5) and CFLGS_OFF_SLAB is set. Move
+			 * the cache on-slab.
+			 */
+			flags &= ~CFLGS_OFF_SLAB;
+			left_over = calculate_slab_order(cachep, size, cachep->align, flags);
+		} else {
+			freelist_size = off_freelist_size;
 
 #ifdef CONFIG_PAGE_POISONING
-		/* If we're going to use the generic kernel_map_pages()
-		 * poisoning, then it's going to smash the contents of
-		 * the redzone and userword anyhow, so switch them off.
-		 */
-		if (size % PAGE_SIZE == 0 && flags & SLAB_POISON)
-			flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
+			/*
+			 * If we're going to use the generic kernel_map_pages()
+			 * poisoning, then it's going to smash the contents of
+			 * the redzone and userword anyhow, so switch them off.
+			 */
+			if (size % PAGE_SIZE == 0 && flags & SLAB_POISON)
+				flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
 #endif
+		}
+
 	}
 
 	cachep->colour_off = cache_line_size();
@@ -2271,18 +2288,6 @@ __kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
 	cachep->size = size;
 	cachep->reciprocal_buffer_size = reciprocal_value(size);
 
-	if (flags & CFLGS_OFF_SLAB) {
-		cachep->freelist_cache = kmalloc_slab(freelist_size, 0u);
-		/*
-		 * This is a possibility for one of the kmalloc_{dma,}_caches.
-		 * But since we go off slab only for object size greater than
-		 * PAGE_SIZE/8, and kmalloc_{dma,}_caches get created
-		 * in ascending order,this should not happen at all.
-		 * But leave a BUG_ON for some lucky dude.
-		 */
-		BUG_ON(ZERO_OR_NULL_PTR(cachep->freelist_cache));
-	}
-
 	err = setup_cpu_cache(cachep, gfp);
 	if (err) {
 		__kmem_cache_shutdown(cachep);
#15 lore raw
Quoted 3 lines · Catalin Marinas

The simplest option would be to make sure that off slab isn't allowed for caches of KMALLOC_MIN_SIZE or smaller, with the drawback that not only "kmalloc-128" but any other such caches will be on slab.

The reason for an off slab configuration is denser object packing.

Quoted 2 lines

I think a better option would be to first check that there is a kmalloc_caches[] entry for freelist_size before deciding to go off-slab.

Hmmm.. Yes seems to be an option.

Maybe we simply revert commit 8fc9cf420b36 instead? That does not seem to make too much sense to me and the goal of the commit cannot be accomplished on ARM. Your patch essentially reverts the effect anyways.

Smaller slabs really do not need off slab management anyways since they will only loose a few objects per slab page.

maintainer
#16 lore raw
Quoted 6 lines · Christoph Lameter
Quoted 3 lines · Catalin Marinas

The simplest option would be to make sure that off slab isn't allowed for caches of KMALLOC_MIN_SIZE or smaller, with the drawback that not only "kmalloc-128" but any other such caches will be on slab.

Quoted 8 lines

The reason for an off slab configuration is denser object packing.

Quoted 2 lines

I think a better option would be to first check that there is a kmalloc_caches[] entry for freelist_size before deciding to go off-slab.

Hmmm.. Yes seems to be an option.

Maybe we simply revert commit 8fc9cf420b36 instead?

I'm fine with this. Also note that the arm64 commit changing L1_CACHE_BYTES to 128 hasn't been pushed yet (it's queued for 4.4).

Quoted 3 lines

That does not seem to make too much sense to me and the goal of the commit cannot be accomplished on ARM. Your patch essentially reverts the effect anyways.

In theory it only reverts the effect for the first kmalloc_cache ("kmalloc-128" in the arm64 case). Any other bigger cache which would not be mergeable with an existing one still has the potential of off-slab management.

Quoted 2 lines

Smaller slabs really do not need off slab management anyways since they will only loose a few objects per slab page.

IIUC, starting with 128 slab size for a 4KB page, you have 32 objects per page. The freelist takes 32 bytes (or 31), therefore you waste a single slab object. However, only 1/4 of it is used for freelist and the waste gets bigger with 256 slab size, hence the original commit.

BTW, assuming L1_CACHE_BYTES is 512 (I don't ever see this happening but just in theory), we potentially have the same issue. What would save us is that INDEX_NODE would match the first "kmalloc-512" cache, so we have it pre-populated.

Signature · 1 line
Catalin
#17 lore raw
Quoted 4 lines · Catalin Marinas

BTW, assuming L1_CACHE_BYTES is 512 (I don't ever see this happening but just in theory), we potentially have the same issue. What would save us is that INDEX_NODE would match the first "kmalloc-512" cache, so we have it pre-populated.

Ok maybe add some BUILD_BUG_ONs to ensure that builds fail until we have addressed that.

maintainer
#18 lore raw
Quoted 9 lines · Christoph Lameter
Quoted 4 lines · Catalin Marinas

BTW, assuming L1_CACHE_BYTES is 512 (I don't ever see this happening but just in theory), we potentially have the same issue. What would save us is that INDEX_NODE would match the first "kmalloc-512" cache, so we have it pre-populated.

Ok maybe add some BUILD_BUG_ONs to ensure that builds fail until we have addressed that.

A BUILD_BUG_ON should be fine.

Thinking some more, I think if KMALLOC_MIN_SIZE is 128, there is no gain with off-slab management since the freelist allocation would still be 128 bytes. An alternative to reverting while still having a little benefit of off-slab for 256 bytes objects (rather than 512 as we would get with the revert):

mm/slab.c +2 -2
index 4fcc5dd8d5a6..ac32b4a0f2ec 100644
@@ -2212,8 +2212,8 @@ __kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
 	 * it too early on. Always use on-slab management when
 	 * SLAB_NOLEAKTRACE to avoid recursive calls into kmemleak)
 	 */
-	if ((size >= (PAGE_SIZE >> 5)) && !slab_early_init &&
-	    !(flags & SLAB_NOLEAKTRACE))
+	if ((size >= (PAGE_SIZE >> 5)) && (size > KMALLOC_MIN_SIZE) &&
+		!slab_early_init && !(flags & SLAB_NOLEAKTRACE))
 		/*
 		 * Size is large, assume best to place the slab management obj
 		 * off-slab (should allow better packing of objs).

Whichever you prefer.

Signature · 1 line
Catalin
#19 lore raw
Quoted 35 lines · Catalin Marinas
Quoted 9 lines · Christoph Lameter
Quoted 4 lines · Catalin Marinas

BTW, assuming L1_CACHE_BYTES is 512 (I don't ever see this happening but just in theory), we potentially have the same issue. What would save us is that INDEX_NODE would match the first "kmalloc-512" cache, so we have it pre-populated.

Ok maybe add some BUILD_BUG_ONs to ensure that builds fail until we have addressed that.

A BUILD_BUG_ON should be fine.

Thinking some more, I think if KMALLOC_MIN_SIZE is 128, there is no gain with off-slab management since the freelist allocation would still be 128 bytes. An alternative to reverting while still having a little benefit of off-slab for 256 bytes objects (rather than 512 as we would get with the revert):

mm/slab.c +2 -2
index 4fcc5dd8d5a6..ac32b4a0f2ec 100644
@@ -2212,8 +2212,8 @@ __kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
 	 * it too early on. Always use on-slab management when
 	 * SLAB_NOLEAKTRACE to avoid recursive calls into kmemleak)
 	 */
-	if ((size >= (PAGE_SIZE >> 5)) && !slab_early_init &&
-	    !(flags & SLAB_NOLEAKTRACE))
+	if ((size >= (PAGE_SIZE >> 5)) && (size > KMALLOC_MIN_SIZE) &&
+		!slab_early_init && !(flags & SLAB_NOLEAKTRACE))
 		/*
 		 * Size is large, assume best to place the slab management obj
 		 * off-slab (should allow better packing of objs).
Quoted 1 line

Whichever you prefer.

Hello,

I prefer this simple way. It looks like that it can solve the issue on any arbitrary configuration.

Thanks.

maintainer
#20 lore raw

PATCH 1/1 mm: slab: Only move management objects off-slab for sizes larger than KMALLOC_MIN_SIZE

Commit 8fc9cf420b36 ("slab: make more slab management structure off the slab") enables off-slab management objects for sizes starting with PAGE_SIZE >> 5. This means 128 bytes for a 4KB page configuration. However, on systems with a KMALLOC_MIN_SIZE of 128 (arm64 in 4.4), such optimisation does not make sense since the slab management allocation would take 128 bytes anyway (even though freelist_size is 32) with the additional overhead of another allocation.

This patch introduces an OFF_SLAB_MIN_SIZE macro which takes KMALLOC_MIN_SIZE into account. It also solves a slab bug on arm64 where the first kmalloc_cache to be initialised after slab_early_init = 0, "kmalloc-128", fails to allocate off-slab management objects from the same "kmalloc-128" cache.

Fixes 8fc9cf420b36 ("slab: make more slab management structure off the slab")
Cc <[email protected]> # 3.15+
Cc Christoph Lameter <[email protected]>
Cc Pekka Enberg <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joonsoo Kim <[email protected]>
Cc Andrew Morton <[email protected]>
---
 mm/slab.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
mm/slab.c +3 -2
index 4fcc5dd8d5a6..419b649b395e 100644
@@ -282,6 +282,7 @@ static void kmem_cache_node_init(struct kmem_cache_node *parent)
 
 #define CFLGS_OFF_SLAB		(0x80000000UL)
 #define	OFF_SLAB(x)	((x)->flags & CFLGS_OFF_SLAB)
+#define OFF_SLAB_MIN_SIZE	(max_t(size_t, PAGE_SIZE >> 5, KMALLOC_MIN_SIZE + 1))
 
 #define BATCHREFILL_LIMIT	16
 /*
@@ -2212,7 +2213,7 @@ __kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
 	 * it too early on. Always use on-slab management when
 	 * SLAB_NOLEAKTRACE to avoid recursive calls into kmemleak)
 	 */
-	if ((size >= (PAGE_SIZE >> 5)) && !slab_early_init &&
+	if (size >= OFF_SLAB_MIN_SIZE && !slab_early_init &&
 	    !(flags & SLAB_NOLEAKTRACE))
 		/*
 		 * Size is large, assume best to place the slab management obj
@@ -2276,7 +2277,7 @@ __kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
 		/*
 		 * This is a possibility for one of the kmalloc_{dma,}_caches.
 		 * But since we go off slab only for object size greater than
-		 * PAGE_SIZE/8, and kmalloc_{dma,}_caches get created
+		 * OFF_SLAB_MIN_SIZE, and kmalloc_{dma,}_caches get created
 		 * in ascending order,this should not happen at all.
 		 * But leave a BUG_ON for some lucky dude.
 		 */
maintainer
#21 lore raw

Re: [PATCH] mm: slab: Only move management objects off-slab for sizes larger than KMALLOC_MIN_SIZE

Quoted 8 lines · On Thu, 5 Nov 2015 11:50:35 +0000 Catalin Marinas <[email protected]>…

Commit 8fc9cf420b36 ("slab: make more slab management structure off the slab") enables off-slab management objects for sizes starting with PAGE_SIZE >> 5. This means 128 bytes for a 4KB page configuration. However, on systems with a KMALLOC_MIN_SIZE of 128 (arm64 in 4.4), such optimisation does not make sense since the slab management allocation would take 128 bytes anyway (even though freelist_size is 32) with the additional overhead of another allocation.

Quoted 5 lines

This patch introduces an OFF_SLAB_MIN_SIZE macro which takes KMALLOC_MIN_SIZE into account. It also solves a slab bug on arm64 where the first kmalloc_cache to be initialised after slab_early_init = 0, "kmalloc-128", fails to allocate off-slab management objects from the same "kmalloc-128" cache.

That all seems to be quite minor stuff.

Quoted 2 lines
Fixes 8fc9cf420b36 ("slab: make more slab management structure off the slab")
Cc <[email protected]> # 3.15+

Yet you believe the fix should be backported.

So, the usual refrain: when fixing a bug, please describe the end-user visible effects of that bug.

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

maintainer
#22 lore raw

Re: [PATCH] mm: slab: Only move management objects off-slab for sizes larger than KMALLOC_MIN_SIZE

Quoted 10 lines · Andrew Morton
Quoted 8 lines · On Thu, 5 Nov 2015 11:50:35 +0000 Catalin Marinas <[email protected]>…

Commit 8fc9cf420b36 ("slab: make more slab management structure off the slab") enables off-slab management objects for sizes starting with PAGE_SIZE >> 5. This means 128 bytes for a 4KB page configuration. However, on systems with a KMALLOC_MIN_SIZE of 128 (arm64 in 4.4), such optimisation does not make sense since the slab management allocation would take 128 bytes anyway (even though freelist_size is 32) with the additional overhead of another allocation.

Quoted 7 lines
Quoted 5 lines

This patch introduces an OFF_SLAB_MIN_SIZE macro which takes KMALLOC_MIN_SIZE into account. It also solves a slab bug on arm64 where the first kmalloc_cache to be initialised after slab_early_init = 0, "kmalloc-128", fails to allocate off-slab management objects from the same "kmalloc-128" cache.

That all seems to be quite minor stuff.

Apart from "it also solves a bug on arm64...". But I agree, the initial commit log doesn't give any justification for cc stable.

Quoted 7 lines
Quoted 2 lines
Fixes 8fc9cf420b36 ("slab: make more slab management structure off the slab")
Cc <[email protected]> # 3.15+

Yet you believe the fix should be backported.

So, the usual refrain: when fixing a bug, please describe the end-user visible effects of that bug.

What about (unless you prefer this slightly more intrusive fix:
http://article.gmane.org/gmane.linux.ports.sh.devel/50303):

------------------8<--------------------------

Quoted 1 line

From fda8f306b6941f4ddbefcbcfaa59fedef4a679a3 Mon Sep 17 00:00:00 2001

From: Catalin Marinas <[email protected]>
Date: Thu, 5 Nov 2015 11:14:48 +0000
Subject: [PATCH] mm: slab: Only move management objects off-slab for sizes
larger than KMALLOC_MIN_SIZE

On systems with a KMALLOC_MIN_SIZE of 128 (arm64, some mips and powerpc configurations defining ARCH_DMA_MINALIGN to 128), the first kmalloc_caches[] entry to be initialised after slab_early_init = 0 is "kmalloc-128" with index 7. Depending on the debug kernel configuration, sizeof(struct kmem_cache) can be larger than 128 resulting in an INDEX_NODE of 8.

Commit 8fc9cf420b36 ("slab: make more slab management structure off the slab") enables off-slab management objects for sizes starting with PAGE_SIZE >> 5 (128 bytes for a 4KB page configuration) and the creation of the "kmalloc-128" cache would try to place the management objects off-slab. However, since KMALLOC_MIN_SIZE is already 128 and freelist_size == 32 in __kmem_cache_create(), kmalloc_slab(freelist_size) returns NULL (kmalloc_caches[7] not populated yet). This triggers the following bug on arm64:

[    0.000000] kernel BUG at /work/Linux/linux-2.6-aarch64/mm/slab.c:2283!
[    0.000000] Internal error: Oops - BUG: 0 [#1] SMP
[    0.000000] Modules linked in:
[    0.000000] CPU: 0 PID: 0 Comm: swapper Not tainted 4.3.0-rc4+ #540
[    0.000000] Hardware name: Juno (DT)
[    0.000000] task: ffffffc0006962b0 ti: ffffffc00068c000 task.ti: ffffffc00068c000
[    0.000000] PC is at __kmem_cache_create+0x21c/0x280
[    0.000000] LR is at __kmem_cache_create+0x210/0x280
[...]
[    0.000000] Call trace:
[    0.000000] [<ffffffc000154948>] __kmem_cache_create+0x21c/0x280
[    0.000000] [<ffffffc000652da4>] create_boot_cache+0x48/0x80
[    0.000000] [<ffffffc000652e2c>] create_kmalloc_cache+0x50/0x88
[    0.000000] [<ffffffc000652f14>] create_kmalloc_caches+0x4c/0xf4
[    0.000000] [<ffffffc000654a9c>] kmem_cache_init+0x100/0x118
[    0.000000] [<ffffffc0006447d4>] start_kernel+0x214/0x33c

This patch introduces an OFF_SLAB_MIN_SIZE definition to avoid off-slab management objects for sizes equal to or smaller than KMALLOC_MIN_SIZE.

Fixes 8fc9cf420b36 ("slab: make more slab management structure off the slab")
Cc <[email protected]> # 3.15+
Cc Christoph Lameter <[email protected]>
Cc Pekka Enberg <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joonsoo Kim <[email protected]>
Cc Andrew Morton <[email protected]>
---
 mm/slab.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
mm/slab.c +3 -2
index 4fcc5dd8d5a6..419b649b395e 100644
@@ -282,6 +282,7 @@ static void kmem_cache_node_init(struct kmem_cache_node *parent)
 
 #define CFLGS_OFF_SLAB		(0x80000000UL)
 #define	OFF_SLAB(x)	((x)->flags & CFLGS_OFF_SLAB)
+#define OFF_SLAB_MIN_SIZE	(max_t(size_t, PAGE_SIZE >> 5, KMALLOC_MIN_SIZE + 1))
 
 #define BATCHREFILL_LIMIT	16
 /*
@@ -2212,7 +2213,7 @@ __kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
 	 * it too early on. Always use on-slab management when
 	 * SLAB_NOLEAKTRACE to avoid recursive calls into kmemleak)
 	 */
-	if ((size >= (PAGE_SIZE >> 5)) && !slab_early_init &&
+	if (size >= OFF_SLAB_MIN_SIZE && !slab_early_init &&
 	    !(flags & SLAB_NOLEAKTRACE))
 		/*
 		 * Size is large, assume best to place the slab management obj
@@ -2276,7 +2277,7 @@ __kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
 		/*
 		 * This is a possibility for one of the kmalloc_{dma,}_caches.
 		 * But since we go off slab only for object size greater than
-		 * PAGE_SIZE/8, and kmalloc_{dma,}_caches get created
+		 * OFF_SLAB_MIN_SIZE, and kmalloc_{dma,}_caches get created
 		 * in ascending order,this should not happen at all.
 		 * But leave a BUG_ON for some lucky dude.
 		 */
#23 lore raw

Re: [PATCH] mm: slab: Only move management objects off-slab for sizes larger than KMALLOC_MIN_SIZE

Quoted 43 lines · Catalin Marinas

From fda8f306b6941f4ddbefcbcfaa59fedef4a679a3 Mon Sep 17 00:00:00 2001
From: Catalin Marinas <[email protected]>
Date: Thu, 5 Nov 2015 11:14:48 +0000
Subject: [PATCH] mm: slab: Only move management objects off-slab for sizes
larger than KMALLOC_MIN_SIZE

On systems with a KMALLOC_MIN_SIZE of 128 (arm64, some mips and powerpc configurations defining ARCH_DMA_MINALIGN to 128), the first kmalloc_caches[] entry to be initialised after slab_early_init = 0 is "kmalloc-128" with index 7. Depending on the debug kernel configuration, sizeof(struct kmem_cache) can be larger than 128 resulting in an INDEX_NODE of 8.

Commit 8fc9cf420b36 ("slab: make more slab management structure off the slab") enables off-slab management objects for sizes starting with PAGE_SIZE >> 5 (128 bytes for a 4KB page configuration) and the creation of the "kmalloc-128" cache would try to place the management objects off-slab. However, since KMALLOC_MIN_SIZE is already 128 and freelist_size == 32 in __kmem_cache_create(), kmalloc_slab(freelist_size) returns NULL (kmalloc_caches[7] not populated yet). This triggers the following bug on arm64:

[    0.000000] kernel BUG at /work/Linux/linux-2.6-aarch64/mm/slab.c:2283!
[    0.000000] Internal error: Oops - BUG: 0 [#1] SMP
[    0.000000] Modules linked in:
[    0.000000] CPU: 0 PID: 0 Comm: swapper Not tainted 4.3.0-rc4+ #540
[    0.000000] Hardware name: Juno (DT)
[    0.000000] task: ffffffc0006962b0 ti: ffffffc00068c000 task.ti: ffffffc00068c000
[    0.000000] PC is at __kmem_cache_create+0x21c/0x280
[    0.000000] LR is at __kmem_cache_create+0x210/0x280
[...]
[    0.000000] Call trace:
[    0.000000] [<ffffffc000154948>] __kmem_cache_create+0x21c/0x280
[    0.000000] [<ffffffc000652da4>] create_boot_cache+0x48/0x80
[    0.000000] [<ffffffc000652e2c>] create_kmalloc_cache+0x50/0x88
[    0.000000] [<ffffffc000652f14>] create_kmalloc_caches+0x4c/0xf4
[    0.000000] [<ffffffc000654a9c>] kmem_cache_init+0x100/0x118
[    0.000000] [<ffffffc0006447d4>] start_kernel+0x214/0x33c

This patch introduces an OFF_SLAB_MIN_SIZE definition to avoid off-slab management objects for sizes equal to or smaller than KMALLOC_MIN_SIZE.

Quoted 9 lines
Fixes 8fc9cf420b36 ("slab: make more slab management structure off the slab")
Cc <[email protected]> # 3.15+
Cc Christoph Lameter <[email protected]>
Cc Pekka Enberg <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joonsoo Kim <[email protected]>
Cc Andrew Morton <[email protected]>

Thanks a lot!

For the record (the fix is already upstream):

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert at linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

Acked-by on v1 1/1
#24 lore raw

Re: [PATCH] mm: slab: Only move management objects off-slab for sizes larger than KMALLOC_MIN_SIZE

In reply to Catalin Marinas
Quoted 5 lines · Catalin Marinas

This patch introduces an OFF_SLAB_MIN_SIZE macro which takes KMALLOC_MIN_SIZE into account. It also solves a slab bug on arm64 where the first kmalloc_cache to be initialised after slab_early_init = 0, "kmalloc-128", fails to allocate off-slab management objects from the same "kmalloc-128" cache.

#25 lore raw
In reply to Robert Richter
Quoted 9 lines · Robert Richter

From: Tirumalesh Chalamarla <[email protected]>

Increase the standard cacheline size to avoid having locks in the same cacheline.

Cavium's ThunderX core implements cache lines of 128 byte size. With current granulare size of 64 bytes (L1_CACHE_SHIFT=6) two locks could share the same cache line leading a performance degradation. Increasing the size fixes that.

Beside, slab-side bug, I don't think this argument is valid. Even if this change is applied, statically allocated spinlock could share the same cache line.

If two locks should not share the same cache line, you'd better to use compiler attribute such as ____cacheline_aligned_in_smp in appropriate place.

Thanks.

Quoted 32 lines

Increasing the size has no negative impact to cache invalidation on systems with a smaller cache line. There is an impact on memory usage, but that's not too important for arm64 use cases.

---
 arch/arm64/include/asm/cache.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/include/asm/cache.h +1 -1
index bde449936e2f..5082b30bc2c0 100644
@@ -18,7 +18,7 @@
 
 #include <asm/cachetype.h>
 
-#define L1_CACHE_SHIFT		6
+#define L1_CACHE_SHIFT		7
 #define L1_CACHE_BYTES		(1 << L1_CACHE_SHIFT)
 
 /*
Signature · 7 lines
2.1.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/
maintainer
#26 lore raw
Quoted 6 lines · Joonsoo Kim
Quoted 5 lines · Robert Richter

From: Tirumalesh Chalamarla <[email protected]>

Increase the standard cacheline size to avoid having locks in the same cacheline.

Quoted 8 lines
Quoted 4 lines

Cavium's ThunderX core implements cache lines of 128 byte size. With current granulare size of 64 bytes (L1_CACHE_SHIFT=6) two locks could share the same cache line leading a performance degradation. Increasing the size fixes that.

Beside, slab-side bug, I don't think this argument is valid. Even if this change is applied, statically allocated spinlock could share the same cache line.

The benchmarks didn't show any difference with or without this patch applied. What convinced me to apply it was this email:

http://lkml.kernel.org/g/CAOZdJXUiRMAguDV+HEJqPg57MyBNqEcTyaH+ya=U93NHb-pdJA at mail.gmail.com

On ARM we have a notion of cache writeback granule (CWG) which tells us "the maximum size of memory that can be overwritten as a result of the eviction of a cache entry that has had a memory location in it modified". What we actually needed was ARCH_DMA_MINALIGN to be 128 (currently defined to the L1_CACHE_BYTES value). However, this wouldn't have fixed the KMALLOC_MIN_SIZE, unless we somehow generate different kmalloc_caches[] and kmalloc_dma_caches[] and probably introduce a size_dma_index[].

Quoted 3 lines

If two locks should not share the same cache line, you'd better to use compiler attribute such as ____cacheline_aligned_in_smp in appropriate place.

We could decouple SMP_CACHE_BYTES from L1_CACHE_BYTES but see above for the other issue we had to solve.

Signature · 1 line
Catalin
#27 lore raw

2015-11-05 19:32 GMT+09:00 Catalin Marinas <[email protected]>:

Quoted 12 lines · Joonsoo Kim
Quoted 11 lines · Robert Richter
Quoted 9 lines

From: Tirumalesh Chalamarla <[email protected]>

Increase the standard cacheline size to avoid having locks in the same cacheline.

Cavium's ThunderX core implements cache lines of 128 byte size. With current granulare size of 64 bytes (L1_CACHE_SHIFT=6) two locks could share the same cache line leading a performance degradation. Increasing the size fixes that.

Quoted 8 lines
Quoted 3 lines

Beside, slab-side bug, I don't think this argument is valid. Even if this change is applied, statically allocated spinlock could share the same cache line.

The benchmarks didn't show any difference with or without this patch applied. What convinced me to apply it was this email:

http://lkml.kernel.org/g/CAOZdJXUiRMAguDV+HEJqPg57MyBNqEcTyaH+ya=U93NHb-pdJA at mail.gmail.com

Okay.

Quoted 8 lines

On ARM we have a notion of cache writeback granule (CWG) which tells us "the maximum size of memory that can be overwritten as a result of the eviction of a cache entry that has had a memory location in it modified". What we actually needed was ARCH_DMA_MINALIGN to be 128 (currently defined to the L1_CACHE_BYTES value). However, this wouldn't have fixed the KMALLOC_MIN_SIZE, unless we somehow generate different kmalloc_caches[] and kmalloc_dma_caches[] and probably introduce a size_dma_index[].

If we create separate kmalloc caches for dma, can we apply this alignment requirement only to dma caches? I guess some memory allocation request that will be used for DMA operation doesn't specify GFP_DMA because it doesn't want the memory from ZONE_DMA. In this case, we should apply dma alignment requirement to all types of caches.

In fact, I know someone who try to implement this alignment separation like as you mentioned to reduce memory waste. I first suggest this solution to him but now I realize that it isn't possible because of above reason.

Am I missing?

If it isn't possible, is there another way to reduce memory waste due to increase of dma alignment requirement in arm64?

Thanks.

maintainer
#28 lore raw
Quoted 10 lines · Joonsoo Kim

2015-11-05 19:32 GMT+09:00 Catalin Marinas <[email protected]>:

Quoted 8 lines

On ARM we have a notion of cache writeback granule (CWG) which tells us "the maximum size of memory that can be overwritten as a result of the eviction of a cache entry that has had a memory location in it modified". What we actually needed was ARCH_DMA_MINALIGN to be 128 (currently defined to the L1_CACHE_BYTES value). However, this wouldn't have fixed the KMALLOC_MIN_SIZE, unless we somehow generate different kmalloc_caches[] and kmalloc_dma_caches[] and probably introduce a size_dma_index[].

Quoted 5 lines

If we create separate kmalloc caches for dma, can we apply this alignment requirement only to dma caches? I guess some memory allocation request that will be used for DMA operation doesn't specify GFP_DMA because it doesn't want the memory from ZONE_DMA. In this case, we should apply dma alignment requirement to all types of caches.

I think you are right. While something like swiotlb (through the streaming DMA API) could do bounce buffering and allocate one from ZONE_DMA, this is not guaranteed if the buffer physical address happens to match the dma_mask. Similarly with an IOMMU, no bouncing happens but the alignment is still required.

Quoted 2 lines

If it isn't possible, is there another way to reduce memory waste due to increase of dma alignment requirement in arm64?

I first need to see how significant the impact is (especially for embedded/mobiles platforms).

An alternative is to leave L1_CACHE_BYTES to 64 by default but warn if the CWG is 128 on systems with non-coherent DMA (and hope that we won't have any). It's not really fix, rather an assumption. Anyway I would very much like the same kernel image for all platforms and no Kconfig entry for the cache line size but if the waste is significant, we may add one for some specific builds (like a mobile phone; or such vendors could patch the kernel themselves).

Signature · 1 line
Catalin
#29 lore raw

2015-11-05 21:17 GMT+09:00 Catalin Marinas <[email protected]>:

Quoted 23 lines · Joonsoo Kim
Quoted 10 lines

2015-11-05 19:32 GMT+09:00 Catalin Marinas <[email protected]>:

Quoted 8 lines

On ARM we have a notion of cache writeback granule (CWG) which tells us "the maximum size of memory that can be overwritten as a result of the eviction of a cache entry that has had a memory location in it modified". What we actually needed was ARCH_DMA_MINALIGN to be 128 (currently defined to the L1_CACHE_BYTES value). However, this wouldn't have fixed the KMALLOC_MIN_SIZE, unless we somehow generate different kmalloc_caches[] and kmalloc_dma_caches[] and probably introduce a size_dma_index[].

Quoted 5 lines

If we create separate kmalloc caches for dma, can we apply this alignment requirement only to dma caches? I guess some memory allocation request that will be used for DMA operation doesn't specify GFP_DMA because it doesn't want the memory from ZONE_DMA. In this case, we should apply dma alignment requirement to all types of caches.

I think you are right. While something like swiotlb (through the streaming DMA API) could do bounce buffering and allocate one from ZONE_DMA, this is not guaranteed if the buffer physical address happens to match the dma_mask. Similarly with an IOMMU, no bouncing happens but the alignment is still required.

Quoted 5 lines
Quoted 2 lines

If it isn't possible, is there another way to reduce memory waste due to increase of dma alignment requirement in arm64?

I first need to see how significant the impact is (especially for embedded/mobiles platforms).

I don't have any ARM64 device. What I have just one report about slab usage from our developer.

The report shows slab usage just after android boot is done in ARM64.

Total slab usage: 90 MB
kmalloc usage: 25 MB
kmalloc (<=64) usage: 7 MB

This would be measured without slab_nomerge so there is a possibility that some usages on kmem_cache is merged into usage of kmalloc (<=64).

Anyway, if ARM64 increase L1_CACHE_BYTES to 128, roughly 7 MB would be wasted. I don't know how this picture is varied in runtime, but, even boot time overhead, 7 MB looks large to me.

Quoted 7 lines

An alternative is to leave L1_CACHE_BYTES to 64 by default but warn if the CWG is 128 on systems with non-coherent DMA (and hope that we won't have any). It's not really fix, rather an assumption. Anyway I would very much like the same kernel image for all platforms and no Kconfig entry for the cache line size but if the waste is significant, we may add one for some specific builds (like a mobile phone; or such vendors could patch the kernel themselves).

Okay. In fact, I'm not very familiar with android device so it'd be better for you to ask some other android developer about how significant the impact is in android or mobile device.

Thanks.

maintainer
#30 lore raw
Quoted 18 lines · Joonsoo Kim

2015-11-05 21:17 GMT+09:00 Catalin Marinas <[email protected]>:

Quoted 6 lines · Joonsoo Kim
Quoted 2 lines

If it isn't possible, is there another way to reduce memory waste due to increase of dma alignment requirement in arm64?

I first need to see how significant the impact is (especially for embedded/mobiles platforms).

I don't have any ARM64 device. What I have just one report about slab usage from our developer.

The report shows slab usage just after android boot is done in ARM64.

Total slab usage: 90 MB
kmalloc usage: 25 MB
kmalloc (<=64) usage: 7 MB

Quoted 7 lines

This would be measured without slab_nomerge so there is a possibility that some usages on kmem_cache is merged into usage of kmalloc (<=64).

Anyway, if ARM64 increase L1_CACHE_BYTES to 128, roughly 7 MB would be wasted. I don't know how this picture is varied in runtime, but, even boot time overhead, 7 MB looks large to me.

7MB is considerable but I guess it wouldn't be all wasted with L1_CACHE_BYTES == 128, maybe half or slightly over. It would be good to know the other kmalloc caches, maybe up to 256.

I don't have an Android filesystem but I just tried to boot Arch (aarch64). Immediately after boot and slab_nomerge, with 128 L1 I get:

kmalloc-128: 6624
kmalloc-256: 1488

With L1 64, I get:

kmalloc-64: 5760
kmalloc-128: 1152
kmalloc-192: 1155
kmalloc-256: 320

So that's about 1.2MB vs 0.8MB. The ratio is 3:2, though I'm not sure it will stay the same as the slab usage increases.

It would be good to get more numbers, we could add a Kconfig option just for specific builds while keeping the default to 128.

Signature · 1 line
Catalin
#31 lore raw
Quoted 44 lines · Catalin Marinas
Quoted 18 lines · Joonsoo Kim

2015-11-05 21:17 GMT+09:00 Catalin Marinas <[email protected]>:

Quoted 6 lines · Joonsoo Kim
Quoted 2 lines

If it isn't possible, is there another way to reduce memory waste due to increase of dma alignment requirement in arm64?

I first need to see how significant the impact is (especially for embedded/mobiles platforms).

I don't have any ARM64 device. What I have just one report about slab usage from our developer.

The report shows slab usage just after android boot is done in ARM64.

Total slab usage: 90 MB
kmalloc usage: 25 MB
kmalloc (<=64) usage: 7 MB

Quoted 7 lines

This would be measured without slab_nomerge so there is a possibility that some usages on kmem_cache is merged into usage of kmalloc (<=64).

Anyway, if ARM64 increase L1_CACHE_BYTES to 128, roughly 7 MB would be wasted. I don't know how this picture is varied in runtime, but, even boot time overhead, 7 MB looks large to me.

7MB is considerable but I guess it wouldn't be all wasted with L1_CACHE_BYTES == 128, maybe half or slightly over. It would be good to know the other kmalloc caches, maybe up to 256.

I don't have an Android filesystem but I just tried to boot Arch (aarch64). Immediately after boot and slab_nomerge, with 128 L1 I get:

kmalloc-128: 6624
kmalloc-256: 1488

With L1 64, I get:

kmalloc-64: 5760
kmalloc-128: 1152
kmalloc-192: 1155
kmalloc-256: 320

Quoted 5 lines

So that's about 1.2MB vs 0.8MB. The ratio is 3:2, though I'm not sure it will stay the same as the slab usage increases.

It would be good to get more numbers, we could add a Kconfig option just for specific builds while keeping the default to 128.

Okay.

Thanks.