From patchwork Mon Jun 13 20:13:52 2022 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: 12880068 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 067E2C43334 for ; Mon, 13 Jun 2022 20:15:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B5C408D01BF; Mon, 13 Jun 2022 16:15:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ACBCD8D01C2; Mon, 13 Jun 2022 16:15:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6FBA18D01BF; Mon, 13 Jun 2022 16:15:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 55BA98D01C0 for ; Mon, 13 Jun 2022 16:15:26 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id 2F63180E9E for ; Mon, 13 Jun 2022 20:15:26 +0000 (UTC) X-FDA: 79574317452.26.AA18EF2 Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by imf14.hostedemail.com (Postfix) with ESMTP id 79EA210006F for ; Mon, 13 Jun 2022 20:15:25 +0000 (UTC) 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=1655151324; 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=M2+xlJvtrt/Ak+lac7lfuJjlG7HoHnjjsXRwZzO2wkQ=; b=D7+1mF++R9rQSyexDmLe4W43C6kZeCzNSBrDNdQTA+MttNF7gXjdHPZhzzpixGpWrrZc6t QTCBf0IP0biwlIgnvWlisHawkeaiNLKJyhDsmcZfG5NfvfKzCCHagM+dR5BuUGdO4uv0TP LRUfpmJCeWw5nPyMl1dFBPg0mKF7iiw= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 01/32] kasan: check KASAN_NO_FREE_META in __kasan_metadata_size Date: Mon, 13 Jun 2022 22:13:52 +0200 Message-Id: <91406e5f2a1c0a1fddfc4e7f17df22fda852591c.1655150842.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655151325; 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=M2+xlJvtrt/Ak+lac7lfuJjlG7HoHnjjsXRwZzO2wkQ=; b=BzGkwjW0xP3y03aIWoJgy5GK27mn3g3Je0bBwx7fvfHUDTvL2bLct8x7FjQ+/9d/kEI9hP 22eHJvzGWza6qT/O1hdEEkpXhWKdcHpEQgsk4W++3KkT8bn29ZMquLAw+/h4BfoeObF3kH EtuJ2glrNARuZK2xVibFVzMpgGF4Yg8= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=D7+1mF++; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf14.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151325; a=rsa-sha256; cv=none; b=n/kAggZsQ+rGNxFd86Wl7WA2gxhYipP/Fj03kxF1Bhbg1QY6NPDzQ1+gHuyZMe9HmYDCYL x08FnzFuSfVQc2p0h20nWu0EmktGMnku6qfXzyO7guzp86QanvZwSCr03P0nUAbDlWKwyG v2ZomCUs7xrssJD0iRiC72Y8P50iJno= X-Stat-Signature: 6c6wz8s74trcsstq815cpbjipg4dpsze X-Rspamd-Queue-Id: 79EA210006F X-Rspam-User: X-Rspamd-Server: rspam05 Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=D7+1mF++; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf14.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-HE-Tag: 1655151325-951243 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: From: Andrey Konovalov __kasan_metadata_size() calculates the size of the redzone for objects in a slab cache. When accounting for presence of kasan_free_meta in the redzone, this function only compares free_meta_offset with 0. But free_meta_offset could also be equal to KASAN_NO_FREE_META, which indicates that kasan_free_meta is not present at all. Add a comparison with KASAN_NO_FREE_META into __kasan_metadata_size(). Signed-off-by: Andrey Konovalov Reviewed-by: Marco Elver --- This is a minor fix that only affects slub_debug runs, so it is probably not worth backporting. --- mm/kasan/common.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index c40c0e7b3b5f..968d2365d8c1 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -223,8 +223,9 @@ size_t __kasan_metadata_size(struct kmem_cache *cache) return 0; return (cache->kasan_info.alloc_meta_offset ? sizeof(struct kasan_alloc_meta) : 0) + - (cache->kasan_info.free_meta_offset ? - sizeof(struct kasan_free_meta) : 0); + ((cache->kasan_info.free_meta_offset && + cache->kasan_info.free_meta_offset != KASAN_NO_FREE_META) ? + sizeof(struct kasan_free_meta) : 0); } struct kasan_alloc_meta *kasan_get_alloc_meta(struct kmem_cache *cache, From patchwork Mon Jun 13 20:13:53 2022 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: 12880069 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 9B086CCA47B for ; Mon, 13 Jun 2022 20:15:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 503158D01C2; Mon, 13 Jun 2022 16:15:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 48B2A8D01C0; Mon, 13 Jun 2022 16:15:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 32A178D01C2; Mon, 13 Jun 2022 16:15:27 -0400 (EDT) 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 20DD08D01C0 for ; Mon, 13 Jun 2022 16:15:27 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id F0A6F80E33 for ; Mon, 13 Jun 2022 20:15:26 +0000 (UTC) X-FDA: 79574317494.15.CC09384 Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by imf24.hostedemail.com (Postfix) with ESMTP id 25429180084 for ; Mon, 13 Jun 2022 20:15:25 +0000 (UTC) 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=1655151325; 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=j0yWL0nsrOMOMqm9cWej2oW3wFadR2W9VWyegnjYrXA=; b=B7wM0RDK6hT1iWd1oUZiXw42RuPFZUK3/bZLcb0cf6S+yJLUMeFAXOOhzQih3C24QAGjKu ePUXricowL++fSJ9e4X99sgTfnTXB5Gb7y6BsYumoyibHSSIGQKAHwJJtceNv7t7xMo8tQ 2bSz7OqlMXc+P6DOs7u4kWu0lyWZllE= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 02/32] kasan: rename kasan_set_*_info to kasan_save_*_info Date: Mon, 13 Jun 2022 22:13:53 +0200 Message-Id: <50cdd8e8d696a8958b7b59c940561c6ed8042436.1655150842.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151326; a=rsa-sha256; cv=none; b=sC+ALm1QHrl2wMR2puesthfMROb3IZsnhH5/hZh9fh5/GlkYX6AYHqBbVSpHO3X4A1lNi7 hQoyY8Rc9MpiZ11pughTMq22S1ygjiZdfGA4eL4/jck4K5ptj6soeA5mSBP8r7kI8etl1D MWg6VXw1+SWm2EpWK0C+dQlT8PqI9C8= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=B7wM0RDK; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf24.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 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=1655151326; 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=j0yWL0nsrOMOMqm9cWej2oW3wFadR2W9VWyegnjYrXA=; b=et5GVZT4M6cl31Z34UdKAVv9NP52boGFwJyeBuJl7xSpfKhoofMUZQcuh3r4ug4MIMFaJI s8EJrTCv/VTyshzTzrrN+oMS1x+xtgkzEdAUSdRHewFcba9GnkaX23y4mb0t/Ci+8lPuK1 n4x75iFo8ku207vMB5uTKaoAibZEiEU= X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 25429180084 X-Stat-Signature: srnzprmya6cie6jdtgpxx38q3bmaka49 Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=B7wM0RDK; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf24.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-Rspam-User: X-HE-Tag: 1655151325-179287 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: From: Andrey Konovalov Rename set_alloc_info() and kasan_set_free_info() to save_alloc_info() and kasan_save_free_info(). The new names make more sense. Signed-off-by: Andrey Konovalov Reviewed-by: Marco Elver --- mm/kasan/common.c | 8 ++++---- mm/kasan/generic.c | 2 +- mm/kasan/kasan.h | 2 +- mm/kasan/tags.c | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 968d2365d8c1..753775b894b6 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -364,7 +364,7 @@ static inline bool ____kasan_slab_free(struct kmem_cache *cache, void *object, return false; if (kasan_stack_collection_enabled()) - kasan_set_free_info(cache, object, tag); + kasan_save_free_info(cache, object, tag); return kasan_quarantine_put(cache, object); } @@ -423,7 +423,7 @@ void __kasan_slab_free_mempool(void *ptr, unsigned long ip) } } -static void set_alloc_info(struct kmem_cache *cache, void *object, +static void save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags, bool is_kmalloc) { struct kasan_alloc_meta *alloc_meta; @@ -467,7 +467,7 @@ void * __must_check __kasan_slab_alloc(struct kmem_cache *cache, /* Save alloc info (if possible) for non-kmalloc() allocations. */ if (kasan_stack_collection_enabled()) - set_alloc_info(cache, (void *)object, flags, false); + save_alloc_info(cache, (void *)object, flags, false); return tagged_object; } @@ -513,7 +513,7 @@ static inline void *____kasan_kmalloc(struct kmem_cache *cache, * This also rewrites the alloc info when called from kasan_krealloc(). */ if (kasan_stack_collection_enabled()) - set_alloc_info(cache, (void *)object, flags, true); + save_alloc_info(cache, (void *)object, flags, true); /* Keep the tag that was set by kasan_slab_alloc(). */ return (void *)object; diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c index 437fcc7e77cf..03a3770cfeae 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -358,7 +358,7 @@ void kasan_record_aux_stack_noalloc(void *addr) return __kasan_record_aux_stack(addr, false); } -void kasan_set_free_info(struct kmem_cache *cache, +void kasan_save_free_info(struct kmem_cache *cache, void *object, u8 tag) { struct kasan_free_meta *free_meta; diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index 610d60d6e5b8..6df8d7b01073 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -284,7 +284,7 @@ struct slab *kasan_addr_to_slab(const void *addr); depot_stack_handle_t kasan_save_stack(gfp_t flags, bool can_alloc); void kasan_set_track(struct kasan_track *track, gfp_t flags); -void kasan_set_free_info(struct kmem_cache *cache, void *object, u8 tag); +void kasan_save_free_info(struct kmem_cache *cache, void *object, u8 tag); struct kasan_track *kasan_get_free_track(struct kmem_cache *cache, void *object, u8 tag); diff --git a/mm/kasan/tags.c b/mm/kasan/tags.c index 8f48b9502a17..b453a353bc86 100644 --- a/mm/kasan/tags.c +++ b/mm/kasan/tags.c @@ -17,7 +17,7 @@ #include "kasan.h" -void kasan_set_free_info(struct kmem_cache *cache, +void kasan_save_free_info(struct kmem_cache *cache, void *object, u8 tag) { struct kasan_alloc_meta *alloc_meta; From patchwork Mon Jun 13 20:13:54 2022 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: 12880070 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 68183C433EF for ; Mon, 13 Jun 2022 20:15:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9E5EF8D01C3; Mon, 13 Jun 2022 16:15:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 920568D01C0; Mon, 13 Jun 2022 16:15:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6D23E8D01C4; Mon, 13 Jun 2022 16:15:27 -0400 (EDT) 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 51A318D01C3 for ; Mon, 13 Jun 2022 16:15:27 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 32A52FC0 for ; Mon, 13 Jun 2022 20:15:27 +0000 (UTC) X-FDA: 79574317494.07.A87A449 Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by imf31.hostedemail.com (Postfix) with ESMTP id B2B63200A6 for ; Mon, 13 Jun 2022 20:15:26 +0000 (UTC) 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=1655151325; 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=5YxZt8AJdg20zeg5mlaLc0cE80lCSk74fkwRwLQH28g=; b=vNtdLTu/n3uZXNxwSADnWXXhRvSCEmqvLH3FjLwQYNnr7TcYt1H3qoRfDAzhIfr++YtDwz DKhTZe2bKI12WI17MUbeLtNkumWSZskhDTHfG69P2kj/KpodD0zAj2B3BsQUqDUogXpISQ 4ac6Q6f0pE/XLiSdQl54Sxg/q37QO3s= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 03/32] kasan: move is_kmalloc check out of save_alloc_info Date: Mon, 13 Jun 2022 22:13:54 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655151326; 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=5YxZt8AJdg20zeg5mlaLc0cE80lCSk74fkwRwLQH28g=; b=MZPjNnXiVpyi2Vt3pVGuZbVzDa6AMPjLG+0dXhgxB5TGj+lYn1uwpBkmnqAx/Fp8NZrD7/ 5Xv/bSKfeUHi9J1Z9LVMOI0R/xg0lcEopX0XAS+Squ77QUu1Yq5aBAzamk2FrwFkbGYrFB ZFMU5o9ucg/+pIwcE8XfCCtmyyRxDxo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151326; a=rsa-sha256; cv=none; b=v2P0Eilbpuq+ogtMuzl0nSrMVZ/L5AydmQH5/I5+KNH0lgSlmiRNWGGFrzWWe4fcWvPOoA tpLNMAva91LGvZEQ5PdNprHFkTqcLx/g6A4KlKIUe/1zlmeWLizbGN6bYGihYeVqtxg+qu oPrMqUISNGInRb4lnp1Qn4Xk8yAWjKk= ARC-Authentication-Results: i=1; imf31.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="vNtdLTu/"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf31.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="vNtdLTu/"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf31.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-Rspamd-Server: rspam12 X-Rspam-User: X-Stat-Signature: tcaycepn4ckpta8yrbnqwd84m6bwwg4u X-Rspamd-Queue-Id: B2B63200A6 X-HE-Tag: 1655151326-289639 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: From: Andrey Konovalov Move kasan_info.is_kmalloc check out of save_alloc_info(). This is a preparatory change that simplifies the following patches in this series. Signed-off-by: Andrey Konovalov Reviewed-by: Marco Elver --- mm/kasan/common.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 753775b894b6..a6107e8375e0 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -423,15 +423,10 @@ void __kasan_slab_free_mempool(void *ptr, unsigned long ip) } } -static void save_alloc_info(struct kmem_cache *cache, void *object, - gfp_t flags, bool is_kmalloc) +static void save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags) { struct kasan_alloc_meta *alloc_meta; - /* Don't save alloc info for kmalloc caches in kasan_slab_alloc(). */ - if (cache->kasan_info.is_kmalloc && !is_kmalloc) - return; - alloc_meta = kasan_get_alloc_meta(cache, object); if (alloc_meta) kasan_set_track(&alloc_meta->alloc_track, flags); @@ -466,8 +461,8 @@ void * __must_check __kasan_slab_alloc(struct kmem_cache *cache, kasan_unpoison(tagged_object, cache->object_size, init); /* Save alloc info (if possible) for non-kmalloc() allocations. */ - if (kasan_stack_collection_enabled()) - save_alloc_info(cache, (void *)object, flags, false); + if (kasan_stack_collection_enabled() && !cache->kasan_info.is_kmalloc) + save_alloc_info(cache, (void *)object, flags); return tagged_object; } @@ -512,8 +507,8 @@ static inline void *____kasan_kmalloc(struct kmem_cache *cache, * Save alloc info (if possible) for kmalloc() allocations. * This also rewrites the alloc info when called from kasan_krealloc(). */ - if (kasan_stack_collection_enabled()) - save_alloc_info(cache, (void *)object, flags, true); + if (kasan_stack_collection_enabled() && cache->kasan_info.is_kmalloc) + save_alloc_info(cache, (void *)object, flags); /* Keep the tag that was set by kasan_slab_alloc(). */ return (void *)object; From patchwork Mon Jun 13 20:13:55 2022 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: 12880071 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 E0194CCA47B for ; Mon, 13 Jun 2022 20:15:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 948548D01C4; Mon, 13 Jun 2022 16:15:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8AC5F8D01C0; Mon, 13 Jun 2022 16:15:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 722FB8D01C4; Mon, 13 Jun 2022 16:15:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 61A158D01C0 for ; Mon, 13 Jun 2022 16:15:28 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 154B1344CB for ; Mon, 13 Jun 2022 20:15:28 +0000 (UTC) X-FDA: 79574317536.29.E75BA31 Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by imf01.hostedemail.com (Postfix) with ESMTP id 5BA3840099 for ; Mon, 13 Jun 2022 20:15:27 +0000 (UTC) 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=1655151326; 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=CulRI3DSt2ZPgq4QmoSCGGdERJCYU8brnjMdmOnBudE=; b=McdlmQaiUHC8FqafkCZKCaRBsXTlEsuuctlEWSaG3MlUhgx527mYI+igdmY8l5GTyxXbZI o2dJizocXRqmGuwQruuIEUw8C4gXJX1la7X9eiEo2sHl7ViYwk1NorePQQBCPW6zBWLckY g3gIB0ynH0X3N7BsU7vVammM2+Z2+SE= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 04/32] kasan: split save_alloc_info implementations Date: Mon, 13 Jun 2022 22:13:55 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=McdlmQai; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf01.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 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=1655151327; 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=CulRI3DSt2ZPgq4QmoSCGGdERJCYU8brnjMdmOnBudE=; b=B1wwxuIu5wuxuTGEualHuVpmH1xtnNxZt6HYEiCskY7RJnBx9PxuvZ25Y6LOBFaja7KPxe rRCtF0xdoUCdIk1PT9nhgiPt2Axq2PCjPmm871i8KzhXUE56FKQC3TA3AXuaeRPlSionVn PkUV2shS7dw98vcf0H5M1Jn7wgdqhlU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151327; a=rsa-sha256; cv=none; b=6v90aNko0uHRHizugfUX2ZZ006uX/BhR+aPfYRKWfgjv52+bft6tPHyEBDC3GQcsfCm1ra FPeUBcyQMi3v0M3SeWJayW1X4mhO3k8WZ5Iu6ogfzqApRF5BFBf4vs+/BE9yANVsazQMwF ATDvUhOvv4eJBZPLM6Jw8TWOT+XOL90= X-Stat-Signature: hdwhwj8x59bejtn43bhej5nix8etfrug X-Rspamd-Queue-Id: 5BA3840099 X-Rspam-User: Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=McdlmQai; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf01.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-Rspamd-Server: rspam10 X-HE-Tag: 1655151327-932667 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: From: Andrey Konovalov Provide standalone implementations of save_alloc_info() for the Generic and tag-based modes. For now, the implementations are the same, but they will diverge later in the series. Signed-off-by: Andrey Konovalov Reviewed-by: Marco Elver --- mm/kasan/common.c | 13 ++----------- mm/kasan/generic.c | 9 +++++++++ mm/kasan/kasan.h | 1 + mm/kasan/tags.c | 9 +++++++++ 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index a6107e8375e0..2848c7a2402a 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -423,15 +423,6 @@ void __kasan_slab_free_mempool(void *ptr, unsigned long ip) } } -static void save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags) -{ - struct kasan_alloc_meta *alloc_meta; - - alloc_meta = kasan_get_alloc_meta(cache, object); - if (alloc_meta) - kasan_set_track(&alloc_meta->alloc_track, flags); -} - void * __must_check __kasan_slab_alloc(struct kmem_cache *cache, void *object, gfp_t flags, bool init) { @@ -462,7 +453,7 @@ void * __must_check __kasan_slab_alloc(struct kmem_cache *cache, /* Save alloc info (if possible) for non-kmalloc() allocations. */ if (kasan_stack_collection_enabled() && !cache->kasan_info.is_kmalloc) - save_alloc_info(cache, (void *)object, flags); + kasan_save_alloc_info(cache, (void *)object, flags); return tagged_object; } @@ -508,7 +499,7 @@ static inline void *____kasan_kmalloc(struct kmem_cache *cache, * This also rewrites the alloc info when called from kasan_krealloc(). */ if (kasan_stack_collection_enabled() && cache->kasan_info.is_kmalloc) - save_alloc_info(cache, (void *)object, flags); + kasan_save_alloc_info(cache, (void *)object, flags); /* Keep the tag that was set by kasan_slab_alloc(). */ return (void *)object; diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c index 03a3770cfeae..98c451a3b01f 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -358,6 +358,15 @@ void kasan_record_aux_stack_noalloc(void *addr) return __kasan_record_aux_stack(addr, false); } +void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags) +{ + struct kasan_alloc_meta *alloc_meta; + + alloc_meta = kasan_get_alloc_meta(cache, object); + if (alloc_meta) + kasan_set_track(&alloc_meta->alloc_track, flags); +} + void kasan_save_free_info(struct kmem_cache *cache, void *object, u8 tag) { diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index 6df8d7b01073..610057e651d2 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -284,6 +284,7 @@ struct slab *kasan_addr_to_slab(const void *addr); depot_stack_handle_t kasan_save_stack(gfp_t flags, bool can_alloc); void kasan_set_track(struct kasan_track *track, gfp_t flags); +void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags); void kasan_save_free_info(struct kmem_cache *cache, void *object, u8 tag); struct kasan_track *kasan_get_free_track(struct kmem_cache *cache, void *object, u8 tag); diff --git a/mm/kasan/tags.c b/mm/kasan/tags.c index b453a353bc86..1ba3c8399f72 100644 --- a/mm/kasan/tags.c +++ b/mm/kasan/tags.c @@ -17,6 +17,15 @@ #include "kasan.h" +void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags) +{ + struct kasan_alloc_meta *alloc_meta; + + alloc_meta = kasan_get_alloc_meta(cache, object); + if (alloc_meta) + kasan_set_track(&alloc_meta->alloc_track, flags); +} + void kasan_save_free_info(struct kmem_cache *cache, void *object, u8 tag) { From patchwork Mon Jun 13 20:13:56 2022 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: 12880072 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 41827C433EF for ; Mon, 13 Jun 2022 20:15:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F14A58D01C5; Mon, 13 Jun 2022 16:15:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EC5698D01C0; Mon, 13 Jun 2022 16:15:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DB56D8D01C5; Mon, 13 Jun 2022 16:15:28 -0400 (EDT) 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 C5AFB8D01C0 for ; Mon, 13 Jun 2022 16:15:28 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 9C989351AF for ; Mon, 13 Jun 2022 20:15:28 +0000 (UTC) X-FDA: 79574317536.06.C627DC2 Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by imf03.hostedemail.com (Postfix) with ESMTP id 0812120090 for ; Mon, 13 Jun 2022 20:15:27 +0000 (UTC) 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=1655151327; 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=AJyI1q6f80wQzyJSQxJLFHolqTP9cxvpgBekekTg5FA=; b=oRiXY173+rNYu9q5ejUgqLAcQxKtVVJJwv/RkKdPidlw+963fu7HySl5B3NwUyGb14+Vdm TfaMNGMDf5tUsTrPX5fXXhelmd+jti809gcNElHE51Esui7T68FxKcLzBmi6JDiCS4+Mwn vuiAG7lL1+J8tR2Y56hhGqg1dw5pd5s= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 05/32] kasan: drop CONFIG_KASAN_TAGS_IDENTIFY Date: Mon, 13 Jun 2022 22:13:56 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=oRiXY173; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf03.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 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=1655151328; 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=AJyI1q6f80wQzyJSQxJLFHolqTP9cxvpgBekekTg5FA=; b=3pTwl6oaGsOEBuTxAWKIhbjtpO2G8jrRmCvpPGinrnzNzK0Weg/wD/5/PDfj4ot0eig5VP sRJG7Om6MIS+0f3xu1ftOwlskP8MW7xAkwWkojdjir/Biyg4EZBQXyUWvkgFBKW5iONxP9 Ak8ST7rIoSqEaArcvOSzh+yVFrMhDL0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151328; a=rsa-sha256; cv=none; b=e37td1t5p32zxrweJtlFmL9Hj/yZdwn5ky8Ze29HL/VDw7xObh/5sUU1OmtuxayxCVC8k2 j0JFkiRdH/sJR/HGmKBE+hS6ieyMZeTu7T12JK1W7V+zv28aT0W3KqXUufs1hpSEyuKcXQ oE+w+LFJee5BqpuvzlMxU7PX5U+btws= X-Stat-Signature: e5j3sgrwj79wimi4j4aoh45kr9uun4z8 X-Rspamd-Queue-Id: 0812120090 X-Rspam-User: Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=oRiXY173; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf03.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-Rspamd-Server: rspam10 X-HE-Tag: 1655151327-205220 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: From: Andrey Konovalov Drop CONFIG_KASAN_TAGS_IDENTIFY and related code to simplify making changes to the reporting code. The dropped functionality will be restored in the following patches in this series. Signed-off-by: Andrey Konovalov Reviewed-by: Marco Elver --- lib/Kconfig.kasan | 8 -------- mm/kasan/kasan.h | 12 +----------- mm/kasan/report_tags.c | 28 ---------------------------- mm/kasan/tags.c | 21 ++------------------- 4 files changed, 3 insertions(+), 66 deletions(-) diff --git a/lib/Kconfig.kasan b/lib/Kconfig.kasan index f0973da583e0..ca09b1cf8ee9 100644 --- a/lib/Kconfig.kasan +++ b/lib/Kconfig.kasan @@ -167,14 +167,6 @@ config KASAN_STACK as well, as it adds inline-style instrumentation that is run unconditionally. -config KASAN_TAGS_IDENTIFY - bool "Memory corruption type identification" - depends on KASAN_SW_TAGS || KASAN_HW_TAGS - help - Enables best-effort identification of the bug types (use-after-free - or out-of-bounds) at the cost of increased memory consumption. - Only applicable for the tag-based KASAN modes. - config KASAN_VMALLOC bool "Check accesses to vmalloc allocations" depends on HAVE_ARCH_KASAN_VMALLOC diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index 610057e651d2..aa6b43936f8d 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -168,23 +168,13 @@ struct kasan_track { depot_stack_handle_t stack; }; -#if defined(CONFIG_KASAN_TAGS_IDENTIFY) && defined(CONFIG_KASAN_SW_TAGS) -#define KASAN_NR_FREE_STACKS 5 -#else -#define KASAN_NR_FREE_STACKS 1 -#endif - struct kasan_alloc_meta { struct kasan_track alloc_track; /* Generic mode stores free track in kasan_free_meta. */ #ifdef CONFIG_KASAN_GENERIC depot_stack_handle_t aux_stack[2]; #else - struct kasan_track free_track[KASAN_NR_FREE_STACKS]; -#endif -#ifdef CONFIG_KASAN_TAGS_IDENTIFY - u8 free_pointer_tag[KASAN_NR_FREE_STACKS]; - u8 free_track_idx; + struct kasan_track free_track; #endif }; diff --git a/mm/kasan/report_tags.c b/mm/kasan/report_tags.c index e25d2166e813..35cf3cae4aa4 100644 --- a/mm/kasan/report_tags.c +++ b/mm/kasan/report_tags.c @@ -5,37 +5,9 @@ */ #include "kasan.h" -#include "../slab.h" const char *kasan_get_bug_type(struct kasan_report_info *info) { -#ifdef CONFIG_KASAN_TAGS_IDENTIFY - struct kasan_alloc_meta *alloc_meta; - struct kmem_cache *cache; - struct slab *slab; - const void *addr; - void *object; - u8 tag; - int i; - - tag = get_tag(info->access_addr); - addr = kasan_reset_tag(info->access_addr); - slab = kasan_addr_to_slab(addr); - if (slab) { - cache = slab->slab_cache; - object = nearest_obj(cache, slab, (void *)addr); - alloc_meta = kasan_get_alloc_meta(cache, object); - - if (alloc_meta) { - for (i = 0; i < KASAN_NR_FREE_STACKS; i++) { - if (alloc_meta->free_pointer_tag[i] == tag) - return "use-after-free"; - } - } - return "out-of-bounds"; - } -#endif - /* * If access_size is a negative number, then it has reason to be * defined as out-of-bounds bug type. diff --git a/mm/kasan/tags.c b/mm/kasan/tags.c index 1ba3c8399f72..e0e5de8ce834 100644 --- a/mm/kasan/tags.c +++ b/mm/kasan/tags.c @@ -30,39 +30,22 @@ void kasan_save_free_info(struct kmem_cache *cache, void *object, u8 tag) { struct kasan_alloc_meta *alloc_meta; - u8 idx = 0; alloc_meta = kasan_get_alloc_meta(cache, object); if (!alloc_meta) return; -#ifdef CONFIG_KASAN_TAGS_IDENTIFY - idx = alloc_meta->free_track_idx; - alloc_meta->free_pointer_tag[idx] = tag; - alloc_meta->free_track_idx = (idx + 1) % KASAN_NR_FREE_STACKS; -#endif - - kasan_set_track(&alloc_meta->free_track[idx], GFP_NOWAIT); + kasan_set_track(&alloc_meta->free_track, GFP_NOWAIT); } struct kasan_track *kasan_get_free_track(struct kmem_cache *cache, void *object, u8 tag) { struct kasan_alloc_meta *alloc_meta; - int i = 0; alloc_meta = kasan_get_alloc_meta(cache, object); if (!alloc_meta) return NULL; -#ifdef CONFIG_KASAN_TAGS_IDENTIFY - for (i = 0; i < KASAN_NR_FREE_STACKS; i++) { - if (alloc_meta->free_pointer_tag[i] == tag) - break; - } - if (i == KASAN_NR_FREE_STACKS) - i = alloc_meta->free_track_idx; -#endif - - return &alloc_meta->free_track[i]; + return &alloc_meta->free_track; } From patchwork Mon Jun 13 20:13:57 2022 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: 12880073 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 8251FC43334 for ; Mon, 13 Jun 2022 20:16:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1805E8D01C6; Mon, 13 Jun 2022 16:16:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 12F668D01C0; Mon, 13 Jun 2022 16:16:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F39628D01C6; Mon, 13 Jun 2022 16:16:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id E602B8D01C0 for ; Mon, 13 Jun 2022 16:16:29 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay13.hostedemail.com (Postfix) with ESMTP id C06986076D for ; Mon, 13 Jun 2022 20:16:29 +0000 (UTC) X-FDA: 79574320098.14.C2539F1 Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by imf06.hostedemail.com (Postfix) with ESMTP id 0F2E3180087 for ; Mon, 13 Jun 2022 20:16:28 +0000 (UTC) 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=1655151388; 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=JiOjwm2R97mupyKaCc5esPRsyojrd6x1cjK2UXDXXcc=; b=pbdzxZprz/NhIwY0P7z/+ttWMraq6KMi7wV6xcx+ASVLPMMbAH+kX5rZH71kDY66kRmnT9 Mu9jFDvTd9hGkhzuBtkY6RMAW9h9wEcOMEOO5t1SNdkQ4YpbsVY6v18z+4klJ61YmFbIo9 995IoX4sfLO/o67NszTjINnAJr8EKPA= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 06/32] kasan: introduce kasan_print_aux_stacks Date: Mon, 13 Jun 2022 22:13:57 +0200 Message-Id: <11a7bfb5ed5de141b50db8c08e9c6ad37ef3febc.1655150842.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655151389; 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=JiOjwm2R97mupyKaCc5esPRsyojrd6x1cjK2UXDXXcc=; b=0E52NlcmlzJ6APjNZ1cax5u5M474OlWC1jp27Txkjf+Bylj2b2+WCl7Xge6PmvGdGquK2l LeJJ3gYVyc9IIgi4BvcH2P1djLTuFaFYE9MV2zIynMwgB/66YEDQJ11RplptwnW2dLMHZE 7fvlovIlVVR9sHKB6HmOBMk88DdPWk4= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=pbdzxZpr; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf06.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151389; a=rsa-sha256; cv=none; b=wT5qRY8T4K85FZSv+6GO/jaCuVG+N+M3vWlDSUGj1f/UrexMPDi88QITNoa6Aj/lJvCIrw II1xd30Y0sIX7+DkrsggMZWFatrjODQTIBbNdhd7opRmOvprwYXLddVhVcOL7yvIwb2PY3 p5L0JN8D6MihhF62kLXlbAxd+hbqf4c= X-Rspamd-Queue-Id: 0F2E3180087 X-Rspam-User: X-Stat-Signature: pzp7m1e517cwdp5r9u5hsjishnzdtncr Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=pbdzxZpr; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf06.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-Rspamd-Server: rspam04 X-HE-Tag: 1655151388-321436 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: From: Andrey Konovalov Add a kasan_print_aux_stacks() helper that prints the auxiliary stack traces for the Generic mode. This change hides references to alloc_meta from the common reporting code. This is desired as only the Generic mode will be using per-object metadata after this series. Signed-off-by: Andrey Konovalov --- mm/kasan/kasan.h | 6 ++++++ mm/kasan/report.c | 15 +-------------- mm/kasan/report_generic.c | 20 ++++++++++++++++++++ 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index aa6b43936f8d..bcea5ed15631 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -265,6 +265,12 @@ void kasan_print_address_stack_frame(const void *addr); static inline void kasan_print_address_stack_frame(const void *addr) { } #endif +#ifdef CONFIG_KASAN_GENERIC +void kasan_print_aux_stacks(struct kmem_cache *cache, const void *object); +#else +static inline void kasan_print_aux_stacks(struct kmem_cache *cache, const void *object) { } +#endif + bool kasan_report(unsigned long addr, size_t size, bool is_write, unsigned long ip); void kasan_report_invalid_free(void *object, unsigned long ip); diff --git a/mm/kasan/report.c b/mm/kasan/report.c index b341a191651d..35dd8aeb115c 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -266,20 +266,7 @@ static void describe_object_stacks(struct kmem_cache *cache, void *object, pr_err("\n"); } -#ifdef CONFIG_KASAN_GENERIC - if (!alloc_meta) - return; - if (alloc_meta->aux_stack[0]) { - pr_err("Last potentially related work creation:\n"); - stack_depot_print(alloc_meta->aux_stack[0]); - pr_err("\n"); - } - if (alloc_meta->aux_stack[1]) { - pr_err("Second to last potentially related work creation:\n"); - stack_depot_print(alloc_meta->aux_stack[1]); - pr_err("\n"); - } -#endif + kasan_print_aux_stacks(cache, object); } static void describe_object(struct kmem_cache *cache, void *object, diff --git a/mm/kasan/report_generic.c b/mm/kasan/report_generic.c index 6689fb9a919b..348dc207d462 100644 --- a/mm/kasan/report_generic.c +++ b/mm/kasan/report_generic.c @@ -132,6 +132,26 @@ void kasan_metadata_fetch_row(char *buffer, void *row) memcpy(buffer, kasan_mem_to_shadow(row), META_BYTES_PER_ROW); } +void kasan_print_aux_stacks(struct kmem_cache *cache, const void *object) +{ + struct kasan_alloc_meta *alloc_meta; + + alloc_meta = kasan_get_alloc_meta(cache, object); + if (!alloc_meta) + return; + + if (alloc_meta->aux_stack[0]) { + pr_err("Last potentially related work creation:\n"); + stack_depot_print(alloc_meta->aux_stack[0]); + pr_err("\n"); + } + if (alloc_meta->aux_stack[1]) { + pr_err("Second to last potentially related work creation:\n"); + stack_depot_print(alloc_meta->aux_stack[1]); + pr_err("\n"); + } +} + #ifdef CONFIG_KASAN_STACK static bool __must_check tokenize_frame_descr(const char **frame_descr, char *token, size_t max_tok_len, From patchwork Mon Jun 13 20:13:58 2022 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: 12880074 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 414A9C433EF for ; Mon, 13 Jun 2022 20:16:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C0B268D01C7; Mon, 13 Jun 2022 16:16:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B6CDF8D01C0; Mon, 13 Jun 2022 16:16:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 91DC48D01C7; Mon, 13 Jun 2022 16:16:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 80FDE8D01C0 for ; Mon, 13 Jun 2022 16:16:30 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 59A3C74D for ; Mon, 13 Jun 2022 20:16:30 +0000 (UTC) X-FDA: 79574320140.28.0712DE3 Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by imf03.hostedemail.com (Postfix) with ESMTP id B772620042 for ; Mon, 13 Jun 2022 20:16:29 +0000 (UTC) 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=1655151388; 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=aoRv/lacQEeXs4iZT+XDlLjdCIzo6qCQWnCOXZwFqpQ=; b=AI29BOaG9ggfX2T64w5uHQtFR2Zk5jH/FJvAysYuv3mV7ip6E74PPPtjXiO2THMfq+O7MM 3wf9zCPPT1Lyp1ad/ukFDULYGu0Uo91W2UA/qqfuyWNQxDgzwRgqwkGcoP1X3VUn9F/Snq +4Idx/bDkMG/uiXUEA4ZwK3QwDJ9who= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 07/32] kasan: introduce kasan_get_alloc_track Date: Mon, 13 Jun 2022 22:13:58 +0200 Message-Id: <184ac9df81406e73611e1f639c5d4d09f8d7693a.1655150842.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151390; a=rsa-sha256; cv=none; b=Y+pAuvDj1cUwIVgQ3WcZoy4StE1iEKFPg4Udhwj2hOq5ojX/Zxl7Hos32fmt0apwPO5aS/ XUtQSBIWXXZx8gBzYULY26WGgpKnoCppsWwtVezQyrt9I7+8Ed/cx13xCNWelbtAmzA9xM toVvzcK27dM+OYmGe+JZc84GNcb+TDU= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=AI29BOaG; spf=pass (imf03.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 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=1655151390; 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=aoRv/lacQEeXs4iZT+XDlLjdCIzo6qCQWnCOXZwFqpQ=; b=JbOVK6BPJoS39jdsOQPnyLs3ANg70nJXUUZ0k7DJohtKWxqDvoKpbzEQM00qzSOQiut9CL 6PESvNksRIS7/UVLcNzASJdX5I94ovYH/NltOTqa1ODQ50nCVmay4EHv64249yq06Mem6w AwzDPm5Dh0h3KUvfeaEElTjXyHaQ0dc= X-Stat-Signature: 5cd573fxfmwdc3dhoetyictsaa6ezeqi X-Rspamd-Queue-Id: B772620042 Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=AI29BOaG; spf=pass (imf03.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Rspamd-Server: rspam07 X-Rspam-User: X-HE-Tag: 1655151389-816674 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: From: Andrey Konovalov Add a kasan_get_alloc_track() helper that fetches alloc_track for a slab object and use this helper in the common reporting code. For now, the implementations of this helper are the same for the Generic and tag-based modes, but they will diverge later in the series. This change hides references to alloc_meta from the common reporting code. This is desired as only the Generic mode will be using per-object metadata after this series. Signed-off-by: Andrey Konovalov Reviewed-by: Marco Elver --- mm/kasan/generic.c | 14 +++++++++++++- mm/kasan/kasan.h | 4 +++- mm/kasan/report.c | 8 ++++---- mm/kasan/tags.c | 14 +++++++++++++- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c index 98c451a3b01f..f212b9ae57b5 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -381,8 +381,20 @@ void kasan_save_free_info(struct kmem_cache *cache, *(u8 *)kasan_mem_to_shadow(object) = KASAN_SLAB_FREETRACK; } +struct kasan_track *kasan_get_alloc_track(struct kmem_cache *cache, + void *object) +{ + struct kasan_alloc_meta *alloc_meta; + + alloc_meta = kasan_get_alloc_meta(cache, object); + if (!alloc_meta) + return NULL; + + return &alloc_meta->alloc_track; +} + struct kasan_track *kasan_get_free_track(struct kmem_cache *cache, - void *object, u8 tag) + void *object, u8 tag) { if (*(u8 *)kasan_mem_to_shadow(object) != KASAN_SLAB_FREETRACK) return NULL; diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index bcea5ed15631..4005da62a1e1 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -282,8 +282,10 @@ depot_stack_handle_t kasan_save_stack(gfp_t flags, bool can_alloc); void kasan_set_track(struct kasan_track *track, gfp_t flags); void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags); void kasan_save_free_info(struct kmem_cache *cache, void *object, u8 tag); +struct kasan_track *kasan_get_alloc_track(struct kmem_cache *cache, + void *object); struct kasan_track *kasan_get_free_track(struct kmem_cache *cache, - void *object, u8 tag); + void *object, u8 tag); #if defined(CONFIG_KASAN_GENERIC) && \ (defined(CONFIG_SLAB) || defined(CONFIG_SLUB)) diff --git a/mm/kasan/report.c b/mm/kasan/report.c index 35dd8aeb115c..f951fd39db74 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -251,12 +251,12 @@ static void describe_object_addr(struct kmem_cache *cache, void *object, static void describe_object_stacks(struct kmem_cache *cache, void *object, const void *addr, u8 tag) { - struct kasan_alloc_meta *alloc_meta; + struct kasan_track *alloc_track; struct kasan_track *free_track; - alloc_meta = kasan_get_alloc_meta(cache, object); - if (alloc_meta) { - print_track(&alloc_meta->alloc_track, "Allocated"); + alloc_track = kasan_get_alloc_track(cache, object); + if (alloc_track) { + print_track(alloc_track, "Allocated"); pr_err("\n"); } diff --git a/mm/kasan/tags.c b/mm/kasan/tags.c index e0e5de8ce834..7b1fc8e7c99c 100644 --- a/mm/kasan/tags.c +++ b/mm/kasan/tags.c @@ -38,8 +38,20 @@ void kasan_save_free_info(struct kmem_cache *cache, kasan_set_track(&alloc_meta->free_track, GFP_NOWAIT); } +struct kasan_track *kasan_get_alloc_track(struct kmem_cache *cache, + void *object) +{ + struct kasan_alloc_meta *alloc_meta; + + alloc_meta = kasan_get_alloc_meta(cache, object); + if (!alloc_meta) + return NULL; + + return &alloc_meta->alloc_track; +} + struct kasan_track *kasan_get_free_track(struct kmem_cache *cache, - void *object, u8 tag) + void *object, u8 tag) { struct kasan_alloc_meta *alloc_meta; From patchwork Mon Jun 13 20:13:59 2022 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: 12880075 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 B05D6CCA47B for ; Mon, 13 Jun 2022 20:16:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 79FF68D01C8; Mon, 13 Jun 2022 16:16:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 59CD28D01C0; Mon, 13 Jun 2022 16:16:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4163A8D01C8; Mon, 13 Jun 2022 16:16:31 -0400 (EDT) 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 2A0BF8D01C0 for ; Mon, 13 Jun 2022 16:16:31 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 008DC20849 for ; Mon, 13 Jun 2022 20:16:30 +0000 (UTC) X-FDA: 79574320182.06.F8AC238 Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by imf05.hostedemail.com (Postfix) with ESMTP id 876DF100085 for ; Mon, 13 Jun 2022 20:16:30 +0000 (UTC) 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=1655151389; 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=AFE9jCstOlqWjsTW+x7JNp4EBVRaTizWNRauezofwDY=; b=i1iFXVY/xX12A8BbWkfb19YXcJrTjPzh7aIvkBr32UtiJHdrEOF7uA/Q1dKGciy/q5cEOl nNzmk7g6Hx9KH+I2vuJBgPUzZxLTkySN0GctGkWv7LnHu6unfozHZxS5eWxuaUOAsLBHaK WmMmgwEdQrOlcrl9YCnBcWWMd03eUBI= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 08/32] kasan: introduce kasan_init_object_meta Date: Mon, 13 Jun 2022 22:13:59 +0200 Message-Id: <8d1cf94238a325e441f684cbdbb2a1da0db78add.1655150842.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="i1iFXVY/"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf05.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 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=1655151390; 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=AFE9jCstOlqWjsTW+x7JNp4EBVRaTizWNRauezofwDY=; b=cqlBtUIo0EFWl4P4HHq+jHOEGMpGS+Avj2rG/XsXtXfIBjzrftlDNfn2T5ezjRS/3wzeki rkJmoDz3VWBzpOa5/I/BNs7iXphEt4zYejNf1tP0ginyAIsbVUTbjxHSXSwkhkI5EYq3+c AwI93Yjueoq2NStgb+QoBnJgiHunYmM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151390; a=rsa-sha256; cv=none; b=Rc0bLLQnf0QWZC9w63OKd1DNaUCzsfjCduDOLm89XEH/OTfmmkc9NVmGYAKM0zzUwOeHAG NydyiS4fb7NIufKLya2rD9hdgqLV3tABIy7T8RS5Wp5az5Y/qWCLEv3DCABgJvHuwOlLIR PA2cYhcTXtTTms8jDeMjtguRlSzB3EI= X-Stat-Signature: y8ytu4bwwpdjgtbpbpyyhusss537wy3m X-Rspamd-Queue-Id: 876DF100085 X-Rspam-User: Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="i1iFXVY/"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf05.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-Rspamd-Server: rspam10 X-HE-Tag: 1655151390-624066 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: From: Andrey Konovalov Add a kasan_init_object_meta() helper that initializes metadata for a slab object and use it in the common code. For now, the implementations of this helper are the same for the Generic and tag-based modes, but they will diverge later in the series. This change hides references to alloc_meta from the common code. This is desired as only the Generic mode will be using per-object metadata after this series. Signed-off-by: Andrey Konovalov Reviewed-by: Marco Elver --- mm/kasan/common.c | 10 +++------- mm/kasan/generic.c | 9 +++++++++ mm/kasan/kasan.h | 2 ++ mm/kasan/tags.c | 9 +++++++++ 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 2848c7a2402a..f0ee1c1b4b3c 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -312,13 +312,9 @@ static inline u8 assign_tag(struct kmem_cache *cache, void * __must_check __kasan_init_slab_obj(struct kmem_cache *cache, const void *object) { - struct kasan_alloc_meta *alloc_meta; - - if (kasan_stack_collection_enabled()) { - alloc_meta = kasan_get_alloc_meta(cache, object); - if (alloc_meta) - __memset(alloc_meta, 0, sizeof(*alloc_meta)); - } + /* Initialize per-object metadata if it is present. */ + if (kasan_stack_collection_enabled()) + kasan_init_object_meta(cache, object); /* Tag is ignored in set_tag() without CONFIG_KASAN_SW/HW_TAGS */ object = set_tag(object, assign_tag(cache, object, true)); diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c index f212b9ae57b5..5462ddbc21e6 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -328,6 +328,15 @@ DEFINE_ASAN_SET_SHADOW(f3); DEFINE_ASAN_SET_SHADOW(f5); DEFINE_ASAN_SET_SHADOW(f8); +void kasan_init_object_meta(struct kmem_cache *cache, const void *object) +{ + struct kasan_alloc_meta *alloc_meta; + + alloc_meta = kasan_get_alloc_meta(cache, object); + if (alloc_meta) + __memset(alloc_meta, 0, sizeof(*alloc_meta)); +} + static void __kasan_record_aux_stack(void *addr, bool can_alloc) { struct slab *slab = kasan_addr_to_slab(addr); diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index 4005da62a1e1..751c3b17749a 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -278,6 +278,8 @@ void kasan_report_invalid_free(void *object, unsigned long ip); struct page *kasan_addr_to_page(const void *addr); struct slab *kasan_addr_to_slab(const void *addr); +void kasan_init_object_meta(struct kmem_cache *cache, const void *object); + depot_stack_handle_t kasan_save_stack(gfp_t flags, bool can_alloc); void kasan_set_track(struct kasan_track *track, gfp_t flags); void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags); diff --git a/mm/kasan/tags.c b/mm/kasan/tags.c index 7b1fc8e7c99c..2e200969a4b8 100644 --- a/mm/kasan/tags.c +++ b/mm/kasan/tags.c @@ -17,6 +17,15 @@ #include "kasan.h" +void kasan_init_object_meta(struct kmem_cache *cache, const void *object) +{ + struct kasan_alloc_meta *alloc_meta; + + alloc_meta = kasan_get_alloc_meta(cache, object); + if (alloc_meta) + __memset(alloc_meta, 0, sizeof(*alloc_meta)); +} + void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags) { struct kasan_alloc_meta *alloc_meta; From patchwork Mon Jun 13 20:14:00 2022 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: 12880076 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 0822BCCA47D for ; Mon, 13 Jun 2022 20:16:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0E5A28D01C9; Mon, 13 Jun 2022 16:16:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 093F08D01C0; Mon, 13 Jun 2022 16:16:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E76438D01C9; Mon, 13 Jun 2022 16:16:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id D38808D01C0 for ; Mon, 13 Jun 2022 16:16:31 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay13.hostedemail.com (Postfix) with ESMTP id B466560F0F for ; Mon, 13 Jun 2022 20:16:31 +0000 (UTC) X-FDA: 79574320182.26.9D5738F Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by imf24.hostedemail.com (Postfix) with ESMTP id 263E4180080 for ; Mon, 13 Jun 2022 20:16:30 +0000 (UTC) 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=1655151390; 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=WaDMv1eR0VBdMqxxxIgVz2Jc7x+Lk8L5CflH0yXuFbw=; b=CoZZG3fM8L7Sqtv3f/ALsOBGHe/B6lPSXZLM2+G6YSKnZSUG5bd2sFRQ2tMZEIU/JJ3Hn6 nDTHVEnGeNBdY14vRurowiKqtMOR/Umi+25tZS+ygSDZQ+s5nu0kcFvKT9MafLqk99OLyk lYXrDfhLWnrcPTPSRzytHcxjaB3B4lU= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 09/32] kasan: clear metadata functions for tag-based modes Date: Mon, 13 Jun 2022 22:14:00 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151391; a=rsa-sha256; cv=none; b=bYSlOjS/e/DQLMPpEZDrfC0SsvhF97Aq3JIqxR0I/1hVQw+vAjk8m2jnmAuYD7DcniTPkv NvbaIO0oTKJ3CT52+BwW5f1BzPQKxwA/441mhGGPqP1k5VZp0saqms9NxJeH27zaZvrahO H59ZSR5yLcspwqLgQY+4YuiviZc+JcU= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=CoZZG3fM; spf=pass (imf24.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 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=1655151391; 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=WaDMv1eR0VBdMqxxxIgVz2Jc7x+Lk8L5CflH0yXuFbw=; b=pqJMl4UXMbNLf0eWnJRJxpO6JEqGOK4iAVsdux9R6fDpsWt93dsm2ovoxvCpGSNwZVfOBZ 2BnIs06qWPaGBJgHqg2juQh7WCRIJf03q7ik5d+LYVSNfewaIo2WmYBH5bw0lpqYvJTCk3 EF+LH7Zf7w1n6UVaYu/PE1wJqHsoE/k= X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 263E4180080 X-Stat-Signature: f433ch873ptn8mj53udhmdqrqu9u1e1t Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=CoZZG3fM; spf=pass (imf24.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-HE-Tag: 1655151390-147692 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: From: Andrey Konovalov Remove implementations of the metadata-related functions for the tag-based modes. The following patches in the series will provide alternative implementations. As of this patch, the tag-based modes no longer collect alloc and free stack traces. This functionality will be restored later in the series. Signed-off-by: Andrey Konovalov Reviewed-by: Marco Elver --- mm/kasan/tags.c | 33 ++------------------------------- 1 file changed, 2 insertions(+), 31 deletions(-) diff --git a/mm/kasan/tags.c b/mm/kasan/tags.c index 2e200969a4b8..f11c89505c77 100644 --- a/mm/kasan/tags.c +++ b/mm/kasan/tags.c @@ -19,54 +19,25 @@ void kasan_init_object_meta(struct kmem_cache *cache, const void *object) { - struct kasan_alloc_meta *alloc_meta; - - alloc_meta = kasan_get_alloc_meta(cache, object); - if (alloc_meta) - __memset(alloc_meta, 0, sizeof(*alloc_meta)); } void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags) { - struct kasan_alloc_meta *alloc_meta; - - alloc_meta = kasan_get_alloc_meta(cache, object); - if (alloc_meta) - kasan_set_track(&alloc_meta->alloc_track, flags); } void kasan_save_free_info(struct kmem_cache *cache, void *object, u8 tag) { - struct kasan_alloc_meta *alloc_meta; - - alloc_meta = kasan_get_alloc_meta(cache, object); - if (!alloc_meta) - return; - - kasan_set_track(&alloc_meta->free_track, GFP_NOWAIT); } struct kasan_track *kasan_get_alloc_track(struct kmem_cache *cache, void *object) { - struct kasan_alloc_meta *alloc_meta; - - alloc_meta = kasan_get_alloc_meta(cache, object); - if (!alloc_meta) - return NULL; - - return &alloc_meta->alloc_track; + return NULL; } struct kasan_track *kasan_get_free_track(struct kmem_cache *cache, void *object, u8 tag) { - struct kasan_alloc_meta *alloc_meta; - - alloc_meta = kasan_get_alloc_meta(cache, object); - if (!alloc_meta) - return NULL; - - return &alloc_meta->free_track; + return NULL; } From patchwork Mon Jun 13 20:14:01 2022 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: 12880077 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 4B6E6C43334 for ; Mon, 13 Jun 2022 20:16:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C7D698D01CA; Mon, 13 Jun 2022 16:16:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C08338D01C0; Mon, 13 Jun 2022 16:16:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A81C28D01CA; Mon, 13 Jun 2022 16:16:32 -0400 (EDT) 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 91AA38D01C0 for ; Mon, 13 Jun 2022 16:16:32 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 6B08E34092 for ; Mon, 13 Jun 2022 20:16:32 +0000 (UTC) X-FDA: 79574320224.15.319D9AD Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by imf09.hostedemail.com (Postfix) with ESMTP id B2F3D14008A for ; Mon, 13 Jun 2022 20:16:31 +0000 (UTC) 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=1655151390; 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=d1nMQkX323PpKYmvfsU+42+pLQR/Ix0WXYjxZevQJDs=; b=tkqmGQlVmT9T10yy+r5O66k2aRn1tP1CX+k8oCDGFj5Snxv9t4TZYZ7Vi9HZvOZ3BbqGsM MGvdG/umQJR++l04hOTWkanxJx/3wT+/ttiN2hygbiL8++91Fpu+AaahgdJp87G2k6mOes Ye+OlgpaL8/h62wvLV2Z4Xew87cJWuY= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 10/32] kasan: move kasan_get_*_meta to generic.c Date: Mon, 13 Jun 2022 22:14:01 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151392; a=rsa-sha256; cv=none; b=UhQJ2Cm4WZSYYNidMrhgo3pXbj5xJtWTD5a6hI82w6kq6MycQNzYRGXtHbizvZydBzCovx EIoh0mrtmKJVOYfzkqNl/adFbuicCscHk12zMDJfnVYkvKKdrNMrKegaui0moYaYxcI4s8 +0qpl19bpGWJrhzymNc76H2o+qLTqT8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655151392; 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=d1nMQkX323PpKYmvfsU+42+pLQR/Ix0WXYjxZevQJDs=; b=INAeNPiBaUZUmDb9y9zgTJXVGjQR1jNeXVscwABUZtT4K5jCTWc85gkY7nynLi26CQ3wpp C2AjHeSAL0ogpDGeDR/HL3hkS1L0haMObGsBU9gRKLzdT5V27L7AZRWhJFjWbXTwGpY6j8 IiIusaivw9q1DewCFj8NzKEXkG/TLWk= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=tkqmGQlV; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf09.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=tkqmGQlV; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf09.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: B2F3D14008A X-Stat-Signature: 7prbiq1yy7yqkyexg8aked59djtabg95 X-HE-Tag: 1655151391-995249 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: From: Andrey Konovalov Move the implementations of kasan_get_alloc/free_meta() to generic.c, as the common KASAN code does not use these functions anymore. Also drop kasan_reset_tag() from the implementation, as the Generic mode does not tag pointers. Signed-off-by: Andrey Konovalov --- mm/kasan/common.c | 19 ------------------- mm/kasan/generic.c | 17 +++++++++++++++++ mm/kasan/kasan.h | 14 +++++++------- 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index f0ee1c1b4b3c..226eaa714da2 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -228,25 +228,6 @@ size_t __kasan_metadata_size(struct kmem_cache *cache) sizeof(struct kasan_free_meta) : 0); } -struct kasan_alloc_meta *kasan_get_alloc_meta(struct kmem_cache *cache, - const void *object) -{ - if (!cache->kasan_info.alloc_meta_offset) - return NULL; - return kasan_reset_tag(object) + cache->kasan_info.alloc_meta_offset; -} - -#ifdef CONFIG_KASAN_GENERIC -struct kasan_free_meta *kasan_get_free_meta(struct kmem_cache *cache, - const void *object) -{ - BUILD_BUG_ON(sizeof(struct kasan_free_meta) > 32); - if (cache->kasan_info.free_meta_offset == KASAN_NO_FREE_META) - return NULL; - return kasan_reset_tag(object) + cache->kasan_info.free_meta_offset; -} -#endif - void __kasan_poison_slab(struct slab *slab) { struct page *page = slab_page(slab); diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c index 5462ddbc21e6..fa654cb96a0d 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -328,6 +328,23 @@ DEFINE_ASAN_SET_SHADOW(f3); DEFINE_ASAN_SET_SHADOW(f5); DEFINE_ASAN_SET_SHADOW(f8); +struct kasan_alloc_meta *kasan_get_alloc_meta(struct kmem_cache *cache, + const void *object) +{ + if (!cache->kasan_info.alloc_meta_offset) + return NULL; + return (void *)object + cache->kasan_info.alloc_meta_offset; +} + +struct kasan_free_meta *kasan_get_free_meta(struct kmem_cache *cache, + const void *object) +{ + BUILD_BUG_ON(sizeof(struct kasan_free_meta) > 32); + if (cache->kasan_info.free_meta_offset == KASAN_NO_FREE_META) + return NULL; + return (void *)object + cache->kasan_info.free_meta_offset; +} + void kasan_init_object_meta(struct kmem_cache *cache, const void *object) { struct kasan_alloc_meta *alloc_meta; diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index 751c3b17749a..ff7a1597aa51 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -208,13 +208,6 @@ struct kunit_kasan_status { }; #endif -struct kasan_alloc_meta *kasan_get_alloc_meta(struct kmem_cache *cache, - const void *object); -#ifdef CONFIG_KASAN_GENERIC -struct kasan_free_meta *kasan_get_free_meta(struct kmem_cache *cache, - const void *object); -#endif - #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) static inline const void *kasan_shadow_to_mem(const void *shadow_addr) @@ -280,6 +273,13 @@ struct slab *kasan_addr_to_slab(const void *addr); void kasan_init_object_meta(struct kmem_cache *cache, const void *object); +#ifdef CONFIG_KASAN_GENERIC +struct kasan_alloc_meta *kasan_get_alloc_meta(struct kmem_cache *cache, + const void *object); +struct kasan_free_meta *kasan_get_free_meta(struct kmem_cache *cache, + const void *object); +#endif + depot_stack_handle_t kasan_save_stack(gfp_t flags, bool can_alloc); void kasan_set_track(struct kasan_track *track, gfp_t flags); void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags); From patchwork Mon Jun 13 20:14:02 2022 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: 12880078 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 9DD70C433EF for ; Mon, 13 Jun 2022 20:16:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8FDA98D01CB; Mon, 13 Jun 2022 16:16:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7C0568D01C0; Mon, 13 Jun 2022 16:16:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 660358D01CB; Mon, 13 Jun 2022 16:16:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 558AD8D01C0 for ; Mon, 13 Jun 2022 16:16:33 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 0204F3434F for ; Mon, 13 Jun 2022 20:16:33 +0000 (UTC) X-FDA: 79574320266.17.DAFAB29 Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by imf05.hostedemail.com (Postfix) with ESMTP id 655F010008B for ; Mon, 13 Jun 2022 20:16:32 +0000 (UTC) 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=1655151391; 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=AqRJE5kHmUhU9DFMHo/l5MzOgI3EbkZf7j3BisUrZdo=; b=mIrxUO6yNmISFxVhORrnTzrHOpEeL/kLpMCN1K+a49yMcpUcxAigtTULezvJf30sL+wAyw Rp+NlTsOVqQfavGkK1yhonDcpWAej72Zl00+ifLviSvZ4cQgAtAzp1qpKRljb0D6kNe9Xl dJWXbiRA4Ni/5pltLo+AqeQNQWK+Duk= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 11/32] kasan: introduce kasan_requires_meta Date: Mon, 13 Jun 2022 22:14:02 +0200 Message-Id: <4201bc563d9553bca0278124e5ee4f1fe9a84ba6.1655150842.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655151392; 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=AqRJE5kHmUhU9DFMHo/l5MzOgI3EbkZf7j3BisUrZdo=; b=jXXs1srRw0Qp7JvmaUPhZCbPG2zpv12YKv2wMPGxAgno0RElwMlaRufNq619kcjIvpmPoT kdqvJNwZmuLdY8UPpdGAaYZSoXVrjLpMfO0OYK4FTKjrQj7TTEMfmvGVOd8PntTFl7NEK5 obERXxyT8eTsnqZ8KJ/q3KMBLoiLCJI= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=mIrxUO6y; spf=pass (imf05.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151392; a=rsa-sha256; cv=none; b=09bkILy+Rtp4XRpKSL/lwWirVi8YHzPWK4axMlRU67/fRG5LIrVD8qURFWdTDi7BREMnsY 4mvn5DjeCY0g6VSgE69zy5cctq0TAZ1/mhecePn23lBb+dEJnJpbWMOsmJgJ55NrUM8RNz qJJdhE2fyU0uw1eKzshoh0U+cjaDi9U= X-Stat-Signature: a5nz5deiisik5pjnnhfx5411aq1ayueg X-Rspamd-Queue-Id: 655F010008B X-Rspamd-Server: rspam11 X-Rspam-User: Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=mIrxUO6y; spf=pass (imf05.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-HE-Tag: 1655151392-701710 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: From: Andrey Konovalov Add a kasan_requires_meta() helper that indicates whether the enabled KASAN mode requires per-object metadata and use this helper in the common code. Also hide kasan_init_object_meta() under CONFIG_KASAN_GENERIC ifdef check, as Generic is the only mode that uses per-object metadata. To allow for a potential future change that makes Generic KASAN support the kasan.stacktrace command-line parameter, let kasan_requires_meta() return kasan_stack_collection_enabled() instead of simply returning true. Signed-off-by: Andrey Konovalov --- mm/kasan/common.c | 13 +++++-------- mm/kasan/kasan.h | 33 +++++++++++++++++++++++++++++---- mm/kasan/tags.c | 4 ---- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 226eaa714da2..a3dee7cead89 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -88,13 +88,10 @@ asmlinkage void kasan_unpoison_task_stack_below(const void *watermark) } #endif /* CONFIG_KASAN_STACK */ -/* - * Only allow cache merging when stack collection is disabled and no metadata - * is present. - */ +/* Only allow cache merging when no per-object metadata is present. */ slab_flags_t __kasan_never_merge(void) { - if (kasan_stack_collection_enabled()) + if (kasan_requires_meta()) return SLAB_KASAN; return 0; } @@ -151,7 +148,7 @@ void __kasan_cache_create(struct kmem_cache *cache, unsigned int *size, */ *flags |= SLAB_KASAN; - if (!kasan_stack_collection_enabled()) + if (!kasan_requires_meta()) return; ok_size = *size; @@ -219,7 +216,7 @@ void __kasan_cache_create_kmalloc(struct kmem_cache *cache) size_t __kasan_metadata_size(struct kmem_cache *cache) { - if (!kasan_stack_collection_enabled()) + if (!kasan_requires_meta()) return 0; return (cache->kasan_info.alloc_meta_offset ? sizeof(struct kasan_alloc_meta) : 0) + @@ -294,7 +291,7 @@ void * __must_check __kasan_init_slab_obj(struct kmem_cache *cache, const void *object) { /* Initialize per-object metadata if it is present. */ - if (kasan_stack_collection_enabled()) + if (kasan_requires_meta()) kasan_init_object_meta(cache, object); /* Tag is ignored in set_tag() without CONFIG_KASAN_SW/HW_TAGS */ diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index ff7a1597aa51..cf123d99f2fe 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -43,7 +43,7 @@ static inline bool kasan_sync_fault_possible(void) return kasan_mode == KASAN_MODE_SYNC || kasan_mode == KASAN_MODE_ASYMM; } -#else +#else /* CONFIG_KASAN_HW_TAGS */ static inline bool kasan_stack_collection_enabled(void) { @@ -60,7 +60,31 @@ static inline bool kasan_sync_fault_possible(void) return true; } -#endif +#endif /* CONFIG_KASAN_HW_TAGS */ + +#ifdef CONFIG_KASAN_GENERIC + +/* Generic KASAN uses per-object metadata to store stack traces. */ +static inline bool kasan_requires_meta(void) +{ + /* + * Technically, Generic KASAN always collects stack traces right now. + * However, let's use kasan_stack_collection_enabled() in case the + * kasan.stacktrace command-line argument is changed to affect + * Generic KASAN. + */ + return kasan_stack_collection_enabled(); +} + +#else /* CONFIG_KASAN_GENERIC */ + +/* Tag-based KASAN modes do not use per-object metadata. */ +static inline bool kasan_requires_meta(void) +{ + return false; +} + +#endif /* CONFIG_KASAN_GENERIC */ #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) #define KASAN_GRANULE_SIZE (1UL << KASAN_SHADOW_SCALE_SHIFT) @@ -271,13 +295,14 @@ void kasan_report_invalid_free(void *object, unsigned long ip); struct page *kasan_addr_to_page(const void *addr); struct slab *kasan_addr_to_slab(const void *addr); -void kasan_init_object_meta(struct kmem_cache *cache, const void *object); - #ifdef CONFIG_KASAN_GENERIC +void kasan_init_object_meta(struct kmem_cache *cache, const void *object); struct kasan_alloc_meta *kasan_get_alloc_meta(struct kmem_cache *cache, const void *object); struct kasan_free_meta *kasan_get_free_meta(struct kmem_cache *cache, const void *object); +#else +static inline void kasan_init_object_meta(struct kmem_cache *cache, const void *object) { } #endif depot_stack_handle_t kasan_save_stack(gfp_t flags, bool can_alloc); diff --git a/mm/kasan/tags.c b/mm/kasan/tags.c index f11c89505c77..4f24669085e9 100644 --- a/mm/kasan/tags.c +++ b/mm/kasan/tags.c @@ -17,10 +17,6 @@ #include "kasan.h" -void kasan_init_object_meta(struct kmem_cache *cache, const void *object) -{ -} - void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags) { } From patchwork Mon Jun 13 20:14:03 2022 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: 12880080 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 DC5BAC43334 for ; Mon, 13 Jun 2022 20:17:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0F8848D01C0; Mon, 13 Jun 2022 16:17:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 092F98D01CE; Mon, 13 Jun 2022 16:17:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D3C548D01C0; Mon, 13 Jun 2022 16:17:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id B9D3A8D01CC for ; Mon, 13 Jun 2022 16:17:35 -0400 (EDT) Received: from smtpin31.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id 952A2805B2 for ; Mon, 13 Jun 2022 20:17:35 +0000 (UTC) X-FDA: 79574322870.31.C217082 Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by imf13.hostedemail.com (Postfix) with ESMTP id E092020090 for ; Mon, 13 Jun 2022 20:17:34 +0000 (UTC) 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=1655151452; 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=juoeL9iVysZjLYrCYEKOizkTpbFJJdb8UyU7Sz7zXtQ=; b=RPTEnN3Q+Vk+b3sRFX80S/7v5i2XXhKAb7u9lQ7UwKhpB0xwKo8Vr26yP54OM3zYd3Olbf hSqLBs1FP8L9MNdW523biACQ14ecqL+rTnJBNVUWPRe8BAqZWh9Je146iyCwBfYCmvkoha TtmVe6o8a0gp1z+s1bkEeyR24gle/Ns= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 12/32] kasan: introduce kasan_init_cache_meta Date: Mon, 13 Jun 2022 22:14:03 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655151455; 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=juoeL9iVysZjLYrCYEKOizkTpbFJJdb8UyU7Sz7zXtQ=; b=DYIAc+nrSxZnWODT0pP1gn6TkEs73sCKR1wAxJnHQOL40/jwA6ySg6HxmpYn1f3mrh90FZ rE0O3bq4ExwQuihfC3KTsHzANTidPDN4YkmckDNjxXmcYqTbIDYH+ztZM4cQtWfQPBbnrd aDK2ExLNdYm4XwXApxh8iZhQNQvRFvA= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=RPTEnN3Q; spf=pass (imf13.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151455; a=rsa-sha256; cv=none; b=AIiNoRJj27oa23EuygChTQnl7CeeDDy0n/AajeGPkkLIqQCuy9TP/XUSOv7hEhjfHVVHIp HZS24YP4viZrt3HMCQoMq+eCivmXnw6GjQsI+O/92lWNYVc78LDzasK8AnUNH1j0BIiQcS XqzQxigga2rPnFMBGs6ieAwLeyv2kZM= X-Stat-Signature: gi3msrj67dz7e8u8unyy69gjxajmqesd X-Rspamd-Queue-Id: E092020090 X-Rspamd-Server: rspam11 X-Rspam-User: Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=RPTEnN3Q; spf=pass (imf13.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-HE-Tag: 1655151454-611517 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: From: Andrey Konovalov Add a kasan_init_cache_meta() helper that initializes metadata-related cache parameters and use this helper in the common KASAN code. Put the implementation of this new helper into generic.c, as only the Generic mode uses per-object metadata. Signed-off-by: Andrey Konovalov --- mm/kasan/common.c | 80 ++-------------------------------------------- mm/kasan/generic.c | 79 +++++++++++++++++++++++++++++++++++++++++++++ mm/kasan/kasan.h | 2 ++ 3 files changed, 83 insertions(+), 78 deletions(-) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index a3dee7cead89..8a83ca9ad738 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -117,28 +117,9 @@ void __kasan_poison_pages(struct page *page, unsigned int order, bool init) KASAN_PAGE_FREE, init); } -/* - * Adaptive redzone policy taken from the userspace AddressSanitizer runtime. - * For larger allocations larger redzones are used. - */ -static inline unsigned int optimal_redzone(unsigned int object_size) -{ - return - object_size <= 64 - 16 ? 16 : - object_size <= 128 - 32 ? 32 : - object_size <= 512 - 64 ? 64 : - object_size <= 4096 - 128 ? 128 : - object_size <= (1 << 14) - 256 ? 256 : - object_size <= (1 << 15) - 512 ? 512 : - object_size <= (1 << 16) - 1024 ? 1024 : 2048; -} - void __kasan_cache_create(struct kmem_cache *cache, unsigned int *size, slab_flags_t *flags) { - unsigned int ok_size; - unsigned int optimal_size; - /* * SLAB_KASAN is used to mark caches as ones that are sanitized by * KASAN. Currently this flag is used in two places: @@ -148,65 +129,8 @@ void __kasan_cache_create(struct kmem_cache *cache, unsigned int *size, */ *flags |= SLAB_KASAN; - if (!kasan_requires_meta()) - return; - - ok_size = *size; - - /* Add alloc meta into redzone. */ - cache->kasan_info.alloc_meta_offset = *size; - *size += sizeof(struct kasan_alloc_meta); - - /* - * If alloc meta doesn't fit, don't add it. - * This can only happen with SLAB, as it has KMALLOC_MAX_SIZE equal - * to KMALLOC_MAX_CACHE_SIZE and doesn't fall back to page_alloc for - * larger sizes. - */ - if (*size > KMALLOC_MAX_SIZE) { - cache->kasan_info.alloc_meta_offset = 0; - *size = ok_size; - /* Continue, since free meta might still fit. */ - } - - /* Only the generic mode uses free meta or flexible redzones. */ - if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) { - cache->kasan_info.free_meta_offset = KASAN_NO_FREE_META; - return; - } - - /* - * Add free meta into redzone when it's not possible to store - * it in the object. This is the case when: - * 1. Object is SLAB_TYPESAFE_BY_RCU, which means that it can - * be touched after it was freed, or - * 2. Object has a constructor, which means it's expected to - * retain its content until the next allocation, or - * 3. Object is too small. - * Otherwise cache->kasan_info.free_meta_offset = 0 is implied. - */ - if ((cache->flags & SLAB_TYPESAFE_BY_RCU) || cache->ctor || - cache->object_size < sizeof(struct kasan_free_meta)) { - ok_size = *size; - - cache->kasan_info.free_meta_offset = *size; - *size += sizeof(struct kasan_free_meta); - - /* If free meta doesn't fit, don't add it. */ - if (*size > KMALLOC_MAX_SIZE) { - cache->kasan_info.free_meta_offset = KASAN_NO_FREE_META; - *size = ok_size; - } - } - - /* Calculate size with optimal redzone. */ - optimal_size = cache->object_size + optimal_redzone(cache->object_size); - /* Limit it with KMALLOC_MAX_SIZE (relevant for SLAB only). */ - if (optimal_size > KMALLOC_MAX_SIZE) - optimal_size = KMALLOC_MAX_SIZE; - /* Use optimal size if the size with added metas is not large enough. */ - if (*size < optimal_size) - *size = optimal_size; + if (kasan_requires_meta()) + kasan_init_cache_meta(cache, size); } void __kasan_cache_create_kmalloc(struct kmem_cache *cache) diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c index fa654cb96a0d..73aea784040a 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -328,6 +328,85 @@ DEFINE_ASAN_SET_SHADOW(f3); DEFINE_ASAN_SET_SHADOW(f5); DEFINE_ASAN_SET_SHADOW(f8); +/* + * Adaptive redzone policy taken from the userspace AddressSanitizer runtime. + * For larger allocations larger redzones are used. + */ +static inline unsigned int optimal_redzone(unsigned int object_size) +{ + return + object_size <= 64 - 16 ? 16 : + object_size <= 128 - 32 ? 32 : + object_size <= 512 - 64 ? 64 : + object_size <= 4096 - 128 ? 128 : + object_size <= (1 << 14) - 256 ? 256 : + object_size <= (1 << 15) - 512 ? 512 : + object_size <= (1 << 16) - 1024 ? 1024 : 2048; +} + +void kasan_init_cache_meta(struct kmem_cache *cache, unsigned int *size) +{ + unsigned int ok_size; + unsigned int optimal_size; + + ok_size = *size; + + /* Add alloc meta into redzone. */ + cache->kasan_info.alloc_meta_offset = *size; + *size += sizeof(struct kasan_alloc_meta); + + /* + * If alloc meta doesn't fit, don't add it. + * This can only happen with SLAB, as it has KMALLOC_MAX_SIZE equal + * to KMALLOC_MAX_CACHE_SIZE and doesn't fall back to page_alloc for + * larger sizes. + */ + if (*size > KMALLOC_MAX_SIZE) { + cache->kasan_info.alloc_meta_offset = 0; + *size = ok_size; + /* Continue, since free meta might still fit. */ + } + + /* Only the generic mode uses free meta or flexible redzones. */ + if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) { + cache->kasan_info.free_meta_offset = KASAN_NO_FREE_META; + return; + } + + /* + * Add free meta into redzone when it's not possible to store + * it in the object. This is the case when: + * 1. Object is SLAB_TYPESAFE_BY_RCU, which means that it can + * be touched after it was freed, or + * 2. Object has a constructor, which means it's expected to + * retain its content until the next allocation, or + * 3. Object is too small. + * Otherwise cache->kasan_info.free_meta_offset = 0 is implied. + */ + if ((cache->flags & SLAB_TYPESAFE_BY_RCU) || cache->ctor || + cache->object_size < sizeof(struct kasan_free_meta)) { + ok_size = *size; + + cache->kasan_info.free_meta_offset = *size; + *size += sizeof(struct kasan_free_meta); + + /* If free meta doesn't fit, don't add it. */ + if (*size > KMALLOC_MAX_SIZE) { + cache->kasan_info.free_meta_offset = KASAN_NO_FREE_META; + *size = ok_size; + } + } + + /* Calculate size with optimal redzone. */ + optimal_size = cache->object_size + optimal_redzone(cache->object_size); + /* Limit it with KMALLOC_MAX_SIZE (relevant for SLAB only). */ + if (optimal_size > KMALLOC_MAX_SIZE) + optimal_size = KMALLOC_MAX_SIZE; + /* Use optimal size if the size with added metas is not large enough. */ + if (*size < optimal_size) + *size = optimal_size; +} + struct kasan_alloc_meta *kasan_get_alloc_meta(struct kmem_cache *cache, const void *object) { diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index cf123d99f2fe..ab2cd3ff10f3 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -296,12 +296,14 @@ struct page *kasan_addr_to_page(const void *addr); struct slab *kasan_addr_to_slab(const void *addr); #ifdef CONFIG_KASAN_GENERIC +void kasan_init_cache_meta(struct kmem_cache *cache, unsigned int *size); void kasan_init_object_meta(struct kmem_cache *cache, const void *object); struct kasan_alloc_meta *kasan_get_alloc_meta(struct kmem_cache *cache, const void *object); struct kasan_free_meta *kasan_get_free_meta(struct kmem_cache *cache, const void *object); #else +static inline void kasan_init_cache_meta(struct kmem_cache *cache, unsigned int *size) { } static inline void kasan_init_object_meta(struct kmem_cache *cache, const void *object) { } #endif From patchwork Mon Jun 13 20:14:04 2022 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: 12880079 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 556BDC433EF for ; Mon, 13 Jun 2022 20:17:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DDCAB8D01CD; Mon, 13 Jun 2022 16:17:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D8C9F8D01CC; Mon, 13 Jun 2022 16:17:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C2DD98D01CD; Mon, 13 Jun 2022 16:17:35 -0400 (EDT) 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 B05CF8D01C0 for ; Mon, 13 Jun 2022 16:17:35 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 83A3F21087 for ; Mon, 13 Jun 2022 20:17:35 +0000 (UTC) X-FDA: 79574322870.17.4B1C751 Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by imf25.hostedemail.com (Postfix) with ESMTP id E3EFBA0090 for ; Mon, 13 Jun 2022 20:17:34 +0000 (UTC) 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=1655151453; 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=gFl1aLadfWJPQ+Xg4RcAPABChePIqsdWPpe8i7e+EqU=; b=gY7JQXEBuSeNnEDCgLkcnyiE/DA+U2o3XKD0esu5cVnDXMCN4xpUf4g6sutMdMIbUtGf73 a1zplBChQUeo0ROg1thTsy+Fdn6m3+vLMYuukA+vIoYyvUtAmTNvUVdfHLByaaOWECK8os WtcT5mx80L6O+Oual9NGhwQYgarO5bw= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 13/32] kasan: drop CONFIG_KASAN_GENERIC check from kasan_init_cache_meta Date: Mon, 13 Jun 2022 22:14:04 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151455; a=rsa-sha256; cv=none; b=h5ZlT3R5zzHxAN3/GiT59TPQofbPUM3MyKjvNVdBHBMRmedDuEu7gHHj6Y1e0idV2M4iK7 8xaXKbav2vzmxbGhjJeEM+w6XidHMAIo48HFn8xjPLkvUAAx8rv8Zb+aSJGkCKK8z4jNQ4 1dPVOWvKn5mAkXg1TDW0BHQ1AjQB69U= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=gY7JQXEB; spf=pass (imf25.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 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=1655151455; 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=gFl1aLadfWJPQ+Xg4RcAPABChePIqsdWPpe8i7e+EqU=; b=41mxwSf30K+AH/21c+OqV8/PL5kBqqVY1jSj80pr3Jgo4xgV7n8ZIhMoxBALKlLa+2SGk9 dDcCE+5xqr/vtkUQetpHAAC24t6AXXjx+aIft0Is7RG0xGkwNAiYNX/RSXyxt8etr0aGmq nuQmFqtp3RoptEmw6a+vaxdfbr7i7m8= X-Stat-Signature: srnnx5ehke66q4peccq19rgufnikjajb X-Rspamd-Queue-Id: E3EFBA0090 Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=gY7JQXEB; spf=pass (imf25.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Rspam-User: X-Rspamd-Server: rspam02 X-HE-Tag: 1655151454-78784 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: From: Andrey Konovalov As kasan_init_cache_meta() is only defined for the Generic mode, it does not require the CONFIG_KASAN_GENERIC check. Signed-off-by: Andrey Konovalov --- mm/kasan/generic.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c index 73aea784040a..5125fad76f70 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -367,12 +367,6 @@ void kasan_init_cache_meta(struct kmem_cache *cache, unsigned int *size) /* Continue, since free meta might still fit. */ } - /* Only the generic mode uses free meta or flexible redzones. */ - if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) { - cache->kasan_info.free_meta_offset = KASAN_NO_FREE_META; - return; - } - /* * Add free meta into redzone when it's not possible to store * it in the object. This is the case when: From patchwork Mon Jun 13 20:14:05 2022 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: 12880081 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 4594ACCA480 for ; Mon, 13 Jun 2022 20:17:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B94FC8D01CE; Mon, 13 Jun 2022 16:17:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B1B158D01CC; Mon, 13 Jun 2022 16:17:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A0C438D01CE; Mon, 13 Jun 2022 16:17:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 6C9F78D01CC for ; Mon, 13 Jun 2022 16:17:36 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id 36F33806A4 for ; Mon, 13 Jun 2022 20:17:36 +0000 (UTC) X-FDA: 79574322912.07.6D75DB4 Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by imf17.hostedemail.com (Postfix) with ESMTP id BEFB240095 for ; Mon, 13 Jun 2022 20:17:35 +0000 (UTC) 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=1655151454; 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=Pq3SVj2SILQniBWfHmFbw4zt5aRrCme3SaUMW3qAt2g=; b=me0bkbVmYmCzrLFlXtup0jhn6Pil4gvVnK0D2ckpBikr2kc/XBrruIv3MB8seUrHLIcyEX jCHxiIQvWilxxNnppnl2HriB39QLCrHPqwAa5ekVCtmCHndMmuPy38vmyqZ8DM+0atTEwj 7F6uJE25lzsWxTXIuVgpDpMm6tYv/qQ= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 14/32] kasan: only define kasan_metadata_size for Generic mode Date: Mon, 13 Jun 2022 22:14:05 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151455; a=rsa-sha256; cv=none; b=U/KkSK/ZJNdCk123vXpri5YhqdYfSS0ZaFwc/Q17E6ChC1UZM6vDMf4NSix4X8+eTlpeMz t7f6w0zBWJ9Dxk8jSgnwWEcdXV5vxq0YjKAWrOBsqNHxtqYeBbxzjjRB2GxP2J2t1Siw0G xKkG9FnjvTPSve8ELMtcvDFNDbih9UM= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=me0bkbVm; spf=pass (imf17.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 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=1655151455; 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=Pq3SVj2SILQniBWfHmFbw4zt5aRrCme3SaUMW3qAt2g=; b=vfD4hOB7LyNR+pl+7XwHcRDHABGaydxIoJT+AlsTW+Yljm1cbUObLrMiOdaRnq1+Vfl8yE AbRpr9bM23lBWlmbcLVv3g1Wm66k8bZ+PMwKUrg0kNn90Gg4m8scCb31SpqD0keTm3Hr24 ns2ZJJKCmrDS9kau90t1IvL7JhX4XLQ= X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: BEFB240095 X-Stat-Signature: dq3kntw4hdnhzeq8u1gj81pjfk1jey6b Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=me0bkbVm; spf=pass (imf17.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-HE-Tag: 1655151455-786362 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: From: Andrey Konovalov KASAN provides a helper for calculating the size of per-object metadata stored in the redzone. As now only the Generic mode uses per-object metadata, only define kasan_metadata_size() for this mode. Signed-off-by: Andrey Konovalov --- include/linux/kasan.h | 17 ++++++++--------- mm/kasan/common.c | 11 ----------- mm/kasan/generic.c | 11 +++++++++++ 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index b092277bf48d..027df7599573 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -150,14 +150,6 @@ static __always_inline void kasan_cache_create_kmalloc(struct kmem_cache *cache) __kasan_cache_create_kmalloc(cache); } -size_t __kasan_metadata_size(struct kmem_cache *cache); -static __always_inline size_t kasan_metadata_size(struct kmem_cache *cache) -{ - if (kasan_enabled()) - return __kasan_metadata_size(cache); - return 0; -} - void __kasan_poison_slab(struct slab *slab); static __always_inline void kasan_poison_slab(struct slab *slab) { @@ -282,7 +274,6 @@ static inline void kasan_cache_create(struct kmem_cache *cache, unsigned int *size, slab_flags_t *flags) {} static inline void kasan_cache_create_kmalloc(struct kmem_cache *cache) {} -static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; } static inline void kasan_poison_slab(struct slab *slab) {} static inline void kasan_unpoison_object_data(struct kmem_cache *cache, void *object) {} @@ -333,6 +324,8 @@ static inline void kasan_unpoison_task_stack(struct task_struct *task) {} #ifdef CONFIG_KASAN_GENERIC +size_t kasan_metadata_size(struct kmem_cache *cache); + void kasan_cache_shrink(struct kmem_cache *cache); void kasan_cache_shutdown(struct kmem_cache *cache); void kasan_record_aux_stack(void *ptr); @@ -340,6 +333,12 @@ void kasan_record_aux_stack_noalloc(void *ptr); #else /* CONFIG_KASAN_GENERIC */ +/* Tag-based KASAN modes do not use per-object metadata. */ +static inline size_t kasan_metadata_size(struct kmem_cache *cache) +{ + return 0; +} + static inline void kasan_cache_shrink(struct kmem_cache *cache) {} static inline void kasan_cache_shutdown(struct kmem_cache *cache) {} static inline void kasan_record_aux_stack(void *ptr) {} diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 8a83ca9ad738..a0ddbf02aa6d 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -138,17 +138,6 @@ void __kasan_cache_create_kmalloc(struct kmem_cache *cache) cache->kasan_info.is_kmalloc = true; } -size_t __kasan_metadata_size(struct kmem_cache *cache) -{ - if (!kasan_requires_meta()) - return 0; - return (cache->kasan_info.alloc_meta_offset ? - sizeof(struct kasan_alloc_meta) : 0) + - ((cache->kasan_info.free_meta_offset && - cache->kasan_info.free_meta_offset != KASAN_NO_FREE_META) ? - sizeof(struct kasan_free_meta) : 0); -} - void __kasan_poison_slab(struct slab *slab) { struct page *page = slab_page(slab); diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c index 5125fad76f70..806ab92032c3 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -427,6 +427,17 @@ void kasan_init_object_meta(struct kmem_cache *cache, const void *object) __memset(alloc_meta, 0, sizeof(*alloc_meta)); } +size_t kasan_metadata_size(struct kmem_cache *cache) +{ + if (!kasan_requires_meta()) + return 0; + return (cache->kasan_info.alloc_meta_offset ? + sizeof(struct kasan_alloc_meta) : 0) + + ((cache->kasan_info.free_meta_offset && + cache->kasan_info.free_meta_offset != KASAN_NO_FREE_META) ? + sizeof(struct kasan_free_meta) : 0); +} + static void __kasan_record_aux_stack(void *addr, bool can_alloc) { struct slab *slab = kasan_addr_to_slab(addr); From patchwork Mon Jun 13 20:14:06 2022 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: 12880082 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 B50C9CCA47D for ; Mon, 13 Jun 2022 20:17:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B6EA28D01CF; Mon, 13 Jun 2022 16:17:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A80E08D01CC; Mon, 13 Jun 2022 16:17:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8D51C8D01CF; Mon, 13 Jun 2022 16:17:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 7BC8C8D01CC for ; Mon, 13 Jun 2022 16:17:37 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay12.hostedemail.com (Postfix) with ESMTP id 4D1181201D7 for ; Mon, 13 Jun 2022 20:17:37 +0000 (UTC) X-FDA: 79574322954.19.02DEEEC Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by imf23.hostedemail.com (Postfix) with ESMTP id A564914008D for ; Mon, 13 Jun 2022 20:17:36 +0000 (UTC) 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=1655151455; 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=yMpcVjLCf9jqLKxVClgu+qWUo0HLupVHHXi+5F8746E=; b=W7OMMObIdEFuIPfCrKY3YX9PY6I0FySwEipFhkFtUKodGGsq+xWXiJqwS+x9wJegWfNo7z fTUlZFnCQCdtPAfZGVLrrwRHH24cRz4nx72QwSd3+2FCqFet3GsX1TDXf4Vc5xvkFMC7Gu AvI9fkJxdHOC4FfKPyLlHE95kK6ubGs= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 15/32] kasan: only define kasan_never_merge for Generic mode Date: Mon, 13 Jun 2022 22:14:06 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151456; a=rsa-sha256; cv=none; b=k3emm+MXjyn3xWtDYMLj5Sp/VpsLTvUbKvnKyuXpsAk8ph20VC3D7zkY0LOg64i4N5/QVa J9X+DZeLgUfCr6E1WMZ2BOy5c7o8TZ9IIsZweSM+f5eC1wDAQCrjERriQQrixAPktvLcVM F2rCyBg63wA5yQ35Mq/h9r0cW22jz84= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=W7OMMObI; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf23.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 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=1655151456; 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=yMpcVjLCf9jqLKxVClgu+qWUo0HLupVHHXi+5F8746E=; b=3Wzh0nBMiiofRQ8fnaO9SLxpd2QvFjJyCnekoRJWT4YxIJyTvCFZLddM3juXIStRCmiqAi my+cPlBqdr19gaX8plsqr/ideSsdLYwr4JX9BguTtZuGU8QRx6F8483l1VAyi6Hi/FYX4Y v6RoThqE+/ysc7L0K4QlZeiyYVyAhXk= Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=W7OMMObI; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf23.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-Rspam-User: X-Stat-Signature: bn67qmj4sguby19ciq1qhiz7utu4bh3i X-Rspamd-Queue-Id: A564914008D X-Rspamd-Server: rspam08 X-HE-Tag: 1655151456-723601 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: From: Andrey Konovalov KASAN prevents merging of slab caches whose objects have per-object metadata stored in redzones. As now only the Generic mode uses per-object metadata, define kasan_never_merge() only for this mode. Signed-off-by: Andrey Konovalov --- include/linux/kasan.h | 18 ++++++------------ mm/kasan/common.c | 8 -------- mm/kasan/generic.c | 8 ++++++++ 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index 027df7599573..9743d4b3a918 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -103,14 +103,6 @@ struct kasan_cache { bool is_kmalloc; }; -slab_flags_t __kasan_never_merge(void); -static __always_inline slab_flags_t kasan_never_merge(void) -{ - if (kasan_enabled()) - return __kasan_never_merge(); - return 0; -} - void __kasan_unpoison_range(const void *addr, size_t size); static __always_inline void kasan_unpoison_range(const void *addr, size_t size) { @@ -261,10 +253,6 @@ static __always_inline bool kasan_check_byte(const void *addr) #else /* CONFIG_KASAN */ -static inline slab_flags_t kasan_never_merge(void) -{ - return 0; -} static inline void kasan_unpoison_range(const void *address, size_t size) {} static inline void kasan_poison_pages(struct page *page, unsigned int order, bool init) {} @@ -325,6 +313,7 @@ static inline void kasan_unpoison_task_stack(struct task_struct *task) {} #ifdef CONFIG_KASAN_GENERIC size_t kasan_metadata_size(struct kmem_cache *cache); +slab_flags_t kasan_never_merge(void); void kasan_cache_shrink(struct kmem_cache *cache); void kasan_cache_shutdown(struct kmem_cache *cache); @@ -338,6 +327,11 @@ static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; } +/* And thus nothing prevents cache merging. */ +static inline slab_flags_t kasan_never_merge(void) +{ + return 0; +} static inline void kasan_cache_shrink(struct kmem_cache *cache) {} static inline void kasan_cache_shutdown(struct kmem_cache *cache) {} diff --git a/mm/kasan/common.c b/mm/kasan/common.c index a0ddbf02aa6d..f8ef40fa31e3 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -88,14 +88,6 @@ asmlinkage void kasan_unpoison_task_stack_below(const void *watermark) } #endif /* CONFIG_KASAN_STACK */ -/* Only allow cache merging when no per-object metadata is present. */ -slab_flags_t __kasan_never_merge(void) -{ - if (kasan_requires_meta()) - return SLAB_KASAN; - return 0; -} - void __kasan_unpoison_pages(struct page *page, unsigned int order, bool init) { u8 tag; diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c index 806ab92032c3..25333bf3c99f 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -328,6 +328,14 @@ DEFINE_ASAN_SET_SHADOW(f3); DEFINE_ASAN_SET_SHADOW(f5); DEFINE_ASAN_SET_SHADOW(f8); +/* Only allow cache merging when no per-object metadata is present. */ +slab_flags_t kasan_never_merge(void) +{ + if (!kasan_requires_meta()) + return 0; + return SLAB_KASAN; +} + /* * Adaptive redzone policy taken from the userspace AddressSanitizer runtime. * For larger allocations larger redzones are used. From patchwork Mon Jun 13 20:14:07 2022 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: 12880083 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 17F01CCA47B for ; Mon, 13 Jun 2022 20:17:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 469E58D01D0; Mon, 13 Jun 2022 16:17:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 37D398D01D1; Mon, 13 Jun 2022 16:17:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F3BA98D01D0; Mon, 13 Jun 2022 16:17:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id DA6628D01CC for ; Mon, 13 Jun 2022 16:17:38 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay13.hostedemail.com (Postfix) with ESMTP id B199C60C02 for ; Mon, 13 Jun 2022 20:17:38 +0000 (UTC) X-FDA: 79574322996.12.6D89FB1 Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by imf29.hostedemail.com (Postfix) with ESMTP id B8A21120088 for ; Mon, 13 Jun 2022 20:17:37 +0000 (UTC) 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=1655151456; 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=tSwVFyyCCOKYSS6nxmIV1PC3/qH9HgKDBeITqQWAPk0=; b=WPoF2wA8zrQJNUhNOi67mLPqkW+l6qCNPsDjnmD3jT2JzXaRnbEZfdN1PG9hjWxvcBGE/p YaM8gS1aZsx2KRB+eZgKeQGJbzkwaQyiC9eW09wivY8anWjgE47sCTEBIvu6laZIFCup3r 3KjAbbgkMrIctOvPB4lHtNWIK5aDe9k= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 16/32] kasan: only define metadata offsets for Generic mode Date: Mon, 13 Jun 2022 22:14:07 +0200 Message-Id: <56df12dd774101d121cfcfbbab69d71851d8671a.1655150842.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151458; a=rsa-sha256; cv=none; b=hYyAFtGI/Re3UM0LSpb2zq0eMGdRuHhPKt3azRplyFXukf+AxELxIfUp6C1+yC7UdPmJUV 3pHZ41NT10AbCTeOZTSGy4/+XYpGE0AKWwoFl1+TRXC7E5rJFLOayKvHzP8RKCFtwRnCRi 7TDNM70eByGJcAZIcinfWtFtb7DHjCg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655151458; 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=tSwVFyyCCOKYSS6nxmIV1PC3/qH9HgKDBeITqQWAPk0=; b=ikgA54ZG6sgf0pcvc4rUslcCj/lgN9b4ze5NdustsHPFP/BUlO6zlh8GpYaLK4tMmX9akL LEuHhnAntDCnqMInJjclwXW0G+z5qfT2H60bATi1BHXZraIhauRjHossvc5D+iaDIKGeZl ib1085aQb+H+At2GBjBipiQST0t7BZE= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=WPoF2wA8; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf29.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=WPoF2wA8; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf29.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: B8A21120088 X-Stat-Signature: mo1je3we68imwq58omygouxr8d499o5g X-HE-Tag: 1655151457-563339 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: From: Andrey Konovalov Hide the definitions of alloc_meta_offset and free_meta_offset under an ifdef CONFIG_KASAN_GENERIC check, as these fields are now only used when the Generic mode is enabled. Signed-off-by: Andrey Konovalov --- include/linux/kasan.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index 9743d4b3a918..a212c2e3f32d 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -98,8 +98,10 @@ static inline bool kasan_has_integrated_init(void) #ifdef CONFIG_KASAN struct kasan_cache { +#ifdef CONFIG_KASAN_GENERIC int alloc_meta_offset; int free_meta_offset; +#endif bool is_kmalloc; }; From patchwork Mon Jun 13 20:14:08 2022 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: 12880084 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 87AA0C433EF for ; Mon, 13 Jun 2022 20:17:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 85E7C8D01D1; Mon, 13 Jun 2022 16:17:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 80E3E8D01CC; Mon, 13 Jun 2022 16:17:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 54F2C8D01D2; Mon, 13 Jun 2022 16:17:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 33D998D01CC for ; Mon, 13 Jun 2022 16:17:39 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay13.hostedemail.com (Postfix) with ESMTP id 1567160C02 for ; Mon, 13 Jun 2022 20:17:39 +0000 (UTC) X-FDA: 79574323038.03.6B1E20D Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by imf22.hostedemail.com (Postfix) with ESMTP id 8FE55C00A0 for ; Mon, 13 Jun 2022 20:17:38 +0000 (UTC) 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=1655151457; 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=vo/QgRsJex180B/MZOFV4gS3W10znq++oNOGcslvwl0=; b=Dl9JaXiPgE3KB2W9PytyX2Ydb8CErjoRbyqdS1Z4vTV2EHG4vJto9UgmTzKVC+tzN4k6r4 KDwziMj6rUu7nd8whB1jFF7vvWI6sRETVADmv38Uoj7/ZNOexQsxpkRPRqeeSDyuwqGEvH BwEOie7GxwDdyuGfRMy3sQ8YZis7ANg= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 17/32] kasan: only define metadata structs for Generic mode Date: Mon, 13 Jun 2022 22:14:08 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Dl9JaXiP; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf22.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 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=1655151458; 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=vo/QgRsJex180B/MZOFV4gS3W10znq++oNOGcslvwl0=; b=1jY2zY51m9UN39Aq+xpXv/0I1BS30O4/S3kOkB9K9P8gzZOy0qa6ADh99zSy0Jzn4Ru8FN kfPyM5j/0QbBSrKhERmbP1xU1Dt1v+Ke/U/9PorLqSwLDfd2DEZ0YdQp6VNW0/BYMB8hGI yFg9lcX0xFrduSTlMo3d1YUcqTaTM+A= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151458; a=rsa-sha256; cv=none; b=Mhd7c7OzsGor3NiYg4X0FtFmucsVVIkI7kvpxyYpv4Ua7GGCYAT6aApRYPpOnmX9ZXue0S C5Xs/9+4vGz1QvsP2dbvIIQWSkbwsOTLSLZ2HfSgpvZBrlBMLBKAP23huzBpLWPLgWWJSd RsTLEMiDUSnCCElR6NxChHsWPG9HIw0= X-Stat-Signature: dwxu3ac3j6qoxwnjxpff6s7yb6emu595 X-Rspamd-Queue-Id: 8FE55C00A0 X-Rspam-User: Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Dl9JaXiP; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf22.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-Rspamd-Server: rspam10 X-HE-Tag: 1655151458-155878 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: From: Andrey Konovalov Hide the definitions of kasan_alloc_meta and kasan_free_meta under an ifdef CONFIG_KASAN_GENERIC check, as these structures are now only used when the Generic mode is enabled. Signed-off-by: Andrey Konovalov --- mm/kasan/kasan.h | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index ab2cd3ff10f3..30ec9ebf52c3 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -192,14 +192,12 @@ struct kasan_track { depot_stack_handle_t stack; }; +#ifdef CONFIG_KASAN_GENERIC + struct kasan_alloc_meta { struct kasan_track alloc_track; - /* Generic mode stores free track in kasan_free_meta. */ -#ifdef CONFIG_KASAN_GENERIC + /* Free track is stored in kasan_free_meta. */ depot_stack_handle_t aux_stack[2]; -#else - struct kasan_track free_track; -#endif }; struct qlist_node { @@ -218,12 +216,12 @@ struct qlist_node { * After that, slab allocator stores the freelist pointer in the object. */ struct kasan_free_meta { -#ifdef CONFIG_KASAN_GENERIC struct qlist_node quarantine_link; struct kasan_track free_track; -#endif }; +#endif /* CONFIG_KASAN_GENERIC */ + #if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST) /* Used in KUnit-compatible KASAN tests. */ struct kunit_kasan_status { From patchwork Mon Jun 13 20:14:09 2022 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: 12880093 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 CEEB9C43334 for ; Mon, 13 Jun 2022 20:18:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 63C158D01D2; Mon, 13 Jun 2022 16:18:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5EBB78D01CC; Mon, 13 Jun 2022 16:18:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 48BFC8D01D2; Mon, 13 Jun 2022 16:18:40 -0400 (EDT) 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 3A43A8D01CC for ; Mon, 13 Jun 2022 16:18:40 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 139F8350E5 for ; Mon, 13 Jun 2022 20:18:40 +0000 (UTC) X-FDA: 79574325600.11.387ADD5 Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by imf16.hostedemail.com (Postfix) with ESMTP id 7CA5318008C for ; Mon, 13 Jun 2022 20:18:39 +0000 (UTC) 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=1655151518; 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=krlQc9a+2od73hH5OepUxMqNSDz17Dvev41TaF/lT74=; b=SiOziBQohKmxglt9gyt2z15vyuY6VDkJi05rIsIWQC5urqrUQvTsknwkvFgzNKy5/RM+NT n0qq14O14x0Ms4P0aPkh9AUu7+gR4yNKJKuDKaIWQhAIoayHw5hK6QdgNsXuZ6eQPNI3dC x0d88rqwK2C0H1l+vZH/91YiBhUHW0o= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 18/32] kasan: only define kasan_cache_create for Generic mode Date: Mon, 13 Jun 2022 22:14:09 +0200 Message-Id: <202a0b87b16b683e32a68cf3d71d369268904829.1655150842.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655151519; 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=krlQc9a+2od73hH5OepUxMqNSDz17Dvev41TaF/lT74=; b=YT0/YyXUijr+nt+f54VXRJq/YsUma9/Oy6rX9x4AATdHKsijVet+tuBv9bZfV38I+hpNcO IM/TfCm9roC9ALdGX2Gh4bshZSrEU2rg+uu9nefvuzE1Xijl8pI930L+QZyadVV/tSVV9Q jbWz5rqGo5FJyTBlT6siXJ+2yM4dB3M= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151519; a=rsa-sha256; cv=none; b=ZtilSI5bZk2emBMi4y+lJkLkFazP1FaQDEGnNjinuGw6BSonTNJDOjB9/m3ZAWH/W21KVs MWLTNw8pFBzY2ZRCYGQ/zNlKqb37p5CrRdnhBCPhc3kykUVxceLFkKM/ZCE90zACuyg7vV plU82YV9/nWdpXH3rKTHuMMXr8R/eUs= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=SiOziBQo; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf16.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=SiOziBQo; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf16.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-Rspamd-Server: rspam12 X-Rspam-User: X-Stat-Signature: x1e51rpfenwxfhmfrdzwo8ho7dmeb464 X-Rspamd-Queue-Id: 7CA5318008C X-HE-Tag: 1655151519-717444 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: From: Andrey Konovalov Right now, kasan_cache_create() assigns SLAB_KASAN for all KASAN modes and then sets up metadata-related cache parameters for the Generic mode. SLAB_KASAN is used in two places: 1. In slab_ksize() to account for per-object metadata when calculating the size of the accessible memory within the object. 2. In slab_common.c via kasan_never_merge() to prevent merging of caches with per-object metadata. Both cases are only relevant when per-object metadata is present, which is only the case with the Generic mode. Thus, assign SLAB_KASAN and define kasan_cache_create() only for the Generic mode. Also update the SLAB_KASAN-related comment. Signed-off-by: Andrey Konovalov --- include/linux/kasan.h | 18 ++++++------------ include/linux/slab.h | 2 +- mm/kasan/common.c | 16 ---------------- mm/kasan/generic.c | 17 ++++++++++++++++- 4 files changed, 23 insertions(+), 30 deletions(-) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index a212c2e3f32d..d811b3d7d2a1 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -128,15 +128,6 @@ static __always_inline void kasan_unpoison_pages(struct page *page, __kasan_unpoison_pages(page, order, init); } -void __kasan_cache_create(struct kmem_cache *cache, unsigned int *size, - slab_flags_t *flags); -static __always_inline void kasan_cache_create(struct kmem_cache *cache, - unsigned int *size, slab_flags_t *flags) -{ - if (kasan_enabled()) - __kasan_cache_create(cache, size, flags); -} - void __kasan_cache_create_kmalloc(struct kmem_cache *cache); static __always_inline void kasan_cache_create_kmalloc(struct kmem_cache *cache) { @@ -260,9 +251,6 @@ static inline void kasan_poison_pages(struct page *page, unsigned int order, bool init) {} static inline void kasan_unpoison_pages(struct page *page, unsigned int order, bool init) {} -static inline void kasan_cache_create(struct kmem_cache *cache, - unsigned int *size, - slab_flags_t *flags) {} static inline void kasan_cache_create_kmalloc(struct kmem_cache *cache) {} static inline void kasan_poison_slab(struct slab *slab) {} static inline void kasan_unpoison_object_data(struct kmem_cache *cache, @@ -316,6 +304,8 @@ static inline void kasan_unpoison_task_stack(struct task_struct *task) {} size_t kasan_metadata_size(struct kmem_cache *cache); slab_flags_t kasan_never_merge(void); +void kasan_cache_create(struct kmem_cache *cache, unsigned int *size, + slab_flags_t *flags); void kasan_cache_shrink(struct kmem_cache *cache); void kasan_cache_shutdown(struct kmem_cache *cache); @@ -334,6 +324,10 @@ static inline slab_flags_t kasan_never_merge(void) { return 0; } +/* And no cache-related metadata initialization is required. */ +static inline void kasan_cache_create(struct kmem_cache *cache, + unsigned int *size, + slab_flags_t *flags) {} static inline void kasan_cache_shrink(struct kmem_cache *cache) {} static inline void kasan_cache_shutdown(struct kmem_cache *cache) {} diff --git a/include/linux/slab.h b/include/linux/slab.h index 0fefdf528e0d..1c6b7362e82b 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -106,7 +106,7 @@ # define SLAB_ACCOUNT 0 #endif -#ifdef CONFIG_KASAN +#ifdef CONFIG_KASAN_GENERIC #define SLAB_KASAN ((slab_flags_t __force)0x08000000U) #else #define SLAB_KASAN 0 diff --git a/mm/kasan/common.c b/mm/kasan/common.c index f8ef40fa31e3..f937b6c9e86a 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -109,22 +109,6 @@ void __kasan_poison_pages(struct page *page, unsigned int order, bool init) KASAN_PAGE_FREE, init); } -void __kasan_cache_create(struct kmem_cache *cache, unsigned int *size, - slab_flags_t *flags) -{ - /* - * SLAB_KASAN is used to mark caches as ones that are sanitized by - * KASAN. Currently this flag is used in two places: - * 1. In slab_ksize() when calculating the size of the accessible - * memory within the object. - * 2. In slab_common.c to prevent merging of sanitized caches. - */ - *flags |= SLAB_KASAN; - - if (kasan_requires_meta()) - kasan_init_cache_meta(cache, size); -} - void __kasan_cache_create_kmalloc(struct kmem_cache *cache) { cache->kasan_info.is_kmalloc = true; diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c index 25333bf3c99f..f6bef347de87 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -352,11 +352,26 @@ static inline unsigned int optimal_redzone(unsigned int object_size) object_size <= (1 << 16) - 1024 ? 1024 : 2048; } -void kasan_init_cache_meta(struct kmem_cache *cache, unsigned int *size) +void kasan_cache_create(struct kmem_cache *cache, unsigned int *size, + slab_flags_t *flags) { unsigned int ok_size; unsigned int optimal_size; + if (!kasan_requires_meta()) + return; + + /* + * SLAB_KASAN is used to mark caches that are sanitized by KASAN + * and that thus have per-object metadata. + * Currently this flag is used in two places: + * 1. In slab_ksize() to account for per-object metadata when + * calculating the size of the accessible memory within the object. + * 2. In slab_common.c via kasan_never_merge() to prevent merging of + * caches with per-object metadata. + */ + *flags |= SLAB_KASAN; + ok_size = *size; /* Add alloc meta into redzone. */ From patchwork Mon Jun 13 20:14:10 2022 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: 12880094 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 9EFB6C433EF for ; Mon, 13 Jun 2022 20:18:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2E04C8D01D3; Mon, 13 Jun 2022 16:18:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2689D8D01CC; Mon, 13 Jun 2022 16:18:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 045AA8D01D3; Mon, 13 Jun 2022 16:18:40 -0400 (EDT) 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 E26038D01CC for ; Mon, 13 Jun 2022 16:18:40 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay13.hostedemail.com (Postfix) with ESMTP id B8D0260F16 for ; Mon, 13 Jun 2022 20:18:40 +0000 (UTC) X-FDA: 79574325600.01.9FD157A Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by imf23.hostedemail.com (Postfix) with ESMTP id 377FD140094 for ; Mon, 13 Jun 2022 20:18:40 +0000 (UTC) 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=1655151519; 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=jgPdc9N08SZf7w3C2n6o7Rgh7KKDeUV8LWJsDjGx3bA=; b=NmokpH+0cZI1My+Kia6o4N/O4CH1Ohkjye7zoCZBpU1Oofm2/IDEHcGCtizwC2kU/Qynz0 kCcd+7/0tQh3lhwo+1v6s5zZk7oX4ARtAEv77wfd1kD14waBO/6XQfX/fRbKwWkaT26638 Eap9dZbPU9cl4Q2BDYbidCoIAg6Cj8c= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 19/32] kasan: pass tagged pointers to kasan_save_alloc/free_info Date: Mon, 13 Jun 2022 22:14:10 +0200 Message-Id: <9363b16202fb04a3223de714e70b7a6b72c4367e.1655150842.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655151520; 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=jgPdc9N08SZf7w3C2n6o7Rgh7KKDeUV8LWJsDjGx3bA=; b=Ews4k0tLpTMrs0LoT6wHgx/2C8CtC2aDYAYkFMePWQ8sWOf3mJbtRap4N7cQjQ+cbPsJIW z+xCgJHPWtbiUgS/Gu9qUOWzdr7OCYBO9TlYyL3EEGMylbMbqDr0WpBF0Td2raW9WfjtNo VvDI+OzkMsPk2zt7L5kHWDnTH82iLJw= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=NmokpH+0; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf23.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151520; a=rsa-sha256; cv=none; b=d5jLsQjY0sVbo2ykmQriERGfyNI8H+CUqBfzeyGqTYlybm1VyCm0VcC7gV6qKUVRAC/1jl zAgdjCjGg/AfipPQqqK7IZ5E+CX+xfwfHQv1HZtTDBm5V0zRQj/1iNfdLciO9vgP+NxOy7 Q65sFRP0RnyaRBPbf3U4nYqYER4wZ60= X-Rspamd-Queue-Id: 377FD140094 X-Rspam-User: X-Stat-Signature: rx5rfkc18juwbb96rrmwbqaotpxu1ppd Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=NmokpH+0; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf23.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-Rspamd-Server: rspam04 X-HE-Tag: 1655151520-448033 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: From: Andrey Konovalov Pass tagged pointers to kasan_save_alloc/free_info(). This is a preparatory patch to simplify other changes in the series. Signed-off-by: Andrey Konovalov --- mm/kasan/common.c | 4 ++-- mm/kasan/generic.c | 3 +-- mm/kasan/kasan.h | 2 +- mm/kasan/tags.c | 3 +-- 4 files changed, 5 insertions(+), 7 deletions(-) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index f937b6c9e86a..519fd0b3040b 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -227,7 +227,7 @@ static inline bool ____kasan_slab_free(struct kmem_cache *cache, void *object, return false; if (kasan_stack_collection_enabled()) - kasan_save_free_info(cache, object, tag); + kasan_save_free_info(cache, tagged_object); return kasan_quarantine_put(cache, object); } @@ -316,7 +316,7 @@ void * __must_check __kasan_slab_alloc(struct kmem_cache *cache, /* Save alloc info (if possible) for non-kmalloc() allocations. */ if (kasan_stack_collection_enabled() && !cache->kasan_info.is_kmalloc) - kasan_save_alloc_info(cache, (void *)object, flags); + kasan_save_alloc_info(cache, tagged_object, flags); return tagged_object; } diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c index f6bef347de87..aff39af3c532 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -500,8 +500,7 @@ void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags) kasan_set_track(&alloc_meta->alloc_track, flags); } -void kasan_save_free_info(struct kmem_cache *cache, - void *object, u8 tag) +void kasan_save_free_info(struct kmem_cache *cache, void *object) { struct kasan_free_meta *free_meta; diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index 30ec9ebf52c3..e8329935fbfb 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -308,7 +308,7 @@ static inline void kasan_init_object_meta(struct kmem_cache *cache, const void * depot_stack_handle_t kasan_save_stack(gfp_t flags, bool can_alloc); void kasan_set_track(struct kasan_track *track, gfp_t flags); void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags); -void kasan_save_free_info(struct kmem_cache *cache, void *object, u8 tag); +void kasan_save_free_info(struct kmem_cache *cache, void *object); struct kasan_track *kasan_get_alloc_track(struct kmem_cache *cache, void *object); struct kasan_track *kasan_get_free_track(struct kmem_cache *cache, diff --git a/mm/kasan/tags.c b/mm/kasan/tags.c index 4f24669085e9..fd11d10a4ffc 100644 --- a/mm/kasan/tags.c +++ b/mm/kasan/tags.c @@ -21,8 +21,7 @@ void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags) { } -void kasan_save_free_info(struct kmem_cache *cache, - void *object, u8 tag) +void kasan_save_free_info(struct kmem_cache *cache, void *object) { } From patchwork Mon Jun 13 20:14:11 2022 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: 12880095 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 E1A07CCA47B for ; Mon, 13 Jun 2022 20:18:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BE4358D01D4; Mon, 13 Jun 2022 16:18:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B6A9E8D01CC; Mon, 13 Jun 2022 16:18:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A0B868D01D4; Mon, 13 Jun 2022 16:18:41 -0400 (EDT) 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 8FE388D01CC for ; Mon, 13 Jun 2022 16:18:41 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 6ECB835140 for ; Mon, 13 Jun 2022 20:18:41 +0000 (UTC) X-FDA: 79574325642.30.F4F6EC0 Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by imf26.hostedemail.com (Postfix) with ESMTP id C9EB6140081 for ; Mon, 13 Jun 2022 20:18:40 +0000 (UTC) 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=1655151519; 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=T+1jiBrKGTOAkQ9I8eaP4PIHMCNWMSHqgu/PvHey6xE=; b=F5EnSyRzoYiX0f3p7v1JZyYXYCdly4dsV81EJcSjrE5AgBjsvqGzsSPNsrhg9xnJINY22B JTqZ7R3jNHwJvMGgFjl3QeawSgC3InukMau/z4kNEB+Yd8GVk9awFkEbF9PJy7T04jE2a2 yUW14maYFKV3Zq0LX4bXhSJ6v+M/U7E= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 20/32] kasan: move kasan_get_alloc/free_track definitions Date: Mon, 13 Jun 2022 22:14:11 +0200 Message-Id: <8c647863a2ea158fd2ddc0c79e5e937bb03d86f0.1655150842.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151521; a=rsa-sha256; cv=none; b=FrB4XNo9Dt9L2Ls8Jxe3KNU8Tuia7zDJUphIR4gRGmIHkFaft9rIpbfCXoM9nnhLcCuOes 224vN8v+nr70h7RqweJomUr9YqSF+qJTdnDGxh3sPk5GB0tDiHxmiSj2n1pyv4b1AroUuP SGUQzQ0w5s0pXAGVsculpKPG1Xue2xY= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=F5EnSyRz; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf26.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 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=1655151521; 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=T+1jiBrKGTOAkQ9I8eaP4PIHMCNWMSHqgu/PvHey6xE=; b=OOYVq9m5bTAnLetMSX5h6iRbTSCZaDvMGBI6Q/sEY3inozrd315o3/KWwVWYCStxgLvqAS 69DeZDy18fAsmgbSjuckyVws206WlT+eRcw+zSUmjN3nTYoYpR8dZDryg2K/bjpeqOeR0W dqBNkPwHDJPvy2OSDfLvBpJQtYjbcDs= X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: C9EB6140081 X-Stat-Signature: zbjsidoaeaop1ja5dd8tzqypwh85jxpj Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=F5EnSyRz; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf26.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-Rspam-User: X-HE-Tag: 1655151520-658181 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: From: Andrey Konovalov Move the definitions of kasan_get_alloc/free_track() to report_*.c, as they belong with other the reporting code. Signed-off-by: Andrey Konovalov --- mm/kasan/generic.c | 21 --------------------- mm/kasan/report_generic.c | 21 +++++++++++++++++++++ mm/kasan/report_tags.c | 12 ++++++++++++ mm/kasan/tags.c | 12 ------------ 4 files changed, 33 insertions(+), 33 deletions(-) diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c index aff39af3c532..d8b5590f9484 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -512,24 +512,3 @@ void kasan_save_free_info(struct kmem_cache *cache, void *object) /* The object was freed and has free track set. */ *(u8 *)kasan_mem_to_shadow(object) = KASAN_SLAB_FREETRACK; } - -struct kasan_track *kasan_get_alloc_track(struct kmem_cache *cache, - void *object) -{ - struct kasan_alloc_meta *alloc_meta; - - alloc_meta = kasan_get_alloc_meta(cache, object); - if (!alloc_meta) - return NULL; - - return &alloc_meta->alloc_track; -} - -struct kasan_track *kasan_get_free_track(struct kmem_cache *cache, - void *object, u8 tag) -{ - if (*(u8 *)kasan_mem_to_shadow(object) != KASAN_SLAB_FREETRACK) - return NULL; - /* Free meta must be present with KASAN_SLAB_FREETRACK. */ - return &kasan_get_free_meta(cache, object)->free_track; -} diff --git a/mm/kasan/report_generic.c b/mm/kasan/report_generic.c index 348dc207d462..74d21786ef09 100644 --- a/mm/kasan/report_generic.c +++ b/mm/kasan/report_generic.c @@ -127,6 +127,27 @@ const char *kasan_get_bug_type(struct kasan_report_info *info) return get_wild_bug_type(info); } +struct kasan_track *kasan_get_alloc_track(struct kmem_cache *cache, + void *object) +{ + struct kasan_alloc_meta *alloc_meta; + + alloc_meta = kasan_get_alloc_meta(cache, object); + if (!alloc_meta) + return NULL; + + return &alloc_meta->alloc_track; +} + +struct kasan_track *kasan_get_free_track(struct kmem_cache *cache, + void *object, u8 tag) +{ + if (*(u8 *)kasan_mem_to_shadow(object) != KASAN_SLAB_FREETRACK) + return NULL; + /* Free meta must be present with KASAN_SLAB_FREETRACK. */ + return &kasan_get_free_meta(cache, object)->free_track; +} + void kasan_metadata_fetch_row(char *buffer, void *row) { memcpy(buffer, kasan_mem_to_shadow(row), META_BYTES_PER_ROW); diff --git a/mm/kasan/report_tags.c b/mm/kasan/report_tags.c index 35cf3cae4aa4..79b6497d8a81 100644 --- a/mm/kasan/report_tags.c +++ b/mm/kasan/report_tags.c @@ -21,3 +21,15 @@ const char *kasan_get_bug_type(struct kasan_report_info *info) return "invalid-access"; } + +struct kasan_track *kasan_get_alloc_track(struct kmem_cache *cache, + void *object) +{ + return NULL; +} + +struct kasan_track *kasan_get_free_track(struct kmem_cache *cache, + void *object, u8 tag) +{ + return NULL; +} diff --git a/mm/kasan/tags.c b/mm/kasan/tags.c index fd11d10a4ffc..39a0481e5228 100644 --- a/mm/kasan/tags.c +++ b/mm/kasan/tags.c @@ -24,15 +24,3 @@ void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags) void kasan_save_free_info(struct kmem_cache *cache, void *object) { } - -struct kasan_track *kasan_get_alloc_track(struct kmem_cache *cache, - void *object) -{ - return NULL; -} - -struct kasan_track *kasan_get_free_track(struct kmem_cache *cache, - void *object, u8 tag) -{ - return NULL; -} From patchwork Mon Jun 13 20:14:12 2022 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: 12880096 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 4FE0FC433EF for ; Mon, 13 Jun 2022 20:18:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 665ED8D01D5; Mon, 13 Jun 2022 16:18:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 59E1D8D01CC; Mon, 13 Jun 2022 16:18:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 466078D01D5; Mon, 13 Jun 2022 16:18:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 2AB958D01CC for ; Mon, 13 Jun 2022 16:18:42 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay13.hostedemail.com (Postfix) with ESMTP id 0C0A560EEC for ; Mon, 13 Jun 2022 20:18:42 +0000 (UTC) X-FDA: 79574325684.03.0E55F2B Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by imf14.hostedemail.com (Postfix) with ESMTP id 7DE73100072 for ; Mon, 13 Jun 2022 20:18:41 +0000 (UTC) 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=1655151520; 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=7UlP4rgYEeC4zjzJGZ6kC7jJ637whgBTqafUFs9KdFM=; b=JefL7G+uMUqUoHfYWqpdgoz/IVfPj+68p9IguhAmpQXwywnI1zyQ5WjNpX1CBh3AHwLiXW vyxIhpMNSmc+fBPo6tTB92C9YhdzPz89lbk37knbBj6HEIFOl1Fj4YvvVK2Yf/WNk63+41 DukNNsx5HLFREaWIXGxDjKQqbidMoh8= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 21/32] kasan: simplify invalid-free reporting Date: Mon, 13 Jun 2022 22:14:12 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655151521; 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=7UlP4rgYEeC4zjzJGZ6kC7jJ637whgBTqafUFs9KdFM=; b=fP9pTvqGql+4Gqm49iEK3WMSCoxDoJZ9xgyR4ChMh1Ih98AfybBLpyt1+UNMMaICHG6Afd 5N3crYPOL/+sRbPoPfX7/udkCCO9Xkuhisda8L0LTlCXy4PZUR+65zI8H2QmWPlS//CKqK Iz2yqqhuink25n/Q50QrDvx+Dcs+Prc= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=JefL7G+u; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf14.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151521; a=rsa-sha256; cv=none; b=xnsg9R97G0qt/vecKTx5/LXpuQ/W8basn5/hgvqiFTwBu0uR2TuxQzPYh+D5l2A9Fn+deG 9w7mdmNwaqbRMRTfM56fQwOqY0RUKjHczGL4b3HDK4NE5nH3UTFAwz5RgutSmZ7HGrkf2s BnD5n7ZPt4AXVeLJFW8r2u46vXLjud0= X-Rspamd-Queue-Id: 7DE73100072 X-Rspam-User: X-Stat-Signature: ddma5mnnaxyqo5turj15qoapfhmzhh5b Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=JefL7G+u; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf14.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-Rspamd-Server: rspam04 X-HE-Tag: 1655151521-518358 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: From: Andrey Konovalov Right now, KASAN uses the kasan_report_type enum to describe report types. As this enum only has two options, replace it with a bool variable. Also, unify printing report header for invalid-free and other bug types in print_error_description(). Signed-off-by: Andrey Konovalov --- mm/kasan/kasan.h | 7 +------ mm/kasan/report.c | 16 +++++++--------- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index e8329935fbfb..f696d50b09fb 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -146,16 +146,11 @@ static inline bool kasan_requires_meta(void) #define META_MEM_BYTES_PER_ROW (META_BYTES_PER_ROW * KASAN_GRANULE_SIZE) #define META_ROWS_AROUND_ADDR 2 -enum kasan_report_type { - KASAN_REPORT_ACCESS, - KASAN_REPORT_INVALID_FREE, -}; - struct kasan_report_info { - enum kasan_report_type type; void *access_addr; void *first_bad_addr; size_t access_size; + bool is_free; bool is_write; unsigned long ip; }; diff --git a/mm/kasan/report.c b/mm/kasan/report.c index f951fd39db74..7269b6249488 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -175,14 +175,12 @@ static void end_report(unsigned long *flags, void *addr) static void print_error_description(struct kasan_report_info *info) { - if (info->type == KASAN_REPORT_INVALID_FREE) { - pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", - (void *)info->ip); - return; - } + const char *bug_type = info->is_free ? + "double-free or invalid-free" : kasan_get_bug_type(info); - pr_err("BUG: KASAN: %s in %pS\n", - kasan_get_bug_type(info), (void *)info->ip); + pr_err("BUG: KASAN: %s in %pS\n", bug_type, (void *)info->ip); + if (info->is_free) + return; if (info->access_size) pr_err("%s of size %zu at addr %px by task %s/%d\n", info->is_write ? "Write" : "Read", info->access_size, @@ -435,11 +433,11 @@ void kasan_report_invalid_free(void *ptr, unsigned long ip) start_report(&flags, true); - info.type = KASAN_REPORT_INVALID_FREE; info.access_addr = ptr; info.first_bad_addr = kasan_reset_tag(ptr); info.access_size = 0; info.is_write = false; + info.is_free = true; info.ip = ip; print_report(&info); @@ -468,11 +466,11 @@ bool kasan_report(unsigned long addr, size_t size, bool is_write, start_report(&irq_flags, true); - info.type = KASAN_REPORT_ACCESS; info.access_addr = ptr; info.first_bad_addr = kasan_find_first_bad_addr(ptr, size); info.access_size = size; info.is_write = is_write; + info.is_free = false; info.ip = ip; print_report(&info); From patchwork Mon Jun 13 20:14:13 2022 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: 12880097 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 7B8B8CCA47D for ; Mon, 13 Jun 2022 20:18:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1827C8D01D6; Mon, 13 Jun 2022 16:18:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 10B1E8D01CC; Mon, 13 Jun 2022 16:18:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EC5E88D01D6; Mon, 13 Jun 2022 16:18:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id D80068D01CC for ; Mon, 13 Jun 2022 16:18:42 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id B8DEF20D70 for ; Mon, 13 Jun 2022 20:18:42 +0000 (UTC) X-FDA: 79574325684.06.4E37741 Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by imf18.hostedemail.com (Postfix) with ESMTP id 3FC7E1C0079 for ; Mon, 13 Jun 2022 20:18:42 +0000 (UTC) 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=1655151521; 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=uV62cC5DfHg/D8q7E9RvYjkrSokJV1eiZxRUfsD9VoU=; b=kvRi5OcEoiHBckUmql9zWUb9FKC6wNd2nIhN5sD38XfgUnd8dK14JY90UXlC8uENnECQaQ 3OOnI245K8ZI+Nxmz+gn+P6ox1GkZo2TIpM9SiR9iPnRCP6c/hSq7HcQsVTtoLryjSydIl Zft1R6rRme1veq8TWSAMk6UkoRJMi5M= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 22/32] kasan: cosmetic changes in report.c Date: Mon, 13 Jun 2022 22:14:13 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151522; a=rsa-sha256; cv=none; b=TIfnu0JejN05AXB41mu7YEIbNR7sSivJRip1lFK2e9g1+hCVAurO4POvpzWzFrohPWegIX 8WhV3XBvJ+q4fJyrpumA/jOd5Ppo3mA8wZxaoKFVLK++jUFHlVzX1yIc+Ol/w9sDcytPkM Xf5jCCect06vujSlv6z6WecqeoLOmcE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655151522; 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=uV62cC5DfHg/D8q7E9RvYjkrSokJV1eiZxRUfsD9VoU=; b=0g3Or7FEKaxfP9rPlaNa/09GTb8NJSoIAfyz7BMIYtZAsHDAkYiF7u4zf6UOtQbkBNRbLZ KJGASSfiBd3jNlG0/FqU1f+23G5BQV6fW3WuROJHydyZ4+B8F0og/ktkAEMPVyy6eL5/ZH kjg0H4cjnz5D8JW1D8vEKV/iwfe/vFs= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=kvRi5OcE; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf18.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=kvRi5OcE; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf18.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 3FC7E1C0079 X-Stat-Signature: kyjk4jwyt45muk6egz4369yyiwtedj1w X-HE-Tag: 1655151522-369740 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: From: Andrey Konovalov Do a few non-functional style fixes for the code in report.c. Signed-off-by: Andrey Konovalov --- mm/kasan/report.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/mm/kasan/report.c b/mm/kasan/report.c index 7269b6249488..879f949dc395 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -194,25 +194,22 @@ static void print_error_description(struct kasan_report_info *info) static void print_track(struct kasan_track *track, const char *prefix) { pr_err("%s by task %u:\n", prefix, track->pid); - if (track->stack) { + if (track->stack) stack_depot_print(track->stack); - } else { + else pr_err("(stack is not available)\n"); - } } struct page *kasan_addr_to_page(const void *addr) { - if ((addr >= (void *)PAGE_OFFSET) && - (addr < high_memory)) + if ((addr >= (void *)PAGE_OFFSET) && (addr < high_memory)) return virt_to_head_page(addr); return NULL; } struct slab *kasan_addr_to_slab(const void *addr) { - if ((addr >= (void *)PAGE_OFFSET) && - (addr < high_memory)) + if ((addr >= (void *)PAGE_OFFSET) && (addr < high_memory)) return virt_to_slab(addr); return NULL; } From patchwork Mon Jun 13 20:14:14 2022 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: 12880098 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 B1B99C433EF for ; Mon, 13 Jun 2022 20:18:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B94518D01D7; Mon, 13 Jun 2022 16:18:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B1D1D8D01CC; Mon, 13 Jun 2022 16:18:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9E32E8D01D7; Mon, 13 Jun 2022 16:18:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 8E6828D01CC for ; Mon, 13 Jun 2022 16:18:43 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 62E9720CC9 for ; Mon, 13 Jun 2022 20:18:43 +0000 (UTC) X-FDA: 79574325726.15.9881D22 Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by imf08.hostedemail.com (Postfix) with ESMTP id DCCD2160094 for ; Mon, 13 Jun 2022 20:18:42 +0000 (UTC) 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=1655151521; 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=Iixdi0aUzLucxsbCg6IG+LbS3JzcqWGK/WM+ZRzVqS4=; b=ojnNMVl0weA+1rfdDibEWQiEzf3u9AY6W77/wSQjT9KKfbHNrxLmCsJ0sZKkW8eSCG0dBh +5YrObbqYTyKuVuT1F9XZeFHAhh5e4ObssVEFR3QS+xZkhK0CdzjtTkbI85bkE2UWrMY6E WI2qCIZzoT7d6a6rI1j4nr/qLjTsYE4= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 23/32] kasan: use kasan_addr_to_slab in print_address_description Date: Mon, 13 Jun 2022 22:14:14 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655151523; 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=Iixdi0aUzLucxsbCg6IG+LbS3JzcqWGK/WM+ZRzVqS4=; b=X9zqUpxmgSuKYgk+iJZMwznHnM4H/fUmtLpmYcrIYQ7s9dRhRC6dThFGS2oGXkeOstTBYt 7tFFP14Xipo+YZPdklbpX26DS5ZRNkwsL7Z1fgBJFcPC6PJpG7gQhwyd+QVMo6fB9la6E6 EYkurHlEksbN5dAHnXIEFPaZBN0iCIs= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=ojnNMVl0; spf=pass (imf08.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151523; a=rsa-sha256; cv=none; b=bmVC8Y3hFzTpTamEhYbkn3ImwdQN0NJUgvLfpS+RShLzetc8Z818JaBvYCw0CgmBkvdD3L 7Ot1mvsz12eg7BXINixVEdZdjnt9F2svnhEHSqb+d5fOnjcSIZYQBxZhnfdFiJmTyvx62e Ehx0zs8Yl4rro2lKQo0/EmdvZibCe3k= X-Stat-Signature: mwt4j1zdz6q86dijhes7sxfer7cbcbxx X-Rspamd-Queue-Id: DCCD2160094 X-Rspamd-Server: rspam11 X-Rspam-User: Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=ojnNMVl0; spf=pass (imf08.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-HE-Tag: 1655151522-376330 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: From: Andrey Konovalov Use the kasan_addr_to_slab() helper in print_address_description() instead of separately invoking PageSlab() and page_slab(). Signed-off-by: Andrey Konovalov --- mm/kasan/report.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mm/kasan/report.c b/mm/kasan/report.c index 879f949dc395..1dd6fc8a678f 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -291,12 +291,12 @@ static inline bool init_task_stack_addr(const void *addr) static void print_address_description(void *addr, u8 tag) { struct page *page = kasan_addr_to_page(addr); + struct slab *slab = kasan_addr_to_slab(addr); dump_stack_lvl(KERN_ERR); pr_err("\n"); - if (page && PageSlab(page)) { - struct slab *slab = page_slab(page); + if (slab) { struct kmem_cache *cache = slab->slab_cache; void *object = nearest_obj(cache, slab, addr); From patchwork Mon Jun 13 20:14:15 2022 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: 12880100 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 576EDC433EF for ; Mon, 13 Jun 2022 20:19:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 708578D01DA; Mon, 13 Jun 2022 16:19:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 503078D01DB; Mon, 13 Jun 2022 16:19:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 266A18D01D8; Mon, 13 Jun 2022 16:19:52 -0400 (EDT) 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 01ADB8D01CC for ; Mon, 13 Jun 2022 16:19:51 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id CDE52350FA for ; Mon, 13 Jun 2022 20:19:51 +0000 (UTC) X-FDA: 79574328582.09.F1D0E3C Received: from out2.migadu.com (out2.migadu.com [188.165.223.204]) by imf20.hostedemail.com (Postfix) with ESMTP id 555711C007A for ; Mon, 13 Jun 2022 20:19:51 +0000 (UTC) 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=1655151583; 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=i8Ujjn8ODA+o6f5+82XskCVtNKiV5I0P+bzfQLYNt08=; b=tNdYTuepZITenpSdu58grTXSjaXt9OL81buen5Wm+++wlMotQfJq9n+UcKkTTT5gZ+gc4f uF7AU1aiTFH85x0uVzzaLfcIcYPfeoCLX+3KfygTgZO4Vq+i5p7QD74yy+0ekX5HwMhW75 QJqnizRPQmY2NxNZs3S4Cds/DJ8v8dA= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 24/32] kasan: move kasan_addr_to_slab to common.c Date: Mon, 13 Jun 2022 22:14:15 +0200 Message-Id: <5ea6f55fb645405bb52cb15b8d30544ba3f189b0.1655150842.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151591; a=rsa-sha256; cv=none; b=rND/M3GMYi7qV5jDiTPOh4J/5XD9JXcQD84tgr3GFgfQ3B1/I5tum90Pbkc0WULMH9Cimw Ryq3rjlKzsaRiyKQuYMD3gxWGIbz2BFMK7Rhy+tv7s81ELR236qV59IsfCIItLj6Iztniw vLzOYEkAYCqChxH6ixgW7Kxq12bySq4= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=tNdYTuep; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf20.hostedemail.com: domain of andrey.konovalov@linux.dev designates 188.165.223.204 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=1655151591; 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=i8Ujjn8ODA+o6f5+82XskCVtNKiV5I0P+bzfQLYNt08=; b=E7ZxvPNWGVdLYFOtfE6dku4LZanDLXjtib86askkGGgyZcYqWP5/IfW98cA2rMqUKi0dpf hdb7pcKi2dR6Dux1YC73168zVZ2h61rBxvL6S4nSmM/AueBzIiUvJ0t0fZ8wZqUuUGSr45 8LmnNomLOSd1wLhCqGrNdDgheYFzP8E= Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=tNdYTuep; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf20.hostedemail.com: domain of andrey.konovalov@linux.dev designates 188.165.223.204 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-Rspam-User: X-Stat-Signature: wjojmt46mi1snc7z55x87x551ezpzzkz X-Rspamd-Queue-Id: 555711C007A X-Rspamd-Server: rspam08 X-HE-Tag: 1655151591-414527 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: From: Andrey Konovalov Move the definition of kasan_addr_to_slab() to the common KASAN code, as this function is not only used by the reporting code. Signed-off-by: Andrey Konovalov Reported-by: kernel test robot --- mm/kasan/common.c | 7 +++++++ mm/kasan/report.c | 7 ------- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 519fd0b3040b..5d5b4cfae503 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -30,6 +30,13 @@ #include "kasan.h" #include "../slab.h" +struct slab *kasan_addr_to_slab(const void *addr) +{ + if ((addr >= (void *)PAGE_OFFSET) && (addr < high_memory)) + return virt_to_slab(addr); + return NULL; +} + depot_stack_handle_t kasan_save_stack(gfp_t flags, bool can_alloc) { unsigned long entries[KASAN_STACK_DEPTH]; diff --git a/mm/kasan/report.c b/mm/kasan/report.c index 1dd6fc8a678f..ed8234516bab 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -207,13 +207,6 @@ struct page *kasan_addr_to_page(const void *addr) return NULL; } -struct slab *kasan_addr_to_slab(const void *addr) -{ - if ((addr >= (void *)PAGE_OFFSET) && (addr < high_memory)) - return virt_to_slab(addr); - return NULL; -} - static void describe_object_addr(struct kmem_cache *cache, void *object, const void *addr) { From patchwork Mon Jun 13 20:14:16 2022 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: 12880099 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 BC273C43334 for ; Mon, 13 Jun 2022 20:19:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3CC408D01D9; Mon, 13 Jun 2022 16:19:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 353318D01CC; Mon, 13 Jun 2022 16:19:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 17FEA8D01D9; Mon, 13 Jun 2022 16:19:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 04E108D01D8 for ; Mon, 13 Jun 2022 16:19:52 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id D258033EE2 for ; Mon, 13 Jun 2022 20:19:51 +0000 (UTC) X-FDA: 79574328582.04.EA3D8BE Received: from out2.migadu.com (out2.migadu.com [188.165.223.204]) by imf22.hostedemail.com (Postfix) with ESMTP id 4EB1BC00AA for ; Mon, 13 Jun 2022 20:19:51 +0000 (UTC) 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=1655151583; 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=NlTYbbmb8K8Hz7QxoFNu4DEEzvlhQuGMJFGkSKrxTzk=; b=U/iYTMjjDHVUZCoLW/1BknQkILDEl85phr4wKanZLDwM4Jn+hUSCLwE24HlVfV0VaWnFGZ 5AwsB6PXGbJcILtgvt/a2jxzEYgsMtJLT/bXeJD/yxxPbvpTCCioX7PAW1dxA4jrSRwzdu UhNm0cH9ryd0tC9r9ti+IK4+K3cI+g0= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 25/32] kasan: make kasan_addr_to_page static Date: Mon, 13 Jun 2022 22:14:16 +0200 Message-Id: <810b29bfb50dad8cdc5a5a7075e0da1104de1665.1655150842.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151591; a=rsa-sha256; cv=none; b=yHSEp1W1hh+7OHsqn6qD7xAaQ68Wsvyk74lJv+tn3aU3pxz9+RYTl+l2adlD7VME6bRdqW lXYoHGaebveF8hO+4lspzIq29F3t72U9gpCk/CzYZ033J16teamlaOWJGTuLEBfeFFhbLH 5xHSC6fuF5NkKp13+D8I0EjbHJOqgt4= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="U/iYTMjj"; spf=pass (imf22.hostedemail.com: domain of andrey.konovalov@linux.dev designates 188.165.223.204 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=1655151591; 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=NlTYbbmb8K8Hz7QxoFNu4DEEzvlhQuGMJFGkSKrxTzk=; b=TeMwrexsjixO0+8VvDK10OwP6qAXES9GUg4rAdH5lVb7bNyMGLoTJvc8ZSx/sT0SUOvJcX lXgguAqK1VoSCaaKx34lQl1vNbIrlQTI2Ws8as+F4hP2Isd73hV5lB94HOoT8OPAM6NqzP lVHJxSrar+6THDN1CWvStFSnJsqSF5g= X-Stat-Signature: bco8d6dtj58i6c4zjopsg7jn9zyt9ii1 X-Rspamd-Queue-Id: 4EB1BC00AA Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="U/iYTMjj"; spf=pass (imf22.hostedemail.com: domain of andrey.konovalov@linux.dev designates 188.165.223.204 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Rspamd-Server: rspam07 X-Rspam-User: X-HE-Tag: 1655151591-594343 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: From: Andrey Konovalov As kasan_addr_to_page() is only used in report.c, rename it to addr_to_page() and make it static. Signed-off-by: Andrey Konovalov --- mm/kasan/kasan.h | 1 - mm/kasan/report.c | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index f696d50b09fb..e3f100833154 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -285,7 +285,6 @@ bool kasan_report(unsigned long addr, size_t size, bool is_write, unsigned long ip); void kasan_report_invalid_free(void *object, unsigned long ip); -struct page *kasan_addr_to_page(const void *addr); struct slab *kasan_addr_to_slab(const void *addr); #ifdef CONFIG_KASAN_GENERIC diff --git a/mm/kasan/report.c b/mm/kasan/report.c index ed8234516bab..f3ec6f86b199 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -200,7 +200,7 @@ static void print_track(struct kasan_track *track, const char *prefix) pr_err("(stack is not available)\n"); } -struct page *kasan_addr_to_page(const void *addr) +static inline struct page *addr_to_page(const void *addr) { if ((addr >= (void *)PAGE_OFFSET) && (addr < high_memory)) return virt_to_head_page(addr); @@ -283,7 +283,7 @@ static inline bool init_task_stack_addr(const void *addr) static void print_address_description(void *addr, u8 tag) { - struct page *page = kasan_addr_to_page(addr); + struct page *page = addr_to_page(addr); struct slab *slab = kasan_addr_to_slab(addr); dump_stack_lvl(KERN_ERR); From patchwork Mon Jun 13 20:14:17 2022 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: 12880101 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 A4F15C43334 for ; Mon, 13 Jun 2022 20:19:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AA7A98D01CC; Mon, 13 Jun 2022 16:19:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A26728D01DD; Mon, 13 Jun 2022 16:19:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 687DF8D01D8; Mon, 13 Jun 2022 16:19:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 3E5B38D01DA for ; Mon, 13 Jun 2022 16:19:52 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay13.hostedemail.com (Postfix) with ESMTP id 1EC7160F1B for ; Mon, 13 Jun 2022 20:19:52 +0000 (UTC) X-FDA: 79574328624.06.7DDD7AD Received: from out2.migadu.com (out2.migadu.com [188.165.223.204]) by imf31.hostedemail.com (Postfix) with ESMTP id 8FE06200AC for ; Mon, 13 Jun 2022 20:19:51 +0000 (UTC) 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=1655151584; 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=xD+jOMbc8v/VbHqobo7HTP4Hy7ebCjo8HGnwIkjNmQg=; b=uV4k7v6j0gg2WNGE2AGUi+jftnP6i13EVs8rWhI+Mznu6bMnCcoEx7t6dQvsOLijyruwRb Bnb/ljj+Y/69/ZVheqbQ8nWo1Q2aQYKgjsxn4fv64f2dR7RxPtUroYLFBAZjgai+byrc3O hJnu05PzYAADy2CUt5i3GARZUN+wHes= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 26/32] kasan: simplify print_report Date: Mon, 13 Jun 2022 22:14:17 +0200 Message-Id: <6920a74ae141ec8f45f19c8ebf3622910d10a5ed.1655150842.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655151591; 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=xD+jOMbc8v/VbHqobo7HTP4Hy7ebCjo8HGnwIkjNmQg=; b=pBjBJutw7BL2aKiFT/gSV1HPC5pl+FN+E6+58gc39w2Y4OMCRZ3XLiEEiIz6D2HRF5n+QE taEhgkG0d//17BCbHFhLH6NwcibsEwibSZsuv4zso35gr2EylVws9FErF18NIJd7LYxUL+ hb/M4Ikp4feUPABL/xojAtxmExNtnxk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151591; a=rsa-sha256; cv=none; b=7hkWyf/G8pzv4TVw67xXTheOVskmIrRSFMQPOrxW3LSDa9+w2S44cQddqt8iHj2xx/tp6i +NXGp1JgHVbqfo+pVrHBKb7v+904LtJcKJwnzvv6ltaIehxP9eKfQNwkHsX0l3hXE2YCry rdIWua7/9e9yC76pmuCv/TgqcXxxwBU= ARC-Authentication-Results: i=1; imf31.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=uV4k7v6j; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf31.hostedemail.com: domain of andrey.konovalov@linux.dev designates 188.165.223.204 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=uV4k7v6j; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf31.hostedemail.com: domain of andrey.konovalov@linux.dev designates 188.165.223.204 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-Rspamd-Server: rspam12 X-Rspam-User: X-Stat-Signature: tzr6ownyj643xteyrhkenidd9ukpjrze X-Rspamd-Queue-Id: 8FE06200AC X-HE-Tag: 1655151591-728138 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000002, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Andrey Konovalov To simplify reading the implementation of print_report(), remove the tagged_addr variable and rename untagged_addr to addr. Signed-off-by: Andrey Konovalov --- mm/kasan/report.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/mm/kasan/report.c b/mm/kasan/report.c index f3ec6f86b199..cc35c8c1a367 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -391,17 +391,16 @@ static void print_memory_metadata(const void *addr) static void print_report(struct kasan_report_info *info) { - void *tagged_addr = info->access_addr; - void *untagged_addr = kasan_reset_tag(tagged_addr); - u8 tag = get_tag(tagged_addr); + void *addr = kasan_reset_tag(info->access_addr); + u8 tag = get_tag(info->access_addr); print_error_description(info); - if (addr_has_metadata(untagged_addr)) + if (addr_has_metadata(addr)) kasan_print_tags(tag, info->first_bad_addr); pr_err("\n"); - if (addr_has_metadata(untagged_addr)) { - print_address_description(untagged_addr, tag); + if (addr_has_metadata(addr)) { + print_address_description(addr, tag); print_memory_metadata(info->first_bad_addr); } else { dump_stack_lvl(KERN_ERR); From patchwork Mon Jun 13 20:14:18 2022 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: 12880103 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 2DCECC433EF for ; Mon, 13 Jun 2022 20:19:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 63CF48D01DB; Mon, 13 Jun 2022 16:19:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 59E608D01D8; Mon, 13 Jun 2022 16:19:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 292D58D01DD; Mon, 13 Jun 2022 16:19:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id E29B38D01DB for ; Mon, 13 Jun 2022 16:19:52 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id C00B761103 for ; Mon, 13 Jun 2022 20:19:52 +0000 (UTC) X-FDA: 79574328624.25.34A0F6D Received: from out2.migadu.com (out2.migadu.com [188.165.223.204]) by imf18.hostedemail.com (Postfix) with ESMTP id 466101C0075 for ; Mon, 13 Jun 2022 20:19:52 +0000 (UTC) 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=1655151585; 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=bgGIoP7jXmxJI/MRAshv+0ZSBU3qLuiELKkrVE3erv8=; b=Tx3ufgGib+OSKMRv+3CRINt/7OLqN5cTferSwJ6ogYbMF4V+m/d6x2YflQZxIH1DxTkbs+ djLDhQRR9ITw8i3XavtHbQZ4Xi93tpCSo+pVwH6pNEdeNdh3NKk7rEyn4uB+qK7BZVTgjf ongP+RFoY9TadvbK5JxiwaupvUxSI3c= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 27/32] kasan: introduce complete_report_info Date: Mon, 13 Jun 2022 22:14:18 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151592; a=rsa-sha256; cv=none; b=GedXBxuwoF2+aoPCL7J9NxkkBk1jKG+8q9073/4IREl7ddF5HRsuDO5tZuRVtpcDvAoyKi NJ36twhHLLv/1JzhakfcrLLA43EBaBF2OlmlxScAxRgW/GLhgPmjf2uK/Lios0trDl6Iry vduWNzYLYneztjxpJaReNwAgB4oAbeY= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Tx3ufgGi; spf=pass (imf18.hostedemail.com: domain of andrey.konovalov@linux.dev designates 188.165.223.204 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=1655151592; 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=bgGIoP7jXmxJI/MRAshv+0ZSBU3qLuiELKkrVE3erv8=; b=btJ5JVPYZPm+rPK3SWGgCGsI9KKx7gyUmVRTTz/Vbuj6LltFGtAUcUJMJkfecKBPl2hkiT CbE2CCmHEW+umeNeLtzzLLD+T6zBLe6jsFK7IC5DqceGXbJPOpnnjHgK1KtE0XVW1KUE2P I3PEQc3iVJa0JXNsQ0HT9Nqc8BKuqNA= X-Stat-Signature: 79w5ay3qd7xq4oja6zeskpupeci4oiqa X-Rspamd-Queue-Id: 466101C0075 Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Tx3ufgGi; spf=pass (imf18.hostedemail.com: domain of andrey.konovalov@linux.dev designates 188.165.223.204 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Rspam-User: X-Rspamd-Server: rspam02 X-HE-Tag: 1655151592-77554 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: From: Andrey Konovalov Introduce a complete_report_info() function that fills in the first_bad_addr field of kasan_report_info instead of doing it in kasan_report_*(). This function will be extended in the next patch. Signed-off-by: Andrey Konovalov --- mm/kasan/kasan.h | 5 ++++- mm/kasan/report.c | 17 +++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index e3f100833154..0261d1530055 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -147,12 +147,15 @@ static inline bool kasan_requires_meta(void) #define META_ROWS_AROUND_ADDR 2 struct kasan_report_info { + /* Filled in by kasan_report_*(). */ void *access_addr; - void *first_bad_addr; size_t access_size; bool is_free; bool is_write; unsigned long ip; + + /* Filled in by the common reporting code. */ + void *first_bad_addr; }; /* Do not change the struct layout: compiler ABI. */ diff --git a/mm/kasan/report.c b/mm/kasan/report.c index cc35c8c1a367..214ba7cb654c 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -407,6 +407,17 @@ static void print_report(struct kasan_report_info *info) } } +static void complete_report_info(struct kasan_report_info *info) +{ + void *addr = kasan_reset_tag(info->access_addr); + + if (info->is_free) + info->first_bad_addr = addr; + else + info->first_bad_addr = kasan_find_first_bad_addr( + info->access_addr, info->access_size); +} + void kasan_report_invalid_free(void *ptr, unsigned long ip) { unsigned long flags; @@ -423,12 +434,13 @@ void kasan_report_invalid_free(void *ptr, unsigned long ip) start_report(&flags, true); info.access_addr = ptr; - info.first_bad_addr = kasan_reset_tag(ptr); info.access_size = 0; info.is_write = false; info.is_free = true; info.ip = ip; + complete_report_info(&info); + print_report(&info); end_report(&flags, ptr); @@ -456,12 +468,13 @@ bool kasan_report(unsigned long addr, size_t size, bool is_write, start_report(&irq_flags, true); info.access_addr = ptr; - info.first_bad_addr = kasan_find_first_bad_addr(ptr, size); info.access_size = size; info.is_write = is_write; info.is_free = false; info.ip = ip; + complete_report_info(&info); + print_report(&info); end_report(&irq_flags, ptr); From patchwork Mon Jun 13 20:14:19 2022 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: 12880102 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 E87A9CCA47B for ; Mon, 13 Jun 2022 20:19:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0EB338D01DC; Mon, 13 Jun 2022 16:19:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B91818D01D8; Mon, 13 Jun 2022 16:19:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 996CB8D01DC; Mon, 13 Jun 2022 16:19:52 -0400 (EDT) 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 704618D01CC for ; Mon, 13 Jun 2022 16:19:52 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 464D3351B6 for ; Mon, 13 Jun 2022 20:19:52 +0000 (UTC) X-FDA: 79574328624.19.ACACF35 Received: from out2.migadu.com (out2.migadu.com [188.165.223.204]) by imf25.hostedemail.com (Postfix) with ESMTP id 7BD43A009D for ; Mon, 13 Jun 2022 20:19:51 +0000 (UTC) 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=1655151585; 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=4luxyHsp81FOFiUT/6yoqfYUWRQbE1ZGpW4pjt9Mu8s=; b=wrqv7ohOF96qGiSQuPggQGcjaCvog98ApGuHxDFierBEUu/M31pb0J67CQIkajDOQYJQER YuDnSp2V6YbPoBkcPXwEnEdjyMR+pJGczOFfBWwr4cvGnkAYWCX7vQpjjcSJ9wtyoKpbVh ZPeMiUpOhK1ShOgWWyVcqDCuLD/pVRs= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 28/32] kasan: fill in cache and object in complete_report_info Date: Mon, 13 Jun 2022 22:14:19 +0200 Message-Id: <1e3e75cbcf4f258701b325dbad8b2a43c2633b7b.1655150842.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151591; a=rsa-sha256; cv=none; b=5BFd3/xMrO0ncaicaOheGRilMXnB1z4fnA08sbT/wwv8v6l5XlcIE0/A48e/P/WzltzmCM +Qa6ZOWUyURMgCJljB0u0KY5zdvVIOUXehyjlwWfZ/lRddkfBnxnkXkcJJ2QFTtKx1MGMb XqQHVrZcEz8leRycSO2lxlw6uKYboQI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655151591; 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=4luxyHsp81FOFiUT/6yoqfYUWRQbE1ZGpW4pjt9Mu8s=; b=Ybpgv11eU/VZbG5qApKiSYT4BuRBkW5RJ4LRmAvIrhfUMMsv1eeJhCnp86XNQC9QtIHnoX rL90+yPXEUrWRS/xxhZ7X4R1Ei1qDGT9HkbPdQEAmDfGoVFUzotuLlicLZmg1Dn+YAkTou cu2BP93/Ysw6vrNk9swjqlxgzZvtem4= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=wrqv7ohO; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf25.hostedemail.com: domain of andrey.konovalov@linux.dev designates 188.165.223.204 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=wrqv7ohO; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf25.hostedemail.com: domain of andrey.konovalov@linux.dev designates 188.165.223.204 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 7BD43A009D X-Stat-Signature: ygbj1hapibqbcoxu9coeuw6sb7qpbgga X-HE-Tag: 1655151591-562449 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: From: Andrey Konovalov Add cache and object fields to kasan_report_info and fill them in in complete_report_info() instead of fetching them in the middle of the report printing code. This allows the reporting code to get access to the object information before starting printing the report. One of the following patches uses this information to determine the bug type with the tag-based modes. Signed-off-by: Andrey Konovalov --- mm/kasan/kasan.h | 2 ++ mm/kasan/report.c | 21 +++++++++++++-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index 0261d1530055..b9bd9f1656bf 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -156,6 +156,8 @@ struct kasan_report_info { /* Filled in by the common reporting code. */ void *first_bad_addr; + struct kmem_cache *cache; + void *object; }; /* Do not change the struct layout: compiler ABI. */ diff --git a/mm/kasan/report.c b/mm/kasan/report.c index 214ba7cb654c..a6b36eb4c33b 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -281,19 +281,16 @@ static inline bool init_task_stack_addr(const void *addr) sizeof(init_thread_union.stack)); } -static void print_address_description(void *addr, u8 tag) +static void print_address_description(void *addr, u8 tag, + struct kasan_report_info *info) { struct page *page = addr_to_page(addr); - struct slab *slab = kasan_addr_to_slab(addr); dump_stack_lvl(KERN_ERR); pr_err("\n"); - if (slab) { - struct kmem_cache *cache = slab->slab_cache; - void *object = nearest_obj(cache, slab, addr); - - describe_object(cache, object, addr, tag); + if (info->cache && info->object) { + describe_object(info->cache, info->object, addr, tag); pr_err("\n"); } @@ -400,7 +397,7 @@ static void print_report(struct kasan_report_info *info) pr_err("\n"); if (addr_has_metadata(addr)) { - print_address_description(addr, tag); + print_address_description(addr, tag, info); print_memory_metadata(info->first_bad_addr); } else { dump_stack_lvl(KERN_ERR); @@ -410,12 +407,20 @@ static void print_report(struct kasan_report_info *info) static void complete_report_info(struct kasan_report_info *info) { void *addr = kasan_reset_tag(info->access_addr); + struct slab *slab; if (info->is_free) info->first_bad_addr = addr; else info->first_bad_addr = kasan_find_first_bad_addr( info->access_addr, info->access_size); + + slab = kasan_addr_to_slab(addr); + if (slab) { + info->cache = slab->slab_cache; + info->object = nearest_obj(info->cache, slab, addr); + } else + info->cache = info->object = NULL; } void kasan_report_invalid_free(void *ptr, unsigned long ip) From patchwork Mon Jun 13 20:14:20 2022 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: 12880104 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 82169CCA47F for ; Mon, 13 Jun 2022 20:19:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E59198D01D8; Mon, 13 Jun 2022 16:19:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DB5358D01DD; Mon, 13 Jun 2022 16:19:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C07ED8D01D8; Mon, 13 Jun 2022 16:19:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id A4E608D01DD for ; Mon, 13 Jun 2022 16:19:53 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 7D352CEC for ; Mon, 13 Jun 2022 20:19:53 +0000 (UTC) X-FDA: 79574328666.12.C777FBC Received: from out2.migadu.com (out2.migadu.com [188.165.223.204]) by imf03.hostedemail.com (Postfix) with ESMTP id 9DF9420095 for ; Mon, 13 Jun 2022 20:19:52 +0000 (UTC) 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=1655151586; 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=itBNzX7+Qi1Y3bHVuJEJdtRTIklJZqEOcvL8dc0SsNc=; b=CTO/MXcpYKOePq407VNan80LeLXILV+ErfumGcOaDZOfgkAedUJm/G11UYFbOkw+52G2RK /Gbp33dkm8G6vYCvHqJFwPeXkH2oP2KafObfzMr0HupFt7ugZmEtX34KdpblFzmxqJCqHT 4doyDmIylaAHsOMDsMVFB4wdgdwnSoI= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 29/32] kasan: rework function arguments in report.c Date: Mon, 13 Jun 2022 22:14:20 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655151592; 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=itBNzX7+Qi1Y3bHVuJEJdtRTIklJZqEOcvL8dc0SsNc=; b=hhIELoYOVcaCfFf3Bk/XTdjkwXjH/aDC4XQTEblYmtv9ySPIVUUMUQMHdIIscIopY+tWLV T7Bys8oiKfP6janh8Bse/ECdsPk6ffaU/sJhmCcUdcvdZffsBNH/NGfnNAxjMNoDEJ230G VzhAMFDGej0ptxWgzB9NwywTKZTVSXA= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="CTO/MXcp"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf03.hostedemail.com: domain of andrey.konovalov@linux.dev designates 188.165.223.204 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151592; a=rsa-sha256; cv=none; b=jEgjcUpgpeNUR8s40S20XOajxXnb3RA/+2HGTvh7ZFoZQ0rbs7S39oViMh03lm9WfYHbv8 jWRq/T5dt8eRwF3Klo0vlbIG6KJu5kXA82V2Y9T9C5PoV+kIiOs2fgYwiRCix+3Tc1eXz7 g9iPjA0CTueaUw4PswfT3evqjyrFaUY= X-Stat-Signature: 8ktt4p4kzwxfyksime4x8fquyoifbyh5 X-Rspamd-Queue-Id: 9DF9420095 X-Rspam-User: X-Rspamd-Server: rspam05 Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="CTO/MXcp"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf03.hostedemail.com: domain of andrey.konovalov@linux.dev designates 188.165.223.204 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-HE-Tag: 1655151592-310127 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: From: Andrey Konovalov Pass a pointer to kasan_report_info to describe_object() and describe_object_stacks(), instead of passing the structure's fields. The untagged pointer and the tag are still passed as separate arguments to some of the functions to avoid duplicating the untagging logic. This is preparatory change for the next patch. Signed-off-by: Andrey Konovalov --- mm/kasan/report.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/mm/kasan/report.c b/mm/kasan/report.c index a6b36eb4c33b..a2789d4a05dd 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -207,8 +207,8 @@ static inline struct page *addr_to_page(const void *addr) return NULL; } -static void describe_object_addr(struct kmem_cache *cache, void *object, - const void *addr) +static void describe_object_addr(const void *addr, struct kmem_cache *cache, + void *object) { unsigned long access_addr = (unsigned long)addr; unsigned long object_addr = (unsigned long)object; @@ -236,33 +236,32 @@ static void describe_object_addr(struct kmem_cache *cache, void *object, (void *)(object_addr + cache->object_size)); } -static void describe_object_stacks(struct kmem_cache *cache, void *object, - const void *addr, u8 tag) +static void describe_object_stacks(u8 tag, struct kasan_report_info *info) { struct kasan_track *alloc_track; struct kasan_track *free_track; - alloc_track = kasan_get_alloc_track(cache, object); + alloc_track = kasan_get_alloc_track(info->cache, info->object); if (alloc_track) { print_track(alloc_track, "Allocated"); pr_err("\n"); } - free_track = kasan_get_free_track(cache, object, tag); + free_track = kasan_get_free_track(info->cache, info->object, tag); if (free_track) { print_track(free_track, "Freed"); pr_err("\n"); } - kasan_print_aux_stacks(cache, object); + kasan_print_aux_stacks(info->cache, info->object); } -static void describe_object(struct kmem_cache *cache, void *object, - const void *addr, u8 tag) +static void describe_object(const void *addr, u8 tag, + struct kasan_report_info *info) { if (kasan_stack_collection_enabled()) - describe_object_stacks(cache, object, addr, tag); - describe_object_addr(cache, object, addr); + describe_object_stacks(tag, info); + describe_object_addr(addr, info->cache, info->object); } static inline bool kernel_or_module_addr(const void *addr) @@ -290,7 +289,7 @@ static void print_address_description(void *addr, u8 tag, pr_err("\n"); if (info->cache && info->object) { - describe_object(info->cache, info->object, addr, tag); + describe_object(addr, tag, info); pr_err("\n"); } From patchwork Mon Jun 13 20:14:21 2022 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: 12880105 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 1B12FC43334 for ; Mon, 13 Jun 2022 20:20:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A33E78D01DE; Mon, 13 Jun 2022 16:20:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9BC458D01DD; Mon, 13 Jun 2022 16:20:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8361D8D01DE; Mon, 13 Jun 2022 16:20:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 6EFF88D01DD for ; Mon, 13 Jun 2022 16:20:49 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay12.hostedemail.com (Postfix) with ESMTP id 32E57120263 for ; Mon, 13 Jun 2022 20:20:49 +0000 (UTC) X-FDA: 79574331018.11.451C744 Received: from out2.migadu.com (out2.migadu.com [188.165.223.204]) by imf22.hostedemail.com (Postfix) with ESMTP id 8EFACC00A6 for ; Mon, 13 Jun 2022 20:20:48 +0000 (UTC) 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=1655151647; 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=elbTPIbPzGd31s7aorwI9rYhjRmEHK3gjeAMkGGimu0=; b=h7JWBIF63VjScWnfztbhyKCViyyAtGnpMl/AMGRoKL4V9SJgz13cHxU/JRCpZdHFJ47ZvB XMA3MgXcQs5KNAYimET01vZqFTAxYFvXff6GlCirXyjNqpI08YFhYDEPZ2YLpRHH8BHaZN 7uRZOeI2itAm/C1/N6QVUZKfWJ0p+7Y= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 30/32] kasan: introduce kasan_complete_mode_report_info Date: Mon, 13 Jun 2022 22:14:21 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151648; a=rsa-sha256; cv=none; b=uvdDgODeE8P5STXFWHZDV12d9XVkHuzilp7tqflDkV8iSKzJbXA+eRZTB859yDZIimLMJU SgrC6Yhsx4YWylNErtXi4qfXgUenn3O4jUQ00klyl1AsUOFiFU0rvKr0iB70BxeasZqHO/ tdkzIxp6I7IpxbCJ3l0q1RtDMBJOkBo= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=h7JWBIF6; spf=pass (imf22.hostedemail.com: domain of andrey.konovalov@linux.dev designates 188.165.223.204 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=1655151648; 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=elbTPIbPzGd31s7aorwI9rYhjRmEHK3gjeAMkGGimu0=; b=LU4+AiQNlUgFNaeHGs786sTS0naf8EDSRtfKr7HFS7SatNs/5GeGyQxuX6M50zrxZmC1LJ NcXiJHS9EWASK3bK1qlqZG0OOobM0NZ5VKvv9V65Wop/iBWVkXt+TPjh9BAwH49P+AlWH/ FZ5phxKM8UlUlBiWuL7Ss15Zbty9lAg= X-Stat-Signature: gdsixykw8ir1zbc413ixmqebq5y9qtp8 X-Rspamd-Queue-Id: 8EFACC00A6 Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=h7JWBIF6; spf=pass (imf22.hostedemail.com: domain of andrey.konovalov@linux.dev designates 188.165.223.204 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Rspam-User: X-Rspamd-Server: rspam02 X-HE-Tag: 1655151648-119107 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: From: Andrey Konovalov Add bug_type and alloc/free_track fields to kasan_report_info and add a kasan_complete_mode_report_info() function that fills in these fields. This function is implemented differently for different KASAN mode. Change the reporting code to use the filled in fields instead of invoking kasan_get_bug_type() and kasan_get_alloc/free_track(). For the Generic mode, kasan_complete_mode_report_info() invokes these functions instead. For the tag-based modes, only the bug_type field is filled in; alloc/free_track are handled in the next patch. Using a single function that fills in these fields is required for the tag-based modes, as the values for all three fields are determined in a single procedure implemented in the following patch. Signed-off-by: Andrey Konovalov --- mm/kasan/kasan.h | 33 +++++++++++++++++---------------- mm/kasan/report.c | 29 ++++++++++++++--------------- mm/kasan/report_generic.c | 32 +++++++++++++++++--------------- mm/kasan/report_tags.c | 13 +++---------- 4 files changed, 51 insertions(+), 56 deletions(-) diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index b9bd9f1656bf..c51cea31ced0 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -146,6 +146,13 @@ static inline bool kasan_requires_meta(void) #define META_MEM_BYTES_PER_ROW (META_BYTES_PER_ROW * KASAN_GRANULE_SIZE) #define META_ROWS_AROUND_ADDR 2 +#define KASAN_STACK_DEPTH 64 + +struct kasan_track { + u32 pid; + depot_stack_handle_t stack; +}; + struct kasan_report_info { /* Filled in by kasan_report_*(). */ void *access_addr; @@ -158,6 +165,11 @@ struct kasan_report_info { void *first_bad_addr; struct kmem_cache *cache; void *object; + + /* Filled in by the mode-specific reporting code. */ + const char *bug_type; + struct kasan_track alloc_track; + struct kasan_track free_track; }; /* Do not change the struct layout: compiler ABI. */ @@ -183,14 +195,7 @@ struct kasan_global { #endif }; -/* Structures for keeping alloc and free tracks. */ - -#define KASAN_STACK_DEPTH 64 - -struct kasan_track { - u32 pid; - depot_stack_handle_t stack; -}; +/* Structures for keeping alloc and free meta. */ #ifdef CONFIG_KASAN_GENERIC @@ -264,16 +269,16 @@ static inline bool addr_has_metadata(const void *addr) #endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */ +void *kasan_find_first_bad_addr(void *addr, size_t size); +void kasan_complete_mode_report_info(struct kasan_report_info *info); +void kasan_metadata_fetch_row(char *buffer, void *row); + #if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS) void kasan_print_tags(u8 addr_tag, const void *addr); #else static inline void kasan_print_tags(u8 addr_tag, const void *addr) { } #endif -void *kasan_find_first_bad_addr(void *addr, size_t size); -const char *kasan_get_bug_type(struct kasan_report_info *info); -void kasan_metadata_fetch_row(char *buffer, void *row); - #if defined(CONFIG_KASAN_STACK) void kasan_print_address_stack_frame(const void *addr); #else @@ -308,10 +313,6 @@ depot_stack_handle_t kasan_save_stack(gfp_t flags, bool can_alloc); void kasan_set_track(struct kasan_track *track, gfp_t flags); void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags); void kasan_save_free_info(struct kmem_cache *cache, void *object); -struct kasan_track *kasan_get_alloc_track(struct kmem_cache *cache, - void *object); -struct kasan_track *kasan_get_free_track(struct kmem_cache *cache, - void *object, u8 tag); #if defined(CONFIG_KASAN_GENERIC) && \ (defined(CONFIG_SLAB) || defined(CONFIG_SLUB)) diff --git a/mm/kasan/report.c b/mm/kasan/report.c index a2789d4a05dd..206b7fe64e6b 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -176,7 +176,7 @@ static void end_report(unsigned long *flags, void *addr) static void print_error_description(struct kasan_report_info *info) { const char *bug_type = info->is_free ? - "double-free or invalid-free" : kasan_get_bug_type(info); + "double-free or invalid-free" : info->bug_type; pr_err("BUG: KASAN: %s in %pS\n", bug_type, (void *)info->ip); if (info->is_free) @@ -236,31 +236,25 @@ static void describe_object_addr(const void *addr, struct kmem_cache *cache, (void *)(object_addr + cache->object_size)); } -static void describe_object_stacks(u8 tag, struct kasan_report_info *info) +static void describe_object_stacks(struct kasan_report_info *info) { - struct kasan_track *alloc_track; - struct kasan_track *free_track; - - alloc_track = kasan_get_alloc_track(info->cache, info->object); - if (alloc_track) { - print_track(alloc_track, "Allocated"); + if (info->alloc_track.stack) { + print_track(&info->alloc_track, "Allocated"); pr_err("\n"); } - free_track = kasan_get_free_track(info->cache, info->object, tag); - if (free_track) { - print_track(free_track, "Freed"); + if (info->free_track.stack) { + print_track(&info->free_track, "Freed"); pr_err("\n"); } kasan_print_aux_stacks(info->cache, info->object); } -static void describe_object(const void *addr, u8 tag, - struct kasan_report_info *info) +static void describe_object(const void *addr, struct kasan_report_info *info) { if (kasan_stack_collection_enabled()) - describe_object_stacks(tag, info); + describe_object_stacks(info); describe_object_addr(addr, info->cache, info->object); } @@ -289,7 +283,7 @@ static void print_address_description(void *addr, u8 tag, pr_err("\n"); if (info->cache && info->object) { - describe_object(addr, tag, info); + describe_object(addr, info); pr_err("\n"); } @@ -420,6 +414,9 @@ static void complete_report_info(struct kasan_report_info *info) info->object = nearest_obj(info->cache, slab, addr); } else info->cache = info->object = NULL; + + /* Fill in mode-specific report info fields. */ + kasan_complete_mode_report_info(info); } void kasan_report_invalid_free(void *ptr, unsigned long ip) @@ -437,6 +434,7 @@ void kasan_report_invalid_free(void *ptr, unsigned long ip) start_report(&flags, true); + memset(&info, 0, sizeof(info)); info.access_addr = ptr; info.access_size = 0; info.is_write = false; @@ -471,6 +469,7 @@ bool kasan_report(unsigned long addr, size_t size, bool is_write, start_report(&irq_flags, true); + memset(&info, 0, sizeof(info)); info.access_addr = ptr; info.access_size = size; info.is_write = is_write; diff --git a/mm/kasan/report_generic.c b/mm/kasan/report_generic.c index 74d21786ef09..087c1d8c8145 100644 --- a/mm/kasan/report_generic.c +++ b/mm/kasan/report_generic.c @@ -109,7 +109,7 @@ static const char *get_wild_bug_type(struct kasan_report_info *info) return bug_type; } -const char *kasan_get_bug_type(struct kasan_report_info *info) +static const char *get_bug_type(struct kasan_report_info *info) { /* * If access_size is a negative number, then it has reason to be @@ -127,25 +127,27 @@ const char *kasan_get_bug_type(struct kasan_report_info *info) return get_wild_bug_type(info); } -struct kasan_track *kasan_get_alloc_track(struct kmem_cache *cache, - void *object) +void kasan_complete_mode_report_info(struct kasan_report_info *info) { struct kasan_alloc_meta *alloc_meta; + struct kasan_free_meta *free_meta; - alloc_meta = kasan_get_alloc_meta(cache, object); - if (!alloc_meta) - return NULL; + info->bug_type = get_bug_type(info); - return &alloc_meta->alloc_track; -} + if (!info->cache || !info->object) + return; -struct kasan_track *kasan_get_free_track(struct kmem_cache *cache, - void *object, u8 tag) -{ - if (*(u8 *)kasan_mem_to_shadow(object) != KASAN_SLAB_FREETRACK) - return NULL; - /* Free meta must be present with KASAN_SLAB_FREETRACK. */ - return &kasan_get_free_meta(cache, object)->free_track; + alloc_meta = kasan_get_alloc_meta(info->cache, info->object); + if (alloc_meta) + memcpy(&info->alloc_track, &alloc_meta->alloc_track, + sizeof(info->alloc_track)); + + if (*(u8 *)kasan_mem_to_shadow(info->object) == KASAN_SLAB_FREETRACK) { + /* Free meta must be present with KASAN_SLAB_FREETRACK. */ + free_meta = kasan_get_free_meta(info->cache, info->object); + memcpy(&info->free_track, &free_meta->free_track, + sizeof(info->free_track)); + } } void kasan_metadata_fetch_row(char *buffer, void *row) diff --git a/mm/kasan/report_tags.c b/mm/kasan/report_tags.c index 79b6497d8a81..5cbac2cdb177 100644 --- a/mm/kasan/report_tags.c +++ b/mm/kasan/report_tags.c @@ -6,7 +6,7 @@ #include "kasan.h" -const char *kasan_get_bug_type(struct kasan_report_info *info) +static const char *get_bug_type(struct kasan_report_info *info) { /* * If access_size is a negative number, then it has reason to be @@ -22,14 +22,7 @@ const char *kasan_get_bug_type(struct kasan_report_info *info) return "invalid-access"; } -struct kasan_track *kasan_get_alloc_track(struct kmem_cache *cache, - void *object) +void kasan_complete_mode_report_info(struct kasan_report_info *info) { - return NULL; -} - -struct kasan_track *kasan_get_free_track(struct kmem_cache *cache, - void *object, u8 tag) -{ - return NULL; + info->bug_type = get_bug_type(info); } From patchwork Mon Jun 13 20:14:22 2022 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: 12880106 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 D67BAC433EF for ; Mon, 13 Jun 2022 20:20:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4B7C98D01DF; Mon, 13 Jun 2022 16:20:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 43EAF8D01DD; Mon, 13 Jun 2022 16:20:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 292F58D01DF; Mon, 13 Jun 2022 16:20:50 -0400 (EDT) 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 122008D01DD for ; Mon, 13 Jun 2022 16:20:50 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id CA5AB608A4 for ; Mon, 13 Jun 2022 20:20:49 +0000 (UTC) X-FDA: 79574331018.27.377631A Received: from out2.migadu.com (out2.migadu.com [188.165.223.204]) by imf06.hostedemail.com (Postfix) with ESMTP id 40C07180084 for ; Mon, 13 Jun 2022 20:20:49 +0000 (UTC) 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=1655151648; 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=OqOHaer4kC3Iywced9lKgHi/whHdzXvvFFVWcKJPrko=; b=f5d/7XtUXAv8RzxuTswTXDO4W9PSq9kdFaPjQuxA4iTVQtefrfkzPW4aGdvzX++CKF10na 9AUHYQzZLF8H3k/I2Tuha5dUthdwznGRX8KR64xwBQq8nFKELem71xvwCOLuFf1+udE9fO uzEbRQXAmCq9BsMMyyvh9Cno2phyuLU= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 31/32] kasan: implement stack ring for tag-based modes Date: Mon, 13 Jun 2022 22:14:22 +0200 Message-Id: <3cd76121903de13713581687ffa45e668ef1475a.1655150842.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="f5d/7XtU"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf06.hostedemail.com: domain of andrey.konovalov@linux.dev designates 188.165.223.204 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=1655151649; 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=OqOHaer4kC3Iywced9lKgHi/whHdzXvvFFVWcKJPrko=; b=H8woqpq5xEpNLfxAKFXrhvkz2hnMG1VOqdG9vV30Pd4aLOPwGd3C5ZcQuv8Vnqvr2B+5Xa EZwa+e9VBS1VogqQ+XUIqw/qiXS2u58Mc/A0JcGLH3DNwFP5yxwOf26sNAStXmliBtjcj5 dQGCzT6yB8D9SrBppWvRBdDzesZlYGw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151649; a=rsa-sha256; cv=none; b=A4EiyiHiv9vzx9QtHaENiY0nQTQE+UxCzMrXSIqjrbz8yK0F4fUjbE9EsVZlhpjvLY0hnr Oad4HUOEiPsQiZ8LV0SE93st2aMF9Hunm4zsEFCLH066UOsqtc4ByEhXumGuYDBd9WPeLg v5bzL/tbF3OVOboM/0fpknuRDHczNXI= X-Stat-Signature: m8jgofcaw6jjzd4moswjw8hqt4tus7hp X-Rspamd-Queue-Id: 40C07180084 X-Rspam-User: Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="f5d/7XtU"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf06.hostedemail.com: domain of andrey.konovalov@linux.dev designates 188.165.223.204 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-Rspamd-Server: rspam10 X-HE-Tag: 1655151649-357016 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: From: Andrey Konovalov Implement storing stack depot handles for alloc/free stack traces for slab objects for the tag-based KASAN modes in a ring buffer. This ring buffer is referred to as the stack ring. On each alloc/free of a slab object, the tagged address of the object and the current stack trace are recorded in the stack ring. On each bug report, if the accessed address belongs to a slab object, the stack ring is scanned for matching entries. The newest entries are used to print the alloc/free stack traces in the report: one entry for alloc and one for free. The ring buffer is lock-free. Signed-off-by: Andrey Konovalov --- The number of entries in the stack ring is fixed in this version of the patch. We could either implement it as a config option or a command-line argument. I tilt towards the latter option and will implement it in v2 unless there are objections. --- mm/kasan/kasan.h | 20 ++++++++++++++ mm/kasan/report_tags.c | 61 ++++++++++++++++++++++++++++++++++++++++++ mm/kasan/tags.c | 30 +++++++++++++++++++++ 3 files changed, 111 insertions(+) diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index c51cea31ced0..da9a3c56ef4b 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -2,6 +2,7 @@ #ifndef __MM_KASAN_KASAN_H #define __MM_KASAN_KASAN_H +#include #include #include #include @@ -227,6 +228,25 @@ struct kasan_free_meta { #endif /* CONFIG_KASAN_GENERIC */ +#if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS) + +struct kasan_stack_ring_entry { + atomic64_t ptr; /* void * */ + atomic64_t size; /* size_t */ + atomic_t pid; /* u32 */ + atomic_t stack; /* depot_stack_handle_t */ + atomic_t is_free; /* bool */ +}; + +#define KASAN_STACK_RING_ENTRIES (32 << 10) + +struct kasan_stack_ring { + atomic64_t pos; + struct kasan_stack_ring_entry entries[KASAN_STACK_RING_ENTRIES]; +}; + +#endif /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */ + #if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST) /* Used in KUnit-compatible KASAN tests. */ struct kunit_kasan_status { diff --git a/mm/kasan/report_tags.c b/mm/kasan/report_tags.c index 5cbac2cdb177..21911d1883d3 100644 --- a/mm/kasan/report_tags.c +++ b/mm/kasan/report_tags.c @@ -4,8 +4,12 @@ * Copyright (c) 2020 Google, Inc. */ +#include + #include "kasan.h" +extern struct kasan_stack_ring stack_ring; + static const char *get_bug_type(struct kasan_report_info *info) { /* @@ -24,5 +28,62 @@ static const char *get_bug_type(struct kasan_report_info *info) void kasan_complete_mode_report_info(struct kasan_report_info *info) { + u64 pos; + struct kasan_stack_ring_entry *entry; + void *object; + u32 pid; + depot_stack_handle_t stack; + bool is_free; + bool alloc_found = false, free_found = false; + info->bug_type = get_bug_type(info); + + if (!info->cache || !info->object) + return; + + pos = atomic64_read(&stack_ring.pos); + + for (u64 i = pos - 1; i != pos - 1 - KASAN_STACK_RING_ENTRIES; i--) { + if (alloc_found && free_found) + break; + + entry = &stack_ring.entries[i % KASAN_STACK_RING_ENTRIES]; + + /* Paired with atomic64_set_release() in save_stack_info(). */ + object = (void *)atomic64_read_acquire(&entry->ptr); + + if (kasan_reset_tag(object) != info->object || + get_tag(object) != get_tag(info->access_addr)) + continue; + + pid = atomic_read(&entry->pid); + stack = atomic_read(&entry->stack); + is_free = atomic_read(&entry->is_free); + + /* Try detecting if the entry was changed while being read. */ + smp_mb(); + if (object != (void *)atomic64_read(&entry->ptr)) + continue; + + if (is_free) { + /* + * Second free of the same object. + * Give up on trying to find the alloc entry. + */ + if (free_found) + break; + + info->free_track.pid = pid; + info->free_track.stack = stack; + free_found = true; + } else { + /* Second alloc of the same object. Give up. */ + if (alloc_found) + break; + + info->alloc_track.pid = pid; + info->alloc_track.stack = stack; + alloc_found = true; + } + } } diff --git a/mm/kasan/tags.c b/mm/kasan/tags.c index 39a0481e5228..286011307695 100644 --- a/mm/kasan/tags.c +++ b/mm/kasan/tags.c @@ -6,6 +6,7 @@ * Copyright (c) 2020 Google, Inc. */ +#include #include #include #include @@ -16,11 +17,40 @@ #include #include "kasan.h" +#include "../slab.h" + +struct kasan_stack_ring stack_ring; + +void save_stack_info(struct kmem_cache *cache, void *object, + gfp_t flags, bool is_free) +{ + u64 pos; + struct kasan_stack_ring_entry *entry; + depot_stack_handle_t stack; + + stack = kasan_save_stack(flags, true); + + pos = atomic64_fetch_add(1, &stack_ring.pos); + entry = &stack_ring.entries[pos % KASAN_STACK_RING_ENTRIES]; + + atomic64_set(&entry->size, cache->object_size); + atomic_set(&entry->pid, current->pid); + atomic_set(&entry->stack, stack); + atomic_set(&entry->is_free, is_free); + + /* + * Paired with atomic64_read_acquire() in + * kasan_complete_mode_report_info(). + */ + atomic64_set_release(&entry->ptr, (s64)object); +} void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags) { + save_stack_info(cache, object, flags, false); } void kasan_save_free_info(struct kmem_cache *cache, void *object) { + save_stack_info(cache, object, GFP_NOWAIT, true); } From patchwork Mon Jun 13 20:14:23 2022 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: 12880107 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 5831CCCA47B for ; Mon, 13 Jun 2022 20:20:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 776C88D01E0; Mon, 13 Jun 2022 16:20:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6B0728D01DD; Mon, 13 Jun 2022 16:20:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4DC4E8D01E0; Mon, 13 Jun 2022 16:20:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 2B1FC8D01DD for ; Mon, 13 Jun 2022 16:20:51 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 08DB231EB7 for ; Mon, 13 Jun 2022 20:20:51 +0000 (UTC) X-FDA: 79574331102.12.0AB1728 Received: from out2.migadu.com (out2.migadu.com [188.165.223.204]) by imf13.hostedemail.com (Postfix) with ESMTP id 1E1C220099 for ; Mon, 13 Jun 2022 20:20:49 +0000 (UTC) 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=1655151648; 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=GpOGtaNQnKD78CBtw46te33KktguHzhzP5BMdoXtyCA=; b=Hws0kYchyYiIC/oyq7RBnW0zfkZrmMqOeGChdK9on6KahEz6fkeC5f1RRXnVjNJFsNNSDn 8/NXzEfKEDNr1bA1GKoydEx4OBVdy6AOBxSbf1NtLaCa+xsXaEM4qDBNZ3ENSVeH3ugimI NSB02I298DwQrdmyc6VOdATnCsCpQbQ= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 32/32] kasan: better identify bug types for tag-based modes Date: Mon, 13 Jun 2022 22:14:23 +0200 Message-Id: <89492159bd43c01f7b13a72b050ff15f35e04973.1655150842.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655151650; a=rsa-sha256; cv=none; b=4WxCeQuBJ1MHOlhk5l2HKNK3CdPo4g/W9sqd1x+3gcLt/jJsxvwFskHyq4URsC+S3dmP79 1QODgs8D9TqRRDr81ubNMlVnVIuqvoymULZTVo03Aa759dVgX3ts5VRS4DUIw56ejU+bc2 naPmoYrdLd8pUR9vu9l/0gfpGU6JAxY= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Hws0kYch; spf=pass (imf13.hostedemail.com: domain of andrey.konovalov@linux.dev designates 188.165.223.204 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=1655151650; 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=GpOGtaNQnKD78CBtw46te33KktguHzhzP5BMdoXtyCA=; b=s2kKPF85qyl1eyq9mvVVHseUgxrezpntUCtdT0RQoVofGXbhg1zBdfIGavaJkhK5l9mw8e jB7GpLi9yeSsS05eMD4RMEcRJBh+5wGBKyGt90PgWO4MnhS5vRtUul4BHFuxxSxcbHonAs +Rk5Tsvs5sVgORHmNOP/Cz6M6V7XrU0= X-Stat-Signature: gfwek6emthesbas73rhn9coe7mpo8mrk X-Rspamd-Queue-Id: 1E1C220099 Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Hws0kYch; spf=pass (imf13.hostedemail.com: domain of andrey.konovalov@linux.dev designates 188.165.223.204 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Rspamd-Server: rspam07 X-Rspam-User: X-HE-Tag: 1655151649-724600 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: From: Andrey Konovalov Identify the bug type for the tag-based modes based on the stack trace entries found in the stack ring. If a free entry is found first (meaning that it was added last), mark the bug as use-after-free. If an alloc entry is found first, mark the bug as slab-out-of-bounds. Otherwise, assign the common bug type. This change returns the functionalify of the previously dropped CONFIG_KASAN_TAGS_IDENTIFY. Signed-off-by: Andrey Konovalov --- mm/kasan/report_tags.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/mm/kasan/report_tags.c b/mm/kasan/report_tags.c index 21911d1883d3..dc1f8fc0327f 100644 --- a/mm/kasan/report_tags.c +++ b/mm/kasan/report_tags.c @@ -10,7 +10,7 @@ extern struct kasan_stack_ring stack_ring; -static const char *get_bug_type(struct kasan_report_info *info) +static const char *get_common_bug_type(struct kasan_report_info *info) { /* * If access_size is a negative number, then it has reason to be @@ -36,10 +36,10 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info) bool is_free; bool alloc_found = false, free_found = false; - info->bug_type = get_bug_type(info); - - if (!info->cache || !info->object) + if (!info->cache || !info->object) { + info->bug_type = get_common_bug_type(info); return; + } pos = atomic64_read(&stack_ring.pos); @@ -76,6 +76,13 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info) info->free_track.pid = pid; info->free_track.stack = stack; free_found = true; + + /* + * If a free entry is found first, the bug is likely + * a use-after-free. + */ + if (!info->bug_type) + info->bug_type = "use-after-free"; } else { /* Second alloc of the same object. Give up. */ if (alloc_found) @@ -84,6 +91,17 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info) info->alloc_track.pid = pid; info->alloc_track.stack = stack; alloc_found = true; + + /* + * If an alloc entry is found first, the bug is likely + * an out-of-bounds. + */ + if (!info->bug_type) + info->bug_type = "slab-out-of-bounds"; } } + + /* Assign the common bug type if no entries were found. */ + if (!info->bug_type) + info->bug_type = get_common_bug_type(info); }