linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] delayacct: support re-entrance detection of thrashing accounting
@ 2022-08-13  7:41 cgel.zte
  2022-08-13 23:23 ` Andrew Morton
  0 siblings, 1 reply; 2+ messages in thread
From: cgel.zte @ 2022-08-13  7:41 UTC (permalink / raw)
  To: bsingharora, akpm, iamjoonsoo.kim
  Cc: mingo, bristot, vschneid, willy, linux-kernel, linux-fsdevel,
	linux-mm, Yang Yang, CGEL ZTE, Ran Xiaokai, wangyong

From: Yang Yang <yang.yang29@zte.com.cn>

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 aae466b0052e ("mm/swap: implement workingset detection for anonymous LRU")

Signed-off-by: Yang Yang <yang.yang29@zte.com.cn>
Signed-off-by: CGEL ZTE <cgel.zte@gmail.com>
Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
Reviewed-by: Ran Xiaokai <ran.xiaokai@zte.com.cn>
Reviewed-by: wangyong <wang.yong12@zte.com.cn>
---
 include/linux/delayacct.h | 16 ++++++++--------
 include/linux/sched.h     |  4 ++++
 kernel/delayacct.c        | 13 +++++++++++--
 mm/filemap.c              | 10 ++++++----
 4 files changed, 29 insertions(+), 14 deletions(-)

diff --git a/include/linux/delayacct.h b/include/linux/delayacct.h
index 58aea2d7385c..aeb76f1c894e 100644
--- a/include/linux/delayacct.h
+++ b/include/linux/delayacct.h
@@ -73,8 +73,8 @@ extern int delayacct_add_tsk(struct taskstats *, struct task_struct *);
 extern __u64 __delayacct_blkio_ticks(struct task_struct *);
 extern void __delayacct_freepages_start(void);
 extern void __delayacct_freepages_end(void);
-extern void __delayacct_thrashing_start(void);
-extern void __delayacct_thrashing_end(void);
+extern void __delayacct_thrashing_start(unsigned long *flags);
+extern void __delayacct_thrashing_end(unsigned long *flags);
 extern void __delayacct_swapin_start(void);
 extern void __delayacct_swapin_end(void);
 extern void __delayacct_compact_start(void);
@@ -143,22 +143,22 @@ static inline void delayacct_freepages_end(void)
 		__delayacct_freepages_end();
 }
 
-static inline void delayacct_thrashing_start(void)
+static inline void delayacct_thrashing_start(unsigned long *flags)
 {
 	if (!static_branch_unlikely(&delayacct_key))
 		return;
 
 	if (current->delays)
-		__delayacct_thrashing_start();
+		__delayacct_thrashing_start(flags);
 }
 
-static inline void delayacct_thrashing_end(void)
+static inline void delayacct_thrashing_end(unsigned long *flags)
 {
 	if (!static_branch_unlikely(&delayacct_key))
 		return;
 
 	if (current->delays)
-		__delayacct_thrashing_end();
+		__delayacct_thrashing_end(flags);
 }
 
 static inline void delayacct_swapin_start(void)
@@ -237,9 +237,9 @@ static inline void delayacct_freepages_start(void)
 {}
 static inline void delayacct_freepages_end(void)
 {}
-static inline void delayacct_thrashing_start(void)
+static inline void delayacct_thrashing_start(unsigned long *flags)
 {}
-static inline void delayacct_thrashing_end(void)
+static inline void delayacct_thrashing_end(unsigned long *flags)
 {}
 static inline void delayacct_swapin_start(void)
 {}
diff --git a/include/linux/sched.h b/include/linux/sched.h
index d6b0866c71ed..5fb942e29583 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -943,6 +943,10 @@ struct task_struct {
 #ifdef	CONFIG_CPU_SUP_INTEL
 	unsigned			reported_split_lock:1;
 #endif
+#ifdef CONFIG_TASK_DELAY_ACCT
+	/* delay due to memory thrashing */
+	unsigned                        in_thrashing:1;
+#endif
 
 	unsigned long			atomic_flags; /* Flags requiring atomic access. */
 
diff --git a/kernel/delayacct.c b/kernel/delayacct.c
index 164ed9ef77a3..a5916196022f 100644
--- a/kernel/delayacct.c
+++ b/kernel/delayacct.c
@@ -214,13 +214,22 @@ void __delayacct_freepages_end(void)
 		      &current->delays->freepages_count);
 }
 
-void __delayacct_thrashing_start(void)
+void __delayacct_thrashing_start(unsigned long *flags)
 {
+	*flags = current->in_thrashing;
+	if (*flags)
+		return;
+
+	current->in_thrashing = 1;
 	current->delays->thrashing_start = local_clock();
 }
 
-void __delayacct_thrashing_end(void)
+void __delayacct_thrashing_end(unsigned long *flags)
 {
+	if (*flags)
+		return;
+
+	current->in_thrashing = 0;
 	delayacct_end(&current->delays->lock,
 		      &current->delays->thrashing_start,
 		      &current->delays->thrashing_delay,
diff --git a/mm/filemap.c b/mm/filemap.c
index cfe15e89b3c2..92ceebc75900 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -1222,10 +1222,11 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,
 	wait_queue_entry_t *wait = &wait_page.wait;
 	bool thrashing = false;
 	unsigned long pflags;
+	unsigned long dflags;
 
 	if (bit_nr == PG_locked &&
 	    !folio_test_uptodate(folio) && folio_test_workingset(folio)) {
-		delayacct_thrashing_start();
+		delayacct_thrashing_start(&dflags);
 		psi_memstall_enter(&pflags);
 		thrashing = true;
 	}
@@ -1325,7 +1326,7 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,
 	finish_wait(q, wait);
 
 	if (thrashing) {
-		delayacct_thrashing_end();
+		delayacct_thrashing_end(&dflags);
 		psi_memstall_leave(&pflags);
 	}
 
@@ -1374,12 +1375,13 @@ void migration_entry_wait_on_locked(swp_entry_t entry, pte_t *ptep,
 	wait_queue_entry_t *wait = &wait_page.wait;
 	bool thrashing = false;
 	unsigned long pflags;
+	unsigned long dflags;
 	wait_queue_head_t *q;
 	struct folio *folio = page_folio(pfn_swap_entry_to_page(entry));
 
 	q = folio_waitqueue(folio);
 	if (!folio_test_uptodate(folio) && folio_test_workingset(folio)) {
-		delayacct_thrashing_start();
+		delayacct_thrashing_start(&dflags);
 		psi_memstall_enter(&pflags);
 		thrashing = true;
 	}
@@ -1426,7 +1428,7 @@ void migration_entry_wait_on_locked(swp_entry_t entry, pte_t *ptep,
 	finish_wait(q, wait);
 
 	if (thrashing) {
-		delayacct_thrashing_end();
+		delayacct_thrashing_end(&dflags);
 		psi_memstall_leave(&pflags);
 	}
 }
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH 1/2] delayacct: support re-entrance detection of thrashing accounting
  2022-08-13  7:41 [PATCH 1/2] delayacct: support re-entrance detection of thrashing accounting cgel.zte
@ 2022-08-13 23:23 ` Andrew Morton
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew Morton @ 2022-08-13 23:23 UTC (permalink / raw)
  To: cgel.zte
  Cc: bsingharora, iamjoonsoo.kim, mingo, bristot, vschneid, willy,
	linux-kernel, linux-fsdevel, linux-mm, Yang Yang, Ran Xiaokai,
	wangyong

