Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp359497ybb; Wed, 8 Apr 2020 00:59:03 -0700 (PDT) X-Google-Smtp-Source: APiQypKZQ+xARFywcBFW81l8l4ENH8ZdZP5Vuk6T7gmyuPdLGWR1RQOu/t01O5rA1khnQwpFI9Tg X-Received: by 2002:a05:6808:206:: with SMTP id l6mr1532385oie.147.1586332743232; Wed, 08 Apr 2020 00:59:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586332743; cv=none; d=google.com; s=arc-20160816; b=OHzhN0kjHrAqA/Ncs0yWY/Wkw+KYa4cEyWHgyIK20Z5tuizkcDLqPcPdSgb2Lg1PSN G8EnbPnvKqzaISPVmo+HGQ6XFBgztfkLBRHk6b8QVREDoa5QDkPPehXGgmneTw+d4rBy 3760g/RGCOeZFFvaMDiPjv6BmIA66I19DkNFmPLtXCni1mGrdQFAr/dkGdzoCbowo1wz 2/L3jkTScyTSZ/Xz0iNS1XJqs1gNgsqJlUmISE9/S7+5oFnUI+ee3t9pdfSc+vO189y8 T/FVf2yWfTxz57jfzJ/qptwH0WlpToGkeLtQhHF0cURa17nNHj8RR+5stNAXbiwoAN5X dG2w== 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:date:subject:cc:to:from :dkim-signature; bh=bdvS3ImXktegIGIUSt7DM4X3H//vtPG1T5XbtOqEIxE=; b=rclAwirYWqDA4cRD/8S4DpL2Fs8lmBdTSMmcEtEj+XrUEXpRRgJoOC2ScUvPIEY0NM R7Povfyx79ulP/EBAOCIUs+RuJ2YCkSeuhgepqb+Lb44tpOmDQks2GMCkzdezOC6XBH2 M2oM4k1dF+ReOlDfsOtx0Hpd+SeXbY6YsaOBfplbwOVvNifGU+Kpr4wyiZ0IPDVKNxTn Cvs5DN1jcV7EUHvqNJmZUg5V/B7B37P4Gn81kB0+3La8su2rUvhjk4clJRYVrx8p/rLi qKo16EclE5NsvR/ZCv6LR3qH3PUP8YPDluhQf3hMdXbBe7LI3pyt8ZJ7laUgBABeRwuc tL/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=I34Uf3OW; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b22si3016699ots.245.2020.04.08.00.58.49; Wed, 08 Apr 2020 00:59:03 -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=@sifive.com header.s=google header.b=I34Uf3OW; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727354AbgDHH5T (ORCPT + 99 others); Wed, 8 Apr 2020 03:57:19 -0400 Received: from mail-pj1-f65.google.com ([209.85.216.65]:53344 "EHLO mail-pj1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727089AbgDHH5S (ORCPT ); Wed, 8 Apr 2020 03:57:18 -0400 Received: by mail-pj1-f65.google.com with SMTP id l36so844727pjb.3 for ; Wed, 08 Apr 2020 00:57:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=bdvS3ImXktegIGIUSt7DM4X3H//vtPG1T5XbtOqEIxE=; b=I34Uf3OWOjly54a8zK1Z6P+uRbyGvbAmOXqDlhNGGylMwBHq7lfHMSXiQI9W7wTnTH j72/kWxb5DGM7slCb9wWWEtqXAXn3J7A74hTMWuOM0Apo/nA07YnfV+eCosDd/mO2kK+ zqrlOtqMeX036nlaXiEsqaq19xXo25Eonlu7QYg/gBLuUn8ry9YCStTkJscntjl1E5/I im0ZgOKBamzkCnP7kR6cRKyqokCQ+v026Li3BYsIiFKVSza+WSv7DGnFB4UD27x968pI PdNW7iPHAJK5Kn7ZQpRsWaWJxsN+c3ZTp6Ywxln5tYnNnXRmD6TRgY96QZjt8vkPKXV0 42uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=bdvS3ImXktegIGIUSt7DM4X3H//vtPG1T5XbtOqEIxE=; b=Pqof8dUVrYlnDabFrMGThJjlWVQJ9wV3JZCEcyHjsDdvPuLvTPLP+sgrxKyEz/ewuQ 6JR1wS69h4PF03rYlG6JKzKfwzVhw41pnkvSoVvnUfQnvTwN7xBAH5IJzWm3TH5rD33s vhFSaM2K3CRHXRTNJLt/BBrWD7NfTnhHkTq6U61F63TVC3CEYrUh9MOjGzgPs2ylGKeO wgFAPfNIHjmwWG4GSCcfTBV36K/Ox/TDMrMO9QoGflIrYsedFxQ3S93t/X1XP4abPxII Y4oHTMe0BS+p+Ya61uSENdAt/SH52Icn4ZzMmc2PZBplFAjkk33CYEUrkTqQABLqcKd5 VPcg== X-Gm-Message-State: AGi0PuY0OZGXuseInsdTfaFh/L7tqMSlGkd94CB7le+eLP5QN6PhC6LK icdfV8BLVtR0PpRUWCHbk0HhUfTxKSIUDw== X-Received: by 2002:a17:902:8bc3:: with SMTP id r3mr6124029plo.220.1586332636877; Wed, 08 Apr 2020 00:57:16 -0700 (PDT) Received: from hsinchu02.internal.sifive.com (114-34-229-221.HINET-IP.hinet.net. [114.34.229.221]) by smtp.gmail.com with ESMTPSA id d85sm485599pfd.157.2020.04.08.00.57.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Apr 2020 00:57:16 -0700 (PDT) From: Zong Li To: palmer@dabbelt.com, paul.walmsley@sifive.com, aou@eecs.berkeley.edu, mhiramat@kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Zong Li , Palmer Dabbelt Subject: [PATCH v5 4/9] riscv: add ARCH_HAS_SET_MEMORY support Date: Wed, 8 Apr 2020 15:56:59 +0800 Message-Id: <1bb772acf62521b731305f16240e1f393fab839a.1586332296.git.zong.li@sifive.com> X-Mailer: git-send-email 2.26.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add set_memory_ro/rw/x/nx architecture hooks to change the page attribution. Use own set_memory.h rather than generic set_memory.h (i.e. include/asm-generic/set_memory.h), because we want to add other function prototypes here. Signed-off-by: Zong Li Reviewed-by: Palmer Dabbelt --- arch/riscv/Kconfig | 1 + arch/riscv/include/asm/set_memory.h | 24 +++++ arch/riscv/mm/Makefile | 2 +- arch/riscv/mm/pageattr.c | 150 ++++++++++++++++++++++++++++ 4 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 arch/riscv/include/asm/set_memory.h create mode 100644 arch/riscv/mm/pageattr.c diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 39c93ddff0cb..9044e0dd95ea 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -59,6 +59,7 @@ config RISCV select HAVE_EBPF_JIT if 64BIT select EDAC_SUPPORT select ARCH_HAS_GIGANTIC_PAGE + select ARCH_HAS_SET_MEMORY select ARCH_WANT_HUGE_PMD_SHARE if 64BIT select SPARSEMEM_STATIC if 32BIT select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT if MMU diff --git a/arch/riscv/include/asm/set_memory.h b/arch/riscv/include/asm/set_memory.h new file mode 100644 index 000000000000..79a810f0f38b --- /dev/null +++ b/arch/riscv/include/asm/set_memory.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2019 SiFive + */ + +#ifndef _ASM_RISCV_SET_MEMORY_H +#define _ASM_RISCV_SET_MEMORY_H + +/* + * Functions to change memory attributes. + */ +#ifdef CONFIG_MMU +int set_memory_ro(unsigned long addr, int numpages); +int set_memory_rw(unsigned long addr, int numpages); +int set_memory_x(unsigned long addr, int numpages); +int set_memory_nx(unsigned long addr, int numpages); +#else +static inline int set_memory_ro(unsigned long addr, int numpages) { return 0; } +static inline int set_memory_rw(unsigned long addr, int numpages) { return 0; } +static inline int set_memory_x(unsigned long addr, int numpages) { return 0; } +static inline int set_memory_nx(unsigned long addr, int numpages) { return 0; } +#endif + +#endif /* _ASM_RISCV_SET_MEMORY_H */ diff --git a/arch/riscv/mm/Makefile b/arch/riscv/mm/Makefile index 814e16a8d68a..363ef01c30b1 100644 --- a/arch/riscv/mm/Makefile +++ b/arch/riscv/mm/Makefile @@ -7,7 +7,7 @@ endif obj-y += init.o obj-y += extable.o -obj-$(CONFIG_MMU) += fault.o +obj-$(CONFIG_MMU) += fault.o pageattr.o obj-y += cacheflush.o obj-y += context.o diff --git a/arch/riscv/mm/pageattr.c b/arch/riscv/mm/pageattr.c new file mode 100644 index 000000000000..fcd59ef2835b --- /dev/null +++ b/arch/riscv/mm/pageattr.c @@ -0,0 +1,150 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2019 SiFive + */ + +#include +#include +#include +#include + +struct pageattr_masks { + pgprot_t set_mask; + pgprot_t clear_mask; +}; + +static unsigned long set_pageattr_masks(unsigned long val, struct mm_walk *walk) +{ + struct pageattr_masks *masks = walk->private; + unsigned long new_val = val; + + new_val &= ~(pgprot_val(masks->clear_mask)); + new_val |= (pgprot_val(masks->set_mask)); + + return new_val; +} + +static int pageattr_pgd_entry(pgd_t *pgd, unsigned long addr, + unsigned long next, struct mm_walk *walk) +{ + pgd_t val = READ_ONCE(*pgd); + + if (pgd_leaf(val)) { + val = __pgd(set_pageattr_masks(pgd_val(val), walk)); + set_pgd(pgd, val); + } + + return 0; +} + +static int pageattr_p4d_entry(p4d_t *p4d, unsigned long addr, + unsigned long next, struct mm_walk *walk) +{ + p4d_t val = READ_ONCE(*p4d); + + if (p4d_leaf(val)) { + val = __p4d(set_pageattr_masks(p4d_val(val), walk)); + set_p4d(p4d, val); + } + + return 0; +} + +static int pageattr_pud_entry(pud_t *pud, unsigned long addr, + unsigned long next, struct mm_walk *walk) +{ + pud_t val = READ_ONCE(*pud); + + if (pud_leaf(val)) { + val = __pud(set_pageattr_masks(pud_val(val), walk)); + set_pud(pud, val); + } + + return 0; +} + +static int pageattr_pmd_entry(pmd_t *pmd, unsigned long addr, + unsigned long next, struct mm_walk *walk) +{ + pmd_t val = READ_ONCE(*pmd); + + if (pmd_leaf(val)) { + val = __pmd(set_pageattr_masks(pmd_val(val), walk)); + set_pmd(pmd, val); + } + + return 0; +} + +static int pageattr_pte_entry(pte_t *pte, unsigned long addr, + unsigned long next, struct mm_walk *walk) +{ + pte_t val = READ_ONCE(*pte); + + val = __pte(set_pageattr_masks(pte_val(val), walk)); + set_pte(pte, val); + + return 0; +} + +static int pageattr_pte_hole(unsigned long addr, unsigned long next, + int depth, struct mm_walk *walk) +{ + /* Nothing to do here */ + return 0; +} + +const static struct mm_walk_ops pageattr_ops = { + .pgd_entry = pageattr_pgd_entry, + .p4d_entry = pageattr_p4d_entry, + .pud_entry = pageattr_pud_entry, + .pmd_entry = pageattr_pmd_entry, + .pte_entry = pageattr_pte_entry, + .pte_hole = pageattr_pte_hole, +}; + +static int __set_memory(unsigned long addr, int numpages, pgprot_t set_mask, + pgprot_t clear_mask) +{ + int ret; + unsigned long start = addr; + unsigned long end = start + PAGE_SIZE * numpages; + struct pageattr_masks masks = { + .set_mask = set_mask, + .clear_mask = clear_mask + }; + + if (!numpages) + return 0; + + down_read(&init_mm.mmap_sem); + ret = walk_page_range_novma(&init_mm, start, end, &pageattr_ops, NULL, + &masks); + up_read(&init_mm.mmap_sem); + + flush_tlb_kernel_range(start, end); + + return ret; +} + +int set_memory_ro(unsigned long addr, int numpages) +{ + return __set_memory(addr, numpages, __pgprot(_PAGE_READ), + __pgprot(_PAGE_WRITE)); +} + +int set_memory_rw(unsigned long addr, int numpages) +{ + return __set_memory(addr, numpages, __pgprot(_PAGE_READ | _PAGE_WRITE), + __pgprot(0)); +} + +int set_memory_x(unsigned long addr, int numpages) +{ + return __set_memory(addr, numpages, __pgprot(_PAGE_EXEC), __pgprot(0)); +} + +int set_memory_nx(unsigned long addr, int numpages) +{ + return __set_memory(addr, numpages, __pgprot(0), __pgprot(_PAGE_EXEC)); +} -- 2.26.0