From patchwork Wed Jun 30 13:53:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12352189 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 06093C11F65 for ; Wed, 30 Jun 2021 13:53:24 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8DEEF614A7 for ; Wed, 30 Jun 2021 13:53:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8DEEF614A7 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 E2B508D01AB; Wed, 30 Jun 2021 09:53:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DDB058D01A2; Wed, 30 Jun 2021 09:53:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C7BBF8D01AB; Wed, 30 Jun 2021 09:53:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0180.hostedemail.com [216.40.44.180]) by kanga.kvack.org (Postfix) with ESMTP id A58A68D01A2 for ; Wed, 30 Jun 2021 09:53:22 -0400 (EDT) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 5FAA3180883C9 for ; Wed, 30 Jun 2021 13:53:22 +0000 (UTC) X-FDA: 78310532244.16.4A1A082 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf08.hostedemail.com (Postfix) with ESMTP id 23EFA30000A5 for ; Wed, 30 Jun 2021 13:53:22 +0000 (UTC) Received: by mail-wm1-f73.google.com with SMTP id k16-20020a7bc3100000b02901d849b41038so2867293wmj.7 for ; Wed, 30 Jun 2021 06:53:21 -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=dtH0IQ32mjHBoYzYQxUVAIZ8CYFo4XITSUGYSoSmvuA=; b=n8OrDYvB4lu0VQjvPLvwRPRKVdXxpGavb6OmQT8FlcNgMfasGdem5Xe1GYKcQwQLjF eqsZ/j6Bbhjo4RwvKBjlIA3938u9XJL5kaLuPE+AemK9oldlEH9n/O7jYcK/nQm3nGC/ Nodeu3rbuP4Y585s02N0WHsbJ2x4Ya5xzJ+LCr23nVTYRDU3lUUHASCch1pHLLVWRqzx qK2MpDY+sFqPLYhBmtHXEGpiDV69xYuYF+iyRSt6NjSv0Iz7u/pnaDM80+VLCjkUWrmC MpK9GM8HVUwec7+08q8yLbjtgq8yh5ljCYRIegMIXTR9+YZw73NAB/ZVV8kyVtmNYwC6 MRIQ== 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=dtH0IQ32mjHBoYzYQxUVAIZ8CYFo4XITSUGYSoSmvuA=; b=j4hnns5Cc2IodtlmUlKpiRlIf6mwoGJao6oQfH4X/5L4/YF97Lo/ksOErxUAoh7zEE u6LYRIp2a5IANI2IRX+X7xftRYPv+P08anl1gkI4coDL+vftG90n01IZ9ksnXDo9tC6E BQz5bgVK0mMQA17R2q9Boe4VuFwyLPe6TxkMmwLjLQWoSkPb0X7De7/bUGxxBGYf8Gs+ YbJKbMB3k8XICUM7PspLQekhyA6yr/bPHpyBqWV1fLPzbTRAWei/RRLlhAFfcUORYi5h l4EmNvLWowrAMNFTsG51Q2pyJ41L7pbmTm5I1LqwEtkxid3eldtCxFMdntkhg0AEO0PU rGtw== X-Gm-Message-State: AOAM533mZayC2LoIgduf0ebAi5zXHkpTnMLIXhvOBO+Sw/m4DTeRl63K r9pV481yRKUYSQgP4XUxl5pqkMNI67E= X-Google-Smtp-Source: ABdhPJxEOOWrV1qY5ZQLKAMR0q+wxV55Lm0/JLt5nQqznaRHf1ul1AN8gIWXEvsyC132NIT7onyLV9XuHP4= X-Received: from glider.muc.corp.google.com ([2a00:79e0:15:13:a3fc:e8:8089:1013]) (user=glider job=sendgmr) by 2002:a1c:638a:: with SMTP id x132mr4681717wmb.90.1625061200569; Wed, 30 Jun 2021 06:53:20 -0700 (PDT) Date: Wed, 30 Jun 2021 15:53:12 +0200 Message-Id: <20210630135313.1072577-1-glider@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v3 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: imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20161025 header.b=n8OrDYvB; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf08.hostedemail.com: domain of 3UHfcYAYKCL0jolghujrrjoh.frpolqx0-ppnydfn.ruj@flex--glider.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3UHfcYAYKCL0jolghujrrjoh.frpolqx0-ppnydfn.ruj@flex--glider.bounces.google.com X-Stat-Signature: kf9fsw8m7pcxt4f9w1wdjmwbhkeq49hq X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 23EFA30000A5 X-HE-Tag: 1625061202-856710 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 13:53:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12352191 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 3173EC11F65 for ; Wed, 30 Jun 2021 13:53:26 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E4394614A7 for ; Wed, 30 Jun 2021 13:53:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E4394614A7 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 5EEA08D01AC; Wed, 30 Jun 2021 09:53:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5C57F8D01A2; Wed, 30 Jun 2021 09:53:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 466568D01AC; Wed, 30 Jun 2021 09:53:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0123.hostedemail.com [216.40.44.123]) by kanga.kvack.org (Postfix) with ESMTP id 249978D01A2 for ; Wed, 30 Jun 2021 09:53:25 -0400 (EDT) Received: from smtpin01.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 9ECFE1DE6F for ; Wed, 30 Jun 2021 13:53:24 +0000 (UTC) X-FDA: 78310532328.01.D43445E Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) by imf13.hostedemail.com (Postfix) with ESMTP id 5E6B11000145 for ; Wed, 30 Jun 2021 13:53:24 +0000 (UTC) Received: by mail-wr1-f73.google.com with SMTP id u7-20020a5d46870000b029012786ba1bc9so986914wrq.21 for ; Wed, 30 Jun 2021 06:53:24 -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=+15b98OXWiOrEiKCGfnfeWun+23tJxDlHH/wqzYj+20=; b=WXi9U1Vip4zaz3uBNoqN8a2omyzEGIgwOHLP1ooDckRXK/3CMYy03EQAgTTOXhOVsW jmdHZZ/yF5Ox7Fm9TusoCFGZ0KQvVrQD8c0rnGTiyFQ5kYC/sOYOD8RhHBbipYvQbkua hbATd9xyZ/oofzWhQ8fJ7n2GYJpKXKgTNuIMJvXeJKLcEiWZqt4oH73eDwErt19gObFj ePK7x/B/Ih4lHpv4jC71OibbjGzGJnOtWFBhjgCiOsYOdkloQyCxlTiyVmI87HqAC84k 5hnGD95dAdkRHC2aMiJXl0S33hgVvZJ0D7hXugZAczLXqj4T1Pd3i6XgmT6Mspy8gKv5 hxBg== 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=+15b98OXWiOrEiKCGfnfeWun+23tJxDlHH/wqzYj+20=; b=ledEnaASK1zD56ndpcQ41vIO6cm/sq5Y9tIPdBSMwcVk9bprPodsosIwyqDvSlQyim MPSqYUcodN7HVhCmjvGIV0Lrj08/idFkqqdkki8tHSdGgdaIm5BhD7o75UC8qco+y12k Oe0MMSk7kG7JLEa8XRvMeaM7c8RwK3bFPGRtqv6UI9m2wnBZKYv79PhkEYEpwrqzmN4x tucc3bPGwa4PUckfGgG9vd6M9uCd5rdiDwpUyKvpk9neX1B5bSqBAG4yHaFDXW47Xrgr 6/BRIcFNIFf/otJxfsgRRZQp8kOTM1wO138fhvoaZR0RvRPDKQrk45RjUCAEyG8T6XOF uFrw== X-Gm-Message-State: AOAM533Ba3l2mZEBJs/qMpRbv6FdC2qKJLquIA12ugwzE1Cwy8FOgEmW OMprz0d0FezKa6qBTOxT7IOCI3f+sNc= X-Google-Smtp-Source: ABdhPJwZS4PQ4Qo0dgN0XnUOHZBFG5LDZXGSPSb4r9wuy4xppmcnGhAYbCV0/Tahu7HBzZ78p13B8Ltwwjo= X-Received: from glider.muc.corp.google.com ([2a00:79e0:15:13:a3fc:e8:8089:1013]) (user=glider job=sendgmr) by 2002:a05:600c:3504:: with SMTP id h4mr4860507wmq.118.1625061203035; Wed, 30 Jun 2021 06:53:23 -0700 (PDT) Date: Wed, 30 Jun 2021 15:53:13 +0200 In-Reply-To: <20210630135313.1072577-1-glider@google.com> Message-Id: <20210630135313.1072577-2-glider@google.com> Mime-Version: 1.0 References: <20210630135313.1072577-1-glider@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v3 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: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20161025 header.b=WXi9U1Vi; spf=pass (imf13.hostedemail.com: domain of 3U3fcYAYKCMAmrojkxmuumrk.iusrot03-ssq1giq.uxm@flex--glider.bounces.google.com designates 209.85.221.73 as permitted sender) smtp.mailfrom=3U3fcYAYKCMAmrojkxmuumrk.iusrot03-ssq1giq.uxm@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 5E6B11000145 X-Stat-Signature: mg8ou8agbo6uxobwdr49i6u4ao5stj84 X-HE-Tag: 1625061204-719479 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 DNA) 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 --- 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 --- mm/kfence/core.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/mm/kfence/core.c b/mm/kfence/core.c index 33bb20d91bf6a..d51f77329fd3c 100644 --- a/mm/kfence/core.c +++ b/mm/kfence/core.c @@ -740,6 +740,14 @@ 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