From patchwork Thu Sep 27 17:15:02 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Vyukov X-Patchwork-Id: 10618359 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9F02F913 for ; Thu, 27 Sep 2018 17:15:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7EF6D2BC07 for ; Thu, 27 Sep 2018 17:15:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 730212BC1F; Thu, 27 Sep 2018 17:15:16 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9E0B42BC07 for ; Thu, 27 Sep 2018 17:15:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CB23D8E0006; Thu, 27 Sep 2018 13:15:14 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id C3AB48E0001; Thu, 27 Sep 2018 13:15:14 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B03368E0006; Thu, 27 Sep 2018 13:15:14 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by kanga.kvack.org (Postfix) with ESMTP id 4FF018E0001 for ; Thu, 27 Sep 2018 13:15:14 -0400 (EDT) Received: by mail-wm1-f72.google.com with SMTP id s19-v6so5842883wmh.3 for ; Thu, 27 Sep 2018 10:15:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:mime-version:content-transfer-encoding; bh=lCgBSKSsIa//JdAe43K3SdSgLAs8nAIxIpDtbL584wM=; b=EcQW/Be5RHMX2+WIyiy3qghQX95x7clThO+9jq6yiK5SHEGGUxQDEypABtkInUrx3e OUMm1DWoSmx9FeG6R0jlmNmgai2GZZSR4OQ4/8/lBMSZMWqck/MIbuOC/ieZtLs1Km6l 5HxbAjzzlPUsxJiXRBZ6loOpPki1cJfDLhOL+ci4lUSPiH37U0j7qechkYCZlVTf9WA7 v4ZgpU8pjye127Jd1mBYXqimvpcCNaL/ePZZOlyqgz6VxrbS8di9RuIlmsmQ1JLh1VUr 3/VMvh3apmst8Mby+VCO3Fup6y1Kp0p12LNXF69Pm5XMenfZIySlEGRphyVJAzUm8jVz BeUA== X-Gm-Message-State: ABuFfogMHmzTPGV3RdTduSi9zvSGFf74S0e6RrMtA6UBy7i0eyMbmjpL Si+qslh7Q4nqZ5kHQvOLqZngpTi1/tecyRkbkpmbjlLvDpuSVXpseKtPnIiFdjWpGQCYj7MzaKW d1Gno/qQmOUoaRsxFGwLfx2SNbbcailpG6lWQfVuGkgKrkm89xhGDNDT6XDWq3YU6hQgxpyO3EA iRDx2nKL55nzljRGIbrHHEzZbKEdaQsQjLTbrowiSvtTuT+Cdsjj456qXLr6cwESG6TX8y39wyQ XJRADvBT/BlokNZWYQl5Bqul7OJk4Qn33vZKQVaZX11zYxUy5BxjTzxMmwkOxgZ4vgBJW8a6Lzj 09dUKnGMeIgLEw5cvJnYGat8hgLlUaM35H+Z1ThG8hZHYETD2/hTCJ+7QCXjxl4ARMPvd7gVjnZ v X-Received: by 2002:adf:a10c:: with SMTP id o12-v6mr9634918wro.169.1538068513759; Thu, 27 Sep 2018 10:15:13 -0700 (PDT) X-Received: by 2002:adf:a10c:: with SMTP id o12-v6mr9634873wro.169.1538068512858; Thu, 27 Sep 2018 10:15:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538068512; cv=none; d=google.com; s=arc-20160816; b=QlgW4WqPbqbhPHRW8kOFNa5kKl7bA3ztigkylam01TSZKcz5ZP86p8l8pBSfYdIMUh CzPpvn+Q9HeRTuEFbMIAzoGIzFzYGI9dD+gvGVLl0N/W/lqmWXGVLvOdf+D2eg44hfk6 Sfndz1SGSj/Dpeu8bGJoAHQskivV6CODv8bobJRsn8Llvg8U5EgIVCFwyM4oRkyF2SYY wWMlWHnyX5ONa1BcxIo/N3oYRvmXTA/5TcBWilnVruMIUjS2yeOl9ObuW8Vl7T0aQ9/l pDX84UjHO9dQmggF8Bb8/g6pMp1vDkEXQa4EDnb1AxyGpiRPh4TWrPtIsBAf266Ge0ak e36w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:dkim-signature; bh=lCgBSKSsIa//JdAe43K3SdSgLAs8nAIxIpDtbL584wM=; b=JYQYclHiLs1VaLOK6jT0rBzUuxs/u1T0/BnOJIud8omZlJQmJkOj2GKvya1bgtOhmv U8FalgU85uziizpWW5OBPRlyvbjCfxPRAkjjkacuYq0DT7tXup3MBAS+NqP1bIXoBJZw whRJovT5fRHxLsHFkOQ1uH6MkT4OiL+fVlbOxG6PRkm8s8kpysKR2q5eU3QLxkCoF+c+ FqBM3GjU/WYRRo5/lbm6r0oZBjseR4iwatY73RuNLynnthjrj5yfUHH98T+bAXa9fVox /YSuEpjPLgaVGABk3YEarZKas5EWBAYPVYCPfCnd8dxHU+bNUQZOVDtDlS0UcCZ5ePKz iquw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="OeQx/uVD"; spf=pass (google.com: domain of dvyukov@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=dvyukov@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id d16-v6sor1936233wrp.38.2018.09.27.10.15.12 for (Google Transport Security); Thu, 27 Sep 2018 10:15:12 -0700 (PDT) Received-SPF: pass (google.com: domain of dvyukov@gmail.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="OeQx/uVD"; spf=pass (google.com: domain of dvyukov@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=dvyukov@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=lCgBSKSsIa//JdAe43K3SdSgLAs8nAIxIpDtbL584wM=; b=OeQx/uVD3E7x6LfLTd1Scvz0DQjZite+7lJJZu5H6ICJ1j0yG+BCL3r47Vk+jzo55H urX8IfXPifTJfSsnCDDny+6lI9eDHmqvC8IiEDyOxK+plPWAiauL+Qsy2cimWQhWjdbE cVAFdJ2pnB18yxFlGI8vQ47OKYw/qhIX5POCJwBRdDuYKaOOq1gfuW1+qo1H21NLL3/I TntgejgpyGvginBXruzLz5j+pTnzpaKbOuakTDTGk0hsEieBJmWURgvHgJgYhrSiDU2w S0GYMo9a0F6/wo/7sLV6eqZNz8QYRSBgMt9eDdWaRnr8+nFng69q6l80nMhE20xYg8n1 pfOQ== X-Google-Smtp-Source: ACcGV623OVAhbK3CCUABvgcFX6BuzpSUKSeneJlkVcehYtIEQMbjfFT2XTpYDs+HfssVn+nr05oQkA== X-Received: by 2002:adf:8024:: with SMTP id 33-v6mr9552998wrk.16.1538068512162; Thu, 27 Sep 2018 10:15:12 -0700 (PDT) Received: from dvyukov-z840.muc.corp.google.com ([2a00:79e0:15:10:8971:4ae9:dd1c:10e]) by smtp.gmail.com with ESMTPSA id p9-v6sm1648812wmc.37.2018.09.27.10.15.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 27 Sep 2018 10:15:11 -0700 (PDT) From: Dmitry Vyukov To: cl@linux.com, penberg@kernel.org, akpm@linux-foundation.org, rientjes@google.com, iamjoonsoo.kim@lge.com Cc: Dmitry Vyukov , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] mm: don't warn about large allocations for slab Date: Thu, 27 Sep 2018 19:15:02 +0200 Message-Id: <20180927171502.226522-1-dvyukov@gmail.com> X-Mailer: git-send-email 2.19.0.605.g01d371f741-goog MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP From: Dmitry Vyukov Slub does not call kmalloc_slab() for sizes > KMALLOC_MAX_CACHE_SIZE, instead it falls back to kmalloc_large(). For slab KMALLOC_MAX_CACHE_SIZE == KMALLOC_MAX_SIZE and it calls kmalloc_slab() for all allocations relying on NULL return value for over-sized allocations. This inconsistency leads to unwanted warnings from kmalloc_slab() for over-sized allocations for slab. Returning NULL for failed allocations is the expected behavior. Make slub and slab code consistent by checking size > KMALLOC_MAX_CACHE_SIZE in slab before calling kmalloc_slab(). While we are here also fix the check in kmalloc_slab(). We should check against KMALLOC_MAX_CACHE_SIZE rather than KMALLOC_MAX_SIZE. It all kinda worked because for slab the constants are the same, and slub always checks the size against KMALLOC_MAX_CACHE_SIZE before kmalloc_slab(). But if we get there with size > KMALLOC_MAX_CACHE_SIZE anyhow bad things will happen. For example, in case of a newly introduced bug in slub code. Also move the check in kmalloc_slab() from function entry to the size > 192 case. This partially compensates for the additional check in slab code and makes slub code a bit faster (at least theoretically). Also drop __GFP_NOWARN in the warning check. This warning means a bug in slab code itself, user-passed flags have nothing to do with it. Nothing of this affects slob. Signed-off-by: Dmitry Vyukov Cc: Christoph Lameter Cc: Pekka Enberg Cc: David Rientjes Cc: Joonsoo Kim Cc: Andrew Morton Cc: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org Reported-by: syzbot+87829a10073277282ad1@syzkaller.appspotmail.com Reported-by: syzbot+ef4e8fc3a06e9019bb40@syzkaller.appspotmail.com Reported-by: syzbot+6e438f4036df52cbb863@syzkaller.appspotmail.com Reported-by: syzbot+8574471d8734457d98aa@syzkaller.appspotmail.com Reported-by: syzbot+af1504df0807a083dbd9@syzkaller.appspotmail.com Acked-by: Vlastimil Babka --- Changes since v1: - everything has changed, re-review --- mm/slab.c | 4 ++++ mm/slab_common.c | 12 ++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/mm/slab.c b/mm/slab.c index 9515798f37b2d..2a5654bb3b3ff 100644 --- a/mm/slab.c +++ b/mm/slab.c @@ -3675,6 +3675,8 @@ __do_kmalloc_node(size_t size, gfp_t flags, int node, unsigned long caller) struct kmem_cache *cachep; void *ret; + if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) + return NULL; cachep = kmalloc_slab(size, flags); if (unlikely(ZERO_OR_NULL_PTR(cachep))) return cachep; @@ -3710,6 +3712,8 @@ static __always_inline void *__do_kmalloc(size_t size, gfp_t flags, struct kmem_cache *cachep; void *ret; + if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) + return NULL; cachep = kmalloc_slab(size, flags); if (unlikely(ZERO_OR_NULL_PTR(cachep))) return cachep; diff --git a/mm/slab_common.c b/mm/slab_common.c index 1f903589980f9..7eb8dc136c1cb 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -1023,18 +1023,18 @@ struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags) { unsigned int index; - if (unlikely(size > KMALLOC_MAX_SIZE)) { - WARN_ON_ONCE(!(flags & __GFP_NOWARN)); - return NULL; - } - if (size <= 192) { if (!size) return ZERO_SIZE_PTR; index = size_index[size_index_elem(size)]; - } else + } else { + if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) { + WARN_ON(1); + return NULL; + } index = fls(size - 1); + } return kmalloc_caches[kmalloc_type(flags)][index]; }