From patchwork Wed Jun 30 15:02:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12352379 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.4 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=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 B04E8C11F69 for ; Wed, 30 Jun 2021 15:02:41 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 31BC96147D for ; Wed, 30 Jun 2021 15:02:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 31BC96147D Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 769DB8D01B4; Wed, 30 Jun 2021 11:02:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 740AE8D01A2; Wed, 30 Jun 2021 11:02:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5E2248D01B4; Wed, 30 Jun 2021 11:02:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0235.hostedemail.com [216.40.44.235]) by kanga.kvack.org (Postfix) with ESMTP id 3EB208D01A2 for ; Wed, 30 Jun 2021 11:02:40 -0400 (EDT) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 173CB252AB for ; Wed, 30 Jun 2021 15:02:40 +0000 (UTC) X-FDA: 78310706880.24.0D1F203 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) by imf24.hostedemail.com (Postfix) with ESMTP id BC8DAB0000B6 for ; Wed, 30 Jun 2021 15:02:39 +0000 (UTC) Received: by mail-wr1-f74.google.com with SMTP id v18-20020adfa1d20000b029012c379fbc45so578680wrv.22 for ; Wed, 30 Jun 2021 08:02:39 -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=lvrIaQ6ThTaENrGFdNSy44zlHv6vzHwWs8O331HJaOs=; b=UedDNaHadRhM1ur9CcmhNsbBrMJzHEB1U+0r2AOapItU7e7vsnxt3c9g0LnyNpCox3 oY1ub/Vq0GNFDP4URYTbHrG8G5qqLvTSMvekpZ4F39GE+u/bot8Kanw4LCwn+j36aEhj i47cryF1FdWFZi8blrc9xRb9zSV5lPP5aTPFNlpA3Y2di1UfAV6KrCZKzGTJqnsnSEQU sgUjRWtus0jBIR8XPgwE9r6tldW5PgFmHRGZm3jmRejMWv/x/2NmjRQGf/uWQZsvRxvE ymtFZELBVHFsnM8UZ/4gj3dTCBECQeH/udKlGBdrzTQKS3VQ5J61/+i+aKSV6UlzGt7g 5ARQ== 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=lvrIaQ6ThTaENrGFdNSy44zlHv6vzHwWs8O331HJaOs=; b=MbXooUbYZL0XroHj/ikzM4LqzaRxTcMyBliPyY2H2+c6c6bp4oaOk9QBgzgS915l+D jx4U/l5/uLOvwVP4RtCOPfvzqpK8KY9mRhrmN5HzBVzf9FPQYn4mnbSwTg+8F/6TJVBQ +B6qVIteIGO1DEzUta3wg9xYX5qrrbuT9GWZ17GkYyBV1ZEqu8AhYkpWHxO1r8/02pe3 9mB2UMoPHbq9XFFzdLVdYgzUc7cmafxij4Yu5cNRggQdlb3FiM9NSg9YVKBHu7CFX7bD 8F3PZAHzeAVl0szum5CbJc6kw7haY3zlDfx+ywOhDkZTPyirGnrohLSLF3pP50f5P2TB 4R8Q== X-Gm-Message-State: AOAM533HDtpRTJEtOjRWVXUHmk6AGyLsAsCnvOPziHJDaBbCQ6domQqH 1iQynBocKpthC6bt0623YB67wGFEZkQ= X-Google-Smtp-Source: ABdhPJw3Yb4pAIycwjH7H7zCVelcp5Z4pYuZ2OS90TFwioUrU+wqzGQYm+MMsUvB66LM+CC6SclAOogXvy4= X-Received: from glider.muc.corp.google.com ([2a00:79e0:15:13:a3fc:e8:8089:1013]) (user=glider job=sendgmr) by 2002:a05:600c:1552:: with SMTP id f18mr39350083wmg.184.1625065358251; Wed, 30 Jun 2021 08:02:38 -0700 (PDT) Date: Wed, 30 Jun 2021 17:02:33 +0200 Message-Id: <20210630150234.1109496-1-glider@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v4 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 Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20161025 header.b=UedDNaHa; spf=pass (imf24.hostedemail.com: domain of 3jofcYAYKCB09EB67K9HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--glider.bounces.google.com designates 209.85.221.74 as permitted sender) smtp.mailfrom=3jofcYAYKCB09EB67K9HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Stat-Signature: zx6m3589ips9hdsrzjffza77sb1qtcm6 X-Rspamd-Queue-Id: BC8DAB0000B6 X-Rspamd-Server: rspam06 X-HE-Tag: 1625065359-981541 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: 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 4d21ac44d5d35..33bb20d91bf6a 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 Jun 30 15:02:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12352381 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.4 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=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 002F5C11F6B for ; Wed, 30 Jun 2021 15:02:42 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 7DD76611CC for ; Wed, 30 Jun 2021 15:02:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7DD76611CC Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id F0BE78D01B5; Wed, 30 Jun 2021 11:02:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EE18C8D01A2; Wed, 30 Jun 2021 11:02:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D0E348D01B5; Wed, 30 Jun 2021 11:02:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0020.hostedemail.com [216.40.44.20]) by kanga.kvack.org (Postfix) with ESMTP id A95028D01A2 for ; Wed, 30 Jun 2021 11:02:41 -0400 (EDT) Received: from smtpin25.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 74D191834631E for ; Wed, 30 Jun 2021 15:02:41 +0000 (UTC) X-FDA: 78310706922.25.BCBCDD6 Received: from mail-qt1-f202.google.com (mail-qt1-f202.google.com [209.85.160.202]) by imf14.hostedemail.com (Postfix) with ESMTP id 2490A6001ABD for ; Wed, 30 Jun 2021 15:02:41 +0000 (UTC) Received: by mail-qt1-f202.google.com with SMTP id w17-20020a05622a1351b0290250b0535cc4so1270215qtk.3 for ; Wed, 30 Jun 2021 08:02:40 -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=NsJPxv+Yi9Y0Cxl+/SZ8/0DfgNfKjUrGLSr4GOlizVE=; b=ZSFYc1N7Bh9wv2errbcoilHdQgrtZa4Egl/x1q8DWx087ZrALCwjG7d45PprJ7nOV+ 8ybg4z8OgnOrqDzpnDFp+d256wUotxawgE/vD2sBzRB+i9B08DGeCdhjXrw3GFVws+AS syJ+SBqSs9SGklX7ualr3e0kyhIbqSZqAXs5MsS0ReBBnE3YswrsTgZ4xk6ur3Q3qwkh mlkpUkSm1Y3v1Fgz/jYzX7caVUQz7AWqahH08YA04wZxzv4uDmF8A7Zo8QN3KPPw2DYy wtL6FSIyBrpaFv5c37rI/H2FcsggCqbNJLo/kQWTBPugSIKS0T8xVMd4H67528gI2oDi 6H2Q== 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=NsJPxv+Yi9Y0Cxl+/SZ8/0DfgNfKjUrGLSr4GOlizVE=; b=SYjdIVG12rMwg3HOI3s9jqB+msuG2CT4jqB4yEkdTj12v/bguxQUm9l048CgG7iTzu QGjOihfSbald+zPCA5VIu51ODi396XYV2+8Welua+V3FhgId/FIcdgQ/XixVyhMVeKnQ GVQMRBHBUnD4Hw1+BfFsLqNAo5AP6O9XbYAGLaVFSy2930OixW165kWekwgKxtuCWYQ0 1Ltdbu97v4iYNUALmqycwMOWuHXNRJkDC6OZR1t8EvamJZf+JFnrQbfYR3cSCKjoI64G Z1wrkocOkCDjKnZrGuMl2FBG8Oq+XeqgspB0xMfl1AeLgTiie3+3YQM4U+uOYxnU1YeH 7VQg== X-Gm-Message-State: AOAM5301MEhIQ/4EpsSyqH5HKo4qeniKdyDTwYDN5K+EBQywSfOqgOKn f66W50baC12aGMeTJo9BtGXMn6/Cl3A= X-Google-Smtp-Source: ABdhPJy7krc3G3WIMTiQX8NdRFoM6NqtuGwGFCDI6oCddBMt9ABKJsibDaVZoTeNgXV5FaqCkdk5iSEw/Sg= X-Received: from glider.muc.corp.google.com ([2a00:79e0:15:13:a3fc:e8:8089:1013]) (user=glider job=sendgmr) by 2002:a05:6214:966:: with SMTP id do6mr37584817qvb.57.1625065360398; Wed, 30 Jun 2021 08:02:40 -0700 (PDT) Date: Wed, 30 Jun 2021 17:02:34 +0200 In-Reply-To: <20210630150234.1109496-1-glider@google.com> Message-Id: <20210630150234.1109496-2-glider@google.com> Mime-Version: 1.0 References: <20210630150234.1109496-1-glider@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v4 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 Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20161025 header.b=ZSFYc1N7; spf=pass (imf14.hostedemail.com: domain of 3kIfcYAYKCB8BGD89MBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--glider.bounces.google.com designates 209.85.160.202 as permitted sender) smtp.mailfrom=3kIfcYAYKCB8BGD89MBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 2490A6001ABD X-Stat-Signature: m66igo7r33fcwp4ksu79a64zkoai6dce X-HE-Tag: 1625065361-756134 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: 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 --- mm/kfence/core.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mm/kfence/core.c b/mm/kfence/core.c index 33bb20d91bf6a..1cbdb62e6d0fb 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