diff mbox

[v2] ceph: optimize mds session register

Message ID 1520953267-21288-1-git-send-email-cgxu519@gmx.com (mailing list archive)
State New, archived
Headers show

Commit Message

Chengguang Xu March 13, 2018, 3:01 p.m. UTC
Do memory allocation first, so that avoid unnecessary
initialization of newly allocated session in error case.

Signed-off-by: Chengguang Xu <cgxu519@gmx.com>
---

Changes since v1:
- mds number can't be bigger than newmax, so delete that check.
- kfree old session array after new one set to mdsc->sessions.
- add newly allocated session to session array ater initialization.
- modify commit log.

 fs/ceph/mds_client.c | 37 ++++++++++++++++++++-----------------
 1 file changed, 20 insertions(+), 17 deletions(-)

Comments

Yan, Zheng March 14, 2018, 7:31 a.m. UTC | #1
On Tue, Mar 13, 2018 at 11:01 PM, Chengguang Xu <cgxu519@gmx.com> wrote:
> Do memory allocation first, so that avoid unnecessary
> initialization of newly allocated session in error case.
>
> Signed-off-by: Chengguang Xu <cgxu519@gmx.com>
> ---
>
> Changes since v1:
> - mds number can't be bigger than newmax, so delete that check.
> - kfree old session array after new one set to mdsc->sessions.
> - add newly allocated session to session array ater initialization.
> - modify commit log.
>
>  fs/ceph/mds_client.c | 37 ++++++++++++++++++++-----------------
>  1 file changed, 20 insertions(+), 17 deletions(-)
>
> diff --git a/fs/ceph/mds_client.c b/fs/ceph/mds_client.c
> index e932c06..47c0d92 100644
> --- a/fs/ceph/mds_client.c
> +++ b/fs/ceph/mds_client.c
> @@ -468,6 +468,26 @@ static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
>         s = kzalloc(sizeof(*s), GFP_NOFS);
>         if (!s)
>                 return ERR_PTR(-ENOMEM);
> +
> +       if (mds >= mdsc->max_sessions) {
> +               int newmax = 1 << get_count_order(mds + 1);
> +               struct ceph_mds_session **sa;
> +               struct ceph_mds_session **old = mdsc->sessions;
> +
> +               dout("%s: realloc to %d\n", __func__, newmax);
> +               sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
> +               if (!sa)
> +                       goto fail_realloc;
> +
> +               if (old)
> +                       memcpy(sa, old, mdsc->max_sessions * sizeof(void *));
> +
> +               mdsc->sessions = sa;
> +               mdsc->max_sessions = newmax;
> +               kfree(old);
> +       }
> +
> +       dout("%s: mds%d\n", __func__, mds);
>         s->s_mdsc = mdsc;
>         s->s_mds = mds;
>         s->s_state = CEPH_MDS_SESSION_NEW;
> @@ -496,23 +516,6 @@ static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
>         INIT_LIST_HEAD(&s->s_cap_releases);
>         INIT_LIST_HEAD(&s->s_cap_flushing);
>
> -       dout("register_session mds%d\n", mds);
> -       if (mds >= mdsc->max_sessions) {
> -               int newmax = 1 << get_count_order(mds+1);
> -               struct ceph_mds_session **sa;
> -
> -               dout("register_session realloc to %d\n", newmax);
> -               sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
> -               if (!sa)
> -                       goto fail_realloc;
> -               if (mdsc->sessions) {
> -                       memcpy(sa, mdsc->sessions,
> -                              mdsc->max_sessions * sizeof(void *));
> -                       kfree(mdsc->sessions);
> -               }
> -               mdsc->sessions = sa;
> -               mdsc->max_sessions = newmax;
> -       }
>         mdsc->sessions[mds] = s;
>         atomic_inc(&mdsc->num_sessions);
>         refcount_inc(&s->s_ref);  /* one ref to sessions[], one to caller */
> --
> 1.8.3.1
>

Applied, thanks,

Yan, Zheng

> --
> To unsubscribe from this list: send the line "unsubscribe ceph-devel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
--
To unsubscribe from this list: send the line "unsubscribe ceph-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/fs/ceph/mds_client.c b/fs/ceph/mds_client.c
index e932c06..47c0d92 100644
--- a/fs/ceph/mds_client.c
+++ b/fs/ceph/mds_client.c
@@ -468,6 +468,26 @@  static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
 	s = kzalloc(sizeof(*s), GFP_NOFS);
 	if (!s)
 		return ERR_PTR(-ENOMEM);
+
+	if (mds >= mdsc->max_sessions) {
+		int newmax = 1 << get_count_order(mds + 1);
+		struct ceph_mds_session **sa;
+		struct ceph_mds_session **old = mdsc->sessions;
+
+		dout("%s: realloc to %d\n", __func__, newmax);
+		sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
+		if (!sa)
+			goto fail_realloc;
+
+		if (old)
+			memcpy(sa, old, mdsc->max_sessions * sizeof(void *));
+
+		mdsc->sessions = sa;
+		mdsc->max_sessions = newmax;
+		kfree(old);
+	}
+
+	dout("%s: mds%d\n", __func__, mds);
 	s->s_mdsc = mdsc;
 	s->s_mds = mds;
 	s->s_state = CEPH_MDS_SESSION_NEW;
@@ -496,23 +516,6 @@  static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
 	INIT_LIST_HEAD(&s->s_cap_releases);
 	INIT_LIST_HEAD(&s->s_cap_flushing);
 
-	dout("register_session mds%d\n", mds);
-	if (mds >= mdsc->max_sessions) {
-		int newmax = 1 << get_count_order(mds+1);
-		struct ceph_mds_session **sa;
-
-		dout("register_session realloc to %d\n", newmax);
-		sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
-		if (!sa)
-			goto fail_realloc;
-		if (mdsc->sessions) {
-			memcpy(sa, mdsc->sessions,
-			       mdsc->max_sessions * sizeof(void *));
-			kfree(mdsc->sessions);
-		}
-		mdsc->sessions = sa;
-		mdsc->max_sessions = newmax;
-	}
 	mdsc->sessions[mds] = s;
 	atomic_inc(&mdsc->num_sessions);
 	refcount_inc(&s->s_ref);  /* one ref to sessions[], one to caller */