Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp4597931rdb; Fri, 15 Sep 2023 07:05:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IECy0b+KEKk3ecCIWAEAm2ei1tl/hXUo/2LKYB8L3d8v5Su6GisAmMSGZMqWgmCgWzIQyeQ X-Received: by 2002:a17:90a:6b86:b0:263:730b:f568 with SMTP id w6-20020a17090a6b8600b00263730bf568mr1489967pjj.3.1694786745012; Fri, 15 Sep 2023 07:05:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694786744; cv=none; d=google.com; s=arc-20160816; b=PY6UqzKBIK2Qlhg5DsEjqX8J9JneWDPjszVJeNsCKHKkNKDiEnYRjI+vt8LdYqXg7p lJrbwZ9kCDhlK2siUqkc+V6W62iioPabynI0ClxfcWx/dYttVSqPSF/JdYpnOJ2fHXNC O1TQml9wl0fXRB2R4ZQU56bmfJEE8jd9HBHxA2aKznaTO1pKwzoDY9iM6t/aTWCkWylk nMzpcG8kWZyH7gF9ZitWN8tGtp+fHeuf+vp0Htg9BfYauEp6XJp2OQziALrEwX1ZTkk7 PfpVCMazziEceI7wcoziy4eotLcL9eTn6uEZU5HtG+yQ8Cnq+EegnLIk14sqWg/EyCO8 rkww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=qjH3MV6DYl1G1UYfCD5hIyhq0uUm2VbQwACKU2fMzJc=; fh=nfQSbTp1dWHt2Ier1Up8UhVNdXOqoJJLNvTrpT3jJEk=; b=0ftS7AN3FcruSJkwb1H8wN8SJOrHs3caUV5LEgfK2/5CWXEQjZ60Pv+PxhViU9rtU5 P5814AGCXHiHnp3hTLXVFB+b3h6yRo7ZdKHk6wMVTfvx5Xs9raD6r+W0Nh5bjasUIBv3 56FYyIcnci0j8KjOOnfFxW4PeEBf9JfMajWFB67EY/h8i/Po+Vss91bNZw1rlOZXUydZ lIWJXiy5bT0oTE34XsCvQ5RxUU+YTB6A6R6wFoPL4e3/x1pH/L7xU1JVtISAUpMl0lAH FfgZq4iwWqwnOhbK+sSGVY+nqrkbc9XRXvqsoemEor5t8Ib08f2XjDjMd7Dgd5U77MXz 6dMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=aOUyG6zN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id z20-20020a17090ab11400b0026349debbdfsi5437543pjq.136.2023.09.15.07.05.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 07:05:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=aOUyG6zN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 22B81822CE2B; Wed, 13 Sep 2023 10:19:02 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231435AbjIMRSv (ORCPT + 99 others); Wed, 13 Sep 2023 13:18:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37598 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231425AbjIMRSd (ORCPT ); Wed, 13 Sep 2023 13:18:33 -0400 Received: from out-214.mta1.migadu.com (out-214.mta1.migadu.com [IPv6:2001:41d0:203:375::d6]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 159AA2108 for ; Wed, 13 Sep 2023 10:18:06 -0700 (PDT) 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 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 13 Sep 2023 10:19:02 -0700 (PDT) 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 --- 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) -- 2.25.1