From mboxrd@z Thu Jan 1 00:00:00 1970 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 48FC6C636EA for ; Wed, 28 Aug 2024 17:15:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: Content-Type:In-Reply-To:From:References:Cc:To:Subject:MIME-Version:Date: Message-ID:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=ZcWjsx2GFVbGR3sFLDnaT6Mg3/3VJFqnuVHgVecU75g=; b=H7/sbg3XIv3jo84CdLnb4irCLG 4UT2SDHXqh72TeELZuibGSLk1dctgUK3W3cP4iE/OQVBMCDnasgSs59md+wt2caAmujHNITbJb2OF PoEwXqG0iSjpnKzFFk7qmTW9F4fwyCYPzPRmelBLF/N5fflETcpaC2NBGqMDD/+BAvhChdqX67/gs OCoE6AErizyWH4P63faCor7YjptelUFdKgTWSYMAl6CVJRPRFbSTFEF4zqJDmD9Cz+zfpdpnBJO/h uegZ763UkG8ELgIyIzbEGGUWupQTQKezWUuw0czl0tBoT0tpslB4SQS7fq7t6Le+QNaVU92F731ZQ U7aBI8HQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sjMFv-0000000GKBu-3fKv; Wed, 28 Aug 2024 17:14:51 +0000 Received: from mail-pf1-x42c.google.com ([2607:f8b0:4864:20::42c]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sjMF5-0000000GJuY-0WX5 for linux-arm-kernel@lists.infradead.org; Wed, 28 Aug 2024 17:14:00 +0000 Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-7142a93ea9cso5092961b3a.3 for ; Wed, 28 Aug 2024 10:13:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1724865237; x=1725470037; darn=lists.infradead.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=ZcWjsx2GFVbGR3sFLDnaT6Mg3/3VJFqnuVHgVecU75g=; b=YVIVmMW3ZvLB8FGavadD4WaR8JKshxglSw9+hZXtg+X70HWnBJZ9N86JUoe96BhrQo sScQCRwrMV6cRqUwLjzALY9/T/tCU/p8g5EINO7XOMrHx8zQYxOuhlUVlBjmyTCCKVft rJAbrRBLhsdapW28PffjvUGh2tHYNO0DnFr+c2IZXzeXkwSLkuicM0a4UtPKgkpOaKc1 6CtAC6TXdZ/r1NdXrNuZEn73kGm1PIJm2MungGZ1KUYHciO3iyrVciixkmZqQj25sMNJ 6/fz4GyMYsL0BhU0gqzLAye6DdFagD2iEC3lwxwJlM+4Vtm52vWnbHmUI6qCn7zOB9ol z9aQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724865237; x=1725470037; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ZcWjsx2GFVbGR3sFLDnaT6Mg3/3VJFqnuVHgVecU75g=; b=G/y++R/95jgrDdIy7MW4CRIZo7CLKjQAvPRFEArqzGV26Zi559bs0jwc+vs6QhmsRg JM2cfxotvYztmhmAqrX3v9qoh/xDSawUxSVwLx/TQ1DcjzNpF2CDb3o31melTxb/ByVs iDMfNaTPG8CVq2vSsEofmIN3LBeNjkspdjYy6a+sedRkHm0fsco3+L55diYcbYefBtQs j8kHT3wWDdRp2o1IXBl9tClU9WPiU4QsC9KTAhC8Il+zVT2e7xLoW3ztYvz4Ss3gSqrK ZUgSCoJi3DZAZnatJL88TBj1GOG+dRBy8VPDUvuQwx2uESErSgX56JdAhUy8Q5iHomX9 y6Iw== X-Forwarded-Encrypted: i=1; AJvYcCW9Kloqyb6QsHiBg9y9lE/CAFYYcwdpQJ4IaauK7XUczqIlyfWrR5cojBqpyxYcnvM35AJEX5LVyCMD423j81QQ@lists.infradead.org X-Gm-Message-State: AOJu0YzcR9Woo7XALEGmDpVHx+wvDeAvEdDBJRwLER3rF0pEIjVTm/fD /mBmunpCZTtGECJ43b5blajqq6AsUw9QZsV4iQYiJ2fVBZBwy52rlbgUstjG8Hc= X-Google-Smtp-Source: AGHT+IG95WpJ3Jo/yJiFE1+k5pgv7fknE0kbuwHFJ+rJt/S10BTiSVq+7xHMaRQYqpPuPYgiaBY1xg== X-Received: by 2002:aa7:88c5:0:b0:70d:3777:da8b with SMTP id d2e1a72fcca58-715dfc43e15mr166538b3a.25.1724865237485; Wed, 28 Aug 2024 10:13:57 -0700 (PDT) Received: from [192.168.1.74] (c-73-42-218-101.hsd1.wa.comcast.net. [73.42.218.101]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-714342e2c83sm10342909b3a.133.2024.08.28.10.13.56 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 28 Aug 2024 10:13:57 -0700 (PDT) Message-ID: <526b1130-afec-4c75-8d86-74c5b7e272fe@rivosinc.com> Date: Wed, 28 Aug 2024 10:13:54 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH -fixes] drivers: perf: Fix smp_processor_id() use in preemptible code To: Alexandre Ghiti , Will Deacon Cc: Atish Patra , Anup Patel , Mark Rutland , Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-riscv@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Nam Cao References: <20240826165210.124696-1-alexghiti@rivosinc.com> <20240827125335.GD4772@willie-the-truck> Content-Language: en-US From: Atish Patra In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240828_101359_363848_30C9FDA3 X-CRM114-Status: GOOD ( 26.99 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On 8/28/24 5:36 AM, Alexandre Ghiti wrote: > Hi Will, > > On Tue, Aug 27, 2024 at 2:53 PM Will Deacon wrote: >> >> On Mon, Aug 26, 2024 at 06:52:10PM +0200, Alexandre Ghiti wrote: >>> As reported in [1], the use of smp_processor_id() in >>> pmu_sbi_device_probe() must be protected by disabling the preemption, so >>> simple use get_cpu()/put_cpu() instead. >>> >>> Reported-by: Nam Cao >>> Closes: https://lore.kernel.org/linux-riscv/20240820074925.ReMKUPP3@linutronix.de/ [1] >>> Signed-off-by: Alexandre Ghiti >>> --- >>> drivers/perf/riscv_pmu_sbi.c | 7 ++++++- >>> 1 file changed, 6 insertions(+), 1 deletion(-) >>> >>> diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c >>> index 31a17a56eb3b..25b1b699b3e2 100644 >>> --- a/drivers/perf/riscv_pmu_sbi.c >>> +++ b/drivers/perf/riscv_pmu_sbi.c >>> @@ -1373,11 +1373,15 @@ static int pmu_sbi_device_probe(struct platform_device *pdev) >>> >>> /* SBI PMU Snapsphot is only available in SBI v2.0 */ >>> if (sbi_v2_available) { >>> + int cpu; >>> + >>> ret = pmu_sbi_snapshot_alloc(pmu); >>> if (ret) >>> goto out_unregister; >>> >>> - ret = pmu_sbi_snapshot_setup(pmu, smp_processor_id()); >>> + cpu = get_cpu(); >>> + >>> + ret = pmu_sbi_snapshot_setup(pmu, cpu); >>> if (ret) { >>> /* Snapshot is an optional feature. Continue if not available */ >>> pmu_sbi_snapshot_free(pmu); >>> @@ -1391,6 +1395,7 @@ static int pmu_sbi_device_probe(struct platform_device *pdev) >>> */ >>> static_branch_enable(&sbi_pmu_snapshot_available); >>> } >>> + put_cpu(); >> >> Are you sure it's safe to enable the static key with preemption disabled? >> I thought that could block on a mutex. > Thanks Will for pointing that out. > Yep, it seems you're right, thanks for jumping in. > > I'm discussing with Atish how to fix that differently, I'll be back > with another version very soon. > Looking at the driver core framework code, I am wondering if a probe function can be preempted to run on a different cpu. If it can only be preempted by higher priority kernel threads or interrupts but is guaranteed to run on the same cpu again, we can just use the raw_smp_processor_id. However, if there is no guarantee then we can just invoke get_cpu/put_cpu around pmu_sbi_snapshot_setup. > Thanks again, > > Alex > >> >> Will > > _______________________________________________ > linux-riscv mailing list > linux-riscv@lists.infradead.org > http://lists.infradead.org/mailman/listinfo/linux-riscv