From patchwork Wed Mar 17 08:47:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Elver X-Patchwork-Id: 12145107 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, 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 9D2EAC433E0 for ; Wed, 17 Mar 2021 08:47:48 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 3F99264F09 for ; Wed, 17 Mar 2021 08:47:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3F99264F09 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 BE8726B006E; Wed, 17 Mar 2021 04:47:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BBF8C6B0070; Wed, 17 Mar 2021 04:47:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A61406B0071; Wed, 17 Mar 2021 04:47:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0049.hostedemail.com [216.40.44.49]) by kanga.kvack.org (Postfix) with ESMTP id 89D406B006E for ; Wed, 17 Mar 2021 04:47:47 -0400 (EDT) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 4FA0A181AF5CA for ; Wed, 17 Mar 2021 08:47:47 +0000 (UTC) X-FDA: 77928738174.04.38CC978 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf12.hostedemail.com (Postfix) with ESMTP id E500713A for ; Wed, 17 Mar 2021 08:47:46 +0000 (UTC) Received: by mail-wm1-f73.google.com with SMTP id v5so10394750wml.9 for ; Wed, 17 Mar 2021 01:47:46 -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=mQ9lVwNHA8xNGK/XyR+U8c7RoXUhm2JAea9oSpZR7Gk=; b=PqGjPWsZbO2fcKM9P4rv6POFLtYh4YOitqiF+q7Zg2a2KPrY6b7wV/O9TFvNlAJWS2 MJH8Kue1m2y3aBhCR6X/8L+mdgkMWeRTyNQjfD18x2JfAUTT8qgVJCgZ6WPq6RMAXNzA 7e9VE2eiZ65Xbwbi36YNXS5iyOSjgho+3ROFavU3ltnD0+ABCTKtQryUzeum3vFlpBU7 HY/XACtWT4/bbDHnIwWkmicvgZptD8MtfIgZkgE9otdYGy4e0ydUfU7SkN7bp8PoKyV6 Auzno6pwAz2gnNf5v+TTaaBnDOS9Lh1SC0U6pnCFbGytNO4FkkdxB2JWHyYLZsgw0T25 3g8w== 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=mQ9lVwNHA8xNGK/XyR+U8c7RoXUhm2JAea9oSpZR7Gk=; b=akdKfmNiQj3Gwf3Sn1sKI8TFBGS/Iu4frWc/0BhK0jFxWJ3p+RFSWOVQsIEV5jmxCf Fl0GdjI4+K+2FlyW9qPhS9K+exeWSs7O5+OylJ+8O8fDRVJPsUGjFtj2kzMtctJI968V 5zvUht7piGqo+TqNIV1ujcz5+VZDupzk2CVHo5jEjp0b1CuD4qDcwVXcXhom/TMvS7xB I2niBW4Y8JBBoEkZLiePmvftLWsQM+X+jEjU7+AUAP0d49KfZdtR6MNOuxwNbg1lJRK2 EJ9jG1ykVgky2Idy3fhndEYCBeB8W0u+MRisdzj+mhLkiUUh8mWlOFBaKkIFTh4oFvy8 n7TA== X-Gm-Message-State: AOAM531+gASSBY/hhbl0rt85Xcsyy6oKqFaULX6NaOS/b1fUgAP88vzw vIBd0A8dckSycpP02GbiwIk05f0S+A== X-Google-Smtp-Source: ABdhPJyAxCG5vAkt29ipWf6uJkzF45lSYb3n8NVyVhwEJL5o0nZ3JUAkE9TJevfWiJrpEB38wUVZL/1gWg== X-Received: from elver.muc.corp.google.com ([2a00:79e0:15:13:1d09:9676:5eaa:550]) (user=elver job=sendgmr) by 2002:a7b:c047:: with SMTP id u7mr2656333wmc.98.1615970865676; Wed, 17 Mar 2021 01:47:45 -0700 (PDT) Date: Wed, 17 Mar 2021 09:47:40 +0100 Message-Id: <20210317084740.3099921-1-elver@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.31.0.rc2.261.g7f71774620-goog Subject: [PATCH mm] kfence: make compatible with kmemleak From: Marco Elver To: elver@google.com, akpm@linux-foundation.org Cc: glider@google.com, dvyukov@google.com, andreyknvl@google.com, jannh@google.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, kasan-dev@googlegroups.com, Luis Henriques , Catalin Marinas X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: E500713A X-Stat-Signature: uacofs8jky3n5yrbsnocjfn996xnxye3 Received-SPF: none (flex--elver.bounces.google.com>: No applicable sender policy available) receiver=imf12; identity=mailfrom; envelope-from="<3McJRYAUKCAYkr1kxmuumrk.iusrot03-ssq1giq.uxm@flex--elver.bounces.google.com>"; helo=mail-wm1-f73.google.com; client-ip=209.85.128.73 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1615970866-602833 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: Because memblock allocations are registered with kmemleak, the KFENCE pool was seen by kmemleak as one large object. Later allocations through kfence_alloc() that were registered with kmemleak via slab_post_alloc_hook() would then overlap and trigger a warning. Therefore, once the pool is initialized, we can remove (free) it from kmemleak again, since it should be treated as allocator-internal and be seen as "free memory". The second problem is that kmemleak is passed the rounded size, and not the originally requested size, which is also the size of KFENCE objects. To avoid kmemleak scanning past the end of an object and trigger a KFENCE out-of-bounds error, fix the size if it is a KFENCE object. For simplicity, to avoid a call to kfence_ksize() in slab_post_alloc_hook() (and avoid new IS_ENABLED(CONFIG_DEBUG_KMEMLEAK) guard), just call kfence_ksize() in mm/kmemleak.c:create_object(). Reported-by: Luis Henriques Cc: Catalin Marinas Signed-off-by: Marco Elver Reviewed-by: Catalin Marinas Tested-by: Luis Henriques --- mm/kfence/core.c | 9 +++++++++ mm/kmemleak.c | 3 ++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/mm/kfence/core.c b/mm/kfence/core.c index f7106f28443d..768dbd58170d 100644 --- a/mm/kfence/core.c +++ b/mm/kfence/core.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -481,6 +482,14 @@ static bool __init kfence_init_pool(void) addr += 2 * PAGE_SIZE; } + /* + * The pool is live and will never be deallocated from this point on. + * Remove the pool object from the kmemleak object tree, as it would + * otherwise overlap with allocations returned by kfence_alloc(), which + * are registered with kmemleak through the slab post-alloc hook. + */ + kmemleak_free(__kfence_pool); + return true; err: diff --git a/mm/kmemleak.c b/mm/kmemleak.c index c0014d3b91c1..fe6e3ae8e8c6 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -97,6 +97,7 @@ #include #include +#include #include #include @@ -589,7 +590,7 @@ static struct kmemleak_object *create_object(unsigned long ptr, size_t size, atomic_set(&object->use_count, 1); object->flags = OBJECT_ALLOCATED; object->pointer = ptr; - object->size = size; + object->size = kfence_ksize((void *)ptr) ?: size; object->excess_ref = 0; object->min_count = min_count; object->count = 0; /* white color initially */