Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp2265524ybl; Thu, 29 Aug 2019 06:03:32 -0700 (PDT) X-Google-Smtp-Source: APXvYqwfFQLhZxcpjCe8DO0bQyS/S7pb9xCjBO6DJMVLPJ2iwmULq3WEMuXc7XbRdhve8BBGnlAm X-Received: by 2002:aa7:939c:: with SMTP id t28mr11708996pfe.111.1567083812504; Thu, 29 Aug 2019 06:03:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567083812; cv=none; d=google.com; s=arc-20160816; b=gCuiv94rwwgvfSqGaDQvMphqEgkvuNYGQt0MD72yGzFabGG8NcvmrRcg1T0mcoaFuX vlUZlvoIPQClc3TQ8qpjRWNffvJ5G6f8yyFEA38m8JVHxuUVVHPyF3fF52fhUy1bCaIW NpqRX+bIMfn5i+1qhUlWhJ87BE5OS9JIDhP4hzQTBhmBrQQ7wk6dcEP6ZkomwIDFUE8j Ejj6pUWOqD8pzTpp4FHtPiqx85K8i4qF11aw9iSp7phewg3u8qa1XCXzp20tWCr5NvKV 0/fKQJjgdFKDKOKwXyS49Zg3wcIJMyT+WGxWY74ytP81PjH53yniTNIWZllDRpxf4K4n IYsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=pHLnDmZD0s6VAxoom9eX3yqZhJzgwbuaLN61HQul4Gw=; b=mYIRZpdST4fJxzfzqQuSqHF5b+RQLKRNH3AbjQy5w4gQkj4EhfpnRWyDqnL3LA4XgU L2bSPiBe15m4amHWFrqgYwD9C7jJXGPGBZhVH/i+gu1xewKRGQu4JbA4ak2yehINczJ5 7bh8SJe6j8NHWYU5PGcXZuu7+oFNs1vfVT6X3RrNZuIp9a8+iLbzpLJfZrN//AgHz774 9dqT+bY/3y7xW/fa0rv1GTzWdJ+a1FJZTVfi9jk36ni8jinsGx/wKhQ+4HF6J2wiIgTg IxF6hi+pQ51knrofrJsRe1zyudU1RqCXnuHRQN6N3359QD44wWniGQde5sSinhhPlppZ Qxvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b="sZ1zb/0V"; 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 a5si2512155pfb.240.2019.08.29.06.03.09; Thu, 29 Aug 2019 06:03:32 -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=fail header.i=@infradead.org header.s=bombadil.20170209 header.b="sZ1zb/0V"; 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 S1727046AbfH2NBz (ORCPT + 99 others); Thu, 29 Aug 2019 09:01:55 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:45128 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726739AbfH2NBy (ORCPT ); Thu, 29 Aug 2019 09:01:54 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=pHLnDmZD0s6VAxoom9eX3yqZhJzgwbuaLN61HQul4Gw=; b=sZ1zb/0V/xNREYxYGYBd5QwEz 3kT6f9Uyd+G2C3upWo/X/0zXpOzv31DubPkO3FHOe+6QH00K21V0x7Z9rxh1Ps7DlEPN5x1xYO5ql oIyHlq6KYO3McJVcJwYCRZQ6hCK+Xw69Vcy9sXzNPIgfMagVa23yC1+P8R8Xj1oo+qCrRxdGNh9Ac oUrZ9QGH0uJFH+VFmAUaIBeKUeyGfbnQcARNXpt0C6DlwMQYajZ8p+oMVe9JHCjims9K8w3LIKf35 /7Qjpm/OEJSWW3+kBToei20IpPox+xPmv0hWLl9tj5vm+rHPWRKRJVzWQIDTYgEeq+1ufVktwYuaa yCmbHisoQ==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by bombadil.infradead.org with esmtpsa (Exim 4.92 #3 (Red Hat Linux)) id 1i3K3V-0007Sz-32; Thu, 29 Aug 2019 13:01:37 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 16525301167; Thu, 29 Aug 2019 15:01:00 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 4D21A20C9570C; Thu, 29 Aug 2019 15:01:34 +0200 (CEST) Date: Thu, 29 Aug 2019 15:01:34 +0200 From: Peter Zijlstra To: Thomas Gleixner Cc: Song Liu , Dave Hansen , LKML , "x86@kernel.org" , Joerg Roedel , Andy Lutomirski , Rik van Riel , Steven Rostedt Subject: Re: [PATCH] x86/mm/cpa: Prevent large page split when ftrace flips RW on kernel text Message-ID: <20190829130134.GS2369@hirez.programming.kicks-ass.net> References: <20190828142445.454151604@linutronix.de> <20190828143123.971884723@linutronix.de> <55bb026c-5d54-6ebf-608f-3f376fbec4e5@intel.com> <309E5006-E869-4761-ADE2-ADB7A1A63FF1@fb.com> <9B34E971-20ED-4A58-B086-AB94990B5A26@fb.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Aug 29, 2019 at 12:31:34AM +0200, Thomas Gleixner wrote: > arch/x86/mm/pageattr.c | 26 ++++++++++++++++++-------- > 1 file changed, 18 insertions(+), 8 deletions(-) > > --- a/arch/x86/mm/pageattr.c > +++ b/arch/x86/mm/pageattr.c > @@ -516,7 +516,7 @@ static inline void check_conflict(int wa > */ > static inline pgprot_t static_protections(pgprot_t prot, unsigned long start, > unsigned long pfn, unsigned long npg, > - int warnlvl) > + unsigned long lpsize, int warnlvl) > { > pgprotval_t forbidden, res; > unsigned long end; > @@ -535,9 +535,17 @@ static inline pgprot_t static_protection > check_conflict(warnlvl, prot, res, start, end, pfn, "Text NX"); > forbidden = res; > > - res = protect_kernel_text_ro(start, end); > - check_conflict(warnlvl, prot, res, start, end, pfn, "Text RO"); > - forbidden |= res; > + /* > + * Special case to preserve a large page. If the change spawns the > + * full large page mapping then there is no point to split it > + * up. Happens with ftrace and is going to be removed once ftrace > + * switched to text_poke(). > + */ > + if (lpsize != (npg * PAGE_SIZE) || (start & (lpsize - 1))) { > + res = protect_kernel_text_ro(start, end); > + check_conflict(warnlvl, prot, res, start, end, pfn, "Text RO"); > + forbidden |= res; > + } Right, so this allows the RW (doesn't enforce RO) and thereby doesn't force split, when it is a whole large page. > > /* Check the PFN directly */ > res = protect_pci_bios(pfn, pfn + npg - 1); > @@ -819,7 +827,7 @@ static int __should_split_large_page(pte > * extra conditional required here. > */ > chk_prot = static_protections(old_prot, lpaddr, old_pfn, numpages, > - CPA_CONFLICT); > + psize, CPA_CONFLICT); > > if (WARN_ON_ONCE(pgprot_val(chk_prot) != pgprot_val(old_prot))) { > /* > @@ -855,7 +863,7 @@ static int __should_split_large_page(pte > * protection requirement in the large page. > */ > new_prot = static_protections(req_prot, lpaddr, old_pfn, numpages, > - CPA_DETECT); > + psize, CPA_DETECT); > > /* > * If there is a conflict, split the large page. And these are the callsites in __should_split_large_page(), and you provide psize, and therefore we allow RW to preserve the large pages on the kernel text. > @@ -906,7 +914,8 @@ static void split_set_pte(struct cpa_dat > if (!cpa->force_static_prot) > goto set; > > - prot = static_protections(ref_prot, address, pfn, npg, CPA_PROTECT); > + /* Hand in lpsize = 0 to enforce the protection mechanism */ > + prot = static_protections(ref_prot, address, pfn, npg, 0, CPA_PROTECT); This is when we've already decided to split, in which case we might as well enforce the normal rules, and .lpsize=0 does just that. > > if (pgprot_val(prot) == pgprot_val(ref_prot)) > goto set; > @@ -1503,7 +1512,8 @@ static int __change_page_attr(struct cpa > pgprot_val(new_prot) |= pgprot_val(cpa->mask_set); > > cpa_inc_4k_install(); > - new_prot = static_protections(new_prot, address, pfn, 1, > + /* Hand in lpsize = 0 to enforce the protection mechanism */ > + new_prot = static_protections(new_prot, address, pfn, 1, 0, > CPA_PROTECT); And here we check the protections of a single 4k page, in which case large pages are irrelevant and again .lpsize=0 disables the new code. > > new_prot = pgprot_clear_protnone_bits(new_prot); That all seems OK I suppose. Acked-by: Peter Zijlstra (Intel)