Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp651942imm; Mon, 9 Jul 2018 08:15:44 -0700 (PDT) X-Google-Smtp-Source: AAOMgpfOEo6Xx3JQ/9O554JG1UVHFD4W4CrquZdUXuhLI2in8SaLs2fGCx5sjb2QujJw/bl1CwCT X-Received: by 2002:a65:6211:: with SMTP id d17-v6mr19670827pgv.450.1531149344553; Mon, 09 Jul 2018 08:15:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531149344; cv=none; d=google.com; s=arc-20160816; b=DwqZJWY7pyEPWOWdoD2AwYTpsnaLDr08x5zVAhv57Ute7F1MIyqoC18VFQfJKcR6vG aArqADuH1JYd4AqUjOs5hOyBpJGAnVWvlFnXj+uE6/34VK3ATkSoIk1u5S6Q9XmixuUp /9TwIdlyEOgP5pTZXl2tsrIfvYGLAnbW0N7k64l9swgmXxYIbfeNhw0mVnDvC31IycZZ W83fCJNlOjtapnm49qaDx2WQAtw2Sv5gpsbORf8Z9PYGsngCWktwek5328toKfWsRX7T w6YZbO9gMS4zlcLBMp7yOCR80jsOeD1k92pKHm/rs4UgdSHCbZmzi1Vgg9pJUtTQsI9X fzAA== 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=zfxsr8PT8eOawDgHMrG6V0epmqXU6HjsKsvuVzvpKPKY/cRCT0pJIkxCwbWfwkH931 +BXVXka/98QLRcWFnwB4dzxRHN5jnQ2YhLOCfpujUVdzfulyoPeDTNWz0XL6SxMHB9Um ljHfxo5wAftF9OtF3h6OTnhvQGiqvYDdLc4zz2RDtDBSr39RMvJE1Z8dnwMC/oIa0b4U a8rK43PVS+gasUhW8JePwS9UFmf8NvEKQJg4GZd7Rq5JDcSSVhbdkOR+b0duOAZhAwnC C90QEm59n7GkGVdtlul+j9MAm8EwVD00337XhD9e+WspYsp6gFEyipQeqw1q49yZMLPV ftTg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@colorfullife-com.20150623.gappssmtp.com header.s=20150623 header.b=RiIGiFma; 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 y10-v6si1790112pgf.312.2018.07.09.08.15.23; Mon, 09 Jul 2018 08:15:44 -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=RiIGiFma; 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 S933528AbeGIPMW (ORCPT + 99 others); Mon, 9 Jul 2018 11:12:22 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:36509 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933422AbeGIPKk (ORCPT ); Mon, 9 Jul 2018 11:10:40 -0400 Received: by mail-wr1-f67.google.com with SMTP id h9-v6so11380706wro.3 for ; Mon, 09 Jul 2018 08:10:40 -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=RiIGiFmaPB9Gqa5d+selNYCZGgKEm5eWTXmEFuqM24zXGs+0VIYgnr0xk0oktMosWC QWCeOb369TDV0qtOvvrgD1DoSyq3S29WLfeIjF+NaPBcy4u5sMYG43zUSBBz5l6Kk8aa tB6PS9XjPzbSyNL/BIARChannH8UEzRGhff3U0EbID5Onn3oRqL2vFdtM0luWLA8Au3s goC5/YJq1X5ctS5qMKvYcan8ZnCjql34o7JeWMTWv6uGFB7Xc9KqTs1lh/12AWT/im0P ojDjbxAsjz5mlSFOHI4Ezz3qRyc+AU5p97iRUe2NPSdyp+cpB0n945OZt+6ULa/phorl pp8g== 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=lcD4qiVMLB4L3bLwEV2Fia58t4LA4YtV/8M0r8knWvNyetM6RUhb9a2Tf5VTyDcvI4 oLJmRQlCzNbRYlWdq+sKOCoyOm5MW4KgKoTPvuqzC1FGkRHZt2TNQGz5naq2fU+zXYH+ ig8CNAJvTzQ4TIHTIxy+SpfprGoRgLEVzdbBZVzcIk+fCJTu+69MUlMTSnXKXa3aWFYa ofvMHhBQC3ynhwfL0ABbGQuZNkkXTbsZNWnm+FiNFEMGh0N63oaKP5JDCZmCcEq2jxo7 26wic5tKO8GuGrfT8fOgsFRfx1J5uuI8vfWyHIFRoRQpgE/uZEMyzGcr80X+p8ntG7NH IIkA== X-Gm-Message-State: AOUpUlF7Nw9lTJukw9JpWHi/UxvM3sHHmJwVCJ6+1TWjXgAmwEhlyqK8 +DrJQF0L0Y2nBSjLX320bP+SUA== X-Received: by 2002:adf:b519:: with SMTP id a25-v6mr6287610wrd.273.1531149039829; Mon, 09 Jul 2018 08:10:39 -0700 (PDT) Received: from localhost.localdomain (p200300D993C227000209466FFA2F090C.dip0.t-ipconnect.de. [2003:d9:93c2:2700:209:466f:fa2f:90c]) by smtp.googlemail.com with ESMTPSA id u124-v6sm7817330wme.26.2018.07.09.08.10.38 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 09 Jul 2018 08:10:39 -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 09/12] lib/rhashtable: guarantee initial hashtable allocation Date: Mon, 9 Jul 2018 17:10:16 +0200 Message-Id: <20180709151019.1336-10-manfred@colorfullife.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180709151019.1336-1-manfred@colorfullife.com> References: <20180709151019.1336-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