From patchwork Mon Nov 20 17:47:16 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: 13461832 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 CDF55C197A0 for ; Mon, 20 Nov 2023 17:50:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6C1C26B0382; Mon, 20 Nov 2023 12:50:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 64B436B0384; Mon, 20 Nov 2023 12:50:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 512FA6B0386; Mon, 20 Nov 2023 12:50:46 -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 3FA416B0382 for ; Mon, 20 Nov 2023 12:50:46 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 01DD6160947 for ; Mon, 20 Nov 2023 17:50:45 +0000 (UTC) X-FDA: 81479072892.20.44AFAF0 Received: from out-175.mta1.migadu.com (out-175.mta1.migadu.com [95.215.58.175]) by imf29.hostedemail.com (Postfix) with ESMTP id 431D612001B for ; Mon, 20 Nov 2023 17:50:44 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="altZ3Y/X"; spf=pass (imf29.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.175 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700502644; 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=KczJU125yMZjT/uHw3wn0b625c59tl8ppbJQ730J7hBFahqJ0ekhZ5FnCjVSsXfSf+mCC4 fiY7+0HKyM4+/KdyPlt0rbvTJPOxQD69agOc2+rbtzb8E9lNXcOGOiJr7leB0ZerToggU1 F7YuvMV4sINfQ9HqTopTItBCNl7EqL8= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="altZ3Y/X"; spf=pass (imf29.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.175 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700502644; a=rsa-sha256; cv=none; b=e6OENMddHPsslc9dD9kZFypvXfyE1X7Edd9lQ9GpC4mG2M5VTHpG2fYKaSjpI+eSvLHMVv NqKjgrE/2i2iz2lmw5OjSnbVDKA8RMys6kfaYQZEKv67XJSUJEvQCPUuFBS32E+FJK++9s AYJnY/NzTsu15DFaAALF0V3nD00USFs= 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=1700502642; 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=altZ3Y/XI4JhIu5hrnCRXA2Lh0EqKL+D8A/MAbLwDHf+SQx3XdzFWHdK500Z2nrgBAdsMJ CIl8i/FRs6CobGUwDDY8obdvF4axa9Jsyq1WLFLLkkvZ3UoyLWEgpJBac7eWWb07xxbTHj /N4UEr5XJVfvRHr9tfyWkhDs5sCw/8o= 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 18/22] kasan: remove atomic accesses to stack ring entries Date: Mon, 20 Nov 2023 18:47:16 +0100 Message-Id: <29f59126d9845c5257b6c29cd7ad113b16f19f47.1700502145.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 431D612001B X-Rspam-User: X-Stat-Signature: yrtd83o561jdydqtzsizu48ue7u99dna X-Rspamd-Server: rspam01 X-HE-Tag: 1700502644-727315 X-HE-Meta: U2FsdGVkX189jYyQmdT89i/M+lVreY7SQAyqdppBoBg25pWJ2dULsjuT3cUlLolUtYQfpktBtdlaI1l+QY1CZmGwKn0QDirwcngfpXRn3ir8vTcsmrjYfCM4S6rhbbzrnxmrQbWmfsUROEO62ocmCLdzOEA2STzMd7XUy+iYHP3ba5ENM6biazJ7miAKE7K5IjpSXaPZU3BlZtdyHfdU+1+ol6LYRppd2eMd6Rr8KVe3bLPL8E9jD2l1nYPga8RLVX2/We9Y+PdCQM5WBCUQnSg8uEOblr1Fsr4vly+wKVSWAg7La8sMpEBG7ptbgZlCTM+CuIqz74k9I/Mx0XykcjtzK/oIAL3znX8KivcL+tNV9XrbKpU6yMAQCS1FWJpbna/+vxOzst8O8AiqUGm0fkWxsryHdWy82f5oYkpXL/FJwa5xwQWgRZQ89Qf4ulyx9rMY0NvM8s8LQyuif418RB++F3bRR7rBynVTC1YhoHvD4dOFJ7VdNE/Q9RHMfQHaxrq0Y6nhxZSpyVtdV+21XVTQCZj6/DUgi+fh6yRQL5NfG4k05fai4D2L/BakXuz9xnTKxvB8sV/QT6/k5Qak6vePho3kxlWNDqnER1jexjUnMQsm1KLtVY1nxakB9JsVgdjefSVHcL0jnQgV+tPoZiGQM5CTRyMW4HKzQJUoKyCzT6wYT7uuBECUQRwf66jWRNJxwXZw9M2IA1JptAfmA2ITGaUVsNStTYIrZMz0fbgJPqmHy4YEJXJoOHOAGw0HDYky1P+tqLjy8cYLSKwZt8h3NEfzMLwypMaj/Rs+Rgxm4NO4mY0ezBtqsnem7+n7wiFM5sWOJJytRK9mSioF1ZNstHxKVFxvi/1Ut8NfNOOeFpYTwcNk+NbCYbroys2BBPzuu0NZouRpz3oLd/93hi21mj4eNsosy9NzPbITv8k81lWgh7tX+MpYxO0hSCm9vBUGBsgaC/ftCSYcHI0 7+sjFsCf yYQisLZKIyUnKz5GZ/of6CzG8FoRXt29XYBlwGv98f+m3Qu4Zk1toFSpaD9oTEyK212u4 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); }