From patchwork Wed Sep 13 17:14:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 13383632 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 63B84EE01F0 for ; Wed, 13 Sep 2023 17:18:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 872B56B027D; Wed, 13 Sep 2023 13:18:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 822136B027E; Wed, 13 Sep 2023 13:18:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 710C96B027F; Wed, 13 Sep 2023 13:18:08 -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 588E66B027E for ; Wed, 13 Sep 2023 13:18:08 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 395EF140DCD for ; Wed, 13 Sep 2023 17:18:08 +0000 (UTC) X-FDA: 81232232256.03.4798D31 Received: from out-229.mta1.migadu.com (out-229.mta1.migadu.com [95.215.58.229]) by imf27.hostedemail.com (Postfix) with ESMTP id 7D29940028 for ; Wed, 13 Sep 2023 17:18:06 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=aOUyG6zN; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf27.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.229 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=1694625486; 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=qjH3MV6DYl1G1UYfCD5hIyhq0uUm2VbQwACKU2fMzJc=; b=jU9OAxv7M3c30Kjxzm1F7oFkJ/EBuUImT9tZlbOVvcTYTcJXYMwBO9iE/BeLdRbKI3psIs XlEe2gcpkdQhxcq4DbMmXZcRN+l7thJdwhc0WCP3Xc1lovsizhAOs90bmt2m9WQyernrrL ujYEeIgv2IkhbUKtZUexYy4xeTq23vY= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=aOUyG6zN; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf27.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.229 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694625486; a=rsa-sha256; cv=none; b=37L3rOZzmNGpFM4U5xocOy1YhJHEIBF0qb0lQXqpAy0LditVCTsPNyxlLTaiJLcnzhM2u4 dkh8qAXU93aC/xQDSJ+k0LqY95h3Rb1KnzU36ZHO4KwvlZ75w7WPP9XuT+gYojA/nZo9pj 7Pcotk7teA1yR7Z20WJCRsge5OCRUlo= 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=1694625485; 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=qjH3MV6DYl1G1UYfCD5hIyhq0uUm2VbQwACKU2fMzJc=; b=aOUyG6zNU4qOExhkFzpcOcmJrKwkx0ZsS/bVVWhskgw/aBPt6S1+VAaKP4oT7FsM6oyJVg +IUqSRli1eaRlhDIRzKFieirB2EVqYsJdtwVbWJ4TY/mTzb+JzNUxW7Lbi7K2Q5uY06kf1 yBI+2ElH2T2LDtZB7yOsWKeboKVByXg= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v2 19/19] kasan: use stack_depot_put for tag-based modes Date: Wed, 13 Sep 2023 19:14:44 +0200 Message-Id: <6e2367e7693aa107f05c649abe06180fff847bb4.1694625260.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 7D29940028 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: 5co77ixguikxhxnirxpuqjsm1x178t95 X-HE-Tag: 1694625486-982971 X-HE-Meta: U2FsdGVkX1/1Iu0kuEP0DQMkx6ghmNoQeFYXjB66NY1UgNFHVkXd55/WHkvSelsPRlcxICSGpiMFk1OrZ7OHATmes7zq+lol8zxMvSHwogfehXHNi8AuuUUNJDojUkLwFTYgosOX+aRgOR3WzRMc4D2h+pg5uSZS5PSNEHopaoMmGkAh33ZhVswmuvSshjToxUA1/Lr0CkC0H7GqI0itLFK3s/YJTZ1EKNoAkmyr+dj2su+otw1SArEY5BCuG1RZGcYgnU6cgnMpQjkEld5GFE5OdDrETIWG5Af4slujJa3PDoEevkxdmdvIhZiZM2Q9BSj2WGMfluMmCYtZzAGrkaCLUaykvSnsYtOoaspYo4DKiEh177bO9DvHJqB1DMUsZvCBWymT73blJFDzsv1HL6cq+tEQ9bhPZQacHOLGv5qGwUJn3cdiQbpjN6AXUzLxs924Rpc7jkYV+HnQ3ukm7rBM7LX7TW/ko53KVHsR3U49j86sDyzRaDp5YHhiejB+qtHvRLC8QHweFw2lLxBnFUwifWrYPCLWxSb/OqHGd/NP8RfSH2VK4GnQRHoO57bCB6FopDMyij5I1hFaEcJY3N4a/m9cZNR76w2EcZ3oMx/zVrI6iNTVUomfABmxhL3w7ewVMlYalEr+kOz3TnicUaAQ/iwE1bJOUbbvmCDJgFsgHvzqomzSdT5I6S+t0/JNMYgj4BKz/PZsiy8KQE4E8+6wPmucnFvirj2bHO5+0aikFSg8Y6fjPW6GAEgvHoE2yie4dNvNMOQoE3ZAARTR9RyEUPJdpL7KCHEW+c3+2O+T10gYFQcruqd59FJ4qBmwb27gJIVI1vwjtmy8eqBtTwwz7qbsRmg081fFn5JwBBuYIo1yejxYJ0OjWq1FLgnvSOL+6OaAVEQgUNKmEwWg2mAfC8FMZ8jcoVA/rkhMmfvX1YjN8H2iOQwlWaYTepk+5FgbzW5XJgRVk+aS1jI E7c2Cdka LVsbcpRFZqMh3DARiqyt7UOc6SSSjSUFZLcsxQM8CF7Kj0otbgJRu439ovnuGK9yWp4yjIyblyyb5zXVsPsmDuYVhlrT9UlpHsA3qtEdCSRSmbNIvW4XWxT8ZN+jvtkMapMz+ 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 Make tag-based KASAN modes to evict stack traces from the stack depot once they are evicted from the stack ring. Internally, pass STACK_DEPOT_FLAG_GET to stack_depot_save_flags (via kasan_save_stack) to increment the refcount when saving a new entry to stack ring and call stack_depot_put when removing an entry from stack ring. Signed-off-by: Andrey Konovalov Reviewed-by: Alexander Potapenko --- Changes v1->v2: - Adapt to the stack depot API change. - Drop READ_ONCE when reading entry->stack. --- mm/kasan/report_tags.c | 1 + mm/kasan/tags.c | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/mm/kasan/report_tags.c b/mm/kasan/report_tags.c index 98c238ba3545..55154743f915 100644 --- a/mm/kasan/report_tags.c +++ b/mm/kasan/report_tags.c @@ -7,6 +7,7 @@ #include #include "kasan.h" +#include "../slab.h" extern struct kasan_stack_ring stack_ring; diff --git a/mm/kasan/tags.c b/mm/kasan/tags.c index b6c017e670d8..739ae997463d 100644 --- a/mm/kasan/tags.c +++ b/mm/kasan/tags.c @@ -97,12 +97,13 @@ static void save_stack_info(struct kmem_cache *cache, void *object, gfp_t gfp_flags, bool is_free) { unsigned long flags; - depot_stack_handle_t stack; + depot_stack_handle_t stack, old_stack; u64 pos; struct kasan_stack_ring_entry *entry; void *old_ptr; - stack = kasan_save_stack(gfp_flags, STACK_DEPOT_FLAG_CAN_ALLOC); + stack = kasan_save_stack(gfp_flags, + STACK_DEPOT_FLAG_CAN_ALLOC | STACK_DEPOT_FLAG_GET); /* * Prevent save_stack_info() from modifying stack ring @@ -121,6 +122,8 @@ static void save_stack_info(struct kmem_cache *cache, void *object, if (!try_cmpxchg(&entry->ptr, &old_ptr, STACK_RING_BUSY_PTR)) goto next; /* Busy slot. */ + old_stack = entry->stack; + entry->size = cache->object_size; entry->pid = current->pid; entry->stack = stack; @@ -129,6 +132,9 @@ static void save_stack_info(struct kmem_cache *cache, void *object, entry->ptr = object; read_unlock_irqrestore(&stack_ring.lock, flags); + + if (old_stack) + stack_depot_put(old_stack); } void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags)