Skip to contentSkip to conversation

ceph: fix memory in MDS client cap_auths

PATCH Superseded Luis Henriques (SUSE) 2 participants 3 messages Aug 14, 2024 – Aug 19, 2024 Ceph client

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ceph: fix memory in MDS client cap_auths

The cap_auths that are allocated during an MDS session opening are never released, causing a memory leak detected by kmemleak. Fix this by freeing the memory allocated when shutting down the mds client.

Fixes 1d17de9534cb ("ceph: save cap_auths in MDS client when session is opened")
---
 fs/ceph/mds_client.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
fs/ceph/mds_client.c +14 -0
index 276e34ab3e2c..d798d0a5b2b1 100644
@@ -6015,6 +6015,20 @@ static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
 		ceph_mdsmap_destroy(mdsc->mdsmap);
 	kfree(mdsc->sessions);
 	ceph_caps_finalize(mdsc);
+
+	if (mdsc->s_cap_auths) {
+		int i;
+
+		mutex_lock(&mdsc->mutex);
+		for (i = 0; i < mdsc->s_cap_auths_num; i++) {
+			kfree(mdsc->s_cap_auths[i].match.gids);
+			kfree(mdsc->s_cap_auths[i].match.path);
+			kfree(mdsc->s_cap_auths[i].match.fs_name);
+		}
+		kfree(mdsc->s_cap_auths);
+		mutex_unlock(&mdsc->mutex);
+	}
+
 	ceph_pool_perm_destroy(mdsc);
 }
 
#1 lore raw

Hi Luis,

Good catch!

Quoted 15 lines · Luis Henriques (SUSE)

The cap_auths that are allocated during an MDS session opening are never released, causing a memory leak detected by kmemleak. Fix this by freeing the memory allocated when shutting down the mds client.

Fixes 1d17de9534cb ("ceph: save cap_auths in MDS client when session is opened")
---
  fs/ceph/mds_client.c | 14 ++++++++++++++
  1 file changed, 14 insertions(+)
fs/ceph/mds_client.c +0 -0
index 276e34ab3e2c..d798d0a5b2b1 100644

fs/ceph/mds_client.c static void ceph_mdsc_stop(struct ceph_mds_client *mdsc) · line 6022

Quoted 8 lines
fs/ceph/mds_client.c +5 -0
@@ -6015,6 +6015,20 @@ static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
  		ceph_mdsmap_destroy(mdsc->mdsmap);
  	kfree(mdsc->sessions);
  	ceph_caps_finalize(mdsc);
+
+	if (mdsc->s_cap_auths) {
+		int i;
+
+		mutex_lock(&mdsc->mutex);

BTW, is the lock really needed here ?

IMO it should be safe to remove it because once here the sb has already been killed and the 'mdsc->stopping' will help guarantee that there won't be any other thread will access to 'mdsc', Isn't it ?

Else we need to do the lock from the beginning of this function.

Thanks

- Xiubo

Quoted 13 lines
fs/ceph/mds_client.c +9 -0
+		for (i = 0; i < mdsc->s_cap_auths_num; i++) {
+			kfree(mdsc->s_cap_auths[i].match.gids);
+			kfree(mdsc->s_cap_auths[i].match.path);
+			kfree(mdsc->s_cap_auths[i].match.fs_name);
+		}
+		kfree(mdsc->s_cap_auths);
+		mutex_unlock(&mdsc->mutex);
+	}
+
  	ceph_pool_perm_destroy(mdsc);
  }
 
#2 lore raw
Quoted 31 lines · Xiubo Li

Hi Luis,

Good catch!

Quoted 15 lines · Luis Henriques (SUSE)

The cap_auths that are allocated during an MDS session opening are never released, causing a memory leak detected by kmemleak. Fix this by freeing the memory allocated when shutting down the mds client.

Fixes 1d17de9534cb ("ceph: save cap_auths in MDS client when session is opened")
---
  fs/ceph/mds_client.c | 14 ++++++++++++++
  1 file changed, 14 insertions(+)
fs/ceph/mds_client.c +0 -0
index 276e34ab3e2c..d798d0a5b2b1 100644

fs/ceph/mds_client.c static void ceph_mdsc_stop(struct ceph_mds_client *mdsc) · line 6022

Quoted 8 lines
fs/ceph/mds_client.c +5 -0
@@ -6015,6 +6015,20 @@ static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
  		ceph_mdsmap_destroy(mdsc->mdsmap);
  	kfree(mdsc->sessions);
  	ceph_caps_finalize(mdsc);
+
+	if (mdsc->s_cap_auths) {
+		int i;
+
+		mutex_lock(&mdsc->mutex);

BTW, is the lock really needed here ?

Quoted 3 lines

IMO it should be safe to remove it because once here the sb has already been killed and the 'mdsc->stopping' will help guarantee that there won't be any other thread will access to 'mdsc', Isn't it ?

Ah, yes, good point. Thanks, I'll send v2 shortly.

Cheers,

Signature · 1 line
Luís
Quoted 19 lines

Else we need to do the lock from the beginning of this function.

Thanks

- Xiubo

Quoted 12 lines
fs/ceph/mds_client.c +9 -0
+		for (i = 0; i < mdsc->s_cap_auths_num; i++) {
+			kfree(mdsc->s_cap_auths[i].match.gids);
+			kfree(mdsc->s_cap_auths[i].match.path);
+			kfree(mdsc->s_cap_auths[i].match.fs_name);
+		}
+		kfree(mdsc->s_cap_auths);
+		mutex_unlock(&mdsc->mutex);
+	}
+
  	ceph_pool_perm_destroy(mdsc);
  }