From patchwork Tue Dec 19 22:28:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 13499303 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E1042C41535 for ; Tue, 19 Dec 2023 22:30:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 592D38D0009; Tue, 19 Dec 2023 17:30:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 50F8E8D000E; Tue, 19 Dec 2023 17:30:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2C6148D0009; Tue, 19 Dec 2023 17:30:23 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id F32958D0009 for ; Tue, 19 Dec 2023 17:30:22 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id CBA61C03F4 for ; Tue, 19 Dec 2023 22:30:22 +0000 (UTC) X-FDA: 81585012684.21.6875A47 Received: from out-171.mta0.migadu.com (out-171.mta0.migadu.com [91.218.175.171]) by imf08.hostedemail.com (Postfix) with ESMTP id E745116001D for ; Tue, 19 Dec 2023 22:30:19 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=qKyzmQgF; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf08.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.171 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1703025020; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=uNoAVP0QdjYefiRU2LrZ1wqxo2WmCMSn1BBV5oc4/p4=; b=4yTbi447S04aahNnDDwVuB8hrpTXWjmHYvBXJOcqXojbUoRCWQYoR51QNxe5X7XAk3swnE sb7zflcOrmKp+B6Ud888jEq+sP/aRUU5qcZtLGBWZcMKDYBMF/i62m0wN02t71XvuY8Xku zpT2F8Rrn0qL0uNU7exQbf6bBCJajDA= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=qKyzmQgF; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf08.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.171 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1703025020; a=rsa-sha256; cv=none; b=8rSXu/gwo46dJlcLbWWkbfJ07Vt3KGceVlRMdjebSDCic4Q476453Nc0eLc0TEO9+S37yC cctoI0THpTkVPejfPelgXfzl134HmXPZfvzngBnqtsfmxz6EYCs2U9phiGCg/nYT5bJVUi EgdFCbfVTgjygFSSvWywlK2D0BqNNnc= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1703025017; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uNoAVP0QdjYefiRU2LrZ1wqxo2WmCMSn1BBV5oc4/p4=; b=qKyzmQgFg/Y7DFx/QocHdWjJrVPMWCP48LD0cbTxo/AvGFiyJYN91o7BgFE+N8U+w/yHzN C6OjPdLT2mHcpxPkyqBEu2zYClKYdujcJMMPKEyXb1+/NggG2ze+zsPE+2stPIX/Yx9tta GpLx/JGbzJYPzm4QqujIB6NjsTUMg1k= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Evgenii Stepanov , Breno Leitao , Alexander Lobakin , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 06/21] kasan: introduce kasan_mempool_poison_pages Date: Tue, 19 Dec 2023 23:28:50 +0100 Message-Id: <88dc7340cce28249abf789f6e0c792c317df9ba5.1703024586.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: E745116001D X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: pa3eho1nkpepq5i6jehoc4ec4zz74wjp X-HE-Tag: 1703025019-348941 X-HE-Meta: U2FsdGVkX18yYiyU17sPL80HbrUsSXUiEvC4FYcYCKDYtKujXsiGFhC+KS+bBqlAlPN2Q3u0AoTqhGH8iirSIBOVTp41PVjRs1vQsCYaqXurq9wG1ApSGjxv8NZGoZMc+yN44APHCO5N4VIfVKp50c7Wa4E8uQRsY6K/LCI8ZymeVfhXSau735XtJY0eTZKRy5NGvh78tVibdTRb/GImMyHwazfPUrKwvND8bUK9FhWccbANESz8t+6y+FnyKSKqL9uqXJxoaK1vEr8kXyb1W/3+7C5BDeHEloOvkXILfdj6zraILiNS1S0/2pUG0LYDquo94GNXjoKGuELtOcYkzbsesDwThGmBtM5MC4g+mXakeoaPsrcWG7VYe/9P5lLl4u7VWbx3t6CS/bruI7BRM6yrdOsSPm/8dXT1X7wn5ABPpWZE2+Rlj2vH4t9L1oq6HSKJnsyeL0FFciaGFGuLC9e31WjUYLNTX146DQQLH3q+xEHgIhCv3395MpYOsuA7okmSHLHQYHYeEjv1+Tlm+eWqWi0whA9IWIGSFi1dRcqpqgvj460WkN87qAq2kvW+klyBXnKOjCCPCtxyE9MlFqPbP7TSuh2ll3StyzM2sGirZwEHdE8gyxNQA/yCgK2rTCUsXmzUylcIBDd/NziHLRx8MW3hPcMd4AurroS9ccySYGBhrvd/UVQaM4ZXzy1fED/burOQZ0Cu5TEBs3jMToRMRhkHIECuS+/d4S0+2FEqHvgmdt8qW051Wuq2wkVTrJF+albijPqxLwO9p/ZGoMtwgN2E0EtRTpaHUpqZsUwK4Z8i1o03ql/j5uiAQgcHWl9qcdZ+kmv4ajCSg/3o4O45NcWCzxvex3Mkd3X3d5BGDJ8GKTG9Kk6cFDVauM689tIfei2mqoaynpBaBd7raqMaA78P0vWc/gLCOIIb0TgLxKXzJ/T37cAHqPAa0x/s6t3vJEpSw0Y3LYkcjCL jsYzG5Bo ErMDgf8nlMQ2/Qi9XklGekV4KCfouoRIUk6JkiihRZDM7TQiPQ0NbG+65crRQvYy0x7k1d/eO0vy7sWa/zbsBivRw0YIWyHgFW037y5xh1BKLT6OCXh6tWW/C4S7dzNcfVBmfK7ML3UPYir2yuFlmY39GeQjBmTteaj8K7quXmdXConicouPMk2USJGUQz51MUkOlv/hblbbTjJ5GG4c7JzDKEUpfR8IZHV0I 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: List-Subscribe: List-Unsubscribe: From: Andrey Konovalov Introduce and document a kasan_mempool_poison_pages hook to be used by the mempool code instead of kasan_poison_pages. Compated to kasan_poison_pages, the new hook: 1. For the tag-based modes, skips checking and poisoning allocations that were not tagged due to sampling. 2. Checks for double-free and invalid-free bugs. In the future, kasan_poison_pages can also be updated to handle #2, but this is out-of-scope of this series. Signed-off-by: Andrey Konovalov --- include/linux/kasan.h | 27 +++++++++++++++++++++++++++ mm/kasan/common.c | 23 +++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index c5fe303bc1c2..de2a695ad34d 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -212,6 +212,29 @@ static __always_inline void * __must_check kasan_krealloc(const void *object, return (void *)object; } +bool __kasan_mempool_poison_pages(struct page *page, unsigned int order, + unsigned long ip); +/** + * kasan_mempool_poison_pages - Check and poison a mempool page allocation. + * @page: Pointer to the page allocation. + * @order: Order of the allocation. + * + * This function is intended for kernel subsystems that cache page allocations + * to reuse them instead of freeing them back to page_alloc (e.g. mempool). + * + * This function is similar to kasan_mempool_poison_object() but operates on + * page allocations. + * + * Return: true if the allocation can be safely reused; false otherwise. + */ +static __always_inline bool kasan_mempool_poison_pages(struct page *page, + unsigned int order) +{ + if (kasan_enabled()) + return __kasan_mempool_poison_pages(page, order, _RET_IP_); + return true; +} + bool __kasan_mempool_poison_object(void *ptr, unsigned long ip); /** * kasan_mempool_poison_object - Check and poison a mempool slab allocation. @@ -326,6 +349,10 @@ static inline void *kasan_krealloc(const void *object, size_t new_size, { return (void *)object; } +static inline bool kasan_mempool_poison_pages(struct page *page, unsigned int order) +{ + return true; +} static inline bool kasan_mempool_poison_object(void *ptr) { return true; diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 4b85d35bb8ab..b416f4c265a4 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -426,6 +426,29 @@ void * __must_check __kasan_krealloc(const void *object, size_t size, gfp_t flag return ____kasan_kmalloc(slab->slab_cache, object, size, flags); } +bool __kasan_mempool_poison_pages(struct page *page, unsigned int order, + unsigned long ip) +{ + unsigned long *ptr; + + if (unlikely(PageHighMem(page))) + return true; + + /* Bail out if allocation was excluded due to sampling. */ + if (!IS_ENABLED(CONFIG_KASAN_GENERIC) && + page_kasan_tag(page) == KASAN_TAG_KERNEL) + return true; + + ptr = page_address(page); + + if (check_page_allocation(ptr, ip)) + return false; + + kasan_poison(ptr, PAGE_SIZE << order, KASAN_PAGE_FREE, false); + + return true; +} + bool __kasan_mempool_poison_object(void *ptr, unsigned long ip) { struct folio *folio;