Received: by 10.223.164.202 with SMTP id h10csp15953wrb; Mon, 6 Nov 2017 01:37:36 -0800 (PST) X-Google-Smtp-Source: ABhQp+Ro1v22rJnVxaTPjpH1MPIPK6ePCJ0Qzjb+S2+AtzaW3k3YMomxZAhZnEmjjFEBDszaYL/9 X-Received: by 10.84.212.136 with SMTP id e8mr14092324pli.205.1509961056026; Mon, 06 Nov 2017 01:37:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1509961055; cv=none; d=google.com; s=arc-20160816; b=D0HB5lqMHWgqHwxBkWApZK/tuBRNl4sU2bwjsRi4CatJJNLJQgoCxgmmpcXyAJx4X2 48EBjezQm+4XlnppyEXqhYt2uvWVQDXJVepS3qFuSbm/Ei0yOcgcK+CCsS15HkeOxJl7 iW4v0K0h7pC/cYtrJYmmLFD38xDnAHFOMay7JhxlwlInwHSTvzrI/neQhBzezB0KsJPz WhnSPkU9vWLXXK2JJ/L6K+RuIjNsq46sJgJHQs9BRW2rB4K+Mdc9PVuVBkkJmxVKEid/ 5Zi5ezstU9ywvcyFl6j8rXOP8LmSdFyusDFPiQgMHvtIqqRyyN71uLi9OOMgI3dHR1oZ GXyQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=3NdTBFE0mT7ALxag4+ftFLhZoym4e/54iCbuxgPCTuk=; b=c98nBbcW+FkVkwbrUDkphfGR5Xb3igXsjn2oXS3/wdM9B2kWzAhYZ2BJI7WdMRxP75 JM/aDeZDcCI0ZVIdV9FvnEc+hPCbpV23SB9uHfL5amwHmro7O0j149cGOhxWxgwJepxU Fz1NqohcEeFqKMdBEAei0VH03YsRxKrRWfqT0bVEl9C+PJm6m9N3IEoVeyUBPMLTB3GK oGyBXmT+yhBVaUvubW/YK6UjpcMF9NgMVehWr0Yn1AqjwWQpTYX+bv4Px2KY0jO3tror OH+lpp51DqUHdUOf2tDEXEscEX6kN9e0ke65rbW1Xsrq6jisPaVyCeHejoa6X1Fluyxt NFeQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=Ympl69NQ; 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=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d17si11064369pge.191.2017.11.06.01.37.22; Mon, 06 Nov 2017 01:37:35 -0800 (PST) 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=fail header.i=@gmail.com header.s=20161025 header.b=Ympl69NQ; 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=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751056AbdKFI6w (ORCPT + 98 others); Mon, 6 Nov 2017 03:58:52 -0500 Received: from mail-qt0-f196.google.com ([209.85.216.196]:50918 "EHLO mail-qt0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752117AbdKFI6s (ORCPT ); Mon, 6 Nov 2017 03:58:48 -0500 Received: by mail-qt0-f196.google.com with SMTP id d9so9997272qtd.7; Mon, 06 Nov 2017 00:58:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=3NdTBFE0mT7ALxag4+ftFLhZoym4e/54iCbuxgPCTuk=; b=Ympl69NQo35a5uXpUQeoe1tdqyb04qmzekyN73XNxIJHEDC1ynAUmv5DF8G86+sXSH z1ecfenhFb5WTobzh9c30guPi7WmF3YdLEtShLzyTek0al5oMQxFMDEB0mjyEW+qjFtA 7PvE9dWg8Xy7ose2V53/PWRbXcmcU35iasQpGsGYsq5PVKgx+F/VZev+MtNwb2+pyxjI vcCDxoUEX2oiz0+ICF94tKe3Y/uvizNPmh6jYBwsYVW/BZLYE3f0TxPJjuF8FRlvId4F UyTimd0UjG/2gfMfy6zYtcNnwT301NLrTEK9gAohRLMFhMcHb2efJuLnKizn43nXSXkI palQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=3NdTBFE0mT7ALxag4+ftFLhZoym4e/54iCbuxgPCTuk=; b=DgQK/Ts3rOpE56Dfr7D7Og7zEh1aupwCE5IMnujdmM1I5X1uoWoD+vutjH5WTJRQjC oFW1LZ9zlgvuCJo/po+dZTMgiryshpa6qYhzzHaAal91m58R1vw3WiH4ZRpSp+5tN4p+ RpnkIep6yM4V8gHLynEq/uDq3kVVP8qk3UeqrKMHBBAd04gTOgRg+ncU9FFJp+k7xemv EB0ZsXeSEjZqvAvdVXn2E/AC0GNfv87kf8CVfd09goLs7CC/em1x1NV4rzqXJ8BrsJYW k2d9NKefyzcd2LO4Al9XuA0qJXIdyWUEIool4gwlMh0fMLwfvRXlokEdAtMB9Rkw4xLK +QbQ== X-Gm-Message-State: AMCzsaUmqufol4LFHGHUfA7WZycUTVwXI/2Yd+ws1uAFn1JhNylGnfwt eNsXtzTxD2GigCKfbbQxxVs= X-Received: by 10.200.3.205 with SMTP id z13mr22678028qtg.25.1509958727812; Mon, 06 Nov 2017 00:58:47 -0800 (PST) Received: from localhost.localdomain (50-39-103-96.bvtn.or.frontiernet.net. [50.39.103.96]) by smtp.gmail.com with ESMTPSA id r26sm8001094qki.42.2017.11.06.00.58.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 06 Nov 2017 00:58:47 -0800 (PST) From: Ram Pai To: mpe@ellerman.id.au, mingo@redhat.com, akpm@linux-foundation.org, corbet@lwn.net, arnd@arndb.de Cc: linuxppc-dev@lists.ozlabs.org, linux-mm@kvack.org, x86@kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, dave.hansen@intel.com, benh@kernel.crashing.org, paulus@samba.org, khandual@linux.vnet.ibm.com, aneesh.kumar@linux.vnet.ibm.com, bsingharora@gmail.com, hbabu@us.ibm.com, mhocko@kernel.org, bauerman@linux.vnet.ibm.com, ebiederm@xmission.com, linuxram@us.ibm.com Subject: [PATCH v9 10/51] powerpc: store and restore the pkey state across context switches Date: Mon, 6 Nov 2017 00:57:02 -0800 Message-Id: <1509958663-18737-11-git-send-email-linuxram@us.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1509958663-18737-1-git-send-email-linuxram@us.ibm.com> References: <1509958663-18737-1-git-send-email-linuxram@us.ibm.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Store and restore the AMR, IAMR and UAMOR register state of the task before scheduling out and after scheduling in, respectively. Signed-off-by: Ram Pai --- arch/powerpc/include/asm/mmu_context.h | 3 ++ arch/powerpc/include/asm/pkeys.h | 4 ++ arch/powerpc/include/asm/processor.h | 5 +++ arch/powerpc/kernel/process.c | 7 ++++ arch/powerpc/mm/pkeys.c | 49 +++++++++++++++++++++++++++++++- 5 files changed, 67 insertions(+), 1 deletions(-) diff --git a/arch/powerpc/include/asm/mmu_context.h b/arch/powerpc/include/asm/mmu_context.h index 6d7c4f1..4eccc2f 100644 --- a/arch/powerpc/include/asm/mmu_context.h +++ b/arch/powerpc/include/asm/mmu_context.h @@ -146,6 +146,9 @@ static inline bool arch_vma_access_permitted(struct vm_area_struct *vma, #ifndef CONFIG_PPC_MEM_KEYS #define pkey_initialize() #define pkey_mm_init(mm) +#define thread_pkey_regs_save(thread) +#define thread_pkey_regs_restore(new_thread, old_thread) +#define thread_pkey_regs_init(thread) #endif /* CONFIG_PPC_MEM_KEYS */ #endif /* __KERNEL__ */ diff --git a/arch/powerpc/include/asm/pkeys.h b/arch/powerpc/include/asm/pkeys.h index 652c750..0b2d9f0 100644 --- a/arch/powerpc/include/asm/pkeys.h +++ b/arch/powerpc/include/asm/pkeys.h @@ -156,5 +156,9 @@ static inline void pkey_mm_init(struct mm_struct *mm) mm_pkey_allocation_map(mm) = initial_allocation_mask; } +extern void thread_pkey_regs_save(struct thread_struct *thread); +extern void thread_pkey_regs_restore(struct thread_struct *new_thread, + struct thread_struct *old_thread); +extern void thread_pkey_regs_init(struct thread_struct *thread); extern void pkey_initialize(void); #endif /*_ASM_POWERPC_KEYS_H */ diff --git a/arch/powerpc/include/asm/processor.h b/arch/powerpc/include/asm/processor.h index fab7ff8..e3c417c 100644 --- a/arch/powerpc/include/asm/processor.h +++ b/arch/powerpc/include/asm/processor.h @@ -309,6 +309,11 @@ struct thread_struct { struct thread_vr_state ckvr_state; /* Checkpointed VR state */ unsigned long ckvrsave; /* Checkpointed VRSAVE */ #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */ +#ifdef CONFIG_PPC_MEM_KEYS + unsigned long amr; + unsigned long iamr; + unsigned long uamor; +#endif #ifdef CONFIG_KVM_BOOK3S_32_HANDLER void* kvm_shadow_vcpu; /* KVM internal data */ #endif /* CONFIG_KVM_BOOK3S_32_HANDLER */ diff --git a/arch/powerpc/kernel/process.c b/arch/powerpc/kernel/process.c index a0c74bb..148b934 100644 --- a/arch/powerpc/kernel/process.c +++ b/arch/powerpc/kernel/process.c @@ -42,6 +42,7 @@ #include #include #include +#include #include #include @@ -1085,6 +1086,8 @@ static inline void save_sprs(struct thread_struct *t) t->tar = mfspr(SPRN_TAR); } #endif + + thread_pkey_regs_save(t); } static inline void restore_sprs(struct thread_struct *old_thread, @@ -1120,6 +1123,8 @@ static inline void restore_sprs(struct thread_struct *old_thread, mtspr(SPRN_TAR, new_thread->tar); } #endif + + thread_pkey_regs_restore(new_thread, old_thread); } #ifdef CONFIG_PPC_BOOK3S_64 @@ -1705,6 +1710,8 @@ void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp) current->thread.tm_tfiar = 0; current->thread.load_tm = 0; #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */ + + thread_pkey_regs_init(¤t->thread); } EXPORT_SYMBOL(start_thread); diff --git a/arch/powerpc/mm/pkeys.c b/arch/powerpc/mm/pkeys.c index 3ddc13a..469f370 100644 --- a/arch/powerpc/mm/pkeys.c +++ b/arch/powerpc/mm/pkeys.c @@ -16,6 +16,8 @@ bool pkey_execute_disable_supported; int pkeys_total; /* Total pkeys as per device tree */ u32 initial_allocation_mask; /* Bits set for reserved keys */ +u64 pkey_amr_uamor_mask; /* Bits in AMR/UMOR not to be touched */ +u64 pkey_iamr_mask; /* Bits in AMR not to be touched */ #define AMR_BITS_PER_PKEY 2 #define AMR_RD_BIT 0x1UL @@ -74,8 +76,16 @@ void __init pkey_initialize(void) * programming note. */ initial_allocation_mask = ~0x0; - for (i = 2; i < (pkeys_total - os_reserved); i++) + + /* register mask is in BE format */ + pkey_amr_uamor_mask = ~0x0ul; + pkey_iamr_mask = ~0x0ul; + + for (i = 2; i < (pkeys_total - os_reserved); i++) { initial_allocation_mask &= ~(0x1 << i); + pkey_amr_uamor_mask &= ~(0x3ul << pkeyshift(i)); + pkey_iamr_mask &= ~(0x1ul << pkeyshift(i)); + } } static inline u64 read_amr(void) @@ -200,3 +210,40 @@ int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey, init_amr(pkey, new_amr_bits); return 0; } + +void thread_pkey_regs_save(struct thread_struct *thread) +{ + if (static_branch_likely(&pkey_disabled)) + return; + + /* + * TODO: Skip saving registers if @thread hasn't used any keys yet. + */ + thread->amr = read_amr(); + thread->iamr = read_iamr(); + thread->uamor = read_uamor(); +} + +void thread_pkey_regs_restore(struct thread_struct *new_thread, + struct thread_struct *old_thread) +{ + if (static_branch_likely(&pkey_disabled)) + return; + + /* + * TODO: Just set UAMOR to zero if @new_thread hasn't used any keys yet. + */ + if (old_thread->amr != new_thread->amr) + write_amr(new_thread->amr); + if (old_thread->iamr != new_thread->iamr) + write_iamr(new_thread->iamr); + if (old_thread->uamor != new_thread->uamor) + write_uamor(new_thread->uamor); +} + +void thread_pkey_regs_init(struct thread_struct *thread) +{ + write_amr(read_amr() & pkey_amr_uamor_mask); + write_iamr(read_iamr() & pkey_iamr_mask); + write_uamor(read_uamor() & pkey_amr_uamor_mask); +} -- 1.7.1 From 1583307853746238184@xxx Mon Nov 06 09:20:35 +0000 2017 X-GM-THRID: 1583307853746238184 X-Gmail-Labels: Inbox,Category Promotions,HistoricalUnread