Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp5346738rwb; Wed, 21 Sep 2022 06:40:34 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6Y/ixen1zS4xEcVGmEw23CnuqAfSLRWTTcLAlz3mZhE3ncMpp0UfkNuw3PFPhy4xSxYLTF X-Received: by 2002:a05:6402:50d1:b0:452:899e:77c with SMTP id h17-20020a05640250d100b00452899e077cmr24907421edb.0.1663767633994; Wed, 21 Sep 2022 06:40:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663767633; cv=none; d=google.com; s=arc-20160816; b=q71XdnMEXxMU+7jI0p/ujrnq1MbWUxBQv4MGWjRna3ZD53wdKrS7XwpcLHF258ifUf hsqHtGx/WV84k93/C/mz8adOl4ddP3gFtxiPsjMzX59SxVcB8512JIHUJVFbE3DktLYn Zk8KlFxlTNcGz3Zbpqtafgts+XuAiT1tno1GIJX6o++xUva3YD4dQBbPfQ9mUeX4nP7Y udh5uYZYnPG7edCK5uvaEizUO4V4aWfK4nwC9WkCGdP7oGJmjBDunPQ1ZaxyQBDIKTAb RUapQ8aorK55cSQmpvjKC6IKWvx3nnSIqWLX+0srcYlU1cC7W9390mW6yQsVWlaZnPpo EsqA== 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:references:in-reply-to:subject:cc:to:from :dkim-signature; bh=QT/Sl5AvXoUzaa7Mvmu4tMi+mbhfGfOR2s8x//+T2VY=; b=KZcyLhnIrOGpzf0TSsLsKK7/nHADKqRcLG7rN1zg1nmdG/fQHR+EHNfU4S7M5d9wDD wjqZXRqOBsQ4OHE/Zl6ROTquijJPcdhjLzfPlxaMswf5bC7agZyzle7LQixx0ZtkjBdS /BgpAdthnrZCoAVjaK8dV1qK1MTnNQJ0+mQaipH8nIzBgIx9vcqcQq++HC7Il79GDwi6 CJ6DR8XkmDInUp//i4Jzjusj38w3zRZdPJcE0uTJQ09AZwEyeTkVnp88wgcaqQLd7jcM GBcz3H2Ju3yQpHH1NDvCpZgd/wAC8Z55zW/r4gz/rXuzxLByw6uEOFdrrqTd44CzkY0C whLg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ellerman.id.au header.s=201909 header.b=hjA4SvFW; 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 y29-20020a50ce1d000000b004477cac2be2si2543967edi.242.2022.09.21.06.40.04; Wed, 21 Sep 2022 06:40:33 -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=@ellerman.id.au header.s=201909 header.b=hjA4SvFW; 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 S229745AbiIUNA3 (ORCPT + 99 others); Wed, 21 Sep 2022 09:00:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45742 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229496AbiIUNA2 (ORCPT ); Wed, 21 Sep 2022 09:00:28 -0400 Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BDB248A7DD for ; Wed, 21 Sep 2022 06:00:25 -0700 (PDT) Received: from authenticated.ozlabs.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mail.ozlabs.org (Postfix) with ESMTPSA id 4MXdn0035Tz4xG9; Wed, 21 Sep 2022 23:00:19 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ellerman.id.au; s=201909; t=1663765221; bh=QT/Sl5AvXoUzaa7Mvmu4tMi+mbhfGfOR2s8x//+T2VY=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=hjA4SvFWM/5Cex+dNXi9ZS8vBM8YsfJ2esWTPctRWZuPxP8Zr1thsxjaxFiUVdJbm cYkKXj8QJq3eUhadLV8HDf3qMWwXdhtDyNX5yEPJj46HF7MuP5sZE9GQVUtIlJRJFY GAuTf0KYAfy1F09+xy3KD8PC+JMCyd6LSW5Opsbt2Ha7fkxRYPwqsj/KxSp7Rtt3w5 CiwzFpaE3ybt+1ljMDmHxoDaU7Qgnb6nZWlVXCbNV8wwBwjEki6CwtCRlQ75+Rnmnh jISwtL+dfAdsZDPV7EIQe/JjoUdVrHXW93wgz+QCX4S7Oq662PaU/hP58nEUYjFy9/ pgxXLkacus4vw== From: Michael Ellerman To: Christophe Leroy , Samuel Holland , Nicholas Piggin Cc: "Eric W. Biederman" , Kees Cook , Russell Currey , "linux-kernel@vger.kernel.org" , "linuxppc-dev@lists.ozlabs.org" Subject: Re: [PATCH] powerpc: Save AMR/IAMR when switching tasks In-Reply-To: <9a433048-ab0d-6d57-7aa8-c9acbe7b7a99@csgroup.eu> References: <20220916050515.48842-1-samuel@sholland.org> <89049105-64fc-8d5b-d090-2841064786d1@csgroup.eu> <87h713leu8.fsf@mpe.ellerman.id.au> <9a433048-ab0d-6d57-7aa8-c9acbe7b7a99@csgroup.eu> Date: Wed, 21 Sep 2022 23:00:16 +1000 Message-ID: <87a66s287z.fsf@mpe.ellerman.id.au> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable 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 autolearn=ham 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 Christophe Leroy writes: > Le 19/09/2022 =C3=A0 14:37, Michael Ellerman a =C3=A9crit=C2=A0: >> Christophe Leroy writes: >>> Le 16/09/2022 =C3=A0 07:05, Samuel Holland a =C3=A9crit=C2=A0: >>>> With CONFIG_PREEMPT=3Dy (involuntary preemption enabled), it is possib= le >>>> to switch away from a task inside copy_{from,to}_user. This left the C= PU >>>> with userspace access enabled until after the next IRQ or privilege >>>> level switch, when AMR/IAMR got reset to AMR_KU[AE]P_BLOCKED. Then, wh= en >>>> switching back to the original task, the userspace access would fault: >>> >>> This is not supposed to happen. You never switch away from a task >>> magically. Task switch will always happen in an interrupt, that means >>> copy_{from,to}_user() get interrupted. >>=20 >> Unfortunately this isn't true when CONFIG_PREEMPT=3Dy. > > Argh, yes, I wrote the above with the assumption that we properly follow= =20 > the main principles that no complex fonction is to be used while KUAP is= =20 > open ... Which is apparently not true here. x86 would have detected it=20 > with objtool, but we don't have it yet in powerpc. Yes and yes :/ >> We can switch away without an interrupt via: >> __copy_tofrom_user() >> -> __copy_tofrom_user_power7() >> -> exit_vmx_usercopy() >> -> preempt_enable() >> -> __preempt_schedule() >> -> preempt_schedule() >> -> preempt_schedule_common() >> -> __schedule() > > > Should we use preempt_enable_no_resched() to avoid that ? Good point :) ... >>=20 >> Still I think it might be our best option for an easy fix. > > Wouldn't it be even easier and less abusive to use=20 > preemt_enable_no_resched() ? Or is there definitively a good reason to=20 > resched after a VMX copy while we don't with regular copies ? I don't think it's important to reschedule there. I guess it means another task that wants to preempt will have to wait a little longer, but I doubt it's measurable. One reason to do the KUAP lock is it also protects us from running disable_kernel_altivec() with KUAP unlocked. That in turn calls into msr_check_and_clear() and __msr_check_and_clear(), none of which is a problem per-se, but we'd need to make that all notrace to be 100% safe. At the moment we're running that all with KUAP unlocked anyway, so using preempt_enable_no_resched() would fix the actual bug and is much nicer than my patch, so we should probably do that. We can look at making disable_kernel_altivec() etc. notrace as a follow-up. cheers