Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp1336768rda; Mon, 23 Oct 2023 09:27:05 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF8D8wpLhnLYNGHTy2X6JY2C87/GklkSMqHcKaq1agnVf9/997H+pRoSrUfqWfI1p90khyn X-Received: by 2002:a05:6a20:1589:b0:17a:f0e0:ed07 with SMTP id h9-20020a056a20158900b0017af0e0ed07mr118773pzj.15.1698078424985; Mon, 23 Oct 2023 09:27:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698078424; cv=none; d=google.com; s=arc-20160816; b=i1Sril1jAtBFffGULgfu4NLGZJxpY4Gb/tk7bRiueFi06L9PdhtgAeqAEQndaIvjTZ gqlqmmnGFIfeMhfu1rSLl7Q2IpW4oB6pQkfnSPKEKC1HiGCq2ze/LW17WjnXGyDC6c5+ x4rOBb2xdqBbt9GYNJsb5A2iQjH9ls3yIlYgKdXTGEZ6uJgjUgLG6cHk7e78BKzPuqmz orZBoE12DrFDKasyfcTqqEQLyzYuNHPve82WQmNrCQrF2g/wB6a4ANeJna1KMbi9NFZV KyQvnz15k6cGo3GJP8+uerA+0D54BiChRStdMqBF+/BHhQUS9TDzhtTNqw15CBdUFtnc Kwbw== 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=nfQSbTp1dWHt2Ier1Up8UhVNdXOqoJJLNvTrpT3jJEk=; b=VvyqqCkjXnQPc3izuAT1TPBfLsLK8fD0HVQquyywPpY0XyQd/nIyLAN9jtffXAiJoi OoWAgktOMcOMKyRFQ6nPWOgAzlrcFdNPfapePQpfqCwCVHNtL1FIYAq/SuzoqAlgYZBg igz/w4Odw2isfa0wXrDTkumlLHtgg3wwrxMr2nTMjt4ou8aKjDdD1+MnQcakgYIv8V64 OfVcmf0ob2lHA4+Sm5TWzc5MG80L/jVaObfNeEpv2LIRdfVeZzsW8ZvYNuJqoa7ZWMqu QVEKUPf0LsyD4iJWyDZdXvHMMsvVTY32NkTADKu7VQEpJqj+/j1DGz8J3a7Hnztg/6+w 45sA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b="Tr/G6YIY"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id m9-20020a656a09000000b0056b0b30d05csi7019666pgu.245.2023.10.23.09.27.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Oct 2023 09:27:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b="Tr/G6YIY"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (Postfix) with ESMTP id F39C7805B208; Mon, 23 Oct 2023 09:26:58 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232406AbjJWQ0c (ORCPT + 99 others); Mon, 23 Oct 2023 12:26:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38458 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233694AbjJWQ0R (ORCPT ); Mon, 23 Oct 2023 12:26:17 -0400 Received: from out-199.mta1.migadu.com (out-199.mta1.migadu.com [IPv6:2001:41d0:203:375::c7]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B80EB127 for ; Mon, 23 Oct 2023 09:26:15 -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=1698078373; 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=Tr/G6YIYPrWXP9fx3O7aG2jl6kXfEVrm47rOgoEDzFX/BZ50Z23L4IXwsR4vCJshDX5TDZ HrweRnvTS3UanFhDAX4boFO1pOlEosl/bvhOjewwp0T7l51nrmSzESgRmuTG8srBHitAMu 47CJygk1kptnKFhCWZImQgFTsPZh9NY= 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 v3 19/19] kasan: use stack_depot_put for tag-based modes Date: Mon, 23 Oct 2023 18:22:50 +0200 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.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.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 (pete.vger.email [0.0.0.0]); Mon, 23 Oct 2023 09:26:59 -0700 (PDT) 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