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: 13433199 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3180AC25B41 for ; Mon, 23 Oct 2023 16:26:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9A0DB6B0118; Mon, 23 Oct 2023 12:26:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 928D66B0119; Mon, 23 Oct 2023 12:26:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7078E6B011A; Mon, 23 Oct 2023 12:26:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 516166B0118 for ; Mon, 23 Oct 2023 12:26:17 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 30837A0AC8 for ; Mon, 23 Oct 2023 16:26:17 +0000 (UTC) X-FDA: 81377253594.07.18E600B Received: from out-197.mta1.migadu.com (out-197.mta1.migadu.com [95.215.58.197]) by imf19.hostedemail.com (Postfix) with ESMTP id 512DE1A001F for ; Mon, 23 Oct 2023 16:26:15 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="Tr/G6YIY"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf19.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.197 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698078375; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Qc2DDhk7eSddbt2Q3kjJLSbCNGbLZWOl9JsGZF81sG4=; b=S7QVzQSwLk0Yjupwq9cv8p16IHoqlBiMo5dSgIjzMMU4KzbLo9h7hab8cncjAB4ELaPmoC UwHC/19n5+gLx3HCO+pnRFp+a9g7D135EtTA4Rz7YOnC6L+14QGWlJWdeDK8TpEeIbL8cN AjMFlfLudr9HthLDMOang4uTY0y2sEY= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="Tr/G6YIY"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf19.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.197 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698078375; a=rsa-sha256; cv=none; b=iNkVNn7pBPNYE3MebU4AJrA7DB+6rGRwC2Y7W5YK9hzkfKWIPlnmMK0CBuO3YVn9KZU5zD r101R5s7JsurjoXoVYls9g9UQb7fKROQ5ycBzgzHl3GzfeCUHreqf4y52NZNh+XgXtkN0Y HkCEUCKj9uW2zmJO1l7QcA9rPqNzXfk= 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-Rspamd-Queue-Id: 512DE1A001F X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: stnpi6rg1yr78x9xz8iob1p4xms9gyeu X-HE-Tag: 1698078375-791442 X-HE-Meta: U2FsdGVkX1+6VD2sEUmm5azYMV3NVCzqiIHl49ows2Vh18l1PHhpiLcEY+qc92v32orMEJI3oplJaH/DKAfdn2X+thVHhwPeneqpFuN+uYLHNwnKodL2MuJqpT6IUgUjSdP7g5G4EPUU3AuaCeCFacLjJEl42/tnWKKkN177DuduLM0Cl9xRIk04Knpz0oeirtxXV4wuNRu0IzSX7o6aAo+bo5fRfRgen72UMpROuydBNpaDEdjltJBjwT2/iZiY1K1iQ2wE6NLjajkjBGCe1JVNo4rguFVN994dGi6oR7H7ovw/AwoUqIjX3Y+3a5sYKB4DtGcDUTG4xmw/VGHWXTbSLUuJZvD3X8eEtGGY3x5IvFWYvdBY9LkCoGduYQyQzoxUk0HpOUvCo6hgxIymNZBwxmGsNYxvcBk2DWmR2uuRVBQqLDpNtIlMvw/sWXfxmZ1tDE7Gohe2xKRSNnDDwUrnoR+Qjx6NN+Xo135RAWm8tr2VMK9TMcueB0uj/g95iEQHsPEF2k33UuN8FfUPIxVlQ0kFmMi3N6aVl2yoYo/nAFb1mOpIZkLRTD6oX9xMjD8p/xWiswIgmso93bBzs4mJ3wM+6VkLyBKzyTkCs89XrEXucgbRRKFikMYDcc+ATdR5iOZJNW8F2XRDuDzPmr7l4uze/QwTImL6N1RcoP/DpH/xp4/l9YPdjpvVJ8e5F5jm/NZpNa7lhKiokPHi1ah+XxZ45gP6WgXalvm5ycINMTOjbgr9Ss5eEq0T/DVYqW28sPd1DqejiRB8jmnPS7jU8xFfgWRJwNG0fy+Lg/KNHSz+E/uCSadcBHV0Z8fkK505SNeNTNtrBZIdckSoOQkwSg26ekuP+hllWKsRfzAeLrf45RGM7+wRHvoS+8PliZUQ9L6FQqgXCSJ/GVP/IHjPm8nuDM3xqcX7u0kNaVNSGwcwhV9yf/50/jDsCqe7zGahdiuimKtdmJXTtm+ gR5FHzAB mXTw6gxhL58DCajX9V9C1EM+hbJsFNh1XwQQyHSdIOXzy2dwyyrpQ6lGB39UEHD/PR5DxVcSogpJpybTMCjTABhETjgpZMKyNeJcvUYyURaI+o6OgpDMHNBMbjuLTkBdaoAYvrdhSRR3NaFlWZNPF5BTC9A== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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)