Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp6434905ybl; Wed, 15 Jan 2020 04:42:44 -0800 (PST) X-Google-Smtp-Source: APXvYqyRZtB12+K4CSCupY8thxd5b9vfk7g6TqBAlgrIs8etKmPMyB3we6MyjKb1c6pbZsLpsl4Y X-Received: by 2002:a9d:6b91:: with SMTP id b17mr2589992otq.321.1579092164050; Wed, 15 Jan 2020 04:42:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579092164; cv=none; d=google.com; s=arc-20160816; b=K29QKzHaQe1q6M5fiYgGm2KF9cujbW20AX3R4Y6l0h5uuDgc0dzD5hb4OmyA4uBv4/ jABpAqR7rS2gY0JOmLHqWhDXULcNeSBGxNy3KYf4F5MCrNKHbbozrVwAgkumzowoKQKO byreiY+6Tozg7vUXVlo8Lt1oYxqnwRmk51SpAoXtx2zKnrROIv6R0KqAWtHmMPXIiIfZ YbgbhD3E4D7XgBma3hsDG8whHqkdzZNXcQTFSMkrZ288ThL2DyUYXd2SP7Wh9/ecaPd6 KLX6Q9GyXnJbVhYnqoUWu/866sk5Vsuu99wXBOF2Z5MqlPQ3pLc1LFweg6/MsftbYs5H pY3w== 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:cc:to:from :dkim-signature; bh=knALKMo/vBzqMljTSE6d/V4+yMOj9hGNxPEn5ah+ySo=; b=sy5eWIWyanK5+II3Nir7WK7sspNV7KfAYFgng1/VnPraL7kzC9X5YkuqX9w979OioU yMxIxntW/M/3EjCDDnoR7yrodKWjCEAGbLUU3wAi+XGxVmoG+n3cud7x2I3BguUFibDs GhRRsA5FxbHyLHQ9IUoB07Zq5wPASUJHUMooPn6miIjL1KazK4ZHjhkCrW+pJY/SXCW1 sYQ13/x9ZprZTYE2CU0RjcooOaUDRde83vjWyFLDVceAvg2Rcal/o4jnmNVNdVQzvaZ6 r8t8nAJEXVPTxkkn3F3ELkvkTtvoDRlUMipdrbjLgNN5VeUI/k4AJTz8DRxUrvBEEhNA SqpQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail (test mode) header.i=@shipmail.org header.s=mail header.b=sP1irSv1; 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 w4si10100090otp.30.2020.01.15.04.42.31; Wed, 15 Jan 2020 04:42:44 -0800 (PST) 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 (test mode) header.i=@shipmail.org header.s=mail header.b=sP1irSv1; 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 S1729016AbgAOMl2 (ORCPT + 99 others); Wed, 15 Jan 2020 07:41:28 -0500 Received: from pio-pvt-msa1.bahnhof.se ([79.136.2.40]:55060 "EHLO pio-pvt-msa1.bahnhof.se" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726310AbgAOMlZ (ORCPT ); Wed, 15 Jan 2020 07:41:25 -0500 Received: from localhost (localhost [127.0.0.1]) by pio-pvt-msa1.bahnhof.se (Postfix) with ESMTP id 4337A3F5B9; Wed, 15 Jan 2020 13:41:22 +0100 (CET) Authentication-Results: pio-pvt-msa1.bahnhof.se; dkim=pass (1024-bit key; unprotected) header.d=shipmail.org header.i=@shipmail.org header.b="sP1irSv1"; dkim-atps=neutral X-Virus-Scanned: Debian amavisd-new at bahnhof.se X-Spam-Flag: NO X-Spam-Score: -2.099 X-Spam-Level: X-Spam-Status: No, score=-2.099 tagged_above=-999 required=6.31 tests=[BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no Received: from pio-pvt-msa1.bahnhof.se ([127.0.0.1]) by localhost (pio-pvt-msa1.bahnhof.se [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id aTovGtqjR91B; Wed, 15 Jan 2020 13:41:19 +0100 (CET) Received: from mail1.shipmail.org (h-205-35.A357.priv.bahnhof.se [155.4.205.35]) (Authenticated sender: mb878879) by pio-pvt-msa1.bahnhof.se (Postfix) with ESMTPA id F040C3F573; Wed, 15 Jan 2020 13:41:17 +0100 (CET) Received: from localhost.localdomain.localdomain (h-205-35.A357.priv.bahnhof.se [155.4.205.35]) by mail1.shipmail.org (Postfix) with ESMTPSA id 6A9DD36034F; Wed, 15 Jan 2020 13:41:17 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=shipmail.org; s=mail; t=1579092077; bh=NQPc7hArRXnkkg/rpqui/KurRt8CpyiG/SnSNxQvx/0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sP1irSv1VCas9e21Phz219by0e/e18VwMPlDPMeeBrj56LAyMFYA+tIM4sxbvR1Z0 E1myErsKLxDJmompwMASYFwTFfZzQuSwv1H+a+Te3SWoirTN7dv1//or0n3a1Wrdaf QaaSXqpKWDWJoLKSy3N4BnLojL2j6v4OZyI5BoFs= From: =?UTF-8?q?Thomas=20Hellstr=C3=B6m=20=28VMware=29?= To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org Cc: linux-graphics-maintainer@vmware.com, Thomas Hellstrom , Andrew Morton , Michal Hocko , "Matthew Wilcox (Oracle)" , "Kirill A. Shutemov" , Ralph Campbell , =?UTF-8?q?J=C3=A9r=C3=B4me=20Glisse?= , =?UTF-8?q?Christian=20K=C3=B6nig?= Subject: [PATCH v5 2/2] mm, drm/ttm: Fix vm page protection handling Date: Wed, 15 Jan 2020 13:41:07 +0100 Message-Id: <20200115124107.3845-3-thomas_os@shipmail.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20200115124107.3845-1-thomas_os@shipmail.org> References: <20200115124107.3845-1-thomas_os@shipmail.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Thomas Hellstrom TTM graphics buffer objects may, transparently to user-space, move between IO and system memory. When that happens, all PTEs pointing to the old location are zapped before the move and then faulted in again if needed. When that happens, the page protection caching mode- and encryption bits may change and be different from those of struct vm_area_struct::vm_page_prot. We were using an ugly hack to set the page protection correctly. Fix that and instead export and use vmf_insert_mixed_prot() or use vmf_insert_pfn_prot(). Also get the default page protection from struct vm_area_struct::vm_page_prot rather than using vm_get_page_prot(). This way we catch modifications done by the vm system for drivers that want write-notification. Cc: Andrew Morton Cc: Michal Hocko Cc: "Matthew Wilcox (Oracle)" Cc: "Kirill A. Shutemov" Cc: Ralph Campbell Cc: "Jérôme Glisse" Cc: "Christian König" Signed-off-by: Thomas Hellstrom Reviewed-by: Christian König Acked-by: Andrew Morton --- drivers/gpu/drm/ttm/ttm_bo_vm.c | 22 +++++++++++++++------- mm/memory.c | 1 + 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/drivers/gpu/drm/ttm/ttm_bo_vm.c b/drivers/gpu/drm/ttm/ttm_bo_vm.c index 3e8c3de91ae4..78bfab81cf04 100644 --- a/drivers/gpu/drm/ttm/ttm_bo_vm.c +++ b/drivers/gpu/drm/ttm/ttm_bo_vm.c @@ -173,7 +173,6 @@ vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf, pgoff_t num_prefault) { struct vm_area_struct *vma = vmf->vma; - struct vm_area_struct cvma = *vma; struct ttm_buffer_object *bo = vma->vm_private_data; struct ttm_bo_device *bdev = bo->bdev; unsigned long page_offset; @@ -244,7 +243,7 @@ vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf, goto out_io_unlock; } - cvma.vm_page_prot = ttm_io_prot(bo->mem.placement, prot); + prot = ttm_io_prot(bo->mem.placement, prot); if (!bo->mem.bus.is_iomem) { struct ttm_operation_ctx ctx = { .interruptible = false, @@ -260,7 +259,7 @@ vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf, } } else { /* Iomem should not be marked encrypted */ - cvma.vm_page_prot = pgprot_decrypted(cvma.vm_page_prot); + prot = pgprot_decrypted(prot); } /* @@ -283,11 +282,20 @@ vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf, pfn = page_to_pfn(page); } + /* + * Note that the value of @prot at this point may differ from + * the value of @vma->vm_page_prot in the caching- and + * encryption bits. This is because the exact location of the + * data may not be known at mmap() time and may also change + * at arbitrary times while the data is mmap'ed. + * See vmf_insert_mixed_prot() for a discussion. + */ if (vma->vm_flags & VM_MIXEDMAP) - ret = vmf_insert_mixed(&cvma, address, - __pfn_to_pfn_t(pfn, PFN_DEV)); + ret = vmf_insert_mixed_prot(vma, address, + __pfn_to_pfn_t(pfn, PFN_DEV), + prot); else - ret = vmf_insert_pfn(&cvma, address, pfn); + ret = vmf_insert_pfn_prot(vma, address, pfn, prot); /* Never error on prefaulted PTEs */ if (unlikely((ret & VM_FAULT_ERROR))) { @@ -319,7 +327,7 @@ vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf) if (ret) return ret; - prot = vm_get_page_prot(vma->vm_flags); + prot = vma->vm_page_prot; ret = ttm_bo_vm_fault_reserved(vmf, prot, TTM_BO_VM_NUM_PREFAULT); if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) return ret; diff --git a/mm/memory.c b/mm/memory.c index 269a8a871e83..0f262acd1018 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1798,6 +1798,7 @@ vm_fault_t vmf_insert_mixed_prot(struct vm_area_struct *vma, unsigned long addr, { return __vm_insert_mixed(vma, addr, pfn, pgprot, false); } +EXPORT_SYMBOL(vmf_insert_mixed_prot); vm_fault_t vmf_insert_mixed(struct vm_area_struct *vma, unsigned long addr, pfn_t pfn) -- 2.21.0