From patchwork Mon Nov 20 17:47:18 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: 13461834 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 C38C3C5AD4C for ; Mon, 20 Nov 2023 17:50:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B07FF6B0386; Mon, 20 Nov 2023 12:50:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A42EF6B0388; Mon, 20 Nov 2023 12:50:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 86E366B038C; Mon, 20 Nov 2023 12:50:47 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 62CFD6B0388 for ; Mon, 20 Nov 2023 12:50:47 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 42F63A095B for ; Mon, 20 Nov 2023 17:50:47 +0000 (UTC) X-FDA: 81479072934.06.304D4E1 Received: from out-182.mta1.migadu.com (out-182.mta1.migadu.com [95.215.58.182]) by imf04.hostedemail.com (Postfix) with ESMTP id 5EE7C40007 for ; Mon, 20 Nov 2023 17:50:45 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=SjlBdcCR; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf04.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.182 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=1700502645; 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=w8vt3EAh3AJwU0IeQs45B2J2tAvYG0I4nC0Eq34unOYgYKNot6jFPaicT7opDYykZpD+VB btg4Pgm7oBSj1x++h3samy5zf5z9iNiXyZ51Wv9BafFs5wc80GWqiyn15k9nnD6HjWu1IR ptIeP5X5ILZ9Gu/3Z16NSiZXYdZQa7A= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=SjlBdcCR; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf04.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.182 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700502645; a=rsa-sha256; cv=none; b=yCRN3gUspESFTGS41TuThKR6Sex7kButfp8FFcm2kgYDIQI1lX0mtakZDGo8gDnPpO3hkt l6aM2d/YU2rD86c2v5VOOEjvNPBEetLFu+3AqhNfeawoyWrl5P5oRe0muLOSxZRrdJL1ip fMqwSzVAVgh7JJre5y8QC/0ix1EN6mU= 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 X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Stat-Signature: xtxyfmpuxur3bn5pcfndc3s63weszqc3 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 5EE7C40007 X-HE-Tag: 1700502645-286687 X-HE-Meta: U2FsdGVkX1/6Mqw0a/YKSyMhOGBUOpEddE14+/E4tGysTxlQPxuDynaS/l43BKmX4z81scp2MngJTL69aW+fwkp9dkY0TscYAfWrAE2cKl+gLL5ZZkz3NIJPWk6Aj1V+P319DIu9ha53akaRgBHeCSxyJtxnP5+TAxgDAilv9zB2hC0Xz1VYtteOgsHoSkPpHtHm5C/b+7BaeWmZl1vWv9rvIjtfm8G4oV5he84vKTWmN6SYVcfPr3+6nrd2Ut86OgqSy/J6F0R0m2/9zUTRQMfklFOW0pAeO4iqeBKNAVijGK6ytZTHfPvfjLeHoY8g9eflffKtsB7FxoU1J9owmE9y//hI4fYhtJK3jVDAibS8Kny8gdwNB1rVbXe+A2165aNFJlD1vg17shrKirzq6SSZ65cGnAGFjQV4vKBSot1fQ0mOylGxRXhCZMYsvChmHyziRN7rWFBoSCePAWHSIr0k7TYsPXCTIrKO7C8cr1N8P7rkvZi1bug9WsOs4Tt0GuRSK8zN0+kQGcI1T2t45sUUPJXORCYktYjZWs2X4EsozwKzkmAh0uXs6lSZM7GgviZAWJ51PXZWElG15tHQa1JHR2Rki/n9rA/PBhXRYYp0YXWIrni/x3u5S+00euQmsZmGT/VOsgwY9WzuxiucQZHfK2w+mWQ9M2JkQOLvUfyk3zzJ2aQrWVaiKj//vrxA9n3jZTYDu2iKpFwVUzygK7r5OnD7IXbLbtnTdUl1UpCCnknTm20nk7XxTWzAflncxUZ2GV+GUzVHgSi1lUxFlW/STktuk474smIKc3i+BdPntFLWF4Ng9zbYjlz7I/EdotP1gWK7hFwNkfrD1Jl83wZE9Ea49f3KLHq2yYeLufuo8cX12uJy0AtCQP1Veh0OvrIvfaVV8TVG7120+HFaAQjr+rNOmEHcR0tcw7e9MpgONg/e54PArZyCM8842Cqqs3qpAN8p/2f2JduIUTX agips3D3 VB5z3hfRRnIDyDdkR+v2C2WD4OAg2cj0fEzGZGrGFQ7ZSIn3W9RitarIdanF+xvq7vysRLCJWTHBCqHF8aBVmlkKXtbUrFr9tFBkl 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)