From patchwork Fri Nov 13 22:15:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 11905025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2C247C4742C for ; Fri, 13 Nov 2020 22:27:23 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id C6418207DE for ; Fri, 13 Nov 2020 22:27:22 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="qQy12gt5"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="ve07XVd1"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="MPUYatJk" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C6418207DE Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:To:From:Subject:References:Mime-Version:Message-Id: In-Reply-To:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=G7cg78ylYRt2NKDsBpcHFhd1Mc2p3eet0zuiylyTLM0=; b=qQy12gt5QsIVfWcOYYIGCPnqi iYlOI8HcizFOxL+WjlRS5qU+tmJNReNiZ2pHUSIsE8HgtoIR4AwkMNRO841hCtQJGRp75h6E9Lmcp /DtF6Q5tnt1Axn/uyZv2ndA/c5QFKlYa4+t+V2MlzAJ7/YVHYGvaMavGNucoFLOuqGZgs2bpbkApB WkPkEHAOZkwO4JG0zCjoeqyR+ocmY35CcYqR9qedbSoKVfK8GdyT9AJ599fQO4uyUSXzlHJyCQ70B F0S4mpmope5cb0vayhn27uNJj+YzG0gYBoyU3uKmYBbbtWnxzfo9Fa//Z6nbhX9413OyPje6clHD7 lwqXSU8vg==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kdhVh-0003lD-8I; Fri, 13 Nov 2020 22:25:39 +0000 Received: from casper.infradead.org ([90.155.50.34]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kdhNm-0000Hx-2o for linux-arm-kernel@merlin.infradead.org; Fri, 13 Nov 2020 22:17:26 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:Cc:To:From:Subject: References:Mime-Version:Message-Id:In-Reply-To:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=Ho6gRtmpYK99NRY0nqMDMTWGuBlu7BoOKXCHJVaPFrA=; b=ve07XVd1cwALrM9SdMaX5pTitX bm2XFt50c0A8hVpcYxtnv04LoSNzkTiffDTaR4IFPuBQu8wjbsolNqtlvIDgwQAmEe8aS1gmz93Tz 20PErQOmRMeJ1scPJLKigArp1+knicTYI2hSmcLiu5A2l1GB+2gEyiXMt3u8SaYlNhgfAdehHaSn1 fCZ8Kru2nmkmlRDsN6AZjS6wlqRuaqzkFi57S5g4VPZa8a+KX2Ny3JjHvcxN1T6XaMZL3AufBjAtX BIlGDBCYQOSSIZ5iKU/BNqhEKyPT/2pDSMUH15cpVR21NuBQGOoSXWinLbI9kUE6I4JGcCfd7L67q wj+GcJfw==; Received: from mail-ej1-x649.google.com ([2a00:1450:4864:20::649]) by casper.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kdhNg-0008BX-PQ for linux-arm-kernel@lists.infradead.org; Fri, 13 Nov 2020 22:17:24 +0000 Received: by mail-ej1-x649.google.com with SMTP id v21so4910404ejy.3 for ; Fri, 13 Nov 2020 14:17:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=Ho6gRtmpYK99NRY0nqMDMTWGuBlu7BoOKXCHJVaPFrA=; b=MPUYatJk7xzMsJxOWFcCa6BB02oLTJrhQbX6JQ7njNHRxutqkHM7Nf/rADloq7EOOd 1bOP73z+zEvZcc1/Rza5+PLrWfrHzsx3Kn4X1K/wF10kJV3wK/zwjavAis3RZUrC9Q86 07FBoH1iNkLllu20EOOOye3QtXKS23Wfg8XEt7QTHyh6fAaZiu0SLHyOB/k4Gj4ntaVd WHsmuRr9rs2GdcUzkKJqwMc/7K31SR26FRmxo5AcmO/+eHUOYTBsQAlHePI8VC7wVVio IqAAfiyCa2Qvhhp5ZIHWfyp4U2bFFecERfDNQTImby6l6XCKmRDgBDrmJJVq5IdYfD6Q RmYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=Ho6gRtmpYK99NRY0nqMDMTWGuBlu7BoOKXCHJVaPFrA=; b=cjdoDMH6QXry5ykYIzJgiXdi6xxiRTt8P+j0Yd1QvdcLZ6slDucqYb07tpnFjnSe6C 072qHayW6PJ8IN/U38uVPZEcJtFtT62cULJKFAo1kWu5iIh5oScU30XiXCPHUkC7X9ms iF24UvQMAEH0Rig7x3dbmgvo/5a2GjoIzTFc43fvpFxHFgxaLpPm/fiyFFmnmAaic6Vg h6Guvor6y5DNMekRLpMqPyvpK+HLEyj5q35Ify6+UANIMBKNr0a2OgN9UTNYDv1v8e4V Jh7WNKQ5Y3lxUcLp+lIVmzQcFZOV+RBKHg2S/E8Oj16n2pbJFnaxbBGzxkHUtWrDPxyo 5Kvg== X-Gm-Message-State: AOAM5317DTnGzSavBtg7O6b6qkbxhgcZ15DHs+Cz+2WVXtwPr3hOdWfq KRqKJj0SdibWzM4LZMopXdJsAxd5AOWFACaF X-Google-Smtp-Source: ABdhPJwwDdmdZNdEOH2L6Fb2cJeAT+sPEneLl017puSQqEBadhnH3ZkI6kBSMGHj9GT8pFPmpfdJrjnS9Zn7EaOn X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:a05:6402:141:: with SMTP id s1mr4617132edu.87.1605305837947; Fri, 13 Nov 2020 14:17:17 -0800 (PST) Date: Fri, 13 Nov 2020 23:15:54 +0100 In-Reply-To: Message-Id: <18bca1ff61bf6605289e7213153b3fd5b8f81e27.1605305705.git.andreyknvl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.29.2.299.gdc1121823c-goog Subject: [PATCH mm v10 26/42] arm64: mte: Reset the page tag in page->flags From: Andrey Konovalov To: Andrew Morton X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201113_221721_222697_1C80AD9E X-CRM114-Status: GOOD ( 18.01 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-arm-kernel@lists.infradead.org, Marco Elver , Catalin Marinas , Kevin Brodsky , Will Deacon , Branislav Rankov , kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Alexander Potapenko , Evgenii Stepanov , Andrey Konovalov , Andrey Ryabinin , Vincenzo Frascino , Dmitry Vyukov Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Vincenzo Frascino The hardware tag-based KASAN for compatibility with the other modes stores the tag associated to a page in page->flags. Due to this the kernel faults on access when it allocates a page with an initial tag and the user changes the tags. Reset the tag associated by the kernel to a page in all the meaningful places to prevent kernel faults on access. Note: An alternative to this approach could be to modify page_to_virt(). This though could end up being racy, in fact if a CPU checks the PG_mte_tagged bit and decides that the page is not tagged but another CPU maps the same with PROT_MTE and becomes tagged the subsequent kernel access would fail. Signed-off-by: Vincenzo Frascino Signed-off-by: Andrey Konovalov Reviewed-by: Catalin Marinas --- Change-Id: I8451d438bb63364de2a3e68041e3a27866921d4e --- arch/arm64/kernel/hibernate.c | 5 +++++ arch/arm64/kernel/mte.c | 9 +++++++++ arch/arm64/mm/copypage.c | 9 +++++++++ arch/arm64/mm/mteswap.c | 9 +++++++++ 4 files changed, 32 insertions(+) diff --git a/arch/arm64/kernel/hibernate.c b/arch/arm64/kernel/hibernate.c index 42003774d261..9c9f47e9f7f4 100644 --- a/arch/arm64/kernel/hibernate.c +++ b/arch/arm64/kernel/hibernate.c @@ -371,6 +371,11 @@ static void swsusp_mte_restore_tags(void) unsigned long pfn = xa_state.xa_index; struct page *page = pfn_to_online_page(pfn); + /* + * It is not required to invoke page_kasan_tag_reset(page) + * at this point since the tags stored in page->flags are + * already restored. + */ mte_restore_page_tags(page_address(page), tags); mte_free_tag_storage(tags); diff --git a/arch/arm64/kernel/mte.c b/arch/arm64/kernel/mte.c index 8f99c65837fd..86d554ce98b6 100644 --- a/arch/arm64/kernel/mte.c +++ b/arch/arm64/kernel/mte.c @@ -34,6 +34,15 @@ static void mte_sync_page_tags(struct page *page, pte_t *ptep, bool check_swap) return; } + page_kasan_tag_reset(page); + /* + * We need smp_wmb() in between setting the flags and clearing the + * tags because if another thread reads page->flags and builds a + * tagged address out of it, there is an actual dependency to the + * memory access, but on the current thread we do not guarantee that + * the new page->flags are visible before the tags were updated. + */ + smp_wmb(); mte_clear_page_tags(page_address(page)); } diff --git a/arch/arm64/mm/copypage.c b/arch/arm64/mm/copypage.c index 70a71f38b6a9..b5447e53cd73 100644 --- a/arch/arm64/mm/copypage.c +++ b/arch/arm64/mm/copypage.c @@ -23,6 +23,15 @@ void copy_highpage(struct page *to, struct page *from) if (system_supports_mte() && test_bit(PG_mte_tagged, &from->flags)) { set_bit(PG_mte_tagged, &to->flags); + page_kasan_tag_reset(to); + /* + * We need smp_wmb() in between setting the flags and clearing the + * tags because if another thread reads page->flags and builds a + * tagged address out of it, there is an actual dependency to the + * memory access, but on the current thread we do not guarantee that + * the new page->flags are visible before the tags were updated. + */ + smp_wmb(); mte_copy_page_tags(kto, kfrom); } } diff --git a/arch/arm64/mm/mteswap.c b/arch/arm64/mm/mteswap.c index c52c1847079c..7c4ef56265ee 100644 --- a/arch/arm64/mm/mteswap.c +++ b/arch/arm64/mm/mteswap.c @@ -53,6 +53,15 @@ bool mte_restore_tags(swp_entry_t entry, struct page *page) if (!tags) return false; + page_kasan_tag_reset(page); + /* + * We need smp_wmb() in between setting the flags and clearing the + * tags because if another thread reads page->flags and builds a + * tagged address out of it, there is an actual dependency to the + * memory access, but on the current thread we do not guarantee that + * the new page->flags are visible before the tags were updated. + */ + smp_wmb(); mte_restore_page_tags(page_address(page), tags); return true;