Skip to contentSkip to conversation

gfs2: Add missing set_freezable() for freezable kthread

PATCH In review Kevin Hao 2 participants 4 messages Dec 18, 2023 – Dec 21, 2023 GFS2 file system

lore
Share & tools
lore

Kevin Hao

author
#0 lore raw

PATCH 1/2 gfs2: Add missing set_freezable() for freezable kthread

The kernel thread function gfs2_logd() and gfs2_quotad() invoke the try_to_freeze() in its loop. But all the kernel threads are no-freezable by default. So if we want to make a kernel thread to be freezable, we have to invoke set_freezable() explicitly.

---
 fs/gfs2/log.c   | 1 +
 fs/gfs2/quota.c | 1 +
 2 files changed, 2 insertions(+)
fs/gfs2/log.c +1 -0
index e5271ae87d1c..a268b69bb636 100644
@@ -1297,6 +1297,7 @@ int gfs2_logd(void *data)
 	struct gfs2_sbd *sdp = data;
 	unsigned long t = 1;
 
+	set_freezable();
 	while (!kthread_should_stop()) {
 		if (gfs2_withdrawn(sdp))
 			break;
fs/gfs2/quota.c +1 -0
index fa47824acf13..173581736a6e 100644
@@ -1583,6 +1583,7 @@ int gfs2_quotad(void *data)
 	unsigned long quotad_timeo = 0;
 	unsigned long t = 0;
 
+	set_freezable();
 	while (!kthread_should_stop()) {
 		if (gfs2_withdrawn(sdp))
 			break;
Signature · 1 line
2.39.2
author
#1 lore raw

PATCH 2/2 gfs2: Use wait_event_freezable_timeout() for freezable kthread

A freezable kernel thread can enter frozen state during freezing by either calling try_to_freeze() or using wait_event_freezable() and its variants. So for the following snippet of code in a kernel thread loop:
try_to_freeze();
wait_event_interruptible_timeout();

We can change it to a simple wait_event_freezable_timeout() and then eliminate a function call.

---
 fs/gfs2/log.c   | 4 +---
 fs/gfs2/quota.c | 4 +---
 2 files changed, 2 insertions(+), 6 deletions(-)
fs/gfs2/log.c +1 -3
index a268b69bb636..20059fc913e7 100644
@@ -1331,9 +1331,7 @@ int gfs2_logd(void *data)
 
 		t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
 
-		try_to_freeze();
-
-		t = wait_event_interruptible_timeout(sdp->sd_logd_waitq,
+		t = wait_event_freezable_timeout(sdp->sd_logd_waitq,
 				test_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags) ||
 				gfs2_ail_flush_reqd(sdp) ||
 				gfs2_jrnl_flush_reqd(sdp) ||
fs/gfs2/quota.c +1 -3
index 173581736a6e..05ef8409b614 100644
@@ -1603,11 +1603,9 @@ int gfs2_quotad(void *data)
 		quotad_check_timeo(sdp, "sync", gfs2_quota_sync, t,
 				   &quotad_timeo, &tune->gt_quota_quantum);
 
-		try_to_freeze();
-
 		t = min(quotad_timeo, statfs_timeo);
 
-		t = wait_event_interruptible_timeout(sdp->sd_quota_wait,
+		t = wait_event_freezable_timeout(sdp->sd_quota_wait,
 				sdp->sd_statfs_force_sync ||
 				gfs2_withdrawn(sdp) ||
 				kthread_should_stop(),
Signature · 1 line
2.39.2
#2 lore raw

Re: [PATCH 2/2] gfs2: Use wait_event_freezable_timeout() for freezable kthread

Kevin,

Quoted 48 lines · Kevin Hao

A freezable kernel thread can enter frozen state during freezing by either calling try_to_freeze() or using wait_event_freezable() and its variants. So for the following snippet of code in a kernel thread loop:
try_to_freeze();
wait_event_interruptible_timeout();

We can change it to a simple wait_event_freezable_timeout() and then eliminate a function call.

---
 fs/gfs2/log.c   | 4 +---
 fs/gfs2/quota.c | 4 +---
 2 files changed, 2 insertions(+), 6 deletions(-)
fs/gfs2/log.c +1 -3
index a268b69bb636..20059fc913e7 100644
@@ -1331,9 +1331,7 @@ int gfs2_logd(void *data)
 
                t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
 
-               try_to_freeze();
-
-               t = wait_event_interruptible_timeout(sdp->sd_logd_waitq,
+               t = wait_event_freezable_timeout(sdp->sd_logd_waitq,
                                test_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags) ||
                                gfs2_ail_flush_reqd(sdp) ||
                                gfs2_jrnl_flush_reqd(sdp) ||
fs/gfs2/quota.c +1 -4
index 173581736a6e..05ef8409b614 100644
@@ -1603,11 +1603,9 @@ int gfs2_quotad(void *data)
                quotad_check_timeo(sdp, "sync", gfs2_quota_sync, t,
                                   &quotad_timeo, &tune->gt_quota_quantum);
 
-               try_to_freeze();
-
                t = min(quotad_timeo, statfs_timeo);
 
-               t = wait_event_interruptible_timeout(sdp->sd_quota_wait,
+               t = wait_event_freezable_timeout(sdp->sd_quota_wait,
                                sdp->sd_statfs_force_sync ||
                                gfs2_withdrawn(sdp) ||
                                kthread_should_stop(),
--
Quoted 2 lines

2.39.2

This also is looking good; applied as well.

Thank you,
Andreas

#3 lore raw
In reply to Kevin Hao

Kevin,

Quoted 36 lines · Kevin Hao

The kernel thread function gfs2_logd() and gfs2_quotad() invoke the try_to_freeze() in its loop. But all the kernel threads are no-freezable by default. So if we want to make a kernel thread to be freezable, we have to invoke set_freezable() explicitly.

---
 fs/gfs2/log.c   | 1 +
 fs/gfs2/quota.c | 1 +
 2 files changed, 2 insertions(+)
fs/gfs2/log.c +1 -0
index e5271ae87d1c..a268b69bb636 100644
@@ -1297,6 +1297,7 @@ int gfs2_logd(void *data)
        struct gfs2_sbd *sdp = data;
        unsigned long t = 1;
 
+       set_freezable();
        while (!kthread_should_stop()) {
                if (gfs2_withdrawn(sdp))
                        break;
fs/gfs2/quota.c +1 -1
index fa47824acf13..173581736a6e 100644
@@ -1583,6 +1583,7 @@ int gfs2_quotad(void *data)
        unsigned long quotad_timeo = 0;
        unsigned long t = 0;
 
+       set_freezable();
        while (!kthread_should_stop()) {
                if (gfs2_withdrawn(sdp))
                        break;
--
Quoted 2 lines

2.39.2

This is looking good, thank you. I've applied it to our for-next branch.

Andreas