From patchwork Wed Jul 14 09:22:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 478350 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.2 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, URIBL_RED, USER_AGENT_GIT, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B05DAC07E9A for ; Wed, 14 Jul 2021 09:22:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 92157613B2 for ; Wed, 14 Jul 2021 09:22:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238638AbhGNJZU (ORCPT ); Wed, 14 Jul 2021 05:25:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47148 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238487AbhGNJZT (ORCPT ); Wed, 14 Jul 2021 05:25:19 -0400 Received: from mail-wr1-x449.google.com (mail-wr1-x449.google.com [IPv6:2a00:1450:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4C79DC06175F for ; Wed, 14 Jul 2021 02:22:28 -0700 (PDT) Received: by mail-wr1-x449.google.com with SMTP id k3-20020a5d52430000b0290138092aea94so1163580wrc.20 for ; Wed, 14 Jul 2021 02:22:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=Qyc6ohzjuGWO87gzL/Z+Is02vLbY2suPFPQ1nEFYzSU=; b=AgQnCrNOd7i5dBNRA+Pqzh0QH1Aavvx/jvqVMlgotATgmfiQ9tML534h2Yt0Wp6UVD dPCrTzqm3k828aTupcVUKNvi1boNjXELkbENaolWJCSx3FLNUG5vZY14y1N3UaxiQka5 +q+oEgzkBkNs6xz8EJ6+xX00OSYkkSX47ur8ATJ2uzpAtZh4TbLoyw6PlDiOrEPI7ePs h3eyPY50SvRZx/kOkqepKO3iRLDAsYfLa6LAL+akoR+aYpsPu7dxAcRztxsEHgLOr6OL Zx1N3zurNs4uK7PulQxbL3PGcncwcgxZBhtst0DHt/kWg1ICnEil0BewSj5afs/vQH71 JD7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=Qyc6ohzjuGWO87gzL/Z+Is02vLbY2suPFPQ1nEFYzSU=; b=k8316bg32H6r8yxtt64srmdp25tJQgFxM26dBROsLYY9yhbj+vqlFiW8zuaFpl6E86 Zh9SdUtbsy6Mda8/+IQEZZahQAdqvvH7Tl+KxO+Q9pymDfFKV/yuuxrmpnWnLTWV3KwG bA09arBMkwd5nShYL77F6nbABBtYL1T+2xb4mvzx8B+MZv4v+xN76p0MBdSIMlOq4U4Z q3QUfanSkO/iv6bVEVD+iAVJ7R+h/i9DnZCpTP6Sx0pTt8AuJjYicsmuN/R6Sa2K6VCe 5uIYZNBV9PBDkDBRKrc86H9QhjIM6iWNb7q0C1gyLfZSNU9Lm9zgOS7AMQm+UPm1wuvz AZug== X-Gm-Message-State: AOAM5306u5IauuUq7e0vVF2Aby04RXPflm9d0TH5PBJ80IL4NDx2pYgH a54vJY8puoJPIQfGGfR8L5UriSqflF0= X-Google-Smtp-Source: ABdhPJzYuKhDVZIN8Rj3HEws6Yo8UjiKSv5LM81gKwrWyru/o3IaiXXaws7mE9L6DdcQ7W3fAYE05yBPxQ4= X-Received: from glider.muc.corp.google.com ([2a00:79e0:15:13:89ec:cd11:7e8b:63fe]) (user=glider job=sendgmr) by 2002:a05:600c:2248:: with SMTP id a8mr2892376wmm.141.1626254546797; Wed, 14 Jul 2021 02:22:26 -0700 (PDT) Date: Wed, 14 Jul 2021 11:22:21 +0200 Message-Id: <20210714092222.1890268-1-glider@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH mm v5 1/2] kfence: move the size check to the beginning of __kfence_alloc() From: Alexander Potapenko To: akpm@linux-foundation.org Cc: dvyukov@google.com, elver@google.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, stable@vger.kernel.org, gregkh@linuxfoundation.org, jrdr.linux@gmail.com, Alexander Potapenko Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org Check the allocation size before toggling kfence_allocation_gate. This way allocations that can't be served by KFENCE will not result in waiting for another CONFIG_KFENCE_SAMPLE_INTERVAL without allocating anything. Suggested-by: Marco Elver Cc: Andrew Morton Cc: Dmitry Vyukov Cc: Marco Elver Cc: Greg Kroah-Hartman Cc: stable@vger.kernel.org # 5.12+ Signed-off-by: Alexander Potapenko Reviewed-by: Marco Elver --- mm/kfence/core.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/mm/kfence/core.c b/mm/kfence/core.c index d7666ace9d2e4..2623ff401a104 100644 --- a/mm/kfence/core.c +++ b/mm/kfence/core.c @@ -733,6 +733,13 @@ void kfence_shutdown_cache(struct kmem_cache *s) void *__kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags) { + /* + * Perform size check before switching kfence_allocation_gate, so that + * we don't disable KFENCE without making an allocation. + */ + if (size > PAGE_SIZE) + return NULL; + /* * allocation_gate only needs to become non-zero, so it doesn't make * sense to continue writing to it and pay the associated contention @@ -757,9 +764,6 @@ void *__kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags) if (!READ_ONCE(kfence_enabled)) return NULL; - if (size > PAGE_SIZE) - return NULL; - return kfence_guarded_alloc(s, size, flags); } From patchwork Wed Jul 14 09:22:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 477378 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 38C23C07E9C for ; Wed, 14 Jul 2021 09:22:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 229CB61369 for ; Wed, 14 Jul 2021 09:22:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238806AbhGNJZ0 (ORCPT ); Wed, 14 Jul 2021 05:25:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47182 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238804AbhGNJZZ (ORCPT ); Wed, 14 Jul 2021 05:25:25 -0400 Received: from mail-wm1-x34a.google.com (mail-wm1-x34a.google.com [IPv6:2a00:1450:4864:20::34a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 22BE0C061760 for ; Wed, 14 Jul 2021 02:22:34 -0700 (PDT) Received: by mail-wm1-x34a.google.com with SMTP id 1-20020a05600c0201b029022095f349f3so1769684wmi.0 for ; Wed, 14 Jul 2021 02:22:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=j81w1peEP7UvtOh9scTpnrf58uB+d+GVhtisHm569kE=; b=Wn+sU3hYqyY2DRBVayRYqUr+uoLeQFtStUDyB49M23dTRIE5Mb+56ZuVjVaXSIeA3T LgeGB5AfACS2L4xSVSmhwGg156jyL/c7dX799ifeGP7wM47pdfxX2SJI8gLcYgdXWfxS A3BySvNVrKmQeApKE1wzKsnk8qa5mJDHtoUm1WAUydYSGsk+1x3TOD0ySDi0l2EUYmfG F/qhjFpEliRF1u684yCgoh+4JFNaJj3vHjfpjmyzDbL49faelRqIv+s/akebcJb8MGVD egg+LvPuxaCmQ3fFpqVp1B4xzvvXlNLIP2dVvmobUWnBmCRp/Xh44rQ7865/+Ygo/mOY xN0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=j81w1peEP7UvtOh9scTpnrf58uB+d+GVhtisHm569kE=; b=SknkDQ/EeTp8SV69qU1wqGySBovMYlijmGs+3WOVVETdOpqAms8ilrCTuQWOmFMykZ TLU1OWSQyK1XroBAGjFKzJjqdaUFzwrEHcVPocWItJWPqYH4diniFHJ2x3xTnbj62vYw 2v4+TYW784MgUSOy15RhNcQq5YxO0i9dbHTnitqsSal3cWO2/53D8p2WODO8TEFK20A3 iVcsYnDWeW7yKJ27/H1Fail88xuR5lagA9lr+q3zewsb5WbV2W3lcnXNtSF0EDOefb26 HkhSbu9LyXEHXHhssqOaoWBokEIapmTw2UxOmCtrGcM1sSYJ0W2IABKrXt2TKvtxJDpX xX8w== X-Gm-Message-State: AOAM533aLJQ6yvz9tvERwtrdBtpt2YbNsZKZy+RkiLZADgEGfjeZsnQh ivc69QjVaFo4A4wlVv2FLm2eJy1livs= X-Google-Smtp-Source: ABdhPJz9BdeMwe2nrBQhJv0JCr7x/yTJ76W/xvJREEaino26Mi/Ya2NpWI0J3DfFVbsmC1bKxZHHDP1FrlI= X-Received: from glider.muc.corp.google.com ([2a00:79e0:15:13:89ec:cd11:7e8b:63fe]) (user=glider job=sendgmr) by 2002:a7b:c052:: with SMTP id u18mr3009546wmc.105.1626254552678; Wed, 14 Jul 2021 02:22:32 -0700 (PDT) Date: Wed, 14 Jul 2021 11:22:22 +0200 In-Reply-To: <20210714092222.1890268-1-glider@google.com> Message-Id: <20210714092222.1890268-2-glider@google.com> Mime-Version: 1.0 References: <20210714092222.1890268-1-glider@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH mm v5 2/2] kfence: skip all GFP_ZONEMASK allocations From: Alexander Potapenko To: akpm@linux-foundation.org Cc: dvyukov@google.com, elver@google.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, stable@vger.kernel.org, gregkh@linuxfoundation.org, jrdr.linux@gmail.com, Alexander Potapenko Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org Allocation requests outside ZONE_NORMAL (MOVABLE, HIGHMEM or DMA) cannot be fulfilled by KFENCE, because KFENCE memory pool is located in a zone different from the requested one. Because callers of kmem_cache_alloc() may actually rely on the allocation to reside in the requested zone (e.g. memory allocations done with __GFP_DMA must be DMAable), skip all allocations done with GFP_ZONEMASK and/or respective SLAB flags (SLAB_CACHE_DMA and SLAB_CACHE_DMA32). Fixes: 0ce20dd84089 ("mm: add Kernel Electric-Fence infrastructure") Cc: Andrew Morton Cc: Dmitry Vyukov Cc: Marco Elver Cc: Greg Kroah-Hartman Cc: Souptick Joarder Cc: stable@vger.kernel.org # 5.12+ Signed-off-by: Alexander Potapenko Reviewed-by: Marco Elver Acked-by: Souptick Joarder --- v2: - added parentheses around the GFP clause, as requested by Marco v3: - ignore GFP_ZONEMASK, which also covers __GFP_HIGHMEM and __GFP_MOVABLE - move the flag check at the beginning of the function, as requested by Souptick Joarder v4: - minor fixes to description and comment formatting v5: - pick Acked-by:, rebase --- mm/kfence/core.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mm/kfence/core.c b/mm/kfence/core.c index 2623ff401a104..575c685aa6422 100644 --- a/mm/kfence/core.c +++ b/mm/kfence/core.c @@ -740,6 +740,15 @@ void *__kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags) if (size > PAGE_SIZE) return NULL; + /* + * Skip allocations from non-default zones, including DMA. We cannot + * guarantee that pages in the KFENCE pool will have the requested + * properties (e.g. reside in DMAable memory). + */ + if ((flags & GFP_ZONEMASK) || + (s->flags & (SLAB_CACHE_DMA | SLAB_CACHE_DMA32))) + return NULL; + /* * allocation_gate only needs to become non-zero, so it doesn't make * sense to continue writing to it and pay the associated contention