Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp3960373pxb; Tue, 17 Nov 2020 07:56:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJy7ohX79jGNByPhhDrtoeQnAA86Ptm5kIyHWHr511u6KgllRpfuMAe5QiPUJjYEdnStDi3r X-Received: by 2002:a50:ab07:: with SMTP id s7mr22054657edc.287.1605628597786; Tue, 17 Nov 2020 07:56:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605628597; cv=none; d=google.com; s=arc-20160816; b=iwvdvS7YI898LQo3V578Vx8ZQbXw4Bj2oTnt+VM+KDSocZak/af1xVdaUx3fTaQ4qP 5gqb2bnArg5UmrOFpAM2i1s6kkhEg+M/dJlDPxvgGevifB3jKAMoj5Hattiie2KWtOrT C3vxqbe3e/lJdmfAUem2rUZtfM5hQS6Ab12XEvtUoAEMJG8qqSFnt1d9nDgO+EzeHfgJ jrUcx99Y1PpBkGsBh6xjiRgkTqDwA11+0PM9GR0DI1y3PT5f6JApE+Qso9ghaMfwvCSW Ng14stTS+GsWEpfwLCIFZa17qZ3nGr8xrfB6+VL8WQsnt3Ry0RVjsbyVBEX1Rg5VCoAp NXQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=tLyx1dfyk686DXFi013paneo9j26TTj1I2YPdqwxgJY=; b=OB7xU06GS+CCTnq9GY/NTGg6zlbu7DApypTpA/378jqJ4vm3nR7KVzndY9BEty9gSG M4SynkF4K3hD5wf8AOzVbwoRXQZpIBOljSA4GRAdFVsWuXvdPrjqPetAB+LjsdRS5NDe ph6G4aZg6CFPa4nAcnJPp52c8w4wZyEwNrog8cl+fusn5fKCnN6DmK95MYXJItnzhLH1 vpGaUOqsoo5QaRXFBA8iIK/9FhFY6rqnFEN//8EphvavBBi9kOhuOTl6VghLQ6u2nhZT zCsnHK3dVAdrOKa/k7afeKKCFg06IyGgc7Mk2gq2/7WI1urgzz9IZc1lQnP9B2yjWZc+ crGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=wng07msA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id qp24si1864181ejb.526.2020.11.17.07.56.14; Tue, 17 Nov 2020 07:56:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=wng07msA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726524AbgKQPwv (ORCPT + 99 others); Tue, 17 Nov 2020 10:52:51 -0500 Received: from mail.kernel.org ([198.145.29.99]:60768 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726498AbgKQPwv (ORCPT ); Tue, 17 Nov 2020 10:52:51 -0500 Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E5B7C246A4 for ; Tue, 17 Nov 2020 15:52:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1605628370; bh=n5hKtnSFbRkV9Ne7F3tzTOQSjkjOGG9/Snmw1ldMllw=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=wng07msAE8++JfieGqzZYgVw6nSQ52Zny/os6+FInAAdBYxt737ime6etV+2z58Yk jZDkSmOz9r0h4poU7EmRmoS/9HB+AjshlaMerJZMpWBTUTZq9EEGY83tFGTyH1KtBf 76ekIZR7dJDSBr5gAbBWlYjcbsQ2EU8hryh4m3jg= Received: by mail-wr1-f54.google.com with SMTP id r17so23695013wrw.1 for ; Tue, 17 Nov 2020 07:52:49 -0800 (PST) X-Gm-Message-State: AOAM5304KIDee2ICLdmn3F0je6oUNEKLCydB99WF7fgqrZ+8fN5BHKEf jWpNa5IHVS1Yoi5oiUwIrsx9XpmVlb2VMx+Mq82WAQ== X-Received: by 2002:a5d:5482:: with SMTP id h2mr204314wrv.18.1605628368294; Tue, 17 Nov 2020 07:52:48 -0800 (PST) MIME-Version: 1.0 References: <20201116144757.1920077-1-alexandre.chartre@oracle.com> <20201116144757.1920077-13-alexandre.chartre@oracle.com> In-Reply-To: From: Andy Lutomirski Date: Tue, 17 Nov 2020 07:52:33 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [RFC][PATCH v2 12/21] x86/pti: Use PTI stack instead of trampoline stack To: Alexandre Chartre Cc: Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , X86 ML , Dave Hansen , Peter Zijlstra , LKML , Tom Lendacky , Joerg Roedel , Konrad Rzeszutek Wilk , jan.setjeeilers@oracle.com, Junaid Shahid , oweisse@google.com, Mike Rapoport , Alexander Graf , mgross@linux.intel.com, kuzuno@gmail.com Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Nov 17, 2020 at 7:07 AM Alexandre Chartre wrote: > > > > On 11/16/20 7:34 PM, Andy Lutomirski wrote: > > On Mon, Nov 16, 2020 at 10:10 AM Alexandre Chartre > > wrote: > >> > >> > >> On 11/16/20 5:57 PM, Andy Lutomirski wrote: > >>> On Mon, Nov 16, 2020 at 6:47 AM Alexandre Chartre > >>> wrote: > >>>> > >>>> When entering the kernel from userland, use the per-task PTI stack > >>>> instead of the per-cpu trampoline stack. Like the trampoline stack, > >>>> the PTI stack is mapped both in the kernel and in the user page-table. > >>>> Using a per-task stack which is mapped into the kernel and the user > >>>> page-table instead of a per-cpu stack will allow executing more code > >>>> before switching to the kernel stack and to the kernel page-table. > >>> > >>> Why? > >> > >> When executing more code in the kernel, we are likely to reach a point > >> where we need to sleep while we are using the user page-table, so we need > >> to be using a per-thread stack. > >> > >>> I can't immediately evaluate how nasty the page table setup is because > >>> it's not in this patch. > >> > >> The page-table is the regular page-table as introduced by PTI. It is just > >> augmented with a few additional mapping which are in patch 11 (x86/pti: > >> Extend PTI user mappings). > >> > >>> But AFAICS the only thing that this enables is sleeping with user pagetables. > >> > >> That's precisely the point, it allows to sleep with the user page-table. > >> > >>> Do we really need to do that? > >> > >> Actually, probably not with this particular patchset, because I do the page-table > >> switch at the very beginning and end of the C handler. I had some code where I > >> moved the page-table switch deeper in the kernel handler where you definitively > >> can sleep (for example, if you switch back to the user page-table before > >> exit_to_user_mode_prepare()). > >> > >> So a first step should probably be to not introduce the per-task PTI trampoline stack, > >> and stick with the existing trampoline stack. The per-task PTI trampoline stack can > >> be introduced later when the page-table switch is moved deeper in the C handler and > >> we can effectively sleep while using the user page-table. > > > > Seems reasonable. > > > > I finally remember why I have introduced a per-task PTI trampoline stack right now: > that's to be able to move the CR3 switch anywhere in the C handler. To do so, we need > a per-task stack to enter (and return) from the C handler as the handler can potentially > go to sleep. > > Without a per-task trampoline stack, we would be limited to call the switch CR3 functions > from the assembly entry code before and after calling the C function handler (also called > from assembly). The noinstr part of the C entry code won't sleep. --Andy