Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 1 | .. SPDX-License-Identifier: GPL-2.0 |
| 2 | |
| 3 | ============ |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 4 | x86 Topology |
| 5 | ============ |
| 6 | |
| 7 | This documents and clarifies the main aspects of x86 topology modelling and |
| 8 | representation in the kernel. Update/change when doing changes to the |
| 9 | respective code. |
| 10 | |
| 11 | The architecture-agnostic topology definitions are in |
| 12 | Documentation/cputopology.txt. This file holds x86-specific |
| 13 | differences/specialities which must not necessarily apply to the generic |
| 14 | definitions. Thus, the way to read up on Linux topology on x86 is to start |
| 15 | with the generic one and look at this one in parallel for the x86 specifics. |
| 16 | |
| 17 | Needless to say, code should use the generic functions - this file is *only* |
| 18 | here to *document* the inner workings of x86 topology. |
| 19 | |
| 20 | Started by Thomas Gleixner <tglx@linutronix.de> and Borislav Petkov <bp@alien8.de>. |
| 21 | |
| 22 | The main aim of the topology facilities is to present adequate interfaces to |
| 23 | code which needs to know/query/use the structure of the running system wrt |
| 24 | threads, cores, packages, etc. |
| 25 | |
| 26 | The kernel does not care about the concept of physical sockets because a |
| 27 | socket has no relevance to software. It's an electromechanical component. In |
| 28 | the past a socket always contained a single package (see below), but with the |
| 29 | advent of Multi Chip Modules (MCM) a socket can hold more than one package. So |
| 30 | there might be still references to sockets in the code, but they are of |
| 31 | historical nature and should be cleaned up. |
| 32 | |
| 33 | The topology of a system is described in the units of: |
| 34 | |
| 35 | - packages |
| 36 | - cores |
| 37 | - threads |
| 38 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 39 | Package |
| 40 | ======= |
| 41 | Packages contain a number of cores plus shared resources, e.g. DRAM |
| 42 | controller, shared caches etc. |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 43 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 44 | AMD nomenclature for package is 'Node'. |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 45 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 46 | Package-related topology information in the kernel: |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 47 | |
| 48 | - cpuinfo_x86.x86_max_cores: |
| 49 | |
| 50 | The number of cores in a package. This information is retrieved via CPUID. |
| 51 | |
| 52 | - cpuinfo_x86.phys_proc_id: |
| 53 | |
| 54 | The physical ID of the package. This information is retrieved via CPUID |
| 55 | and deduced from the APIC IDs of the cores in the package. |
| 56 | |
Len Brown | ef7c772 | 2019-02-26 01:19:59 -0500 | [diff] [blame] | 57 | - cpuinfo_x86.logical_proc_id: |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 58 | |
| 59 | The logical ID of the package. As we do not trust BIOSes to enumerate the |
| 60 | packages in a consistent way, we introduced the concept of logical package |
| 61 | ID so we can sanely calculate the number of maximum possible packages in |
| 62 | the system and have the packages enumerated linearly. |
| 63 | |
| 64 | - topology_max_packages(): |
| 65 | |
| 66 | The maximum possible number of packages in the system. Helpful for per |
| 67 | package facilities to preallocate per package information. |
| 68 | |
Borislav Petkov | a268b5f | 2016-11-17 10:45:57 +0100 | [diff] [blame] | 69 | - cpu_llc_id: |
| 70 | |
| 71 | A per-CPU variable containing: |
Borislav Petkov | a268b5f | 2016-11-17 10:45:57 +0100 | [diff] [blame] | 72 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 73 | - On Intel, the first APIC ID of the list of CPUs sharing the Last Level |
| 74 | Cache |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 75 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 76 | - On AMD, the Node ID or Core Complex ID containing the Last Level |
| 77 | Cache. In general, it is a number identifying an LLC uniquely on the |
| 78 | system. |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 79 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 80 | Cores |
| 81 | ===== |
| 82 | A core consists of 1 or more threads. It does not matter whether the threads |
| 83 | are SMT- or CMT-type threads. |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 84 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 85 | AMDs nomenclature for a CMT core is "Compute Unit". The kernel always uses |
| 86 | "core". |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 87 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 88 | Core-related topology information in the kernel: |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 89 | |
| 90 | - smp_num_siblings: |
| 91 | |
| 92 | The number of threads in a core. The number of threads in a package can be |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 93 | calculated by:: |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 94 | |
| 95 | threads_per_package = cpuinfo_x86.x86_max_cores * smp_num_siblings |
| 96 | |
| 97 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 98 | Threads |
| 99 | ======= |
| 100 | A thread is a single scheduling unit. It's the equivalent to a logical Linux |
| 101 | CPU. |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 102 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 103 | AMDs nomenclature for CMT threads is "Compute Unit Core". The kernel always |
| 104 | uses "thread". |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 105 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 106 | Thread-related topology information in the kernel: |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 107 | |
| 108 | - topology_core_cpumask(): |
| 109 | |
| 110 | The cpumask contains all online threads in the package to which a thread |
| 111 | belongs. |
| 112 | |
| 113 | The number of online threads is also printed in /proc/cpuinfo "siblings." |
| 114 | |
Dou Liyang | 0c52f7c | 2018-02-22 16:48:12 +0800 | [diff] [blame] | 115 | - topology_sibling_cpumask(): |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 116 | |
| 117 | The cpumask contains all online threads in the core to which a thread |
| 118 | belongs. |
| 119 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 120 | - topology_logical_package_id(): |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 121 | |
| 122 | The logical package ID to which a thread belongs. |
| 123 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 124 | - topology_physical_package_id(): |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 125 | |
| 126 | The physical package ID to which a thread belongs. |
| 127 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 128 | - topology_core_id(); |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 129 | |
| 130 | The ID of the core to which a thread belongs. It is also printed in /proc/cpuinfo |
| 131 | "core_id." |
| 132 | |
| 133 | |
| 134 | |
| 135 | System topology examples |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 136 | ======================== |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 137 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 138 | .. note:: |
| 139 | The alternative Linux CPU enumeration depends on how the BIOS enumerates the |
| 140 | threads. Many BIOSes enumerate all threads 0 first and then all threads 1. |
| 141 | That has the "advantage" that the logical Linux CPU numbers of threads 0 stay |
| 142 | the same whether threads are enabled or not. That's merely an implementation |
| 143 | detail and has no practical impact. |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 144 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 145 | 1) Single Package, Single Core:: |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 146 | |
| 147 | [package 0] -> [core 0] -> [thread 0] -> Linux CPU 0 |
| 148 | |
| 149 | 2) Single Package, Dual Core |
| 150 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 151 | a) One thread per core:: |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 152 | |
| 153 | [package 0] -> [core 0] -> [thread 0] -> Linux CPU 0 |
| 154 | -> [core 1] -> [thread 0] -> Linux CPU 1 |
| 155 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 156 | b) Two threads per core:: |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 157 | |
| 158 | [package 0] -> [core 0] -> [thread 0] -> Linux CPU 0 |
| 159 | -> [thread 1] -> Linux CPU 1 |
| 160 | -> [core 1] -> [thread 0] -> Linux CPU 2 |
| 161 | -> [thread 1] -> Linux CPU 3 |
| 162 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 163 | Alternative enumeration:: |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 164 | |
| 165 | [package 0] -> [core 0] -> [thread 0] -> Linux CPU 0 |
| 166 | -> [thread 1] -> Linux CPU 2 |
| 167 | -> [core 1] -> [thread 0] -> Linux CPU 1 |
| 168 | -> [thread 1] -> Linux CPU 3 |
| 169 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 170 | AMD nomenclature for CMT systems:: |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 171 | |
| 172 | [node 0] -> [Compute Unit 0] -> [Compute Unit Core 0] -> Linux CPU 0 |
| 173 | -> [Compute Unit Core 1] -> Linux CPU 1 |
| 174 | -> [Compute Unit 1] -> [Compute Unit Core 0] -> Linux CPU 2 |
| 175 | -> [Compute Unit Core 1] -> Linux CPU 3 |
| 176 | |
| 177 | 4) Dual Package, Dual Core |
| 178 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 179 | a) One thread per core:: |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 180 | |
| 181 | [package 0] -> [core 0] -> [thread 0] -> Linux CPU 0 |
| 182 | -> [core 1] -> [thread 0] -> Linux CPU 1 |
| 183 | |
| 184 | [package 1] -> [core 0] -> [thread 0] -> Linux CPU 2 |
| 185 | -> [core 1] -> [thread 0] -> Linux CPU 3 |
| 186 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 187 | b) Two threads per core:: |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 188 | |
| 189 | [package 0] -> [core 0] -> [thread 0] -> Linux CPU 0 |
| 190 | -> [thread 1] -> Linux CPU 1 |
| 191 | -> [core 1] -> [thread 0] -> Linux CPU 2 |
| 192 | -> [thread 1] -> Linux CPU 3 |
| 193 | |
| 194 | [package 1] -> [core 0] -> [thread 0] -> Linux CPU 4 |
| 195 | -> [thread 1] -> Linux CPU 5 |
| 196 | -> [core 1] -> [thread 0] -> Linux CPU 6 |
| 197 | -> [thread 1] -> Linux CPU 7 |
| 198 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 199 | Alternative enumeration:: |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 200 | |
| 201 | [package 0] -> [core 0] -> [thread 0] -> Linux CPU 0 |
| 202 | -> [thread 1] -> Linux CPU 4 |
| 203 | -> [core 1] -> [thread 0] -> Linux CPU 1 |
| 204 | -> [thread 1] -> Linux CPU 5 |
| 205 | |
| 206 | [package 1] -> [core 0] -> [thread 0] -> Linux CPU 2 |
| 207 | -> [thread 1] -> Linux CPU 6 |
| 208 | -> [core 1] -> [thread 0] -> Linux CPU 3 |
| 209 | -> [thread 1] -> Linux CPU 7 |
| 210 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 211 | AMD nomenclature for CMT systems:: |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 212 | |
| 213 | [node 0] -> [Compute Unit 0] -> [Compute Unit Core 0] -> Linux CPU 0 |
| 214 | -> [Compute Unit Core 1] -> Linux CPU 1 |
| 215 | -> [Compute Unit 1] -> [Compute Unit Core 0] -> Linux CPU 2 |
| 216 | -> [Compute Unit Core 1] -> Linux CPU 3 |
| 217 | |
| 218 | [node 1] -> [Compute Unit 0] -> [Compute Unit Core 0] -> Linux CPU 4 |
| 219 | -> [Compute Unit Core 1] -> Linux CPU 5 |
| 220 | -> [Compute Unit 1] -> [Compute Unit Core 0] -> Linux CPU 6 |
| 221 | -> [Compute Unit Core 1] -> Linux CPU 7 |