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: 139037 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:a8d:b0:3f2:4152:657d with SMTP id gr13csp16303vqb; Wed, 13 Sep 2023 11:52:40 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH7sq1WO1ybhUeP9ogaMWcABeznPVR0hwNH11mIuRNF8uOj6eeptosLD3ki7LZfqoSuwHHc X-Received: by 2002:a17:902:e84a:b0:1b5:64a4:bea0 with SMTP id t10-20020a170902e84a00b001b564a4bea0mr4356793plg.10.1694631160396; Wed, 13 Sep 2023 11:52:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694631160; cv=none; d=google.com; s=arc-20160816; b=TBRWV+zUaXQxROkI0EaHTlXmW2Xwlrva06NKsT6Pu1W5lJ1tLXxMCUXwNV75rAvnv1 xEPsd+ZNuexwOwLKOEkTvwLcaRz2wtoiIuv/io5pOCkIkIA39FvTr5gUQ/sbXyGlUBrX f74hW3MNgiTrtK5KFi0fPb4J+R3Jvg+u5C8kw10wN4AdpwEVRilgVDBwN8abeImefVI0 gwVz28zrCCYvfBDyRCnvCftagdNs932YwRXicpXnq3SrMA0axbnp914w8sumn87tVkNp 3wdwxJ2U5mDI5SL8e0mgdr0GzTAvE0TUzYX7vol0iGUtE8bYGfclquwMR0hCO9AeYmxq 1aSA== 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 23.128.96.35 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 groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id p11-20020a170902eacb00b001b89b77ce99si10533386pld.458.2023.09.13.11.52.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 11:52:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; 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 23.128.96.35 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 groat.vger.email (Postfix) with ESMTP id B8F4182771A3; Wed, 13 Sep 2023 10:19:09 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231431AbjIMRSw (ORCPT + 34 others); Wed, 13 Sep 2023 13:18:52 -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 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 (groat.vger.email [0.0.0.0]); Wed, 13 Sep 2023 10:19:09 -0700 (PDT) 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 autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776949564094579167 X-GMAIL-MSGID: 1776949564094579167 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)