Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp760572imm; Fri, 1 Jun 2018 09:04:27 -0700 (PDT) X-Google-Smtp-Source: ADUXVKLfy8sJvi2vKdqV/utPhLRmBHtvjnfUhqNGdJ+JS0WrofwOPswSBFYFoNBv3LR4CRuHRgFZ X-Received: by 2002:a17:902:14d:: with SMTP id 71-v6mr11656537plb.275.1527869067871; Fri, 01 Jun 2018 09:04:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527869067; cv=none; d=google.com; s=arc-20160816; b=nTEjyo9omQ5ITRHFMMSyeZBttUqiJZKKJNE2Wb0dDmczHSYXRAq5tL/4oACWIdVR0E JWQ2OU6qlUt0ZNclSWIga8P09xvRRrrNeJOAQxGv3+tRzr86RTR2LPJqUVszP8TMiGKG HED9tESDvz08xf1ioWUcLXdC9V5jhKrFJRuhInTPTyu0Q4a0/f/rjWDh6xea49vMjzew kQtNZzNVozdEukb3w/cmkCPeyXvM6CIvH996BbEGcRanMvDjUFu+zKY+RXPBRw7uEL0j xhjhKAtSvQWltJ7aoUqDc1c9qDF+0zaaCzO4GtKYIvRVl7UwaPS4tiv0c76vT6k9kdEe RZhw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=gdfmcVUmv8zFz5d6xC9EHgpE8bjtLIZikTkbnxDeEU4=; b=RW4lWSuRA2cPUhv2KsNzXNIGsr3fKKBY/Ew34JQLzUsirGnP/SFAUeH8/xpfAnfBYl YfbvRDvBVL7eQjtyaI69kPWij6S+wy8PDnwIWXAM2lCwIG8fbvUCQ8QHL2lKuAPS8Ca2 izcmfcWZJZiAe2JcML+pKRhsQV9MA11G9+wrCIk+LS6JHwH6jLlmOMBjpEspcV7rmU87 Q5ImEAfUNwba4KP8BXWJgXcZFJvX+ZGv88PgjVUMC2Q7Qqv6+WOT3+fMHwonYvDApNP0 S+EuBQY7dbxgMED6mcsTj3ZuirCIpAuxm9jR57GT/A60a+/SBjhBArVD+yT9l/kTyoyB KG2Q== 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 v11-v6si31287190plo.130.2018.06.01.09.04.11; Fri, 01 Jun 2018 09:04:27 -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 S1753236AbeFAQDI (ORCPT + 99 others); Fri, 1 Jun 2018 12:03:08 -0400 Received: from smtp2.provo.novell.com ([137.65.250.81]:47664 "EHLO smtp2.provo.novell.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752113AbeFAQCF (ORCPT ); Fri, 1 Jun 2018 12:02:05 -0400 Received: from localhost.localdomain (prv-ext-foundry1int.gns.novell.com [137.65.251.240]) by smtp2.provo.novell.com with ESMTP (TLS encrypted); Fri, 01 Jun 2018 10:01:59 -0600 From: Davidlohr Bueso To: akpm@linux-foundation.org, torvalds@linux-foundation.org Cc: tgraf@suug.ch, herbert@gondor.apana.org.au, manfred@colorfullife.com, mhocko@kernel.org, guillaume.knispel@supersonicimagine.com, linux-api@vger.kernel.org, linux-kernel@vger.kernel.org, Davidlohr Bueso , Davidlohr Bueso Subject: [PATCH 2/5] lib/rhashtable: guarantee initial hashtable allocation Date: Fri, 1 Jun 2018 09:01:22 -0700 Message-Id: <20180601160125.30031-3-dave@stgolabs.net> X-Mailer: git-send-email 2.16.3 In-Reply-To: <20180601160125.30031-1-dave@stgolabs.net> References: <20180601160125.30031-1-dave@stgolabs.net> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org rhashtable_init() may fail due to -ENOMEM, thus making the entire api unusable. This patch removes this scenario, however unlikely. In order to guarantee memory allocation, this patch always ends up doing GFP_KERNEL|__GFP_NOFAIL for both the tbl as well as alloc_bucket_spinlocks(). Upon the first table allocation failure, we shrink the size to the smallest value that makes sense retry with __GFP_NOFAIL semantics. With the defaults, this means that from 64 buckets, we retry with only 4. Any later issues regarding performance due to collisions or larger table resizing (when more memory becomes available) is the last of our problems. Suggested-by: Linus Torvalds Signed-off-by: Davidlohr Bueso --- lib/rhashtable.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/rhashtable.c b/lib/rhashtable.c index 05a4b1b8b8ce..ae17da6f0c75 100644 --- a/lib/rhashtable.c +++ b/lib/rhashtable.c @@ -175,7 +175,7 @@ static struct bucket_table *bucket_table_alloc(struct rhashtable *ht, int i; size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]); - if (gfp != GFP_KERNEL) + if ((gfp & ~__GFP_NOFAIL) != GFP_KERNEL) tbl = kzalloc(size, gfp | __GFP_NOWARN | __GFP_NORETRY); else tbl = kvzalloc(size, gfp); @@ -1067,9 +1067,16 @@ int rhashtable_init(struct rhashtable *ht, } } + /* + * This is api initialization and thus we need to guarantee the + * initial rhashtable allocation. Upon failure, retry with the + * smallest possible size with __GFP_NOFAIL semantics. + */ tbl = bucket_table_alloc(ht, size, GFP_KERNEL); - if (tbl == NULL) - return -ENOMEM; + if (unlikely(tbl == NULL)) { + size = min_t(u16, ht->p.min_size, HASH_MIN_SIZE); + tbl = bucket_table_alloc(ht, size, GFP_KERNEL | __GFP_NOFAIL); + } atomic_set(&ht->nelems, 0); -- 2.16.3