@@ -290,7 +290,8 @@ EXPORT_SYMBOL(nr_online_nodes);
290290#endif
291291
292292static bool page_contains_unaccepted (struct page * page , unsigned int order );
293- static bool cond_accept_memory (struct zone * zone , unsigned int order );
293+ static bool cond_accept_memory (struct zone * zone , unsigned int order ,
294+ int alloc_flags );
294295static bool __free_unaccepted (struct page * page );
295296
296297int page_group_by_mobility_disabled __read_mostly ;
@@ -1151,14 +1152,9 @@ static inline void pgalloc_tag_sub(struct page *page, unsigned int nr)
11511152 __pgalloc_tag_sub (page , nr );
11521153}
11531154
1154- static inline void pgalloc_tag_sub_pages (struct page * page , unsigned int nr )
1155+ /* When tag is not NULL, assuming mem_alloc_profiling_enabled */
1156+ static inline void pgalloc_tag_sub_pages (struct alloc_tag * tag , unsigned int nr )
11551157{
1156- struct alloc_tag * tag ;
1157-
1158- if (!mem_alloc_profiling_enabled ())
1159- return ;
1160-
1161- tag = __pgalloc_tag_get (page );
11621158 if (tag )
11631159 this_cpu_sub (tag -> counters -> bytes , PAGE_SIZE * nr );
11641160}
@@ -1168,7 +1164,7 @@ static inline void pgalloc_tag_sub_pages(struct page *page, unsigned int nr)
11681164static inline void pgalloc_tag_add (struct page * page , struct task_struct * task ,
11691165 unsigned int nr ) {}
11701166static inline void pgalloc_tag_sub (struct page * page , unsigned int nr ) {}
1171- static inline void pgalloc_tag_sub_pages (struct page * page , unsigned int nr ) {}
1167+ static inline void pgalloc_tag_sub_pages (struct alloc_tag * tag , unsigned int nr ) {}
11721168
11731169#endif /* CONFIG_MEM_ALLOC_PROFILING */
11741170
@@ -3616,7 +3612,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
36163612 }
36173613 }
36183614
3619- cond_accept_memory (zone , order );
3615+ cond_accept_memory (zone , order , alloc_flags );
36203616
36213617 /*
36223618 * Detect whether the number of free pages is below high
@@ -3643,7 +3639,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
36433639 gfp_mask )) {
36443640 int ret ;
36453641
3646- if (cond_accept_memory (zone , order ))
3642+ if (cond_accept_memory (zone , order , alloc_flags ))
36473643 goto try_this_zone ;
36483644
36493645 /*
@@ -3696,7 +3692,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
36963692
36973693 return page ;
36983694 } else {
3699- if (cond_accept_memory (zone , order ))
3695+ if (cond_accept_memory (zone , order , alloc_flags ))
37003696 goto try_this_zone ;
37013697
37023698 /* Try again if zone has deferred pages */
@@ -4849,7 +4845,7 @@ unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid,
48494845 goto failed ;
48504846 }
48514847
4852- cond_accept_memory (zone , 0 );
4848+ cond_accept_memory (zone , 0 , alloc_flags );
48534849retry_this_zone :
48544850 mark = wmark_pages (zone , alloc_flags & ALLOC_WMARK_MASK ) + nr_pages ;
48554851 if (zone_watermark_fast (zone , 0 , mark ,
@@ -4858,7 +4854,7 @@ unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid,
48584854 break ;
48594855 }
48604856
4861- if (cond_accept_memory (zone , 0 ))
4857+ if (cond_accept_memory (zone , 0 , alloc_flags ))
48624858 goto retry_this_zone ;
48634859
48644860 /* Try again if zone has deferred pages */
@@ -5065,11 +5061,13 @@ static void ___free_pages(struct page *page, unsigned int order,
50655061{
50665062 /* get PageHead before we drop reference */
50675063 int head = PageHead (page );
5064+ /* get alloc tag in case the page is released by others */
5065+ struct alloc_tag * tag = pgalloc_tag_get (page );
50685066
50695067 if (put_page_testzero (page ))
50705068 __free_frozen_pages (page , order , fpi_flags );
50715069 else if (!head ) {
5072- pgalloc_tag_sub_pages (page , (1 << order ) - 1 );
5070+ pgalloc_tag_sub_pages (tag , (1 << order ) - 1 );
50735071 while (order -- > 0 )
50745072 __free_frozen_pages (page + (1 << order ), order ,
50755073 fpi_flags );
@@ -7174,16 +7172,8 @@ bool has_managed_dma(void)
71747172
71757173#ifdef CONFIG_UNACCEPTED_MEMORY
71767174
7177- /* Counts number of zones with unaccepted pages. */
7178- static DEFINE_STATIC_KEY_FALSE (zones_with_unaccepted_pages );
7179-
71807175static bool lazy_accept = true;
71817176
7182- void unaccepted_cleanup_work (struct work_struct * work )
7183- {
7184- static_branch_dec (& zones_with_unaccepted_pages );
7185- }
7186-
71877177static int __init accept_memory_parse (char * p )
71887178{
71897179 if (!strcmp (p , "lazy" )) {
@@ -7208,11 +7198,7 @@ static bool page_contains_unaccepted(struct page *page, unsigned int order)
72087198static void __accept_page (struct zone * zone , unsigned long * flags ,
72097199 struct page * page )
72107200{
7211- bool last ;
7212-
72137201 list_del (& page -> lru );
7214- last = list_empty (& zone -> unaccepted_pages );
7215-
72167202 account_freepages (zone , - MAX_ORDER_NR_PAGES , MIGRATE_MOVABLE );
72177203 __mod_zone_page_state (zone , NR_UNACCEPTED , - MAX_ORDER_NR_PAGES );
72187204 __ClearPageUnaccepted (page );
@@ -7221,28 +7207,6 @@ static void __accept_page(struct zone *zone, unsigned long *flags,
72217207 accept_memory (page_to_phys (page ), PAGE_SIZE << MAX_PAGE_ORDER );
72227208
72237209 __free_pages_ok (page , MAX_PAGE_ORDER , FPI_TO_TAIL );
7224-
7225- if (last ) {
7226- /*
7227- * There are two corner cases:
7228- *
7229- * - If allocation occurs during the CPU bring up,
7230- * static_branch_dec() cannot be used directly as
7231- * it causes a deadlock on cpu_hotplug_lock.
7232- *
7233- * Instead, use schedule_work() to prevent deadlock.
7234- *
7235- * - If allocation occurs before workqueues are initialized,
7236- * static_branch_dec() should be called directly.
7237- *
7238- * Workqueues are initialized before CPU bring up, so this
7239- * will not conflict with the first scenario.
7240- */
7241- if (system_wq )
7242- schedule_work (& zone -> unaccepted_cleanup );
7243- else
7244- unaccepted_cleanup_work (& zone -> unaccepted_cleanup );
7245- }
72467210}
72477211
72487212void accept_page (struct page * page )
@@ -7279,20 +7243,17 @@ static bool try_to_accept_memory_one(struct zone *zone)
72797243 return true;
72807244}
72817245
7282- static inline bool has_unaccepted_memory (void )
7283- {
7284- return static_branch_unlikely (& zones_with_unaccepted_pages );
7285- }
7286-
7287- static bool cond_accept_memory (struct zone * zone , unsigned int order )
7246+ static bool cond_accept_memory (struct zone * zone , unsigned int order ,
7247+ int alloc_flags )
72887248{
72897249 long to_accept , wmark ;
72907250 bool ret = false;
72917251
7292- if (! has_unaccepted_memory ( ))
7252+ if (list_empty ( & zone -> unaccepted_pages ))
72937253 return false;
72947254
7295- if (list_empty (& zone -> unaccepted_pages ))
7255+ /* Bailout, since try_to_accept_memory_one() needs to take a lock */
7256+ if (alloc_flags & ALLOC_TRYLOCK )
72967257 return false;
72977258
72987259 wmark = promo_wmark_pages (zone );
@@ -7325,22 +7286,17 @@ static bool __free_unaccepted(struct page *page)
73257286{
73267287 struct zone * zone = page_zone (page );
73277288 unsigned long flags ;
7328- bool first = false;
73297289
73307290 if (!lazy_accept )
73317291 return false;
73327292
73337293 spin_lock_irqsave (& zone -> lock , flags );
7334- first = list_empty (& zone -> unaccepted_pages );
73357294 list_add_tail (& page -> lru , & zone -> unaccepted_pages );
73367295 account_freepages (zone , MAX_ORDER_NR_PAGES , MIGRATE_MOVABLE );
73377296 __mod_zone_page_state (zone , NR_UNACCEPTED , MAX_ORDER_NR_PAGES );
73387297 __SetPageUnaccepted (page );
73397298 spin_unlock_irqrestore (& zone -> lock , flags );
73407299
7341- if (first )
7342- static_branch_inc (& zones_with_unaccepted_pages );
7343-
73447300 return true;
73457301}
73467302
@@ -7351,7 +7307,8 @@ static bool page_contains_unaccepted(struct page *page, unsigned int order)
73517307 return false;
73527308}
73537309
7354- static bool cond_accept_memory (struct zone * zone , unsigned int order )
7310+ static bool cond_accept_memory (struct zone * zone , unsigned int order ,
7311+ int alloc_flags )
73557312{
73567313 return false;
73577314}
@@ -7422,11 +7379,6 @@ struct page *try_alloc_pages_noprof(int nid, unsigned int order)
74227379 if (!pcp_allowed_order (order ))
74237380 return NULL ;
74247381
7425- #ifdef CONFIG_UNACCEPTED_MEMORY
7426- /* Bailout, since try_to_accept_memory_one() needs to take a lock */
7427- if (has_unaccepted_memory ())
7428- return NULL ;
7429- #endif
74307382 /* Bailout, since _deferred_grow_zone() needs to take a lock */
74317383 if (deferred_pages_enabled ())
74327384 return NULL ;
0 commit comments