From patchwork Mon May 4 14:58:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Chartre X-Patchwork-Id: 11526465 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9628118EC for ; Mon, 4 May 2020 15:00:07 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5835721655 for ; Mon, 4 May 2020 15:00:07 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="kovaqTW9" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5835721655 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=oracle.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 1E94E8E002F; Mon, 4 May 2020 11:00:06 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 19C348E0024; Mon, 4 May 2020 11:00:06 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 089AE8E002F; Mon, 4 May 2020 11:00:06 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0062.hostedemail.com [216.40.44.62]) by kanga.kvack.org (Postfix) with ESMTP id E2A0D8E0024 for ; Mon, 4 May 2020 11:00:05 -0400 (EDT) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id A974E18F5D for ; Mon, 4 May 2020 15:00:05 +0000 (UTC) X-FDA: 76779346770.09.sea70_3aea0b4b77254 X-Spam-Summary: 1,0,0,,d41d8cd98f00b204,alexandre.chartre@oracle.com,,RULES_HIT:30054:30064:30070,0,RBL:156.151.31.86:@oracle.com:.lbl8.mailshell.net-64.10.201.10 62.18.0.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:ft,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:26,LUA_SUMMARY:none X-HE-Tag: sea70_3aea0b4b77254 X-Filterd-Recvd-Size: 7757 Received: from userp2130.oracle.com (userp2130.oracle.com [156.151.31.86]) by imf48.hostedemail.com (Postfix) with ESMTP for ; Mon, 4 May 2020 15:00:04 +0000 (UTC) Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 044Elb0V116465; Mon, 4 May 2020 14:59:55 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2020-01-29; bh=sEQgjOdo4evI2vW/F7vLXsA4dt9pdUoHUZxHqkXr9QA=; b=kovaqTW9aibWjjOMYJyd5NFACyd7FB91PiCgvJWE159s4VLHkgk8SO2v0OxgU+W2LuYx 1crYD2Cy3Z+BDMesBUvZN4pir+op/dLUD78XgAlF7bc6fSjypaWY2GkqUuRP5CATgm/R lhEYlPvfgi5OIuZ3OiNMJgOqqwzbtXtR8wf4a1XKJApZK6Gvhl8cdqX0ITQzzPPoYaTG XWAqnccys3S3GzN5Plit7Fampc6/mwxd86NtLnJyZeYz49iC4pn0yoZfO8PfTAfdpayK UjfPWQiZs51ZKRYCc23YjN+4wj6DyheCs8UZ8dwUO4AhzMD6Hc49aq+41WVb+dizfi6u rw== Received: from userp3020.oracle.com (userp3020.oracle.com [156.151.31.79]) by userp2130.oracle.com with ESMTP id 30s09qyfrk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 04 May 2020 14:59:55 +0000 Received: from pps.filterd (userp3020.oracle.com [127.0.0.1]) by userp3020.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 044EmVHV105667; Mon, 4 May 2020 14:59:54 GMT Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by userp3020.oracle.com with ESMTP id 30sjjw0g7j-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 04 May 2020 14:59:54 +0000 Received: from abhmp0001.oracle.com (abhmp0001.oracle.com [141.146.116.7]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id 044ExrQj001259; Mon, 4 May 2020 14:59:53 GMT Received: from linux-1.home.com (/10.175.9.166) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Mon, 04 May 2020 07:59:52 -0700 From: Alexandre Chartre To: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, x86@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, konrad.wilk@oracle.com, jan.setjeeilers@oracle.com, liran.alon@oracle.com, junaids@google.com, graf@amazon.de, rppt@linux.vnet.ibm.com, kuzuno@gmail.com, mgross@linux.intel.com, alexandre.chartre@oracle.com Subject: [RFC v4][PATCH part-2 05/13] mm/dpt: Add decorated page-table entry set functions Date: Mon, 4 May 2020 16:58:02 +0200 Message-Id: <20200504145810.11882-6-alexandre.chartre@oracle.com> X-Mailer: git-send-email 2.18.2 In-Reply-To: <20200504145810.11882-1-alexandre.chartre@oracle.com> References: <20200504145810.11882-1-alexandre.chartre@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9610 signatures=668687 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 mlxscore=0 phishscore=0 bulkscore=0 malwarescore=0 spamscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2003020000 definitions=main-2005040123 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9610 signatures=668687 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxscore=0 lowpriorityscore=0 spamscore=0 adultscore=0 clxscore=1015 suspectscore=0 priorityscore=1501 malwarescore=0 mlxlogscore=999 phishscore=0 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2003020000 definitions=main-2005040123 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Add wrappers around the page table entry (pgd/p4d/pud/pmd) set functions which check that an existing entry is not being overwritten. Signed-off-by: Alexandre Chartre --- arch/x86/mm/dpt.c | 126 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) diff --git a/arch/x86/mm/dpt.c b/arch/x86/mm/dpt.c index a2f54ba00255..7a1b4cd53b03 100644 --- a/arch/x86/mm/dpt.c +++ b/arch/x86/mm/dpt.c @@ -258,6 +258,132 @@ static p4d_t *dpt_p4d_alloc(struct dpt *dpt, pgd_t *pgd, unsigned long addr) return p4d; } +/* + * dpt_set_pXX() functions are equivalent to kernel set_pXX() functions + * but, in addition, they ensure that they are not overwriting an already + * existing reference in the decorated page table. Otherwise an error is + * returned. + */ + +static int dpt_set_pte(struct dpt *dpt, pte_t *pte, pte_t pte_value) +{ +#ifdef DEBUG + /* + * The pte pointer should come from dpt_pte_alloc() or dpt_pte_offset() + * both of which check if the pointer is in the decorated page table. + * So this is a paranoid check to ensure the pointer is really in the + * decorated page table. + */ + if (!dpt_valid_offset(dpt, pte)) { + pr_err("DPT %p: PTE %px not found\n", dpt, pte); + return -EINVAL; + } +#endif + set_pte(pte, pte_value); + + return 0; +} + +static int dpt_set_pmd(struct dpt *dpt, pmd_t *pmd, pmd_t pmd_value) +{ +#ifdef DEBUG + /* + * The pmd pointer should come from dpt_pmd_alloc() or dpt_pmd_offset() + * both of which check if the pointer is in the decorated page table. + * So this is a paranoid check to ensure the pointer is really in the + * decorated page table. + */ + if (!dpt_valid_offset(dpt, pmd)) { + pr_err("DPT %p: PMD %px not found\n", dpt, pmd); + return -EINVAL; + } +#endif + if (pmd_val(*pmd) == pmd_val(pmd_value)) + return 0; + + if (!pmd_none(*pmd)) { + pr_err("DPT %p: PMD %px overwriting %lx with %lx\n", + dpt, pmd, pmd_val(*pmd), pmd_val(pmd_value)); + return -EBUSY; + } + + set_pmd(pmd, pmd_value); + + return 0; +} + +static int dpt_set_pud(struct dpt *dpt, pud_t *pud, pud_t pud_value) +{ +#ifdef DEBUG + /* + * The pud pointer should come from dpt_pud_alloc() or dpt_pud_offset() + * both of which check if the pointer is in the decorated page table. + * So this is a paranoid check to ensure the pointer is really in the + * decorated page table. + */ + if (!dpt_valid_offset(dpt, pud)) { + pr_err("DPT %p: PUD %px not found\n", dpt, pud); + return -EINVAL; + } +#endif + if (pud_val(*pud) == pud_val(pud_value)) + return 0; + + if (!pud_none(*pud)) { + pr_err("DPT %p: PUD %px overwriting %lx with %lx\n", + dpt, pud, pud_val(*pud), pud_val(pud_value)); + return -EBUSY; + } + + set_pud(pud, pud_value); + + return 0; +} + +static int dpt_set_p4d(struct dpt *dpt, p4d_t *p4d, p4d_t p4d_value) +{ +#ifdef DEBUG + /* + * The p4d pointer should come from dpt_p4d_alloc() or dpt_p4d_offset() + * both of which check if the pointer is in the decorated page table. + * So this is a paranoid check to ensure the pointer is really in the + * decorated page table. + */ + if (!dpt_valid_offset(dpt, p4d)) { + pr_err("DPT %p: P4D %px not found\n", dpt, p4d); + return -EINVAL; + } +#endif + if (p4d_val(*p4d) == p4d_val(p4d_value)) + return 0; + + if (!p4d_none(*p4d)) { + pr_err("DPT %p: P4D %px overwriting %lx with %lx\n", + dpt, p4d, p4d_val(*p4d), p4d_val(p4d_value)); + return -EBUSY; + } + + set_p4d(p4d, p4d_value); + + return 0; +} + +static int dpt_set_pgd(struct dpt *dpt, pgd_t *pgd, pgd_t pgd_value) +{ + if (pgd_val(*pgd) == pgd_val(pgd_value)) + return 0; + + if (!pgd_none(*pgd)) { + pr_err("DPT %p: PGD %px overwriting %lx with %lx\n", + dpt, pgd, pgd_val(*pgd), pgd_val(pgd_value)); + return -EBUSY; + } + + set_pgd(pgd, pgd_value); + + return 0; +} + /* * dpt_create - allocate a page-table and create a corresponding * decorated page-table. The page-table is allocated and aligned