From patchwork Mon Oct 23 16:22:48 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: 13433197 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 1F0ACC004C0 for ; Mon, 23 Oct 2023 16:25:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DE71D6B0111; Mon, 23 Oct 2023 12:25:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D45F26B0112; Mon, 23 Oct 2023 12:25:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C0D816B0113; Mon, 23 Oct 2023 12:25:14 -0400 (EDT) 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 A283F6B0111 for ; Mon, 23 Oct 2023 12:25:14 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 778831202D7 for ; Mon, 23 Oct 2023 16:25:14 +0000 (UTC) X-FDA: 81377250948.25.11750B4 Received: from out-207.mta0.migadu.com (out-207.mta0.migadu.com [91.218.175.207]) by imf21.hostedemail.com (Postfix) with ESMTP id A1FD01C0021 for ; Mon, 23 Oct 2023 16:25:12 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=CQBo9f5r; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf21.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.207 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=1698078312; 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=h9lSbnFVvjj2GZw0LrViVCVgXWgHHPxOTEIrM79fNvQ=; b=ob2IHi1GLbZzJc6V2fZutVodCSoNuymoeYkZtIJTKmUiV2sMquzVOKK10NLBBcSnVozCOD bRgBPV6wvJcihgxxHsLvLfAXFYPg/4yDDuoHz8xg5LDLjEopr2Ib8uufvHYGnSvuv3hIqk +GZBAmUHp2yZuTfZTK0Ww4xi6G67A0c= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=CQBo9f5r; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf21.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.207 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698078312; a=rsa-sha256; cv=none; b=SpbwrEZfRPsKr1hGQq1TIbE9WqYXyZIWJ2m260ZoO17ficBA8eHhiwiHugPMiyf1krq/BE A3RkwuacIt1MFvCvLvR0EQCht7eTLvXrAKmvbI/m++/nFICCzJ0JwEdICMfAOaJjDu3wb2 g/jflwMgAHG8/bX7DPF2H2lz9Q+Vi2Q= 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=1698078311; 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=h9lSbnFVvjj2GZw0LrViVCVgXWgHHPxOTEIrM79fNvQ=; b=CQBo9f5r78HDYIzuMKRdcegVRv3KmK60h3RNuj3rljg/vJLY0On77Rw0SAXFAcOXknm+Yn sWYecQHmhDaiksWaRwRkX2AT+Gd9TdjhzprwlFBsP9luxtS1bbrxbl6j0iwAjY4bMTGHZc IWeq/EXSKTJEUgePln3o5gvif7vZcqM= 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 17/19] kasan: remove atomic accesses to stack ring entries Date: Mon, 23 Oct 2023 18:22:48 +0200 Message-Id: <81b8b7984846e0e7abfd794aad3bafee97c89c29.1698077459.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: A1FD01C0021 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: r5c73exemhw7uk3w43yfur65b1wsatg1 X-HE-Tag: 1698078312-488182 X-HE-Meta: U2FsdGVkX18G7yvAMAk5UtWSc5eR/Si8j+TzGMx9okRiJZBnt+JAKYvKoVb7rIdxHqTSQgs746F++Z8UdCcjsxJ1uaVvBujia5jytHHV0Ajj7Y09kI87SnUT6B1ph22whKy6eArkF0ei6uQHGR1k3ekVQOr1GVH4onlc9WAVcg7/m4YgLVqx9M6Kce/FBMlghZU9uJlZER1nn879o6Un1nLR/5bivdUaPlkJRJov4w4HbM0ZPOBkFIYl11fiYI3DbhbKmh+Ix05ySS5rmt2mnUl/CELsQ9iG4hdq6OuUBGU7gfIsHFIi7RsjcuQcSEOKRivhHIIDAetcwgdQTVse4gJfCOM8jYG0qyXzCMaZLYjnEgXRUk2Fc6z8notUopuOsq7wq3tcOkNQ7mhmtURG3ssuBxqQtP1ZfOKhtTfQd1uce/ujma0ygyf+zapp6dvd5Di1GCvLqaAJ3IBbQVus1pGT3kbil4R7Qg+GOHUJLEnxqihkY3cOLWnvzWpr4DbnKL2a/dx2MeBUC3c8LZKnhLS7rCYbgOgR2w6/frDcY1NXn/0rK2G671W+q1ARr9cjN7BGSCXIINdvvBaQPWSPvpMXAaZp93Z6Q4kbeGi30RWnn54JfxyWhXUxpIfPUoc5MHeQMvneS4k6RhOKOnpIpNWU6CaCIaCjyEpWaqE2Eob8C+2nnPEcYp228yYZadoiaiK40QDO1VjpNMmmWQpLBdmBTarHNMqXpzeN95Un+GSpRLk77VqwnLopC9YoDpwSu1SMhT08LKqXEqIQ7R0iMpCaYYy79QLu0gG8dbkLXTdBE3jULwIMHHjaJEs9HLtfaU5uAGWwqsKRN0TDJFbRgAAiOznuw+sogRCuTEGxkEIfi/Q5314ieOXXv1Q6DuHmuRr91ZJhxDAeLWmrN5WYMLmGBgeN74ZImqpj9YMOd4G5thIgJ17wZ8arteIZ+UflymzbC/PM6Lut8fqwwjt LKUBirvk CbQnfYrdIoqhFxuoiJBnaTrt1BUy9T6ZLfOKM9XQYOXMYI3RDcR2EAL3UyB3vK/meED66PkiaqY6W9AOduNNQdilnI2BknubU8TrS 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 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. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- 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); }