Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp4635953imm; Mon, 14 May 2018 10:18:50 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqmFQ8Qp4I0l2ZbuKOP35lvGVr2gnmzo1sJu9qSeLUtDTxQkQeGSn0c1aDRPrNksA1qkd2H X-Received: by 2002:a17:902:b585:: with SMTP id a5-v6mr10948933pls.53.1526318330388; Mon, 14 May 2018 10:18:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526318330; cv=none; d=google.com; s=arc-20160816; b=tnmrVd1Q09zVn613kS+wXQgIfC1mDjMlubT9V2smj3tAtTbYVjtCRyID6Y1UQX+C4N xSow+Yr+gkuTnu5p6YAt3LrnrqeVUua7k9OWMMNOfjv3miUHIyb+VGcnwacuw50878fo fIBGatkTF57AVK4Ez6MpCJrUJaGGzrchgJllq7S3W555EOWHbgyPVwWjzhLK8VSg/K/0 hdtJ8urH/apazOtmaM1VmEgC4q9L4l0+KS6GppZQ8GhHqM8shEoPf3GYVp/HPW4FiQDG QKvmLWwqzHLg2E5G/cMtFjvckyDTDa7otCbq2viwgrogYJIW+5hv1HuhiHU3R/qVxEPY +SLg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :arc-authentication-results; bh=F4m4T7Er92CVBUM09DbiMDq2GZCs4pZRqtRyst+/pZY=; b=Xv2Eta0mCfXrwun/qSwX/+NHgkpDflCPXbtK2SEJkoxsI58NnU5zHhDdFqvI0xpeOq D5W1wrqRi1a9ZEKH8HONI84vFuzKRoJYF17KWuuJLT+4DaK+JUAUOCK+3xF/NTOx7Ron GwdH94FtuPli8YW+ln5AwkkF+FSy62oxHaV3JK+BWnKs+TRzqtb3k1a4XuiNGRNQHZZZ Wllbc+ic9Roui5GV2phuaQNWGNEOMglM3HRMlOSEkDoalawDDnwtGIeRl+d+D+0VYDTD 5soqX99H0gFvaG4TtQuPzfRfF+dLaAvncA6FRFY8xuVjTizoliJ8oHVfBgkLYghiSIBl cvxA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e1-v6si7923864pgr.167.2018.05.14.10.18.35; Mon, 14 May 2018 10:18:50 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754553AbeENP3p (ORCPT + 99 others); Mon, 14 May 2018 11:29:45 -0400 Received: from smtp2.provo.novell.com ([137.65.250.81]:41162 "EHLO smtp2.provo.novell.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752118AbeENP3n (ORCPT ); Mon, 14 May 2018 11:29:43 -0400 Received: from linux-n805.suse.de (prv-ext-foundry1int.gns.novell.com [137.65.251.240]) by smtp2.provo.novell.com with ESMTP (TLS encrypted); Mon, 14 May 2018 09:29:37 -0600 From: Davidlohr Bueso To: akpm@linux-foundation.org, tgraf@suug.ch, herbert@gondor.apana.org.au Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, dave@stgolabs.net, Davidlohr Bueso Subject: [PATCH] lib/rhashtable: reorder some inititalization sequences Date: Mon, 14 May 2018 08:13:32 -0700 Message-Id: <20180514151332.31352-1-dave@stgolabs.net> X-Mailer: git-send-email 2.13.6 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org rhashtable_init() allocates memory at the very end of the call, once everything is setup; with the exception of the nelems parameter. However, unless the user is doing something bogus with params for which -EINVAL is returned, memory allocation is the only operation that can trigger the call to fail. Thus move bucket_table_alloc() up such that we fail back to the caller asap, instead of doing useless checks. This is safe as the the table allocation isn't using the halfly setup 'ht' structure and bucket_table_alloc() call chain only ends up using the ht->nulls_base member in INIT_RHT_NULLS_HEAD. Also move the locking initialization down to the end. Signed-off-by: Davidlohr Bueso --- lib/rhashtable.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/rhashtable.c b/lib/rhashtable.c index 9427b5766134..68aadd6bff60 100644 --- a/lib/rhashtable.c +++ b/lib/rhashtable.c @@ -1022,8 +1022,6 @@ int rhashtable_init(struct rhashtable *ht, struct bucket_table *tbl; size_t size; - size = HASH_DEFAULT_SIZE; - if ((!params->key_len && !params->obj_hashfn) || (params->obj_hashfn && !params->obj_cmpfn)) return -EINVAL; @@ -1032,10 +1030,17 @@ int rhashtable_init(struct rhashtable *ht, return -EINVAL; memset(ht, 0, sizeof(*ht)); - mutex_init(&ht->mutex); - spin_lock_init(&ht->lock); memcpy(&ht->p, params, sizeof(*params)); + if (!params->nelem_hint) + size = HASH_DEFAULT_SIZE; + else + size = rounded_hashtable_size(&ht->p); + + tbl = bucket_table_alloc(ht, size, GFP_KERNEL); + if (tbl == NULL) + return -ENOMEM; + if (params->min_size) ht->p.min_size = roundup_pow_of_two(params->min_size); @@ -1050,9 +1055,6 @@ int rhashtable_init(struct rhashtable *ht, ht->p.min_size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE); - if (params->nelem_hint) - size = rounded_hashtable_size(&ht->p); - if (params->locks_mul) ht->p.locks_mul = roundup_pow_of_two(params->locks_mul); else @@ -1068,10 +1070,8 @@ int rhashtable_init(struct rhashtable *ht, } } - tbl = bucket_table_alloc(ht, size, GFP_KERNEL); - if (tbl == NULL) - return -ENOMEM; - + mutex_init(&ht->mutex); + spin_lock_init(&ht->lock); atomic_set(&ht->nelems, 0); RCU_INIT_POINTER(ht->tbl, tbl); -- 2.13.6