Received: by 2002:a05:7412:b130:b0:e2:908c:2ebd with SMTP id az48csp2405729rdb; Mon, 20 Nov 2023 09:52:44 -0800 (PST) X-Google-Smtp-Source: AGHT+IGT9CiN12cf0oWfEmtwTQZA3+bNQVueD9iKsLbF2RcSmbggqeXaWfNeu/1C8AZRdbFDgTig X-Received: by 2002:a17:90a:ab01:b0:280:200c:2e20 with SMTP id m1-20020a17090aab0100b00280200c2e20mr5715912pjq.27.1700502763813; Mon, 20 Nov 2023 09:52:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700502763; cv=none; d=google.com; s=arc-20160816; b=lSK23eXN3eBoeuR+hd2quuY+M8HwYlbtCZXF0mC08blkwAmdN3rrTRPFnyhfQsARJ/ 1PaI2Viqw+eKqzlFDX5UHqL3z5LF9YPDjvbv8qjhW3MorVLWMHONB81OQuVmHfURZ06F DoOhjzC195YsWIgSvsSYgMb51Jex3Z11Yt6tdKOcsEcQejI5w8F0WLNF3NbH+3txiuZQ rHlEEYOUaI+kob3vmOipVTj19iJQkFJCCXVPi5+rIY6bHnyH3gFuhjINwIVw0JUVpDQg F9HBgm8mfLVy3wJ0zNeEiq/hKtjjuo9x5N3I1C9Uy6HqBySACcK98KsJRi+OCZk/OXT2 o1ug== 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=Qc2DDhk7eSddbt2Q3kjJLSbCNGbLZWOl9JsGZF81sG4=; fh=qWoxmPN1zRqnLSoCWGpfFsDtzJsx/SGdqx98lbP+Uho=; b=pHrEKmxirucO4qlDOva53QbNIbGjs7WIgFCxX2FWJ3NQVgNCI/qhwSVk2t4aWgO1+G 5t+3C0lW9ULqPzAsl5L4VheSXU8t33N0qVJPPUkeugMUragxya/B2yBCEqGltHNM3nXE tZcx/ETSUp8nEYy2/rnp6SnM1upSrcdMVfQGOeJZX5VEh/YvPdxfLajiBd1xMSDk4RQE 7G6ncZSq+ihR6exe+c9iBpFEpYj3T/Xtl9ARL1rsfj8T+tpPDKdcslrZ+KX/Tsx9J8sd MeauVZ20OzG8P9A4XXXmTm+IwHDsSFl3cr/Fr/Al6h8pwoxbodpIe+KUccYhfBTeSXpl l0wQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=SjlBdcCR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id v4-20020a17090a898400b0027450695a78si11209278pjn.172.2023.11.20.09.52.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Nov 2023 09:52:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=SjlBdcCR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (Postfix) with ESMTP id 0BB0880725A6; Mon, 20 Nov 2023 09:52:36 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233838AbjKTRvx (ORCPT + 99 others); Mon, 20 Nov 2023 12:51:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49786 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234384AbjKTRvY (ORCPT ); Mon, 20 Nov 2023 12:51:24 -0500 Received: from out-183.mta1.migadu.com (out-183.mta1.migadu.com [95.215.58.183]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 10C69D5F for ; Mon, 20 Nov 2023 09:50:45 -0800 (PST) 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=1700502644; 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=Qc2DDhk7eSddbt2Q3kjJLSbCNGbLZWOl9JsGZF81sG4=; b=SjlBdcCRByh5PrG2MOhc8aFxhKI3iaDpNaxTHUEIaJyLo5jT4ZNFXt6Bt3sd9u50OfOKSg CaLcy9JTN+VHkT3VWRZiUR5iYfkV9u6uxcxdsNTqWTVW/b0iVcXorrCGAHL4XcSBww8rJU PD/hyomJcMBAWfjY4j+0VCD4sMN1M4g= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v4 20/22] kasan: use stack_depot_put for tag-based modes Date: Mon, 20 Nov 2023 18:47:18 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email 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 (fry.vger.email [0.0.0.0]); Mon, 20 Nov 2023 09:52:36 -0800 (PST) From: Andrey Konovalov Make tag-based KASAN modes 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. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- Changes v1->v2: - Adapt to the stack depot API change. - Drop READ_ONCE when reading entry->stack. --- mm/kasan/tags.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) 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