Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp2027595imm; Thu, 12 Jul 2018 11:54:55 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdmEAkI9sbxt6T66fx/hXIndQO4EdHJ2M3FleO4iL8O/CDbkbG5DjipYwUQQqlMXNbQC0dR X-Received: by 2002:a17:902:6b84:: with SMTP id p4-v6mr3281664plk.272.1531421694952; Thu, 12 Jul 2018 11:54:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531421694; cv=none; d=google.com; s=arc-20160816; b=FYsWzZT333A1Msujz0U+wfR82xBdH373JKqGAteyNbrINv6nUHhFKgUwt7HfECD4RQ KCbucULyWR+ezh76S6c6/uofsxRD3JtbRT17Vj2Jyn+JK5eK//NXKjF/IXAbPI/yON/i LG5hMJ4iF/ajmrdN2sl6YQyTXelemgVjb7U+jDVgK3eVXzPmIjQSplMusPJDbsAkLzSO +f69AMqiv+Wm/PsD0aFx25aEwlOkEPF4NXXXpzXtmqjLplBGr4GTfA4PIXdgXaJKmZhe E4lFwJhj80Vce8FedbKnp6xV58lCaDTdT4yEkm07R2D3vYISMeGzH/DrhCW3pYeCPkRS vtNw== 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:dkim-signature:arc-authentication-results; bh=5jQgELfyD7VNwzfhRyNUzqx6eCK+QCKeBT5JYYC3SAs=; b=ksQmuTIUEg3PHrzxcANq42W6aTarbZZUirgztMLj4lxvujqMfSCvbUkwta4qw/DLKo 79A04SSZwgnR3rSrCGpuVQjgZFBz8kUg0UvXgN5xrTCBtr1UZmo1J3hYe1k8TnDE3DJ0 Fu0240K2yXuDpQIy4DK1MGdobR8AY1N+9/ZMBfqEGobRGkZbF3m/9yggKvd9ZHNGPSzT P3o5gxteIcKlZrovhBP1SdhA0GKSEkAs64gtQaR0Wu3kEntZf50MFE+hN+iRBr8yQNYb K3rCqyAL8yP64/rkwdZKB+gPuWRf6ceR0T6X0vkoAjvGz3vWXvcaTqH5LlUL/ixj4ydM odBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@colorfullife-com.20150623.gappssmtp.com header.s=20150623 header.b=dLV42eCV; 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-v6si21617962plp.182.2018.07.12.11.54.39; Thu, 12 Jul 2018 11:54:54 -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; dkim=pass header.i=@colorfullife-com.20150623.gappssmtp.com header.s=20150623 header.b=dLV42eCV; 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 S1732419AbeGLTEc (ORCPT + 99 others); Thu, 12 Jul 2018 15:04:32 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:42786 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726500AbeGLTED (ORCPT ); Thu, 12 Jul 2018 15:04:03 -0400 Received: by mail-wr1-f67.google.com with SMTP id e7-v6so3163647wrs.9 for ; Thu, 12 Jul 2018 11:53:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=colorfullife-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=5jQgELfyD7VNwzfhRyNUzqx6eCK+QCKeBT5JYYC3SAs=; b=dLV42eCVC8/pldkn6MVgibLRU+ekXFam4i7M9HN9pQ+1rMmD+tf4livFkN859PfwVL LLfkbeNVVnDv7Ug+ib0w0o2Ql9RgE4dTVw+X0wL1uIupqdhabZLXpspyp5FKTCdh/CMt pzOIDF7rDUXT7PvClzc2UXYXnff40BqWCdmVdvMciH7PH80qybiovXL7A86liwIxO+TP efbfbvZ/IOK+TjamrjIhdYXry3KkdnH6EdQ4jLoyR6nKawqpc4SK2+bdKthXASBRWvtT HMt+Q4JBU23paGczaVDpCQxzwQRrvrU0Ln74HHvpzC/hayDYy3qYIXV5hDt3y9lfZFZL Lfxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=5jQgELfyD7VNwzfhRyNUzqx6eCK+QCKeBT5JYYC3SAs=; b=nSr140GAC7gIN2EOjP3skg7iQWJgwqrXSqVy4cbsLisMYWJIcVDk5fWUBISpC2i1m+ jqgC+7/4TM9Tm1oJ1dVkMiprAHuu65B9Nrr6Va0StNPs3E166lHTLRhpLFMkxgsbQU5+ FF9JA6ZmExCEuDp2PNjZrVAXMPKV9ShpnJ8azqXj0k7Lmi2RFmfw3NqF0DDHWBJxcRl6 cnk6au5ALeEO96CyLcKCamHkcB+q1FhmLYL368p7Wkgc+iFMcE766sWw+5aFKpl0uf7y sRYP9hJ5C4J0vjqmqNNH2/HOHaE/+yaKFgqz6hiFr4TsxxbSQlxIjzqibzbo7Ju6df9m FWeg== X-Gm-Message-State: AOUpUlG0Kv9Ezan2qk3efDrpZJW/3QcrUMqDGo3I2fXQL9Ru7tCE66kk g6J8l/+Vb4X0fzTNZhs81WcHOA== X-Received: by 2002:adf:8103:: with SMTP id 3-v6mr2214951wrm.213.1531421593900; Thu, 12 Jul 2018 11:53:13 -0700 (PDT) Received: from localhost.localdomain (p200300D993C98700CB5FA3798C189FE1.dip0.t-ipconnect.de. [2003:d9:93c9:8700:cb5f:a379:8c18:9fe1]) by smtp.googlemail.com with ESMTPSA id h5-v6sm13557841wrr.19.2018.07.12.11.53.13 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 12 Jul 2018 11:53:13 -0700 (PDT) From: Manfred Spraul To: Andrew Morton , Davidlohr Bueso , Dmitry Vyukov Cc: LKML , 1vier1@web.de, Kees Cook , Davidlohr Bueso , Manfred Spraul Subject: [PATCH 08/12] lib/rhashtable: guarantee initial hashtable allocation Date: Thu, 12 Jul 2018 20:52:37 +0200 Message-Id: <20180712185241.4017-9-manfred@colorfullife.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180712185241.4017-1-manfred@colorfullife.com> References: <20180712185241.4017-1-manfred@colorfullife.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Davidlohr Bueso 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 Acked-by: Herbert Xu Signed-off-by: Manfred Spraul --- lib/rhashtable.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/rhashtable.c b/lib/rhashtable.c index 083f871491a1..0026cf3e3f27 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.17.1