From patchwork Tue Dec 19 22:28:51 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: 13499300 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 56AAEC41535 for ; Tue, 19 Dec 2023 22:30:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EADA38D000A; Tue, 19 Dec 2023 17:30:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E5DD98D0009; Tue, 19 Dec 2023 17:30:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D269B8D000A; Tue, 19 Dec 2023 17:30:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id C2E0D8D0009 for ; Tue, 19 Dec 2023 17:30:21 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 95D751A07EE for ; Tue, 19 Dec 2023 22:30:21 +0000 (UTC) X-FDA: 81585012642.09.377977B Received: from out-174.mta0.migadu.com (out-174.mta0.migadu.com [91.218.175.174]) by imf28.hostedemail.com (Postfix) with ESMTP id 14816C001A for ; Tue, 19 Dec 2023 22:30:19 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=mmCEXgMa; spf=pass (imf28.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.174 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=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=WvnDVDl+9gd8JY/2icI2mu85dJxeSXpMDACLsdprvYg=; b=lRQEdqFOqiOyeHrrtJeYpQCdahmod/YGPL41+feMK8VCg3ySOeHpSSI6Cy5ydBksPFayjl H+WlwsSpF2M4JoyAnW7DuwxWf45iYAj8dOzrlhHEzNcScTg3J3KdCn6bD7Yv57hBFTT1iC PGFZ/rEZo6hfr/oxnVAHP1X4q9FXNZs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1703025020; a=rsa-sha256; cv=none; b=cthS6jHRh6BrnSor1EIGKB9okrlyNXAB3MqGcyRuJek4aDh0921IF8XZ9FU5/qvin+266Z QlwUqf7V43n+sxq+hIu0hM9WyvwLe9a0fU+xf18TUHppnCWcLnoi4vCe4w91B40uxg7+tb NdwXpjqkUFcw8ewcIominkfrywqgc6s= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=mmCEXgMa; spf=pass (imf28.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.174 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=1703025018; 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=WvnDVDl+9gd8JY/2icI2mu85dJxeSXpMDACLsdprvYg=; b=mmCEXgMaxz3LTDhN5Aelii+nb4wwQAwSPgj8RLHqQ7+wmGOwxjtSh8dT+vmH5EYV5C6oSg QM5LoKW2tweYqFLGq6U5930x/xHPsIBo/zhwS0iRxm/r+0RSCb6zBs2bH9dNdRzIbXalym qshEQaM+itGw4MLCZQr6zHqTS6ub/qA= 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 07/21] kasan: introduce kasan_mempool_unpoison_pages Date: Tue, 19 Dec 2023 23:28:51 +0100 Message-Id: <239bd9af6176f2cc59f5c25893eb36143184daff.1703024586.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Stat-Signature: h8e8ncczaz968k9g4e39uz17gtxqbspf X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 14816C001A X-Rspam-User: X-HE-Tag: 1703025019-527414 X-HE-Meta: U2FsdGVkX18EetFwCmI13Psq5mmrkh4mR11b21MGNN9gcwJ/eQJec96u6G5Q1lakUTGw/VxLcTw/5BJMSUcBO//xvdrIAVI4gmyrxxdwXnu6Y6uTrDFuePelVRaBHW/ITQGQzcqOVg5lqRpz+Aaut4H3J1Zye5ODeWIygXUVfXDUHPmABlWgx74N1TCFGT4rrYou1PyUHEhm7+ZGQPXUEXsYPn/7QrnbqiktDHx4ur6B8ZeyIlydff8JsB4lXxvhTv4A0FifcSpdhkVsUt77zjhAHZmzg4pjKZBDGTxCV7N1GKjxa7R7YeQ+6nj63vcaq0v58Q3aakUiYJRKrAeEx0dUCCm9SL2N320OoVEW+FO1XQKTLN0aSzBR2z8EJlRjGrLHODROMwT8Y77GwkQgUNMbLmK+J8ethToIAZxjZoobQcLmuUFgxyi1/uGGfgkbr7kLPdasgn7WAXHDL19Id1J+2INpwh7TXjZUqpc8jGpojnm0M0k9HTPbW4AQaRfr7NUXCJm0Pl3hxPdsabG1ANOuheCP+mWjf0F+A/wM0mio+ORokaCazSK0r708dqnoFbasj/FJejvVCB+e0V2tNp9QFSrsca1qfaCOenUrkhltCI/fYUGKZmy7B4HLPaMss7Gn90Nydzp9iN50j1pmIlY+/B50Ia0HRikv1cBRcoUtASiUOvRJ398Z1nhBtay3mKHs8HsFLtUJQ7hRvscjzTSh+IajXItqDgciJXHyZ0fh1QzSCBW2Qz/6i66JxPx2BGtG0JEdxT6UxhUc1D73Qiqru70lvDVtA5o5Da8NSdoSFntkQzS91UtqQtJdwgh6MHYHsC9l4vPK5kKIK0fg5B3Q1S6oOoSsnNf/XF7AqjBfYAXalxXPiIDlxxB0gqynO2XObFHQRAdd2MyB5zJNvChc9Q/DI52u7Zp5M0v7jbWyq9BqBmNRgPlldA90D7je5p8nml9wWaI= 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 b416f4c265a4..7ebc001d0fcd 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -449,6 +449,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;