From patchwork Tue Apr 17 10:15:40 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengguang Xu X-Patchwork-Id: 10344667 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 08BC460365 for ; Tue, 17 Apr 2018 10:16:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EBD2B28A42 for ; Tue, 17 Apr 2018 10:16:07 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E05FF28A44; Tue, 17 Apr 2018 10:16:07 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,FREEMAIL_FROM, MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 595D528A42 for ; Tue, 17 Apr 2018 10:16:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752082AbeDQKQF (ORCPT ); Tue, 17 Apr 2018 06:16:05 -0400 Received: from mout.gmx.net ([212.227.15.19]:35645 "EHLO mout.gmx.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751810AbeDQKQE (ORCPT ); Tue, 17 Apr 2018 06:16:04 -0400 Received: from juanniu018037.ss.mogujie.org ([122.225.81.134]) by mail.gmx.com (mrgmx002 [212.227.17.184]) with ESMTPSA (Nemesis) id 0M5Lmp-1eEMoU1NR1-00zSsq; Tue, 17 Apr 2018 12:15:58 +0200 From: Chengguang Xu To: idryomov@gmail.com, zyan@redhat.com Cc: ceph-devel@vger.kernel.org, Chengguang Xu Subject: [PATCH] libceph: optimize ceph_msg_new Date: Tue, 17 Apr 2018 18:15:40 +0800 Message-Id: <1523960140-86687-1-git-send-email-cgxu519@gmx.com> X-Mailer: git-send-email 1.8.3.1 X-Provags-ID: V03:K1:eXBjBAU5liHDuk1qGK8GJwuoQmGF+Hwgj5uRKWbVb/eXZMF63zu DyTpsjn8/4zXdHux/ziJW1PROa3ZVYl7HYhWY23hwG2RVLDsRhd9SpVuSlG6wbMp9BRDbRI g/DvB2mBBSCurZ7VA0nzTbYFuMxnswRZiInos6Nb4a74klYLZDva0BKtxPmIrARqa1C05xn DseBn8yIz66r5dTIJy7Jw== X-UI-Out-Filterresults: notjunk:1; V01:K0:5DigAGhxzsg=:qmeD76TrRtywXGJgPM18wf 2UzDinFFVtCW0PdKqrQAnIE/ge5XWhb7ffVj7GULfoRRe5E9jdbdEiigudzmm/O/rMui6xAgu +eewFLXx0UUOWgngcpKn/+rS0fD9l7A4xUjB5B0W72HBMeN1yYxZciUn85zncBiUMgkGQaRn7 xhH5SrhDk2f6ktSyLwfradZBNUYBex4IWmYN8JzPSquCel6DbndRaK0fpG2SInSfgx+OM9vPt 33G9SE/OAeRflwoWOAGuLCJSGZxNv43jK6oQMmt4eM1uaezg63ccgGWs9AwbFzZvOTlqzIEQ9 mvLre5J70JQhhMhlP4awPaQkCjKvdOMvOpkguI0ACC0kjJheujHvLa1oxhRxLaZXrU3LBPQgJ buec2dBNwFKRXz9sJhN359Q01e7tWP8zW0/CohAW9fWXiCUpahg935d4DFgoZ3N1N7/fAkS7u BWDfTFrVvmlbU9BuaBTEDLUzL3wmx9beBjfHozTWRQ/BcFIWaUiKWHTkxMVuuej86N9sxvymZ KVBdlPpseFcPAvP/L80BPZqRsgb22SuwS0jU2ymLopjkJAUtEmo4VwaJeTxyt8Uoo7oDElkH2 NUgDlFcXkYa+RCA/Z0lXPl2f/DwzrMrGI+GZIWi4PIEhDSTzMlxODsqxoAwKVJjCLibn9iyZG y/vRHbsPhRMkWD+UQDepX5m4vLS+pXSC+25Y+kWrJ3l+W2x7nUQ0KEFZk+lQ906WhsPhlb4M0 uRca0CtXyNGvtw99hG+LV067L0CHoYXv0tC8fbGp67Klg9Og0tSq8hNDnV8+NwRrNN8TBUqWA o0Mf08A2wCR1RFr4pOlHHNpGBqUZg== Sender: ceph-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Do memory allocation first, so that avoid unnecessary initialization of newly allocated message in error case. Signed-off-by: Chengguang Xu --- net/ceph/messenger.c | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/net/ceph/messenger.c b/net/ceph/messenger.c index fcb40c1..5a8c5cd 100644 --- a/net/ceph/messenger.c +++ b/net/ceph/messenger.c @@ -3331,6 +3331,16 @@ void ceph_msg_data_add_bvecs(struct ceph_msg *msg, EXPORT_SYMBOL(ceph_msg_data_add_bvecs); /* + * Free a generically kmalloc'd message. + */ +static void ceph_msg_free(struct ceph_msg *m) +{ + dout("%s %p\n", __func__, m); + kvfree(m->front.iov_base); + kmem_cache_free(ceph_msg_cache, m); +} + +/* * construct a new message with given type, size * the new msg has a ref count of 1. */ @@ -3343,14 +3353,6 @@ struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags, if (m == NULL) goto out; - m->hdr.type = cpu_to_le16(type); - m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT); - m->hdr.front_len = cpu_to_le32(front_len); - - INIT_LIST_HEAD(&m->list_head); - kref_init(&m->kref); - INIT_LIST_HEAD(&m->data); - /* front */ if (front_len) { m->front.iov_base = ceph_kvmalloc(front_len, flags); @@ -3359,16 +3361,23 @@ struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags, front_len); goto out2; } - } else { - m->front.iov_base = NULL; } + + m->hdr.type = cpu_to_le16(type); + m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT); + m->hdr.front_len = cpu_to_le32(front_len); + + INIT_LIST_HEAD(&m->list_head); + kref_init(&m->kref); + INIT_LIST_HEAD(&m->data); + m->front_alloc_len = m->front.iov_len = front_len; dout("ceph_msg_new %p front %d\n", m, front_len); return m; out2: - ceph_msg_put(m); + ceph_msg_free(m); out: if (!can_fail) { pr_err("msg_new can't create type %d front %d\n", type, @@ -3467,17 +3476,6 @@ static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip) return ret; } - -/* - * Free a generically kmalloc'd message. - */ -static void ceph_msg_free(struct ceph_msg *m) -{ - dout("%s %p\n", __func__, m); - kvfree(m->front.iov_base); - kmem_cache_free(ceph_msg_cache, m); -} - static void ceph_msg_release(struct kref *kref) { struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);