Received: by 2002:ac0:a581:0:0:0:0:0 with SMTP id m1-v6csp96332imm; Thu, 21 Jun 2018 14:36:59 -0700 (PDT) X-Google-Smtp-Source: ADUXVKIAeX+/WN/dRGDru85Egzj31gECkW8b2oqy7J0RkYzR9FPw0Wq26m2uYObG9BqhLI4f5diB X-Received: by 2002:a62:d97:: with SMTP id 23-v6mr29245730pfn.202.1529617019782; Thu, 21 Jun 2018 14:36:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529617019; cv=none; d=google.com; s=arc-20160816; b=vYUA4NekFlFlXEORXTMik22Xx9h5Kqz5JAC9Ed87MBPz9l1txqOUGopGK1B2tOUu2X mMookqSl20H5Nj/5fUfy8N1pPBJDiTguaP4xlvGIqp1tuzA6rgkFGaE0AHsiHoPGx0+2 c7S25hHuREtcC4DWFnP5WIyD2wb4CKwxYarv3yqJD5crXlzQgAhKoWJyHTtzJG0B1lkA zVa6w969RzC8OLYW0xwd7DR5igXOCSh9sug6HgVDfZ8RpO70UpZRGAMj6HuwljwTkIVM 5dSv6ZN3ZucN+Frzio68/dYmHB8z5FkZc/N6AOh4Cd3yGl57UyCLv8VqL/o0ttTYbsn7 DwQg== 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=q14l5AtJiUS+JOivd3j1xBQnxdjTts2FvJvnCg2szaA=; b=zGxiHVvEgqzrKVcq7fuzcd0pXG3UNjQNK11NDHSFxtwaWdg+xXiVrdj1hPuXkLTXX8 oWdlt54l/6ehVeajMIffc0ldGv8QJknlmpin4sZw6kESvTXEUHPplrh0UFsxvUic3EBl PAFNtoIaKak9n0Ea5vowgrIVdRfJIoXy/48t67NOcUfbIlxgTtqbvU2F8yx1Lmt54F1/ j5XsOG8KexvBH0ZRejHeb7JpRWjXp/I4euhxN5it3cwcImNGUZ+PHz28Uy0DufB2O8zz 1R6JN7Dpa7k47EsipNh+fF4/nP8ELuR3MNrYx97WXDhNHp0OyAFzV+BpDDM4afpTdOFC T34Q== 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 s9-v6si4823085pgr.474.2018.06.21.14.36.44; Thu, 21 Jun 2018 14:36:59 -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 S933900AbeFUV3E (ORCPT + 99 others); Thu, 21 Jun 2018 17:29:04 -0400 Received: from smtp2.provo.novell.com ([137.65.250.81]:60559 "EHLO smtp2.provo.novell.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933880AbeFUV3A (ORCPT ); Thu, 21 Jun 2018 17:29:00 -0400 Received: from linux-r8p5.suse.cz (prv-ext-foundry1int.gns.novell.com [137.65.251.240]) by smtp2.provo.novell.com with ESMTP (TLS encrypted); Thu, 21 Jun 2018 15:28:50 -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, dave@stgolabs.net, Davidlohr Bueso Subject: [PATCH 2/4] lib/rhashtable: guarantee initial hashtable allocation Date: Thu, 21 Jun 2018 14:28:23 -0700 Message-Id: <20180621212825.3059-3-dave@stgolabs.net> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20180621212825.3059-1-dave@stgolabs.net> References: <20180621212825.3059-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 and 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 least of our problems. Signed-off-by: Davidlohr Bueso --- lib/rhashtable.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/rhashtable.c b/lib/rhashtable.c index 26c9cd8a985a..411c4041ce83 100644 --- a/lib/rhashtable.c +++ b/lib/rhashtable.c @@ -179,10 +179,11 @@ static struct bucket_table *bucket_table_alloc(struct rhashtable *ht, size = nbuckets; - if (tbl == NULL && gfp != GFP_KERNEL) { + if (tbl == NULL && (gfp & ~__GFP_NOFAIL) != GFP_KERNEL) { tbl = nested_bucket_table_alloc(ht, nbuckets, gfp); nbuckets = 0; } + if (tbl == NULL) return NULL; @@ -1065,9 +1066,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 = max_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.4