From patchwork Wed Sep 13 17:14:42 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: 13383630 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 E09C9EE01F0 for ; Wed, 13 Sep 2023 17:17:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6ECCF6B0276; Wed, 13 Sep 2023 13:17:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 64D956B0277; Wed, 13 Sep 2023 13:17:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 479606B0278; Wed, 13 Sep 2023 13:17:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 303CC6B0276 for ; Wed, 13 Sep 2023 13:17:07 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id EBE91804A3 for ; Wed, 13 Sep 2023 17:17:06 +0000 (UTC) X-FDA: 81232229652.22.8AD36B4 Received: from out-222.mta1.migadu.com (out-222.mta1.migadu.com [95.215.58.222]) by imf22.hostedemail.com (Postfix) with ESMTP id 209EAC0016 for ; Wed, 13 Sep 2023 17:17:04 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=dUDWfm9G; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf22.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.222 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=1694625425; 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=P7zzoSFiMvzmGoSw/qnaD/dTzTEPv955C3ixzUuxzB0=; b=yAvQ3OjNmAi/QdqWSYa3xPYVy2VfQUFoDNdDifmJLLgrvSHlErd0jM5xayuVs9CvhAjkRd svLvD0GhjWJQSpijAJHip0UjzhMm9eQFmwVMBJwuHLF/k+fP+W/gMTMd8aGaoTg49UDXTw juGsl9KnN9/WmzKZ7XN6uxjUg9TldlU= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=dUDWfm9G; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf22.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.222 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694625425; a=rsa-sha256; cv=none; b=anJ1egy1rEwNEl02MfJQyYSEIDi/CIIEYecc8/uiEWRJZOWVd6WUlAhGVjgTC5SL72XMKX KidJUV+vyiLJVpCP5xXem8bAXk3Q29LlGbOHhQkEL+iNZuMMHKpO63kUgrRCwUhGDfqzvY htnuJ+JKMzy4FbeVQ2mgkgePYtQH6fE= 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=1694625422; 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=P7zzoSFiMvzmGoSw/qnaD/dTzTEPv955C3ixzUuxzB0=; b=dUDWfm9GQJCwXn5Ns2lo8cMMrhaO+r45+sngvwNP0vGullsCgE1WTcWwBv1boZZKILIxby Iwf95QD/YaEE0XqYwS1ZkwuDWLFwJogmyau0zsHzueAIHLAi5uTqqsd2dYHi8ddYBLSeuh ZXP72m6TnkiIFBuVA1VHgbYB3a0DFuM= 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 17/19] kasan: remove atomic accesses to stack ring entries Date: Wed, 13 Sep 2023 19:14:42 +0200 Message-Id: <556085476eb7d2e3703d62dc2fa920931aadf459.1694625260.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 209EAC0016 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: domysw565mharj53zpm44sdaszh5cnqz X-HE-Tag: 1694625424-548670 X-HE-Meta: U2FsdGVkX1/2rt9XlFp3ufVuWQXxd+c3Y03WVoe4GywF+iwY/kR/tMSBfQRMXXepm+618lBQI+NC9ugUVWIuHvS045/17+NpixP0bnI0dXTtTye9L0k5+FDhzfvcFdRRvsWNspuJojq0NI7pRYNdCRE6Kgnxn6S/bzaEfpC70LMKUrFFO7WHuGMs++BsyxxhoS+2BovGEAEoevHQK6WRgFT7M2xTnfJMm4AiRBKGnd+Bk3PcoB4fd9ksPvdAUykqlm/0TGg/SOM8/0U9WP13li+BaK8Vb3Wr0PVtZOzt9209kr5jsL57A3C5iArz3Kdw3Ix4IzXUV6EvF/fbz+78fXFloayJWeAWaGJVIUEIdV4IUT1X05Dd20XfrOANAYBRkrAY8AP8XasDtqMuwG31WiBiT88grXJV4ov/a6afx5wBFvtI5gewtCdmPKl19IaxCobKfWNDGGZ7o66WMItMZR7RyYvEDk3VHNKVVIWfhkE4azeJt+3ZcBryCcevWFJc+E3wqcrKSH/jdB2/KP7L6mPCkFwiw9+uSFlLvIJbP3egeHlI7LRuwwBKtp/Zq6uhJHZcnTSrs5wfAmDB5vHTz5yTEhxdHRHwhqxZyVGnADDgf9UCCb3rfRpO3ZKueTwYZfmQy7W4dju0w1lSQkXWqxCtxA4enjS6tCdimQNlle6H5z56Db3V7TR8vxT7/cBDKarkpNMu0HLGrQx8yqK1Ye4mcqe/zSXb9dFmoQS6jFyFmLC5ZtpuvKOoLdRHq22HgjJ9SSJnV1/GXfK3+tpULwZLiahUvH5PbGSOXBmXbGphFtdkA2jg8MtuHS6yBLbHkF4ItxwRavETp4tW34NDuQYawYJeO1iFPzRbSVlhBqfn/JIBX5510JJv0ITHVI6tkm2/xZKdJHPXiv0o2S+xsP8d6OutvIXuZnox0R25PdyuvHjAFaUwFzz8CmEAhgVUcp/C4GrJLU8bBgjPai/ RgIuSNFr bFVUNecsRrKmHkp4CDI/zeUPJgOQCnaCx87IGj+mEA8rmwsXeyU/eB8qPJDo0wQVkoy4SN7wZd5+yruhX1DkFpQsZ2yE6a6Vbe74s 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: From: Andrey Konovalov Remove the atomic accesses to entry fields in save_stack_info and kasan_complete_mode_report_info for tag-based KASAN modes. These atomics are not required, as the read/write lock prevents the entries from being read (in kasan_complete_mode_report_info) while being written (in save_stack_info) and the try_cmpxchg prevents the same entry from being rewritten (in save_stack_info) in the unlikely case of wrapping during writing. Signed-off-by: Andrey Konovalov Reviewed-by: Alexander Potapenko --- Changes v1->v2: - This is a new patch. --- mm/kasan/report_tags.c | 25 +++++++------------------ mm/kasan/tags.c | 13 +++++-------- 2 files changed, 12 insertions(+), 26 deletions(-) diff --git a/mm/kasan/report_tags.c b/mm/kasan/report_tags.c index 8b8bfdb3cfdb..78abdcde5da9 100644 --- a/mm/kasan/report_tags.c +++ b/mm/kasan/report_tags.c @@ -31,10 +31,6 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info) unsigned long flags; u64 pos; struct kasan_stack_ring_entry *entry; - void *ptr; - u32 pid; - depot_stack_handle_t stack; - bool is_free; bool alloc_found = false, free_found = false; if ((!info->cache || !info->object) && !info->bug_type) { @@ -61,18 +57,11 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info) entry = &stack_ring.entries[i % stack_ring.size]; - /* Paired with smp_store_release() in save_stack_info(). */ - ptr = (void *)smp_load_acquire(&entry->ptr); - - if (kasan_reset_tag(ptr) != info->object || - get_tag(ptr) != get_tag(info->access_addr)) + if (kasan_reset_tag(entry->ptr) != info->object || + get_tag(entry->ptr) != get_tag(info->access_addr)) continue; - pid = READ_ONCE(entry->pid); - stack = READ_ONCE(entry->stack); - is_free = READ_ONCE(entry->is_free); - - if (is_free) { + if (entry->is_free) { /* * Second free of the same object. * Give up on trying to find the alloc entry. @@ -80,8 +69,8 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info) if (free_found) break; - info->free_track.pid = pid; - info->free_track.stack = stack; + info->free_track.pid = entry->pid; + info->free_track.stack = entry->stack; free_found = true; /* @@ -95,8 +84,8 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info) if (alloc_found) break; - info->alloc_track.pid = pid; - info->alloc_track.stack = stack; + info->alloc_track.pid = entry->pid; + info->alloc_track.stack = entry->stack; alloc_found = true; /* diff --git a/mm/kasan/tags.c b/mm/kasan/tags.c index 4fd32121b0fd..b6c017e670d8 100644 --- a/mm/kasan/tags.c +++ b/mm/kasan/tags.c @@ -121,15 +121,12 @@ 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. */ - WRITE_ONCE(entry->size, cache->object_size); - WRITE_ONCE(entry->pid, current->pid); - WRITE_ONCE(entry->stack, stack); - WRITE_ONCE(entry->is_free, is_free); + entry->size = cache->object_size; + entry->pid = current->pid; + entry->stack = stack; + entry->is_free = is_free; - /* - * Paired with smp_load_acquire() in kasan_complete_mode_report_info(). - */ - smp_store_release(&entry->ptr, (s64)object); + entry->ptr = object; read_unlock_irqrestore(&stack_ring.lock, flags); }