From patchwork Mon Nov 18 12:19:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Krzysztof Karas X-Patchwork-Id: 13878469 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 36C1FD4920F for ; Mon, 18 Nov 2024 12:19:37 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C221910E446; Mon, 18 Nov 2024 12:19:36 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.b="F4AyT5Mr"; dkim-atps=neutral Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) by gabe.freedesktop.org (Postfix) with ESMTPS id B4A3F10E446 for ; Mon, 18 Nov 2024 12:19:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1731932375; x=1763468375; h=date:from:to:cc:subject:message-id:mime-version; bh=cRRFbn6l4zkJdCXXG/o3CAM1rbnXNXV0ouCktMoHJJE=; b=F4AyT5MrIUIKW7gX0dZtkFksIli4A/+FOZVNyB2XaM/Xq9TWLUNIjxW5 S7IMVLFfWtglqXMF30SDp9Vrbjlsne75+sGAFj0UyfQHAMz9/S/NPNVLJ XTn++7fPlkNzUlvmhQwOkk/sdhUiOGbgaJJgJXiiLzn1YQZ/j+tkxWY6f i7rLwX4u86ryNiOcHooL66wTOK0veCYF1C8nF3ITjHjmnVXs1++108kiZ 0spClnk+VzCj02Y/gAAZaS+0W10lUsiTWUxUOFdpAgO0f5cK3ugtlcxAm M/124ZK6rUKPMCZy1bmT3yFKMWfnMpIoi2/0slhzm+6XReGxHin88L5xM Q==; X-CSE-ConnectionGUID: 8jxDBqJJSG6qfE57uNnR2g== X-CSE-MsgGUID: kMC/h27pTIK/sAh9272Xlg== X-IronPort-AV: E=McAfee;i="6700,10204,11259"; a="32024356" X-IronPort-AV: E=Sophos;i="6.12,164,1728975600"; d="scan'208";a="32024356" Received: from orviesa008.jf.intel.com ([10.64.159.148]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Nov 2024 04:19:35 -0800 X-CSE-ConnectionGUID: M0HsB6CiRrqzfODjw9owSQ== X-CSE-MsgGUID: 5aqte5NyTCaQFmKR3YxxFA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.12,164,1728975600"; d="scan'208";a="90003688" Received: from orsmsx601.amr.corp.intel.com ([10.22.229.14]) by orviesa008.jf.intel.com with ESMTP/TLS/AES256-GCM-SHA384; 18 Nov 2024 04:19:36 -0800 Received: from orsmsx601.amr.corp.intel.com (10.22.229.14) by ORSMSX601.amr.corp.intel.com (10.22.229.14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Mon, 18 Nov 2024 04:19:35 -0800 Received: from ORSEDG601.ED.cps.intel.com (10.7.248.6) by orsmsx601.amr.corp.intel.com (10.22.229.14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39 via Frontend Transport; Mon, 18 Nov 2024 04:19:35 -0800 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (104.47.74.40) by edgegateway.intel.com (134.134.137.102) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.39; Mon, 18 Nov 2024 04:19:34 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=oON5MOeiSeKcXD5kVX8PwwE+T/tXOLVYt8RYTpE+bVyX37BCh+MRvcQcd0za/qOCPGxpK5mTZ17QiqBfQO0ySTfeDXYqbUiYx45nx//4xYrzPSCBLN9H2S6hy/s7M7kiHj0SKDTPEhN1Iq5fonFhTVByR2IYjRsDo7hicM4DABJSP7WQPRtk8qX7HAv8zYoB3dmb0AQWdN+4kGK+dhnEgalUZszra1bolGG4nQLw2cTkzvpy18oM5GVK277/rsxJ/fPtk2vVI2H6zquYWVMATmCzwX3RcI7xAEhzGu6ho+WyzUnkxS0MVyUl3TgRGEFgvcBuR8uMsko0IgayLGpSdQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=J8PRco1lK9kj70xrA1lvWL+9OLd4xBP5kjYUAwxMsjk=; b=KejteCdc0IUdBQ/8HhZNVCXkF5cR5hu5paLBDtQoCTj69Ro7vr+oF5tJXolNsMh3r9jyac0nT9XTjiRJs6o7A9itztJQD3FO3uuoNJ00PxXrOE/W7d7t43moZOkoI7kZIrg5JUJhE9hfeiT7kDVz4b+ugo02sVvM7NQkD/FhJDieL+jYx+sCC5ldt05OcsFgi/B1oPav/d1iCE8sHPM5VgkCFf8DjJTcgGxdnDXofG9tAVikHdWYORVFuSKw4db6qZOno2DVdkHP2cLf03oRa3j5eYb5kKAc5nH3aaL7VXZ/8EKR32y5ln1RjQkGcSsb5YgdqxM3qL7oG8XadUlKRQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=intel.com; dmarc=pass action=none header.from=intel.com; dkim=pass header.d=intel.com; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=intel.com; Received: from CO1PR11MB5057.namprd11.prod.outlook.com (2603:10b6:303:6c::15) by PH8PR11MB6682.namprd11.prod.outlook.com (2603:10b6:510:1c5::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.24; Mon, 18 Nov 2024 12:19:31 +0000 Received: from CO1PR11MB5057.namprd11.prod.outlook.com ([fe80::4610:6d6c:9af6:2548]) by CO1PR11MB5057.namprd11.prod.outlook.com ([fe80::4610:6d6c:9af6:2548%7]) with mapi id 15.20.8158.023; Mon, 18 Nov 2024 12:19:31 +0000 Date: Mon, 18 Nov 2024 12:19:22 +0000 From: Krzysztof Karas To: CC: Subject: [PATCH v5] drm/i915: ensure segment offset never exceeds allowed max Message-ID: "Organization: Intel Technology Poland sp. z o.o. - ul. Slowackiego 173, 80-298 Gdansk - KRS 101882 - NIP 957-07-52-316" Content-Disposition: inline X-ClientProxiedBy: MI1P293CA0001.ITAP293.PROD.OUTLOOK.COM (2603:10a6:290:2::8) To CO1PR11MB5057.namprd11.prod.outlook.com (2603:10b6:303:6c::15) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1PR11MB5057:EE_|PH8PR11MB6682:EE_ X-MS-Office365-Filtering-Correlation-Id: dcdef329-7c10-4c17-eb49-08dd07cb4191 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|1800799024|366016|27256017; X-Microsoft-Antispam-Message-Info: =?utf-8?q?XTrylX0KGzoGM8iCLnN5sIpmuzJvSVR?= =?utf-8?q?oH+TSHN+OsL45VnB4gXU8i1l57jRmZJSYVyFQVY1HQD7s2ciebZQWGgY7V57Uk93k?= =?utf-8?q?kcWvaxVzz+sIbUoEfAFhqtU6pv1gSyww7tWv4v1ZSIdx1UpraUbl+9LV7izFz6qg9?= =?utf-8?q?I53m0h14lkY1dmYxXiWX5dhCmcdr/OfEPktv21TT8blWZHyYx98S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f-8?q?tn5XAo0kyg6BpUwMeYRctgFIzTo/OK6jyGfXkrhAgb9C8a2avaAwROR6gW8ogT07m?= =?utf-8?q?v5Ww+3M+GUk2+f3oz7itjgH3lTM1YILH7ouiHTE4h1RtMwzFUtHfj85VK7A9TlSgo?= =?utf-8?q?SFscP+fivd3mNyJGO16I0IgHshFrQcndHPg+zkCuthtyYqgOv4MVpPlb4=3D?= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CO1PR11MB5057.namprd11.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(376014)(1800799024)(366016)(27256017); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?Qfe54VuFozcN3fo10SpTybcjT7yx?= =?utf-8?q?Bn9yYab/+ZfuwVowVtRyluxE7GhnaU6Pmoajiyyvz7u6R8oUK1wnxlqg7iFSLdYln?= =?utf-8?q?qvoEXnVln2Bm4CJLUWeRY+MjOq1BRFgsZ9ZteFvUPAyN2Q1JQRf8uvo+TOyD8dHmY?= =?utf-8?q?4+7EfUwzk94p0Ja7QOBjmL6/qT7Z2T4oyPpJDwh0eN8n6aswmmJVbxOj5fJzSxW67?= =?utf-8?q?y57wSHYDFPzXrQChQD2t4TcLNXykYwSRvpU1TSytcHhDqQYi4vR5X9GG5TbtsDvHS?= =?utf-8?q?iTm8wqc6wo+sQS670kLMRfm1nhCRNTtGyk8LyjbAi4eNS3fEIfQPmE/I3PAEfYHoh?= =?utf-8?q?XLJBozq+EoZmKA+BcAEfLN6zeQmEV7DLL8WaiufdOdNGpf3zVlY6PEFCLPUOuhZMo?= =?utf-8?q?GfaIGJpfIwUwg5dxh1ba5vI2L7yLkzBgcP6qAKEk/JRL6cvvUHTucj8btf0pebPwk?= =?utf-8?q?59ttSjOxpL4zP3AhyPQaZt+sN+51y+E0uOgIhR8DnwJfvM+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LVHQ82SLh?= =?utf-8?q?eBv06pu0wK9ZhwsK53cGquicdrRr9ZW8ZHDm+YUwc+mwGTHk71vsBy9hImTGoItRI?= =?utf-8?q?ysMYUOIhkmwazZ+Wjm/0aPf/TbGC2ojgSgi3d6ZaTLugqE+F9Jy1Z7uACMF7Y03xH?= =?utf-8?q?m2VFNM7GGQpK4DroTuU/mRBAN7lT6ub/gA=3D=3D?= X-MS-Exchange-CrossTenant-Network-Message-Id: dcdef329-7c10-4c17-eb49-08dd07cb4191 X-MS-Exchange-CrossTenant-AuthSource: CO1PR11MB5057.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Nov 2024 12:19:31.7147 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 46c98d88-e344-4ed4-8496-4ed7712e255d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: pBN72OgEDwjlCkIFtnZNb42th79ummUN3zqnkdOWKXDMkDTawq4rA/jD76kvS5eoAamDTPsuqEgkMaNVYlA9DGHTBPRLLrXAO/t9Xaz3lXs= X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR11MB6682 X-OriginatorOrg: intel.com X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" Commit 255fc1703e42 ("drm/i915/gem: Calculate object page offset for partial memory mapping") introduced a new offset, which accounts for userspace mapping not starting from the beginning of object's scatterlist. This works fine for cases where first object pte is larger than the new offset - "r->sgt.curr" counter is set to the offset to match the difference in the number of total pages. However, if object's first pte's size is equal to or smaller than the offset, then information about the offset in userspace is covered up by moving "r->sgt" pointer in remap_sg(): r->sgt.curr += PAGE_SIZE; if (r->sgt.curr >= r->sgt.max) r->sgt = __sgt_iter(__sg_next(r->sgt.sgp), use_dma(r->iobase)); This means that two or more pages from virtual memory are counted for only one page in object's memory, because after moving "r->sgt" pointer "r->sgt.curr" will be 0. We should account for this mismatch by moving "r->sgt" pointer to the next pte. For that we may use "r.sgt.max", which already holds the max allowed size. This change also eliminates possible confusion, when looking at i915_scatterlist.h and remap_io_sg() code: former has scatterlist pointer definition, which differentiates "s.max" value based on "dma" flag (sg_dma_len() is used only when the flag is enabled), while latter uses sg_dma_len() indiscriminately. This patch aims to resolve issue: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/12031 v3: - instead of checking if r.sgt.curr would exceed allowed max, changed the value in the while loop to be aligned with `dma` value v4: - remove unnecessary parent relation v5: - update commit message with explanation about page counting mismatch and link to the issue Signed-off-by: Krzysztof Karas --- drivers/gpu/drm/i915/i915_mm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/i915/i915_mm.c b/drivers/gpu/drm/i915/i915_mm.c index f5c97a620962..76e2801619f0 100644 --- a/drivers/gpu/drm/i915/i915_mm.c +++ b/drivers/gpu/drm/i915/i915_mm.c @@ -143,8 +143,8 @@ int remap_io_sg(struct vm_area_struct *vma, /* We rely on prevalidation of the io-mapping to skip track_pfn(). */ GEM_BUG_ON((vma->vm_flags & EXPECTED_FLAGS) != EXPECTED_FLAGS); - while (offset >= sg_dma_len(r.sgt.sgp) >> PAGE_SHIFT) { - offset -= sg_dma_len(r.sgt.sgp) >> PAGE_SHIFT; + while (offset >= r.sgt.max >> PAGE_SHIFT) { + offset -= r.sgt.max >> PAGE_SHIFT; r.sgt = __sgt_iter(__sg_next(r.sgt.sgp), use_dma(iobase)); if (!r.sgt.sgp) return -EINVAL;