Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp224016imm; Thu, 30 Aug 2018 21:43:39 -0700 (PDT) X-Google-Smtp-Source: ANB0VdbISE3cmUCe4pXfEiXoxbmgi38QKgM1SFik7n3DgSEFqiYBdaPogv5qeIEjRS7Ixddnt3NJ X-Received: by 2002:a63:4e5f:: with SMTP id o31-v6mr12727315pgl.256.1535690619298; Thu, 30 Aug 2018 21:43:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535690619; cv=none; d=google.com; s=arc-20160816; b=MLFTDceFx3xyFkBTFRPy1uTMd7AlZkqZksRvP+rHdy+eXoFQmwYxOL8sz/7voXMwab EuVeN5NmMt9CoR0Xz7b7KqI/tLbJH9ODxMZgzhAzJfhQsa0oZf+QjRbxVrfhVk9vJ3mX kYqR/rPnoU93UKOLv68j3XvHuFaDBtareyokleJK8KlKTq/fLLlXaPmZksxssR1r2+dD /6FaMFkHuqcqwq1P6tbQL5WU4D4XPzgTf+1sRs1zz3Hti//6jIxcWSyDSPslbEFJU2Cr nc7SIQlCrI99JqgAQvP/GX6JmDpSq8k2oKPLAm4uNNQ3CTLaipKhBVk6OADRO4pWssdR LsDg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature:arc-authentication-results; bh=9gGqncCFabn0oFQmIJqmSppBBtUkYXvwnrdk4BdtDQM=; b=WcPXmsyEVep+x9UXKHZdgmIhHssaGxwcuOHCIaUL7CeEXYMApQl5QsU52QbA4GmCo0 eGyUWfc8QSZgHqJVqG+B8F56TjT/PhhpO+9OeB/Oa77oxm4hFZNFBSCFoEB4NF2Zk9id HINaWt9h21/965kp/TEIJX7W6PoIF19d2nttG3hdhhCaT5yfdtjjRyDLm3rqycxe0Rwu 6WLMC7jwPfHUVYAAnxcGEAsHRwOIfJ2n8qteGA9E7EfHdAEN87dhrRwg0hzEdsqQoW8l rK2HHAPVG8sP7O/dTfHwDcAEAcy/uTJnHRq/OLYmSSBZca/oMZNLejn8M8Kn9QUNqqdU hWtg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=KR5X8VI7; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id s26-v6si8507527pge.339.2018.08.30.21.43.24; Thu, 30 Aug 2018 21:43:39 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=KR5X8VI7; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1727230AbeHaIrx (ORCPT + 99 others); Fri, 31 Aug 2018 04:47:53 -0400 Received: from mail.kernel.org ([198.145.29.99]:34750 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726660AbeHaIrx (ORCPT ); Fri, 31 Aug 2018 04:47:53 -0400 Received: from devnote (sp183-74-193-25.msb.spmode.ne.jp [183.74.193.25]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 34A9B20837; Fri, 31 Aug 2018 04:42:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1535690537; bh=NZ/cKQccOjbKKUMEnznqnoTg37Rvko16VmtKY+gp6pk=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=KR5X8VI7quvVLGDYA1QMUDAlBWP4mhyQ3Xv7h1ox9zZJhgZ00CgduIcP0wugGa1fB uMacmT0YTiMMP/A1y7YpUnVYw5k79tBG908XyyoSJvuAFs3GzCw+Vs0rQzsCXw4RaW TVoMXIdmKxMhqjj8rsIEoMaAYoipu66cYwvyjnqA= Date: Fri, 31 Aug 2018 13:42:10 +0900 From: Masami Hiramatsu To: Andy Lutomirski Cc: Andy Lutomirski , Nadav Amit , Thomas Gleixner , LKML , Ingo Molnar , X86 ML , Arnd Bergmann , linux-arch , Kees Cook , Peter Zijlstra Subject: Re: [RFC PATCH 2/6] x86/mm: temporary mm struct Message-Id: <20180831134210.0cdadcff14ba063fa0d93abd@kernel.org> In-Reply-To: References: <20180829081147.184610-1-namit@vmware.com> <20180829081147.184610-3-namit@vmware.com> <20180829184925.64caee4dadf705080373f84f@kernel.org> <20180830103859.ac309b71c53d267da2f6e9f8@kernel.org> X-Mailer: Sylpheed 3.5.0 (GTK+ 2.24.30; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 29 Aug 2018 18:59:52 -0700 Andy Lutomirski wrote: > > > > On Aug 29, 2018, at 6:38 PM, Masami Hiramatsu wrote: > > > > On Wed, 29 Aug 2018 08:41:00 -0700 > > Andy Lutomirski wrote: > > > >>> On Wed, Aug 29, 2018 at 2:49 AM, Masami Hiramatsu wrote: > >>> On Wed, 29 Aug 2018 01:11:43 -0700 > >>> Nadav Amit wrote: > >>> > >>>> From: Andy Lutomirski > >>>> > >>>> Sometimes we want to set a temporary page-table entries (PTEs) in one of > >>>> the cores, without allowing other cores to use - even speculatively - > >>>> these mappings. There are two benefits for doing so: > >>>> > >>>> (1) Security: if sensitive PTEs are set, temporary mm prevents their use > >>>> in other cores. This hardens the security as it prevents exploding a > >>>> dangling pointer to overwrite sensitive data using the sensitive PTE. > >>>> > >>>> (2) Avoiding TLB shootdowns: the PTEs do not need to be flushed in > >>>> remote page-tables. > >>>> > >>>> To do so a temporary mm_struct can be used. Mappings which are private > >>>> for this mm can be set in the userspace part of the address-space. > >>>> During the whole time in which the temporary mm is loaded, interrupts > >>>> must be disabled. > >>>> > >>>> The first use-case for temporary PTEs, which will follow, is for poking > >>>> the kernel text. > >>>> > >>>> [ Commit message was written by Nadav ] > >>>> > >>>> Cc: Andy Lutomirski > >>>> Cc: Masami Hiramatsu > >>>> Cc: Kees Cook > >>>> Cc: Peter Zijlstra > >>>> Signed-off-by: Nadav Amit > >>>> --- > >>>> arch/x86/include/asm/mmu_context.h | 20 ++++++++++++++++++++ > >>>> 1 file changed, 20 insertions(+) > >>>> > >>>> diff --git a/arch/x86/include/asm/mmu_context.h b/arch/x86/include/asm/mmu_context.h > >>>> index eeeb9289c764..96afc8c0cf15 100644 > >>>> --- a/arch/x86/include/asm/mmu_context.h > >>>> +++ b/arch/x86/include/asm/mmu_context.h > >>>> @@ -338,4 +338,24 @@ static inline unsigned long __get_current_cr3_fast(void) > >>>> return cr3; > >>>> } > >>>> > >>>> +typedef struct { > >>>> + struct mm_struct *prev; > >>>> +} temporary_mm_state_t; > >>>> + > >>>> +static inline temporary_mm_state_t use_temporary_mm(struct mm_struct *mm) > >>>> +{ > >>>> + temporary_mm_state_t state; > >>>> + > >>>> + lockdep_assert_irqs_disabled(); > >>>> + state.prev = this_cpu_read(cpu_tlbstate.loaded_mm); > >>>> + switch_mm_irqs_off(NULL, mm, current); > >>>> + return state; > >>>> +} > >>> > >>> Hmm, why don't we return mm_struct *prev directly? > >> > >> I did it this way to make it easier to add future debugging stuff > >> later. Also, when I first wrote this, I stashed the old CR3 instead > >> of the old mm_struct, and it seemed like callers should be insulated > >> from details like this. > > > > Hmm, I see. But in that case, we should call it "struct temporary_mm" > > and explicitly allocate (and pass) it, since we can not return the > > data structure from stack. > > Why not? Ah, ok as far as it returns a data structure as immediate value. (I don't recommend it because it hides a copy..) > > > If we can combine it with new mm, it will > > be more encapsulated e.g. > > > > struct temporary_mm { > > struct mm_struct *mm; > > struct mm_struct *prev; > > }; > > > > static struct temporary_mm poking_tmp_mm; > > > > poking_init() > > { > > if (init_temporary_mm(&tmp_mm, &init_mm)) > > goto error; > > ... > > } > > > > text_poke_safe() > > { > > ... > > use_temporary_mm(&tmp_mm); > > ... > > unuse_temporary_mm(&tmp_mm); > > } > > > > Any thought? > > That seems more complicated for not very much gain. Hmm, OK. anyway that is just a style note. The code itself looks good for me. Thank you, > -- Masami Hiramatsu