7272#include <asm/cacheflush.h>
7373#include <asm/sections.h>
7474#include <asm/tlbflush.h>
75+ #include <asm/io.h>
7576
7677#define PCPU_SLOT_BASE_SHIFT 5 /* 1-31 shares the same slot */
7778#define PCPU_DFL_MAP_ALLOC 16 /* start a map with 16 ents */
@@ -151,7 +152,10 @@ static int pcpu_reserved_chunk_limit;
151152 *
152153 * During allocation, pcpu_alloc_mutex is kept locked all the time and
153154 * pcpu_lock is grabbed and released as necessary. All actual memory
154- * allocations are done using GFP_KERNEL with pcpu_lock released.
155+ * allocations are done using GFP_KERNEL with pcpu_lock released. In
156+ * general, percpu memory can't be allocated with irq off but
157+ * irqsave/restore are still used in alloc path so that it can be used
158+ * from early init path - sched_init() specifically.
155159 *
156160 * Free path accesses and alters only the index data structures, so it
157161 * can be safely called from atomic context. When memory needs to be
@@ -350,63 +354,86 @@ static struct pcpu_chunk *pcpu_chunk_addr_search(void *addr)
350354}
351355
352356/**
353- * pcpu_extend_area_map - extend area map for allocation
354- * @chunk: target chunk
357+ * pcpu_need_to_extend - determine whether chunk area map needs to be extended
358+ * @chunk: chunk of interest
355359 *
356- * Extend area map of @chunk so that it can accomodate an allocation.
357- * A single allocation can split an area into three areas, so this
358- * function makes sure that @chunk->map has at least two extra slots.
360+ * Determine whether area map of @chunk needs to be extended to
361+ * accomodate a new allocation.
359362 *
360363 * CONTEXT:
361- * pcpu_alloc_mutex, pcpu_lock. pcpu_lock is released and reacquired
362- * if area map is extended.
364+ * pcpu_lock.
363365 *
364366 * RETURNS:
365- * 0 if noop, 1 if successfully extended, -errno on failure.
367+ * New target map allocation length if extension is necessary, 0
368+ * otherwise.
366369 */
367- static int pcpu_extend_area_map (struct pcpu_chunk * chunk )
368- __releases (lock ) __acquires (lock )
370+ static int pcpu_need_to_extend (struct pcpu_chunk * chunk )
369371{
370372 int new_alloc ;
371- int * new ;
372- size_t size ;
373373
374- /* has enough? */
375374 if (chunk -> map_alloc >= chunk -> map_used + 2 )
376375 return 0 ;
377376
378- spin_unlock_irq (& pcpu_lock );
379-
380377 new_alloc = PCPU_DFL_MAP_ALLOC ;
381378 while (new_alloc < chunk -> map_used + 2 )
382379 new_alloc *= 2 ;
383380
384- new = pcpu_mem_alloc (new_alloc * sizeof (new [0 ]));
385- if (!new ) {
386- spin_lock_irq (& pcpu_lock );
381+ return new_alloc ;
382+ }
383+
384+ /**
385+ * pcpu_extend_area_map - extend area map of a chunk
386+ * @chunk: chunk of interest
387+ * @new_alloc: new target allocation length of the area map
388+ *
389+ * Extend area map of @chunk to have @new_alloc entries.
390+ *
391+ * CONTEXT:
392+ * Does GFP_KERNEL allocation. Grabs and releases pcpu_lock.
393+ *
394+ * RETURNS:
395+ * 0 on success, -errno on failure.
396+ */
397+ static int pcpu_extend_area_map (struct pcpu_chunk * chunk , int new_alloc )
398+ {
399+ int * old = NULL , * new = NULL ;
400+ size_t old_size = 0 , new_size = new_alloc * sizeof (new [0 ]);
401+ unsigned long flags ;
402+
403+ new = pcpu_mem_alloc (new_size );
404+ if (!new )
387405 return - ENOMEM ;
388- }
389406
390- /*
391- * Acquire pcpu_lock and switch to new area map. Only free
392- * could have happened inbetween, so map_used couldn't have
393- * grown.
394- */
395- spin_lock_irq (& pcpu_lock );
396- BUG_ON (new_alloc < chunk -> map_used + 2 );
407+ /* acquire pcpu_lock and switch to new area map */
408+ spin_lock_irqsave (& pcpu_lock , flags );
397409
398- size = chunk -> map_alloc * sizeof (chunk -> map [0 ]);
399- memcpy (new , chunk -> map , size );
410+ if (new_alloc <= chunk -> map_alloc )
411+ goto out_unlock ;
412+
413+ old_size = chunk -> map_alloc * sizeof (chunk -> map [0 ]);
414+ memcpy (new , chunk -> map , old_size );
400415
401416 /*
402417 * map_alloc < PCPU_DFL_MAP_ALLOC indicates that the chunk is
403418 * one of the first chunks and still using static map.
404419 */
405420 if (chunk -> map_alloc >= PCPU_DFL_MAP_ALLOC )
406- pcpu_mem_free ( chunk -> map , size ) ;
421+ old = chunk -> map ;
407422
408423 chunk -> map_alloc = new_alloc ;
409424 chunk -> map = new ;
425+ new = NULL ;
426+
427+ out_unlock :
428+ spin_unlock_irqrestore (& pcpu_lock , flags );
429+
430+ /*
431+ * pcpu_mem_free() might end up calling vfree() which uses
432+ * IRQ-unsafe lock and thus can't be called under pcpu_lock.
433+ */
434+ pcpu_mem_free (old , old_size );
435+ pcpu_mem_free (new , new_size );
436+
410437 return 0 ;
411438}
412439
@@ -1045,7 +1072,8 @@ static void *pcpu_alloc(size_t size, size_t align, bool reserved)
10451072 static int warn_limit = 10 ;
10461073 struct pcpu_chunk * chunk ;
10471074 const char * err ;
1048- int slot , off ;
1075+ int slot , off , new_alloc ;
1076+ unsigned long flags ;
10491077
10501078 if (unlikely (!size || size > PCPU_MIN_UNIT_SIZE || align > PAGE_SIZE )) {
10511079 WARN (true, "illegal size (%zu) or align (%zu) for "
@@ -1054,19 +1082,30 @@ static void *pcpu_alloc(size_t size, size_t align, bool reserved)
10541082 }
10551083
10561084 mutex_lock (& pcpu_alloc_mutex );
1057- spin_lock_irq (& pcpu_lock );
1085+ spin_lock_irqsave (& pcpu_lock , flags );
10581086
10591087 /* serve reserved allocations from the reserved chunk if available */
10601088 if (reserved && pcpu_reserved_chunk ) {
10611089 chunk = pcpu_reserved_chunk ;
1062- if ( size > chunk -> contig_hint ||
1063- pcpu_extend_area_map ( chunk ) < 0 ) {
1064- err = "failed to extend area map of reserved chunk" ;
1090+
1091+ if ( size > chunk -> contig_hint ) {
1092+ err = "alloc from reserved chunk failed " ;
10651093 goto fail_unlock ;
10661094 }
1095+
1096+ while ((new_alloc = pcpu_need_to_extend (chunk ))) {
1097+ spin_unlock_irqrestore (& pcpu_lock , flags );
1098+ if (pcpu_extend_area_map (chunk , new_alloc ) < 0 ) {
1099+ err = "failed to extend area map of reserved chunk" ;
1100+ goto fail_unlock_mutex ;
1101+ }
1102+ spin_lock_irqsave (& pcpu_lock , flags );
1103+ }
1104+
10671105 off = pcpu_alloc_area (chunk , size , align );
10681106 if (off >= 0 )
10691107 goto area_found ;
1108+
10701109 err = "alloc from reserved chunk failed" ;
10711110 goto fail_unlock ;
10721111 }
@@ -1078,14 +1117,20 @@ static void *pcpu_alloc(size_t size, size_t align, bool reserved)
10781117 if (size > chunk -> contig_hint )
10791118 continue ;
10801119
1081- switch (pcpu_extend_area_map (chunk )) {
1082- case 0 :
1083- break ;
1084- case 1 :
1085- goto restart ; /* pcpu_lock dropped, restart */
1086- default :
1087- err = "failed to extend area map" ;
1088- goto fail_unlock ;
1120+ new_alloc = pcpu_need_to_extend (chunk );
1121+ if (new_alloc ) {
1122+ spin_unlock_irqrestore (& pcpu_lock , flags );
1123+ if (pcpu_extend_area_map (chunk ,
1124+ new_alloc ) < 0 ) {
1125+ err = "failed to extend area map" ;
1126+ goto fail_unlock_mutex ;
1127+ }
1128+ spin_lock_irqsave (& pcpu_lock , flags );
1129+ /*
1130+ * pcpu_lock has been dropped, need to
1131+ * restart cpu_slot list walking.
1132+ */
1133+ goto restart ;
10891134 }
10901135
10911136 off = pcpu_alloc_area (chunk , size , align );
@@ -1095,24 +1140,24 @@ static void *pcpu_alloc(size_t size, size_t align, bool reserved)
10951140 }
10961141
10971142 /* hmmm... no space left, create a new chunk */
1098- spin_unlock_irq (& pcpu_lock );
1143+ spin_unlock_irqrestore (& pcpu_lock , flags );
10991144
11001145 chunk = alloc_pcpu_chunk ();
11011146 if (!chunk ) {
11021147 err = "failed to allocate new chunk" ;
11031148 goto fail_unlock_mutex ;
11041149 }
11051150
1106- spin_lock_irq (& pcpu_lock );
1151+ spin_lock_irqsave (& pcpu_lock , flags );
11071152 pcpu_chunk_relocate (chunk , -1 );
11081153 goto restart ;
11091154
11101155area_found :
1111- spin_unlock_irq (& pcpu_lock );
1156+ spin_unlock_irqrestore (& pcpu_lock , flags );
11121157
11131158 /* populate, map and clear the area */
11141159 if (pcpu_populate_chunk (chunk , off , size )) {
1115- spin_lock_irq (& pcpu_lock );
1160+ spin_lock_irqsave (& pcpu_lock , flags );
11161161 pcpu_free_area (chunk , off );
11171162 err = "failed to populate" ;
11181163 goto fail_unlock ;
@@ -1124,7 +1169,7 @@ static void *pcpu_alloc(size_t size, size_t align, bool reserved)
11241169 return __addr_to_pcpu_ptr (chunk -> base_addr + off );
11251170
11261171fail_unlock :
1127- spin_unlock_irq (& pcpu_lock );
1172+ spin_unlock_irqrestore (& pcpu_lock , flags );
11281173fail_unlock_mutex :
11291174 mutex_unlock (& pcpu_alloc_mutex );
11301175 if (warn_limit ) {
@@ -1256,6 +1301,27 @@ void free_percpu(void *ptr)
12561301}
12571302EXPORT_SYMBOL_GPL (free_percpu );
12581303
1304+ /**
1305+ * per_cpu_ptr_to_phys - convert translated percpu address to physical address
1306+ * @addr: the address to be converted to physical address
1307+ *
1308+ * Given @addr which is dereferenceable address obtained via one of
1309+ * percpu access macros, this function translates it into its physical
1310+ * address. The caller is responsible for ensuring @addr stays valid
1311+ * until this function finishes.
1312+ *
1313+ * RETURNS:
1314+ * The physical address for @addr.
1315+ */
1316+ phys_addr_t per_cpu_ptr_to_phys (void * addr )
1317+ {
1318+ if ((unsigned long )addr < VMALLOC_START ||
1319+ (unsigned long )addr >= VMALLOC_END )
1320+ return __pa (addr );
1321+ else
1322+ return page_to_phys (vmalloc_to_page (addr ));
1323+ }
1324+
12591325static inline size_t pcpu_calc_fc_sizes (size_t static_size ,
12601326 size_t reserved_size ,
12611327 ssize_t * dyn_sizep )
0 commit comments