Received: by 2002:a5d:925a:0:0:0:0:0 with SMTP id e26csp362745iol; Thu, 9 Jun 2022 05:30:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyfpFwC9Cp1soVNy7ewDuf1H4lq6Mt0OqYZxwy2K/oKb+pIGwwMdM8wYmnZqiKPSOT+2t2s X-Received: by 2002:a17:906:ca91:b0:70d:52ca:7e7d with SMTP id js17-20020a170906ca9100b0070d52ca7e7dmr30530612ejb.552.1654777848486; Thu, 09 Jun 2022 05:30:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654777848; cv=none; d=google.com; s=arc-20160816; b=Zg/nMpUil0z+OtzT87TdMoyJA/SyJ8t3nZ8XFFiBkO4Izm4LLMngTMP3FDd88+m36N 5+uPiYf30wA77kNHfZsMWXe4JakM5mj7I64hyPLQXogE5q4imOe2LddI4WT/XxB70lKF ZfwJYV8BfGE5HxCpJfwjgPNpE8Bsk7hOkEwZD/X0q6lBvgkPJW1gfAM2EPG/dpKH5qZC y/uB4kTMONVPzJvwbmB3wqmFTAsEdGumQ+xCA98ehmbJA04yecbEwWkQxRfMfhTfNlex wPLrC5k0AFocJ+ybnH2LW4WgCBN9E2AEKU8OdFNmy0kEUQ85COhKOKuNm0cgBxquLU6y kFQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=B/M5r4B90mbTkpJzac0CttJDuKdqEl0o8kb84irHerE=; b=Ztw7ZYm+fTd0641Pt4g8Dqydo9dM5klvxfoWK+F/7QRlDOD0VVBlhGC2ExCAIjxtr2 5idsiJCidzN+UAEeiRkFQFQxUT3sAcKeAss99N9Q13Np0OhVszzVno5fVoBS3VvRQNaF MujYJb/H3g82fM4F4Hw5Ei7QGwyKHMyhSrm27Dnv8NPenqPBjnzYkiZeMbBK2myllnV2 rmGRjtvVjp9QiWBezD2vjsW6/drJ5YQfPc0fj4GtO/pqVQ/wbQBLjytg3dAn2y7cytct 94vK7jHbtKmisEtKzN24bbKEfCWJpZsvfKOw1CXpRow/ifVKz+hcstyzgxl7X06VSZmW Ieuw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@protonic.nl header.s=202111 header.b=o6HzE3W8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b6-20020a056402278600b0042b756321f5si3085847ede.104.2022.06.09.05.30.13; Thu, 09 Jun 2022 05:30:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@protonic.nl header.s=202111 header.b=o6HzE3W8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242744AbiFIMOW (ORCPT + 99 others); Thu, 9 Jun 2022 08:14:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56628 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237776AbiFIMOU (ORCPT ); Thu, 9 Jun 2022 08:14:20 -0400 Received: from smtp28.bhosted.nl (smtp28.bhosted.nl [IPv6:2a02:9e0:8000::40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 77B3F31504 for ; Thu, 9 Jun 2022 05:14:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonic.nl; s=202111; h=content-transfer-encoding:mime-version:message-id:date:subject:cc:to:from: from; bh=B/M5r4B90mbTkpJzac0CttJDuKdqEl0o8kb84irHerE=; b=o6HzE3W8Ij5m1xQnFcTjjKq6lS2+eQtAgGGHD6sihovrqSSJJnxOzaQglQ4byIMoZwkIAbd7RGY5g 37hFqf6tCbEURL8luaZQG2i89IQVM0owNP2JV/hT5o5ZVOU0ncfJZRnSCLLRsZYkJrJzAsx4dDOe7m /UNeWQcZr4imCE6RaYw+Bs15bDAepftCPzslHmzXnxdYe9XFTPvA8zHbX87ZuwcsCdhpS+xz6PkWVd mVOMga98vqwcYxw52YXTNk53FxncTVGYm1109IKlHIxG+h0sdTip7HFeX+nhMa13hxiic1x5mwwv98 rW9c19Zlw6CJRuAclIa6PFb6y0RVITA== X-MSG-ID: ae2393fb-e7ed-11ec-a2aa-0050569d11ae From: David Jander To: Mark Brown Cc: linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org, Linux Next Mailing List , Marc Kleine-Budde , David Jander Subject: [PATCH] spi: Fix per-cpu stats access on 32 bit systems Date: Thu, 9 Jun 2022 14:13:34 +0200 Message-Id: <20220609121334.2984808-1-david@protonic.nl> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 32 bit systems, the following kernel BUG is hit: BUG: using smp_processor_id() in preemptible [00000000] code: swapper/0/1 caller is debug_smp_processor_id+0x18/0x24 CPU: 1 PID: 1 Comm: swapper/0 Not tainted 5.19.0-rc1-00001-g6ae0aec8a366 #181 Hardware name: Freescale i.MX6 Quad/DualLite (Device Tree) Backtrace: dump_backtrace from show_stack+0x20/0x24 r7:81024ffd r6:00000000 r5:81024ffd r4:60000013 show_stack from dump_stack_lvl+0x60/0x78 dump_stack_lvl from dump_stack+0x14/0x1c r7:81024ffd r6:80f652de r5:80bec180 r4:819a2500 dump_stack from check_preemption_disabled+0xc8/0xf0 check_preemption_disabled from debug_smp_processor_id+0x18/0x24 r8:8119b7e0 r7:81205534 r6:819f5c00 r5:819f4c00 r4:c083d724 debug_smp_processor_id from __spi_sync+0x78/0x220 __spi_sync from spi_sync+0x34/0x4c r10:bb7bf4e0 r9:c083d724 r8:00000007 r7:81a068c0 r6:822a83c0 r5:c083d724 r4:819f4c00 spi_sync from spi_mem_exec_op+0x338/0x370 r5:000000b4 r4:c083d910 spi_mem_exec_op from spi_nor_read_id+0x98/0xdc r10:bb7bf4e0 r9:00000000 r8:00000000 r7:00000000 r6:00000000 r5:82358040 r4:819f7c40 spi_nor_read_id from spi_nor_detect+0x38/0x114 r7:82358040 r6:00000000 r5:819f7c40 r4:819f7c40 spi_nor_detect from spi_nor_scan+0x11c/0xbec r10:bb7bf4e0 r9:00000000 r8:00000000 r7:c083da4c r6:00000000 r5:00010101 r4:819f7c40 spi_nor_scan from spi_nor_probe+0x10c/0x2d0 r10:bb7bf4e0 r9:bb7bf4d0 r8:00000000 r7:819f4c00 r6:00000000 r5:00000000 r4:819f7c40 per-cpu access needs to be guarded against preemption. Fixes: 6598b91b5ac3 ("spi: spi.c: Convert statistics to per-cpu u64_stats_t") Reported-by: Marc Kleine-Budde Signed-off-by: David Jander --- drivers/spi/spi.c | 5 ++++- include/linux/spi/spi.h | 10 ++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index d94822bf3cec..ac61824b87b5 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -314,11 +314,13 @@ static void spi_statistics_add_transfer_stats(struct spi_statistics *pcpu_stats, struct spi_controller *ctlr) { int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1; - struct spi_statistics *stats = this_cpu_ptr(pcpu_stats); + struct spi_statistics *stats; if (l2len < 0) l2len = 0; + get_cpu(); + stats = this_cpu_ptr(pcpu_stats); u64_stats_update_begin(&stats->syncp); u64_stats_inc(&stats->transfers); @@ -333,6 +335,7 @@ static void spi_statistics_add_transfer_stats(struct spi_statistics *pcpu_stats, u64_stats_add(&stats->bytes_rx, xfer->len); u64_stats_update_end(&stats->syncp); + put_cpu(); } /* diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h index 2e63b4935deb..c96f526d9a20 100644 --- a/include/linux/spi/spi.h +++ b/include/linux/spi/spi.h @@ -84,18 +84,24 @@ struct spi_statistics { #define SPI_STATISTICS_ADD_TO_FIELD(pcpu_stats, field, count) \ do { \ - struct spi_statistics *__lstats = this_cpu_ptr(pcpu_stats); \ + struct spi_statistics *__lstats; \ + get_cpu(); \ + __lstats = this_cpu_ptr(pcpu_stats); \ u64_stats_update_begin(&__lstats->syncp); \ u64_stats_add(&__lstats->field, count); \ u64_stats_update_end(&__lstats->syncp); \ + put_cpu(); \ } while (0) #define SPI_STATISTICS_INCREMENT_FIELD(pcpu_stats, field) \ do { \ - struct spi_statistics *__lstats = this_cpu_ptr(pcpu_stats); \ + struct spi_statistics *__lstats; \ + get_cpu(); \ + __lstats = this_cpu_ptr(pcpu_stats); \ u64_stats_update_begin(&__lstats->syncp); \ u64_stats_inc(&__lstats->field); \ u64_stats_update_end(&__lstats->syncp); \ + put_cpu(); \ } while (0) /** -- 2.32.0