Skip to content

Commit aa1cf99

Browse files
yangyang20220519akpm00
authored andcommitted
delayacct: support re-entrance detection of thrashing accounting
Once upon a time, we only support accounting thrashing of page cache. Then Joonsoo introduced workingset detection for anonymous pages and we gained the ability to account thrashing of them[1]. For page cache thrashing accounting, there is no suitable place to do it in fs level likes swap_readpage(). So we have to do it in folio_wait_bit_common(). Then for anonymous pages thrashing accounting, we have to do it in both swap_readpage() and folio_wait_bit_common(). This likes PSI, so we should let thrashing accounting supports re-entrance detection. This patch is to prepare complete thrashing accounting, and is based on patch "filemap: make the accounting of thrashing more consistent". [1] commit aae466b ("mm/swap: implement workingset detection for anonymous LRU") Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Yang Yang <[email protected]> Signed-off-by: CGEL ZTE <[email protected]> Reviewed-by: Ran Xiaokai <[email protected]> Reviewed-by: wangyong <[email protected]> Acked-by: Joonsoo Kim <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 7047b5a commit aa1cf99

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

include/linux/delayacct.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ extern int delayacct_add_tsk(struct taskstats *, struct task_struct *);
7373
extern __u64 __delayacct_blkio_ticks(struct task_struct *);
7474
extern void __delayacct_freepages_start(void);
7575
extern void __delayacct_freepages_end(void);
76-
extern void __delayacct_thrashing_start(void);
77-
extern void __delayacct_thrashing_end(void);
76+
extern void __delayacct_thrashing_start(bool *in_thrashing);
77+
extern void __delayacct_thrashing_end(bool *in_thrashing);
7878
extern void __delayacct_swapin_start(void);
7979
extern void __delayacct_swapin_end(void);
8080
extern void __delayacct_compact_start(void);
@@ -143,22 +143,22 @@ static inline void delayacct_freepages_end(void)
143143
__delayacct_freepages_end();
144144
}
145145

146-
static inline void delayacct_thrashing_start(void)
146+
static inline void delayacct_thrashing_start(bool *in_thrashing)
147147
{
148148
if (!static_branch_unlikely(&delayacct_key))
149149
return;
150150

151151
if (current->delays)
152-
__delayacct_thrashing_start();
152+
__delayacct_thrashing_start(in_thrashing);
153153
}
154154

155-
static inline void delayacct_thrashing_end(void)
155+
static inline void delayacct_thrashing_end(bool *in_thrashing)
156156
{
157157
if (!static_branch_unlikely(&delayacct_key))
158158
return;
159159

160160
if (current->delays)
161-
__delayacct_thrashing_end();
161+
__delayacct_thrashing_end(in_thrashing);
162162
}
163163

164164
static inline void delayacct_swapin_start(void)
@@ -237,9 +237,9 @@ static inline void delayacct_freepages_start(void)
237237
{}
238238
static inline void delayacct_freepages_end(void)
239239
{}
240-
static inline void delayacct_thrashing_start(void)
240+
static inline void delayacct_thrashing_start(bool *in_thrashing)
241241
{}
242-
static inline void delayacct_thrashing_end(void)
242+
static inline void delayacct_thrashing_end(bool *in_thrashing)
243243
{}
244244
static inline void delayacct_swapin_start(void)
245245
{}

include/linux/sched.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,10 @@ struct task_struct {
944944
#ifdef CONFIG_CPU_SUP_INTEL
945945
unsigned reported_split_lock:1;
946946
#endif
947+
#ifdef CONFIG_TASK_DELAY_ACCT
948+
/* delay due to memory thrashing */
949+
unsigned in_thrashing:1;
950+
#endif
947951

948952
unsigned long atomic_flags; /* Flags requiring atomic access. */
949953

kernel/delayacct.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,22 @@ void __delayacct_freepages_end(void)
214214
&current->delays->freepages_count);
215215
}
216216

217-
void __delayacct_thrashing_start(void)
217+
void __delayacct_thrashing_start(bool *in_thrashing)
218218
{
219+
*in_thrashing = !!current->in_thrashing;
220+
if (*in_thrashing)
221+
return;
222+
223+
current->in_thrashing = 1;
219224
current->delays->thrashing_start = local_clock();
220225
}
221226

222-
void __delayacct_thrashing_end(void)
227+
void __delayacct_thrashing_end(bool *in_thrashing)
223228
{
229+
if (*in_thrashing)
230+
return;
231+
232+
current->in_thrashing = 0;
224233
delayacct_end(&current->delays->lock,
225234
&current->delays->thrashing_start,
226235
&current->delays->thrashing_delay,

mm/filemap.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,10 +1222,11 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,
12221222
wait_queue_entry_t *wait = &wait_page.wait;
12231223
bool thrashing = false;
12241224
unsigned long pflags;
1225+
bool in_thrashing;
12251226

12261227
if (bit_nr == PG_locked &&
12271228
!folio_test_uptodate(folio) && folio_test_workingset(folio)) {
1228-
delayacct_thrashing_start();
1229+
delayacct_thrashing_start(&in_thrashing);
12291230
psi_memstall_enter(&pflags);
12301231
thrashing = true;
12311232
}
@@ -1325,7 +1326,7 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,
13251326
finish_wait(q, wait);
13261327

13271328
if (thrashing) {
1328-
delayacct_thrashing_end();
1329+
delayacct_thrashing_end(&in_thrashing);
13291330
psi_memstall_leave(&pflags);
13301331
}
13311332

@@ -1374,12 +1375,13 @@ void migration_entry_wait_on_locked(swp_entry_t entry, pte_t *ptep,
13741375
wait_queue_entry_t *wait = &wait_page.wait;
13751376
bool thrashing = false;
13761377
unsigned long pflags;
1378+
bool in_thrashing;
13771379
wait_queue_head_t *q;
13781380
struct folio *folio = page_folio(pfn_swap_entry_to_page(entry));
13791381

13801382
q = folio_waitqueue(folio);
13811383
if (!folio_test_uptodate(folio) && folio_test_workingset(folio)) {
1382-
delayacct_thrashing_start();
1384+
delayacct_thrashing_start(&in_thrashing);
13831385
psi_memstall_enter(&pflags);
13841386
thrashing = true;
13851387
}
@@ -1426,7 +1428,7 @@ void migration_entry_wait_on_locked(swp_entry_t entry, pte_t *ptep,
14261428
finish_wait(q, wait);
14271429

14281430
if (thrashing) {
1429-
delayacct_thrashing_end();
1431+
delayacct_thrashing_end(&in_thrashing);
14301432
psi_memstall_leave(&pflags);
14311433
}
14321434
}

0 commit comments

Comments
 (0)