Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp1343392ybn; Wed, 25 Sep 2019 16:45:29 -0700 (PDT) X-Google-Smtp-Source: APXvYqy/O0tbGLCh7NMC4hfKhIMHlVVuKxThdgfp//FtaUTOWNihXGXz2aeizdPllxbz4S2AATV1 X-Received: by 2002:a17:906:454c:: with SMTP id s12mr656861ejq.69.1569455128958; Wed, 25 Sep 2019 16:45:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1569455128; cv=none; d=google.com; s=arc-20160816; b=o0aLC990TILUUAUW1965QMM7TbcNTa/9ss00hd3IJ8pnwbzadaxvjJqL+ctrieC5uo iBrPxSySxK3K7gvl5rcrATGoOEx7aDlt5sjIAAO4o9Qv37JwX36CcGxTakZcdxKKtASk 4xczBx2BIcmIWvgWRypeRzduIdKmDy5BAbzo0kyjXi4iPvINde9TC3RbRI/Hr2+kSGCz TBWnsJ/6qiNHL2PX1JUQUz/hMTfn2ZVuVMPTtzCcRgdF/ao+xMiWWWTD7KYwTeyr4rKs qVBlQVaDPE/gAWi+3egFcsbISW7seGD5hCkh1NIqSJWJqJYlEIPPMQttJqQYLNmmDrtu 4zVw== 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:to:from :dkim-signature; bh=O0CBEozBtTGW2pERGsJgZuZ1bw9J0cp5B10jh4ayzug=; b=up8rsnA0vSlV5s/yoalnigrB9fBRaKneJNDG4mO1nm7EkaINvbe2ch2xnaiWn+BBil dJ2EyVON/tpvJM6kwSi5HnsA/IUJ6IhypG3BvacXVWgeUbeHO2xya1KPmY5PwV83wQ9A wsFzevyym304ZC0BrTqU8LFhi8H8aIIFrQLTpWRVa3uZ05pFbFGIaJZ1+T9MPSoqRhRw kFtBVxtXJPbYRyrBLBVEihYOBHACugJkaH6WUImMzBxW67KcAFL9D+XjrccsaBCtNuAo NFHNjZaYKpI9gyDGnGjJbuJbZQ4fzBADkD9wO/P0uC6Mo83VWMT4Qm6kxKSP6sz/TF43 O9Tg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@soleen.com header.s=google header.b=lHmp4caK; 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 hh9si183925ejb.313.2019.09.25.16.45.05; Wed, 25 Sep 2019 16:45:28 -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=@soleen.com header.s=google header.b=lHmp4caK; 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 S2502743AbfIWUfB (ORCPT + 99 others); Mon, 23 Sep 2019 16:35:01 -0400 Received: from mail-pf1-f195.google.com ([209.85.210.195]:41808 "EHLO mail-pf1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2502724AbfIWUe7 (ORCPT ); Mon, 23 Sep 2019 16:34:59 -0400 Received: by mail-pf1-f195.google.com with SMTP id q7so9831447pfh.8 for ; Mon, 23 Sep 2019 13:34:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=O0CBEozBtTGW2pERGsJgZuZ1bw9J0cp5B10jh4ayzug=; b=lHmp4caKuhpmRRvFqoxk+Dv5szLbANKDLUEWmrgxe8QsZvtX+S/e4/dgG63d+dz+gj aI23C8K/DoJFcMMTmzwnqOAAjS3aLIOEvZcvivZQQE4WuKhh6Iv6jngNjFd7iRwU/Fcn fhVsrRLCV2BVXysC36+MW9luGtaag0iC2gAz0kIkqm4GNWv9cZPmQEPhxposYUN9Glo1 Os5dkLWoufY5xru5S0scCgOEFTHzcHtm1hSD8md81Kapc5ORK+W9GzSZ2UFkcxAR/Gop ZAlkNeDevYn1xV06eVLnKVqvBlWGolA3Dmoh+DbXwEqMluD8vbujBOJaPlST/ZS2Mnxq +aWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=O0CBEozBtTGW2pERGsJgZuZ1bw9J0cp5B10jh4ayzug=; b=uA/9k9BseLmH+k3EqJhz0GZjz/hszql3JaZXRNBnnn1KuTYKxz/5Dl+49uR9wdL8wv 11PYM+6Hms7p4ihclzh5Bmo1UWFRN/wojcsLICZG+G/Zfw65m5z+CReBkLsO6Em4dmV6 NHfZ59rWS/kDvvcfV3dur2KsX3E8zqMhyAlaRuxUrzE4BK2153wmlVJLT90E05c8ftyQ HIjxpibYkLNgjmXh+j6mUKRFoq3d04NqnegU5IzNSGQbftZmSjS5NYNnRjWKZZf3KII5 BIdTG9vux/WVdX8Vu5mH6xo6MFFcaa0Kh54eDQ5yOodt3BmO9qrBd3HuFQI+tB7yEExY uQXQ== X-Gm-Message-State: APjAAAUA7lJzFD688V04+Of9HLcqI6UuItqEleNIBR4Pn6KCfcwDKXYn 1vsVXrKpKeWrg0/yEoDYnAiFag== X-Received: by 2002:a63:ea14:: with SMTP id c20mr1691039pgi.185.1569270898184; Mon, 23 Sep 2019 13:34:58 -0700 (PDT) Received: from xakep.corp.microsoft.com (c-73-69-118-222.hsd1.nh.comcast.net. [73.69.118.222]) by smtp.gmail.com with ESMTPSA id n29sm12798676pgm.4.2019.09.23.13.34.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Sep 2019 13:34:57 -0700 (PDT) From: Pavel Tatashin To: pasha.tatashin@soleen.com, jmorris@namei.org, sashal@kernel.org, ebiederm@xmission.com, kexec@lists.infradead.org, linux-kernel@vger.kernel.org, corbet@lwn.net, catalin.marinas@arm.com, will@kernel.org, linux-arm-kernel@lists.infradead.org, marc.zyngier@arm.com, james.morse@arm.com, vladimir.murzin@arm.com, matthias.bgg@gmail.com, bhsharma@redhat.com, linux-mm@kvack.org, mark.rutland@arm.com Subject: [PATCH v5 08/17] arm64: hibernate: add trans_pgd public functions Date: Mon, 23 Sep 2019 16:34:18 -0400 Message-Id: <20190923203427.294286-9-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20190923203427.294286-1-pasha.tatashin@soleen.com> References: <20190923203427.294286-1-pasha.tatashin@soleen.com> 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 trans_pgd_create_copy() and trans_pgd_map_page() are going to be the basis for new shared code that handles page tables for cases which are between kernels: kexec, and hibernate. Note: Eventually, get_safe_page() will be moved into a function pointer passed via argument, but for now keep it as is. Signed-off-by: Pavel Tatashin --- arch/arm64/kernel/hibernate.c | 88 ++++++++++++++++++++++++----------- 1 file changed, 60 insertions(+), 28 deletions(-) diff --git a/arch/arm64/kernel/hibernate.c b/arch/arm64/kernel/hibernate.c index ce60bceed357..ded9034b9d39 100644 --- a/arch/arm64/kernel/hibernate.c +++ b/arch/arm64/kernel/hibernate.c @@ -182,39 +182,15 @@ int arch_hibernation_header_restore(void *addr) } EXPORT_SYMBOL(arch_hibernation_header_restore); -/* - * Copies length bytes, starting at src_start into an new page, - * perform cache maintentance, then maps it at the specified address low - * address as executable. - * - * This is used by hibernate to copy the code it needs to execute when - * overwriting the kernel text. This function generates a new set of page - * tables, which it loads into ttbr0. - * - * Length is provided as we probably only want 4K of data, even on a 64K - * page system. - */ -static int create_safe_exec_page(void *src_start, size_t length, - unsigned long dst_addr, - phys_addr_t *phys_dst_addr) +int trans_pgd_map_page(pgd_t *trans_pgd, void *page, + unsigned long dst_addr, + pgprot_t pgprot) { - void *page = (void *)get_safe_page(GFP_ATOMIC); - pgd_t *trans_pgd; pgd_t *pgdp; pud_t *pudp; pmd_t *pmdp; pte_t *ptep; - if (!page) - return -ENOMEM; - - memcpy(page, src_start, length); - __flush_icache_range((unsigned long)page, (unsigned long)page + length); - - trans_pgd = (void *)get_safe_page(GFP_ATOMIC); - if (!trans_pgd) - return -ENOMEM; - pgdp = pgd_offset_raw(trans_pgd, dst_addr); if (pgd_none(READ_ONCE(*pgdp))) { pudp = (void *)get_safe_page(GFP_ATOMIC); @@ -242,6 +218,44 @@ static int create_safe_exec_page(void *src_start, size_t length, ptep = pte_offset_kernel(pmdp, dst_addr); set_pte(ptep, pfn_pte(virt_to_pfn(page), PAGE_KERNEL_EXEC)); + return 0; +} + +/* + * Copies length bytes, starting at src_start into an new page, + * perform cache maintenance, then maps it at the specified address low + * address as executable. + * + * This is used by hibernate to copy the code it needs to execute when + * overwriting the kernel text. This function generates a new set of page + * tables, which it loads into ttbr0. + * + * Length is provided as we probably only want 4K of data, even on a 64K + * page system. + */ +static int create_safe_exec_page(void *src_start, size_t length, + unsigned long dst_addr, + phys_addr_t *phys_dst_addr) +{ + void *page = (void *)get_safe_page(GFP_ATOMIC); + pgd_t *trans_pgd; + int rc; + + if (!page) + return -ENOMEM; + + memcpy(page, src_start, length); + __flush_icache_range((unsigned long)page, (unsigned long)page + length); + + trans_pgd = (void *)get_safe_page(GFP_ATOMIC); + if (!trans_pgd) + return -ENOMEM; + + rc = trans_pgd_map_page(trans_pgd, page, dst_addr, + PAGE_KERNEL_EXEC); + if (rc) + return rc; + /* * Load our new page tables. A strict BBM approach requires that we * ensure that TLBs are free of any entries that may overlap with the @@ -462,6 +476,24 @@ static int copy_page_tables(pgd_t *dst_pgdp, unsigned long start, return 0; } +int trans_pgd_create_copy(pgd_t **dst_pgdp, unsigned long start, + unsigned long end) +{ + int rc; + pgd_t *trans_pgd = (pgd_t *)get_safe_page(GFP_ATOMIC); + + if (!trans_pgd) { + pr_err("Failed to allocate memory for temporary page tables.\n"); + return -ENOMEM; + } + + rc = copy_page_tables(trans_pgd, start, end); + if (!rc) + *dst_pgdp = trans_pgd; + + return rc; +} + /* * Setup then Resume from the hibernate image using swsusp_arch_suspend_exit(). * @@ -488,7 +520,7 @@ int swsusp_arch_resume(void) pr_err("Failed to allocate memory for temporary page tables.\n"); return -ENOMEM; } - rc = copy_page_tables(tmp_pg_dir, PAGE_OFFSET, PAGE_END); + rc = trans_pgd_create_copy(&tmp_pg_dir, PAGE_OFFSET, PAGE_END); if (rc) return rc; -- 2.23.0