Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2906240rwd; Fri, 19 May 2023 11:50:05 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5Agmzqie82Oqy38eR4fnJtRQBPI0VvgiD+GRIhJwEzpze+WtU8l6RTUrSypsJv6/hUFgJq X-Received: by 2002:a17:90a:bd18:b0:252:cd29:d3db with SMTP id y24-20020a17090abd1800b00252cd29d3dbmr2689108pjr.39.1684522205554; Fri, 19 May 2023 11:50:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684522205; cv=none; d=google.com; s=arc-20160816; b=IH7bnTvGChrXcB9xX2JefnR8Ck6sGDbVrbHWnpHeRJb1qtiUfFop5qWkCAf0mtdnvQ 0HbdsrADRFkRkUUKoGJ4j88fdx+J1+kNDbcpvcu80sA56I52JETqUAr6BdAonn0iZnmu ypEGTIR+h88xjqxCCNI9uSKH+0A2HyUwqq86WtcZj2KwEGtvrJIDHsIN/GsGtaINylWY JsS7xHQwo+ItWcxUJsVnmG6NCyeEdjohXQH81HooAwV+h+IgVIJdHAONX3MgNwCl64vP hSEwjBZB+wH61eZkFl0wmG6yWC1Iarp3B+n5po4K3K+JHufMN33lLKxObC2nWk0Q8g2j PyXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=/G2RrEZ/8SH1q13PYBZLMyiEdL9DZdM/KdpotQJGDk4=; b=ZkRtDdEyoWz6Wgu8KNXpSMzFcWaf49bMnDwHND4WSz1flQn8WpON4b9nMXm8plsMBF 3qBjqVtoyhruGiRuaK8/HBz5mzXFNJIFaMBSWHvaH63PRNu/2ETDl+ify8LR45+QGpIm kOr8niQz85pxSauo3i3dPQIjoOmuHt0UACbf0Q4FdOPi2k5oz+6nOo88FMBWg7miCt5G p1taPi+hoOAkIvL3hlJr9Tx0lQ8wbzpd/sOOH3R9wfyhSDVBMSvaoo18y3yxfeGMuQx1 Nn1+4FTwh4/vPIxXIX+InVbJy0ehmN4stuANfd71UmQWVoH38OLCOL8oRHiQWS7qkRfI hX5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=doH2m8a7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s1-20020a17090a760100b002509d96227esi2027361pjk.173.2023.05.19.11.49.50; Fri, 19 May 2023 11:50:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=doH2m8a7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229928AbjESSha (ORCPT + 99 others); Fri, 19 May 2023 14:37:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40210 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229533AbjESSh2 (ORCPT ); Fri, 19 May 2023 14:37:28 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A48A91A6 for ; Fri, 19 May 2023 11:37:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1684521447; x=1716057447; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=OdZfP7pbImUjYsnrLdKphdx9iuKXkY5Ud76I5TW4z4g=; b=doH2m8a79eojD+dmjdPsdHjHRG3FNaMguXXqQ1XIERE/xkovtEsgIy+l YrkKRxmeSHk/6epRQ9kP4Sa+S6oQOFSpwelIKdaNprUwSkSbBlPod9m+d aSP44Ch6lO17pXEpoF6sjZ6vl9pgaNDB8STTyb+Ig2T1r/PexBtvSQ3Yk 9PO13SuYTGRcCHV+VlXNgi7Ek3nR8GwZSwbGvQg2z+FlYyMIZWqHPRs6X 60zXYWITnw0iNKMjTjkILYHnM+RDDZwfCNPlZbAjzJEK5bnBSFT1GB+4O E8WUf4Hm/8H+o3muX5R5xe5eNo4cmtG9PSTow948slUbp9330eQxTW6/f g==; X-IronPort-AV: E=McAfee;i="6600,9927,10715"; a="438802940" X-IronPort-AV: E=Sophos;i="6.00,177,1681196400"; d="scan'208";a="438802940" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 May 2023 11:37:27 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10715"; a="876935457" X-IronPort-AV: E=Sophos;i="6.00,177,1681196400"; d="scan'208";a="876935457" Received: from jkrzyszt-mobl2.ger.corp.intel.com ([10.213.31.132]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 May 2023 11:37:23 -0700 From: Janusz Krzysztofik To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen Cc: x86@kernel.org, "H. Peter Anvin" , Andrew Morton , David Hildenbrand , Yu Zhao , Juergen Gross , linux-kernel@vger.kernel.org, =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= , Andi Shyti , drm-intel@lists.freedesktop.org, dri-devel@lists.freedesktop.org Subject: [RESUBMIT][PATCH] x86/mm: Fix PAT bit missing from page protection modify mask Date: Fri, 19 May 2023 20:36:34 +0200 Message-Id: <20230519183634.190364-1-janusz.krzysztofik@linux.intel.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Visible glitches have been observed when running graphics applications on Linux under Xen hypervisor. Those observations have been confirmed with failures from kms_pwrite_crc Intel GPU test that verifies data coherency of DRM frame buffer objects using hardware CRC checksums calculated by display controllers, exposed to userspace via debugfs. Affected processing paths have then been identified with new IGT test variants that mmap the objects using different methods and caching modes [1]. When running as a Xen PV guest, Linux uses Xen provided PAT configuration which is different from its native one. In particular, Xen specific PTE encoding of write-combining caching, likely used by graphics applications, differs from the Linux default one found among statically defined minimal set of supported modes. Since Xen defines PTE encoding of the WC mode as _PAGE_PAT, it no longer belongs to the minimal set, depends on correct handling of _PAGE_PAT bit, and can be mismatched with write-back caching. When a user calls mmap() for a DRM buffer object, DRM device specific .mmap file operation, called from mmap_region(), takes care of setting PTE encoding bits in a vm_page_prot field of an associated virtual memory area structure. Unfortunately, _PAGE_PAT bit is not preserved when the vma's .vm_flags are then applied to .vm_page_prot via vm_set_page_prot(). Bits to be preserved are determined with _PAGE_CHG_MASK symbol that doesn't cover _PAGE_PAT. As a consequence, WB caching is requested instead of WC when running under Xen (also, WP is silently changed to WT, and UC downgraded to UC_MINUS). When running on bare metal, WC is not affected, but WP and WT extra modes are unintentionally replaced with WC and UC, respectively. WP and WT modes, encoded with _PAGE_PAT bit set, were introduced by commit 281d4078bec3 ("x86: Make page cache mode a real type"). Care was taken to extend _PAGE_CACHE_MASK symbol with that additional bit, but that symbol has never been used for identification of bits preserved when applying page protection flags. Support for all cache modes under Xen, including the problematic WC mode, was then introduced by commit 47591df50512 ("xen: Support Xen pv-domains using PAT"). Extend bitmask used by pgprot_modify() for selecting bits to be preserved with _PAGE_PAT bit. However, since that bit can be reused as _PAGE_PSE, and the _PAGE_CHG_MASK symbol, primarly used by pte_modify(), is likely intentionally defined with that bit not set, keep that symbol unchanged. [1] https://gitlab.freedesktop.org/drm/igt-gpu-tools/-/commit/0f0754413f14 Closes: https://gitlab.freedesktop.org/drm/intel/-/issues/7648 Fixes: 281d4078bec3 ("x86: Make page cache mode a real type") Signed-off-by: Janusz Krzysztofik Tested-by: Marek Marczykowski-Górecki Reviewed-by: Andi Shyti Cc: stable@vger.kernel.org # v3.19+ --- arch/x86/include/asm/pgtable.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index 15ae4d6ba4768..56466afd04307 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -654,8 +654,10 @@ static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot) #define pgprot_modify pgprot_modify static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot) { - pgprotval_t preservebits = pgprot_val(oldprot) & _PAGE_CHG_MASK; - pgprotval_t addbits = pgprot_val(newprot) & ~_PAGE_CHG_MASK; + unsigned long mask = _PAGE_CHG_MASK | _PAGE_CACHE_MASK; + + pgprotval_t preservebits = pgprot_val(oldprot) & mask; + pgprotval_t addbits = pgprot_val(newprot) & ~mask; return __pgprot(preservebits | addbits); } -- 2.40.1