From patchwork Mon Nov 6 20:10:16 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: 13447422 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 39CF2C4167D for ; Mon, 6 Nov 2023 20:11:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 15CA06B0294; Mon, 6 Nov 2023 15:11:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 10FA06B0295; Mon, 6 Nov 2023 15:11:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EC5186B0297; Mon, 6 Nov 2023 15:11:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id CEB036B0295 for ; Mon, 6 Nov 2023 15:11:42 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id AA508160864 for ; Mon, 6 Nov 2023 20:11:42 +0000 (UTC) X-FDA: 81428624844.11.D6EEFDD Received: from out-186.mta1.migadu.com (out-186.mta1.migadu.com [95.215.58.186]) by imf29.hostedemail.com (Postfix) with ESMTP id BC4ED12001B for ; Mon, 6 Nov 2023 20:11:40 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="nqNu/iJt"; spf=pass (imf29.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.186 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1699301500; 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=Z8UoFAswFgd+L4wk44THWP2F8PtTdf8hA2eur1Xmylc=; b=QsowvAoiBZOf6V9YfJqDvP8Y45BHsW4DpV65Et5iqYmDOU35WSDq2xcs4puXLn5p2OA29n Sqe5qMSYGISuitkyEtTwp/XkQFGMl/VxgTcXpxbMGvcGB9plINYLHxCj7JQbkMV9+ZP9A2 1mQYpI5aWdVew3SVD+CrD7mCHn9Rqq0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1699301500; a=rsa-sha256; cv=none; b=443wJUhdeevb9+PlgvOT3zGwP4P2GxjYPYxW5uUfRFO+IMkZgCFtJbEgR+8RVRhi7PAOwl M/KDcd0SsdSk6akBPwXuylBV/DSsgtpaJNL//A9uHdWr2gkodpk4HlRt86osNyZ2FZ0hfr IttjdqGXZtNrNjVCy+xai1r8C6fAPOg= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="nqNu/iJt"; spf=pass (imf29.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.186 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev 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=1699301499; 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=Z8UoFAswFgd+L4wk44THWP2F8PtTdf8hA2eur1Xmylc=; b=nqNu/iJtTBpRGoCOjf1lL74liuMLk3YhpzpUWzj3S+HMg+GQeM+EOVnxft+z4OPpPQbPuS Udqa+ZKx2NChvvQUHxkJu+aUnZoO5Chyi4YdPGofo1DOrNugA3NlPhfyyz2+pR+R6ojuqt 30m2oaGb1ReuPP6HOsDoxxp4rcV7+Ok= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Evgenii Stepanov , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH RFC 07/20] kasan: introduce kasan_mempool_unpoison_pages Date: Mon, 6 Nov 2023 21:10:16 +0100 Message-Id: <573ab13b08f2e13d8add349c3a3900bcb7d79680.1699297309.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: BC4ED12001B X-Rspam-User: X-Stat-Signature: 5xnre8bsp3nfn9ipw8hf6cysqhngta4w X-Rspamd-Server: rspam03 X-HE-Tag: 1699301500-53200 X-HE-Meta: U2FsdGVkX18RJAe5fpqHvvdxI/QKsjExKIMDpJFUyR9uicxqcnaNKqe99SdFLizSFGICgnSMBXXX1wxgJ7yVUVfR4jSTQqs5i+LjBxyyuLt0g/Qs6LZgxdXsTpZqtkmsFNweQOH5CL6laT65tdJn1tESyo/zLZD1GGFrUgNLcalSBo0Zpp6WgJWTf2dpHvdLD3Mub6Y7YAiwh3R6jaR7IguhMpe1ZB4MoDu6L9pvhtoOzyFLl+R+os0tYfC4EGag9skvMwP0cJQ26aEN3EHVnOkTJIa/UySRQwcuhJVXGbogPierEPwy6Kd+qR06QlJb5L935tMP7ZDBpO/sTtAj2v9TyuY7Ip3VJ0AH6pfus/OJ3Yzbq2AlSCBEhbvNH7VgLJ1cP1eSP3hKLLk0q1AZ6pBDpshkBTu4nbOmAJ8fK7VC+j9ae9WicjIe46pVvav2C6dzD6vxNZdDes6svQESeFW/EKwsTVANkh1+a1Vrme20oHakDS30p06aAwODHxCCI3GDR4SBCJqHx2ANstpoN4D7rBjgroNiQ0Iehavhwot9WEcChoEWlGZlFXaoEg0mnoPWJl6V2euNREgeKxqWn6hVCMB+5xUX8TZPmch2zCnCrw1LGBs5ihsnj+9EO4WEpq8qiU8nymoc6isN+r0K70ZjAj5KY6kn5mqvSiAKpU6rS4WD5WJJ72vatTKVcrNoxDPne0oQPncQ48oWJ3oaajw3FgawJotRGtd41on8tOZLosKAa0dOIKx+mSWMWk7KahiNSM4NytQBMDZTEKXL7MP6d/iG5CMAVEZQPpOrzydhXYvpO3P1rLLfozuNuhrnSi1JP71JMYHgwwI8jVyqSz7oUzE9zfy1nlBwWNQhr1+1XRcAnm8fDLeraiH+buUOXLf9ruwDqfnBfSAlHkwm8KWxdNdgKInztvgyhscrDNVAVOSdsiIVHLbxMf/w1j7yyvoP8CLJO6DprVszZKS n8BLNiBh lzn8UGBaSNDv5WZ/z9sngMDIlvbaCISDMBVz3Q/YOgr0pBOJB6WnP/AnnEjo+nsYsbOHJrFY0ILT6F5DwvQ9hDssmrCssJ+U3+WejkORsESgWZUgE/helwZcU9pGCBKl9EjglDg/YTjGDauiPGHUfHkM30q/C+8wVqBl6qkVl06FftexMpICYLrPnItHnU5iM5se0CN/1sWj5UlFed9OGAK72fDgQ8tvtHiij 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 new kasan_mempool_unpoison_pages hook to be used by the mempool code instead of kasan_unpoison_pages. This hook is not functionally different from kasan_unpoison_pages, but using it improves the mempool code readability. Signed-off-by: Andrey Konovalov --- include/linux/kasan.h | 25 +++++++++++++++++++++++++ mm/kasan/common.c | 6 ++++++ 2 files changed, 31 insertions(+) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index de2a695ad34d..f8ebde384bd7 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -225,6 +225,9 @@ bool __kasan_mempool_poison_pages(struct page *page, unsigned int order, * This function is similar to kasan_mempool_poison_object() but operates on * page allocations. * + * Before the poisoned allocation can be reused, it must be unpoisoned via + * kasan_mempool_unpoison_pages(). + * * Return: true if the allocation can be safely reused; false otherwise. */ static __always_inline bool kasan_mempool_poison_pages(struct page *page, @@ -235,6 +238,27 @@ static __always_inline bool kasan_mempool_poison_pages(struct page *page, return true; } +void __kasan_mempool_unpoison_pages(struct page *page, unsigned int order, + unsigned long ip); +/** + * kasan_mempool_unpoison_pages - Unpoison 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 unpoisons a page allocation that was previously poisoned by + * kasan_mempool_poison_pages() without zeroing the allocation's memory. For + * the tag-based modes, this function assigns a new tag to the allocation. + */ +static __always_inline void kasan_mempool_unpoison_pages(struct page *page, + unsigned int order) +{ + if (kasan_enabled()) + __kasan_mempool_unpoison_pages(page, order, _RET_IP_); +} + bool __kasan_mempool_poison_object(void *ptr, unsigned long ip); /** * kasan_mempool_poison_object - Check and poison a mempool slab allocation. @@ -353,6 +377,7 @@ static inline bool kasan_mempool_poison_pages(struct page *page, unsigned int or { return true; } +static inline void kasan_mempool_unpoison_pages(struct page *page, unsigned int order) {} static inline bool kasan_mempool_poison_object(void *ptr) { return true; diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 9ccc78b20cf2..6283f0206ef6 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -439,6 +439,12 @@ bool __kasan_mempool_poison_pages(struct page *page, unsigned int order, return true; } +void __kasan_mempool_unpoison_pages(struct page *page, unsigned int order, + unsigned long ip) +{ + __kasan_unpoison_pages(page, order, false); +} + bool __kasan_mempool_poison_object(void *ptr, unsigned long ip) { struct folio *folio;