From patchwork Mon Oct 23 16:22:50 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: 156985 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp1410383vqx; Mon, 23 Oct 2023 09:26:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGo0l2deuxqdIA0OMBGJ8FwauLQixRWwjvK7QslxmwWZesc/hf72HmjxKIbWaEm4lD509Wa X-Received: by 2002:aa7:94aa:0:b0:6be:30f1:45f8 with SMTP id a10-20020aa794aa000000b006be30f145f8mr10171103pfl.20.1698078408675; Mon, 23 Oct 2023 09:26:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698078408; cv=none; d=google.com; s=arc-20160816; b=0oNRMfNQ0ihjvnJfmhAufp2WNVB3MZGJlFgJqG/0ya4PjrGxnWzOj+maHMakSIbrq7 iWfFB1zi8yN3eDbsJd1/QiWb1gUVergm2Vgb5rmO8MZZYtFzHukcnx7zDEugsS9Y6BCY MuOfOcE7MNsBBlQ5u7ZJxjA9aQglzwP54lKpIzBWfz4aAwfQgcwW59yV3BWLQ/S83ZJX SDcMbSzpW+smFQrdTraWgea/cKJV2Vtj8njVAkmZjGaQMPVTuSt37m6sbSUxh5bfh7iN 9WLLOoCIlS4O12xpntEoibnzTlOE7r+w+7TJgAMaQeoKiiCS4JAOkYURbdbkYfy/sIjL KnHQ== 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 23.128.96.34 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 howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id bk13-20020a056a02028d00b0056433b221b9si6473539pgb.477.2023.10.23.09.26.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Oct 2023 09:26:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; 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 23.128.96.34 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 howler.vger.email (Postfix) with ESMTP id B87B580A1E0A; Mon, 23 Oct 2023 09:26:43 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232469AbjJWQ0d (ORCPT + 27 others); Mon, 23 Oct 2023 12:26:33 -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 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 howler.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 (howler.vger.email [0.0.0.0]); Mon, 23 Oct 2023 09:26:43 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780564265097684752 X-GMAIL-MSGID: 1780564265097684752 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)