On Sat, 13 Aug 2022 07:41:09 +0000 cgel.zte@gmail.com wrote:

> From: Yang Yang <yang.yang29@zte.com.cn>
> 
> 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".
> 
> -static inline void delayacct_thrashing_start(void)
> +static inline void delayacct_thrashing_start(unsigned long *flags)

I don't think that `flags' is a very descriptive name.  It might be
anything.  

> --- a/include/linux/sched.h
> +++ b/include/linux/sched.h
> @@ -943,6 +943,10 @@ struct task_struct {
>  #ifdef	CONFIG_CPU_SUP_INTEL
>  	unsigned			reported_split_lock:1;
>  #endif
> +#ifdef CONFIG_TASK_DELAY_ACCT
> +	/* delay due to memory thrashing */
> +	unsigned                        in_thrashing:1;
> +#endif

OK, saving space in the task_struct is good.

>  	unsigned long			atomic_flags; /* Flags requiring atomic access. */
>  
> diff --git a/kernel/delayacct.c b/kernel/delayacct.c
> index 164ed9ef77a3..a5916196022f 100644
> --- a/kernel/delayacct.c
> +++ b/kernel/delayacct.c
> @@ -214,13 +214,22 @@ void __delayacct_freepages_end(void)
>  		      &current->delays->freepages_count);
>  }
>  
> -void __delayacct_thrashing_start(void)
> +void __delayacct_thrashing_start(unsigned long *flags)
>  {
> +	*flags = current->in_thrashing;
> +	if (*flags)
> +		return;

Can't we rename `flags' to `in_thrashing' throughout?

And may as well give it type `bool'.  And convert that bool to/from the
bitfield when moving it in and out of the task_struct with

	*in_thrashing = !!current->in_thrashing;

	current->in_thrashing = (in_thrashing != 0);




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-08-13 23:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-13  7:41 [PATCH 1/2] delayacct: support re-entrance detection of thrashing accounting cgel.zte
2022-08-13 23:23 ` Andrew Morton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).