Message ID | 20221017093726.2070674-2-zhao1.liu@linux.intel.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1354796wrs; Mon, 17 Oct 2022 02:42:12 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4zF88+pQEk2XTwB4EQ/fdGnnaYaTa9FivhtPj+7u3HAHZhKyK3dNbakWKUEYT7LNuG6YSK X-Received: by 2002:a17:90a:c09:b0:20d:7820:2e60 with SMTP id 9-20020a17090a0c0900b0020d78202e60mr31013075pjs.180.1665999731919; Mon, 17 Oct 2022 02:42:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665999731; cv=none; d=google.com; s=arc-20160816; b=Y8fkcDxKviZnhQh5l8OBuO0pgCG1iLaal+enSqsnTES+ilwDr0UA4WywC+yzcXa/YM edVwOgIN54MMIPd8BVM3kWKuYn4d40ew/cHLZNUXIo7vbABigpibZvJLcpo6qxavHoJB FolO09KNii7pVKMrJnfVjt/UDT973IoHCtJCZhNb0Yo5bz9w7mncrF58hrU7rlGeaZAO oyC0f9yF5IBtEQVsjwvSxhcnlgVFHyjF+C9JQEHPGFqYd0QBh8bsX9DnKaul8UQmHr5Z kjVXcvYzAiZR/g5KpBNMS4hDW5RM4faMhpHqVW4kR4qfJPQ4f93l6IqyLtUmrDCwJW0i WHBQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=7og+DTe91fClZT7f2tVC9darcTNDjgHZDfbwFHs7768=; b=LyXyopc0Rd3m9QG+3jgnNd3I+Qg0cQvSFHEMdjI/qVYz6qXhGh8lg/j/uwmmwKt1mP SMbydyxP9eh4/8HgNniX1CIphZvHlrgM0reFf8pRMQnyDG27MGJ6Lts0JTmIq7Yp2hgy WBvZm5oNGVQ57UE6LQhw98NRb4xZeOjLiEbYvk9ipQAEHHHpSqVnSbI24QJGgtGghHgt Gl5eM8Z9To2O1q8/6TASP2mEgmAeBfdD9HhSZhqMbyYVVaZt9jxcWkVKj4EnbGc6Mzuj G4hEl3a5MlxhR4EQAwTAktJH1yysP095TzcKLIA6vPJ4aIlBQJgVNYliM/uCTJm38fV5 nx1w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=byAIm+CZ; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 69-20020a630448000000b0046af6471adasi12082629pge.282.2022.10.17.02.41.58; Mon, 17 Oct 2022 02:42:11 -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=byAIm+CZ; 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 S231276AbiJQJcf (ORCPT <rfc822;ouuuleilei@gmail.com> + 99 others); Mon, 17 Oct 2022 05:32:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231225AbiJQJc2 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 17 Oct 2022 05:32:28 -0400 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ADDC941D03 for <linux-kernel@vger.kernel.org>; Mon, 17 Oct 2022 02:32: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=1665999147; x=1697535147; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=tiW6Fpm0hVgB6aQj9aJLze7KS0YilKpmQTotRlfBNY0=; b=byAIm+CZyqpTzAvFjOrdoEfFq1gPFEXGD2l04JB16wzJKzd0NHZoP/GN JiUy9STG+cSEMWAIJa1qAluPP11aa6hqBQTno6G2aAehNqhctoEVWV19u qj1o3pfk55vYhaVKVaC3pAgCkvuYbXHGIKfcbozNTASnLYv/YH8TGvEhD k0bIGc7W/69qubuimCi8e8Xbv4zRAPUUJz0U2tPUXhLXi5XMEm23gfj5+ 8DJCziH9HnNuMAeNGdQ9FhA40uGmMuEK/uyBuBMVAjBW9yo5wq8uw+xaJ lJviY/ZAJB8m8PKkT5Uro5P8aytCrtwG9ybytQdwMrvzSwWuvEaawTTUh w==; X-IronPort-AV: E=McAfee;i="6500,9779,10502"; a="305741556" X-IronPort-AV: E=Sophos;i="5.95,191,1661842800"; d="scan'208";a="305741556" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Oct 2022 02:32:27 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10502"; a="717431213" X-IronPort-AV: E=Sophos;i="5.95,191,1661842800"; d="scan'208";a="717431213" Received: from liuzhao-optiplex-7080.sh.intel.com ([10.239.160.132]) by FMSMGA003.fm.intel.com with ESMTP; 17 Oct 2022 02:32:23 -0700 From: Zhao Liu <zhao1.liu@linux.intel.com> To: Jani Nikula <jani.nikula@linux.intel.com>, Joonas Lahtinen <joonas.lahtinen@linux.intel.com>, Rodrigo Vivi <rodrigo.vivi@intel.com>, Tvrtko Ursulin <tvrtko.ursulin@linux.intel.com>, David Airlie <airlied@gmail.com>, Daniel Vetter <daniel@ffwll.ch>, Matthew Auld <matthew.auld@intel.com>, =?utf-8?q?Thomas_Hellstr=C3=B6m?= <thomas.hellstrom@linux.intel.com>, Nirmoy Das <nirmoy.das@intel.com>, Maarten Lankhorst <maarten.lankhorst@linux.intel.com>, Chris Wilson <chris@chris-wilson.co.uk>, =?utf-8?q?Christian_K=C3=B6nig?= <christian.koenig@amd.com>, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org Cc: Ira Weiny <ira.weiny@intel.com>, "Fabio M . De Francesco" <fmdefrancesco@gmail.com>, Zhenyu Wang <zhenyu.z.wang@intel.com>, Zhao Liu <zhao1.liu@intel.com>, Dave Hansen <dave.hansen@intel.com> Subject: [PATCH 1/9] drm/i915: Use kmap_local_page() in gem/i915_gem_object.c Date: Mon, 17 Oct 2022 17:37:17 +0800 Message-Id: <20221017093726.2070674-2-zhao1.liu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221017093726.2070674-1-zhao1.liu@linux.intel.com> References: <20221017093726.2070674-1-zhao1.liu@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_HI,SPF_HELO_NONE, SPF_NONE 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1746927334747460565?= X-GMAIL-MSGID: =?utf-8?q?1746927334747460565?= |
Series |
drm/i915: Replace kmap_atomic() with kmap_local_page()
|
|
Commit Message
Zhao Liu
Oct. 17, 2022, 9:37 a.m. UTC
From: Zhao Liu <zhao1.liu@intel.com> The use of kmap_atomic() is being deprecated in favor of kmap_local_page()[1]. The main difference between atomic and local mappings is that local mappings doesn't disable page faults or preemption. There're 2 reasons why i915_gem_object_read_from_page_kmap() doesn't need to disable pagefaults and preemption for mapping: 1. The flush operation is safe for CPU hotplug when preemption is not disabled. In drm/i915/gem/i915_gem_object.c, the function i915_gem_object_read_from_page_kmap() calls drm_clflush_virt_range() to use CLFLUSHOPT or WBINVD to flush. Since CLFLUSHOPT is global on x86 and WBINVD is called on each cpu in drm_clflush_virt_range(), the flush operation is global and any issue with cpu's being added or removed can be handled safely. 2. Any context switch caused by preemption or sleep (pagefault may cause sleep) doesn't affect the validity of local mapping. Therefore, i915_gem_object_read_from_page_kmap() is a function where the use of kmap_local_page() in place of kmap_atomic() is correctly suited. Convert the calls of kmap_atomic() / kunmap_atomic() to kmap_local_page() / kunmap_local(). And remove the redundant variable that stores the address of the mapped page since kunmap_local() can accept any pointer within the page. [1]: https://lore.kernel.org/all/20220813220034.806698-1-ira.weiny@intel.com Suggested-by: Dave Hansen <dave.hansen@intel.com> Suggested-by: Ira Weiny <ira.weiny@intel.com> Suggested-by: Fabio M. De Francesco <fmdefrancesco@gmail.com> Signed-off-by: Zhao Liu <zhao1.liu@intel.com> --- Suggested by credits: Dave: Referred to his explanation about cache flush. Ira: Referred to his task document, review comments and explanation about cache flush. Fabio: Referred to his boiler plate commit message. --- drivers/gpu/drm/i915/gem/i915_gem_object.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
Comments
On lunedì 17 ottobre 2022 11:37:17 CEST Zhao Liu wrote: > From: Zhao Liu <zhao1.liu@intel.com> > > The use of kmap_atomic() is being deprecated in favor of > kmap_local_page()[1]. > > The main difference between atomic and local mappings is that local > mappings doesn't disable page faults or preemption. You are right about about page faults which are never disabled by kmap_local_page(). However kmap_atomic might not disable preemption. It depends on CONFIG_PREEMPT_RT. Please refer to how kmap_atomic_prot() works (this function is called by kmap_atomic() when kernels have HIGHMEM enabled). > > There're 2 reasons why i915_gem_object_read_from_page_kmap() doesn't > need to disable pagefaults and preemption for mapping: > > 1. The flush operation is safe for CPU hotplug when preemption is not > disabled. I'm confused here. Why are you talking about CPU hotplug? In any case, developers should never rely on implicit calls of preempt_disable() for the reasons said above. Therefore, flush operations should be allowed regardless that kmap_atomic() potential side effect. > In drm/i915/gem/i915_gem_object.c, the function > i915_gem_object_read_from_page_kmap() calls drm_clflush_virt_range() If I recall correctly, drm_clflush_virt_range() can always be called with page faults and preemption enabled. If so, this is enough to say that the conversion is safe. Is this code explicitly related to flushing the cache lines before removing / adding CPUs? If I recall correctly, there are several other reasons behind the need to issue cache lines flushes. Am I wrong about this? Can you please say more about what I'm missing here? > to > use CLFLUSHOPT or WBINVD to flush. Since CLFLUSHOPT is global on x86 > and WBINVD is called on each cpu in drm_clflush_virt_range(), the flush > operation is global and any issue with cpu's being added or removed > can be handled safely. Again your main concern is about CPU hotplug. Even if I'm missing something, do we really need all these details about the inner workings of drm_clflush_virt_range()? I'm not an expert, so may be that I'm wrong about all I wrote above. Therefore, can you please elaborate a little more for readers with very little knowledge of these kinds of things (like me and perhaps others)? > 2. Any context switch caused by preemption or sleep (pagefault may > cause sleep) doesn't affect the validity of local mapping. I'd replace "preemption or sleep" with "preemption and page faults" since yourself then added that page faults lead to tasks being put to sleep. > Therefore, i915_gem_object_read_from_page_kmap() is a function where > the use of kmap_local_page() in place of kmap_atomic() is correctly > suited. > > Convert the calls of kmap_atomic() / kunmap_atomic() to > kmap_local_page() / kunmap_local(). > > And remove the redundant variable that stores the address of the mapped > page since kunmap_local() can accept any pointer within the page. > > [1]: https://lore.kernel.org/all/20220813220034.806698-1-ira.weiny@intel.com > > Suggested-by: Dave Hansen <dave.hansen@intel.com> > Suggested-by: Ira Weiny <ira.weiny@intel.com> > Suggested-by: Fabio M. De Francesco <fmdefrancesco@gmail.com> > Signed-off-by: Zhao Liu <zhao1.liu@intel.com> > --- > Suggested by credits: > Dave: Referred to his explanation about cache flush. > Ira: Referred to his task document, review comments and explanation about > cache flush. > Fabio: Referred to his boiler plate commit message. > --- > drivers/gpu/drm/i915/gem/i915_gem_object.c | 8 +++----- > 1 file changed, 3 insertions(+), 5 deletions(-) > > diff --git a/drivers/gpu/drm/i915/gem/i915_gem_object.c > b/drivers/gpu/drm/i915/gem/i915_gem_object.c index 369006c5317f..a0072abed75e 100644 > --- a/drivers/gpu/drm/i915/gem/i915_gem_object.c > +++ b/drivers/gpu/drm/i915/gem/i915_gem_object.c > @@ -413,17 +413,15 @@ void __i915_gem_object_invalidate_frontbuffer(struct > drm_i915_gem_object *obj, static void > i915_gem_object_read_from_page_kmap(struct drm_i915_gem_object *obj, u64 offset, void > *dst, int size) { > - void *src_map; > void *src_ptr; > > - src_map = kmap_atomic(i915_gem_object_get_page(obj, offset >> PAGE_SHIFT)); > - > - src_ptr = src_map + offset_in_page(offset); > + src_ptr = kmap_local_page(i915_gem_object_get_page(obj, offset >> PAGE_SHIFT)) > + + offset_in_page(offset); > if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)) > drm_clflush_virt_range(src_ptr, size); > memcpy(dst, src_ptr, size); > > - kunmap_atomic(src_map); > + kunmap_local(src_ptr); > } > > static void The changes look good, but I'd like to better understand the commit message. Thanks, Fabio
On Sat, Oct 29, 2022 at 01:17:03PM +0200, Fabio M. De Francesco wrote: > On lunedì 17 ottobre 2022 11:37:17 CEST Zhao Liu wrote: > > From: Zhao Liu <zhao1.liu@intel.com> > > > > The use of kmap_atomic() is being deprecated in favor of > > kmap_local_page()[1]. > > > > The main difference between atomic and local mappings is that local > > mappings doesn't disable page faults or preemption. > > You are right about about page faults which are never disabled by > kmap_local_page(). However kmap_atomic might not disable preemption. It > depends on CONFIG_PREEMPT_RT. > > Please refer to how kmap_atomic_prot() works (this function is called by > kmap_atomic() when kernels have HIGHMEM enabled). > > > > > There're 2 reasons why i915_gem_object_read_from_page_kmap() doesn't > > need to disable pagefaults and preemption for mapping: > > > > 1. The flush operation is safe for CPU hotplug when preemption is not > > disabled. > > I'm confused here. Why are you talking about CPU hotplug? I agree with Fabio here. I'm not making the connection between cpu hotplug and this code path. Ira > In any case, developers should never rely on implicit calls of > preempt_disable() for the reasons said above. Therefore, flush operations > should be allowed regardless that kmap_atomic() potential side effect. > > > In drm/i915/gem/i915_gem_object.c, the function > > i915_gem_object_read_from_page_kmap() calls drm_clflush_virt_range() > > If I recall correctly, drm_clflush_virt_range() can always be called with page > faults and preemption enabled. If so, this is enough to say that the > conversion is safe. > > Is this code explicitly related to flushing the cache lines before removing / > adding CPUs? If I recall correctly, there are several other reasons behind the > need to issue cache lines flushes. Am I wrong about this? > > Can you please say more about what I'm missing here? > > > to > > use CLFLUSHOPT or WBINVD to flush. Since CLFLUSHOPT is global on x86 > > and WBINVD is called on each cpu in drm_clflush_virt_range(), the flush > > operation is global and any issue with cpu's being added or removed > > can be handled safely. > > Again your main concern is about CPU hotplug. > > Even if I'm missing something, do we really need all these details about the > inner workings of drm_clflush_virt_range()? > > I'm not an expert, so may be that I'm wrong about all I wrote above. > > Therefore, can you please elaborate a little more for readers with very little > knowledge of these kinds of things (like me and perhaps others)? > > > 2. Any context switch caused by preemption or sleep (pagefault may > > cause sleep) doesn't affect the validity of local mapping. > > I'd replace "preemption or sleep" with "preemption and page faults" since > yourself then added that page faults lead to tasks being put to sleep. > > > Therefore, i915_gem_object_read_from_page_kmap() is a function where > > the use of kmap_local_page() in place of kmap_atomic() is correctly > > suited. > > > > Convert the calls of kmap_atomic() / kunmap_atomic() to > > kmap_local_page() / kunmap_local(). > > > > And remove the redundant variable that stores the address of the mapped > > page since kunmap_local() can accept any pointer within the page. > > > > [1]: https://lore.kernel.org/all/20220813220034.806698-1-ira.weiny@intel.com > > > > Suggested-by: Dave Hansen <dave.hansen@intel.com> > > Suggested-by: Ira Weiny <ira.weiny@intel.com> > > Suggested-by: Fabio M. De Francesco <fmdefrancesco@gmail.com> > > Signed-off-by: Zhao Liu <zhao1.liu@intel.com> > > --- > > Suggested by credits: > > Dave: Referred to his explanation about cache flush. > > Ira: Referred to his task document, review comments and explanation about > > cache flush. > > Fabio: Referred to his boiler plate commit message. > > --- > > drivers/gpu/drm/i915/gem/i915_gem_object.c | 8 +++----- > > 1 file changed, 3 insertions(+), 5 deletions(-) > > > > diff --git a/drivers/gpu/drm/i915/gem/i915_gem_object.c > > b/drivers/gpu/drm/i915/gem/i915_gem_object.c index > 369006c5317f..a0072abed75e 100644 > > --- a/drivers/gpu/drm/i915/gem/i915_gem_object.c > > +++ b/drivers/gpu/drm/i915/gem/i915_gem_object.c > > @@ -413,17 +413,15 @@ void __i915_gem_object_invalidate_frontbuffer(struct > > drm_i915_gem_object *obj, static void > > i915_gem_object_read_from_page_kmap(struct drm_i915_gem_object *obj, u64 > offset, void > > *dst, int size) { > > - void *src_map; > > void *src_ptr; > > > > - src_map = kmap_atomic(i915_gem_object_get_page(obj, offset >> > PAGE_SHIFT)); > > - > > - src_ptr = src_map + offset_in_page(offset); > > + src_ptr = kmap_local_page(i915_gem_object_get_page(obj, offset >> > PAGE_SHIFT)) > > + + offset_in_page(offset); > > if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)) > > drm_clflush_virt_range(src_ptr, size); > > memcpy(dst, src_ptr, size); > > > > - kunmap_atomic(src_map); > > + kunmap_local(src_ptr); > > } > > > > static void > > The changes look good, but I'd like to better understand the commit message. > > Thanks, > > Fabio > >
On giovedì 3 novembre 2022 17:51:23 CET Ira Weiny wrote: > On Sat, Oct 29, 2022 at 01:17:03PM +0200, Fabio M. De Francesco wrote: > > On lunedì 17 ottobre 2022 11:37:17 CEST Zhao Liu wrote: > > > From: Zhao Liu <zhao1.liu@intel.com> > > > > > > The use of kmap_atomic() is being deprecated in favor of > > > kmap_local_page()[1]. > > > > > > The main difference between atomic and local mappings is that local > > > mappings doesn't disable page faults or preemption. > > > > You are right about about page faults which are never disabled by > > kmap_local_page(). However kmap_atomic might not disable preemption. It > > depends on CONFIG_PREEMPT_RT. > > > > Please refer to how kmap_atomic_prot() works (this function is called by > > kmap_atomic() when kernels have HIGHMEM enabled). > > > > > There're 2 reasons why i915_gem_object_read_from_page_kmap() doesn't > > > need to disable pagefaults and preemption for mapping: > > > > > > 1. The flush operation is safe for CPU hotplug when preemption is not > > > disabled. > > > > I'm confused here. Why are you talking about CPU hotplug? > > I agree with Fabio here. I'm not making the connection between cpu hotplug and > this code path. > > Ira @Zhao, I'd like to add that I was about to put my reviewed-by tag. The other things I objected are minor nits. Please just clarify this connection. Your code is good and deserves to be applied. Fabio > > > In any case, developers should never rely on implicit calls of > > preempt_disable() for the reasons said above. Therefore, flush operations > > should be allowed regardless that kmap_atomic() potential side effect. > > > > > In drm/i915/gem/i915_gem_object.c, the function > > > i915_gem_object_read_from_page_kmap() calls drm_clflush_virt_range() > > > > If I recall correctly, drm_clflush_virt_range() can always be called with page > > faults and preemption enabled. If so, this is enough to say that the > > conversion is safe. > > > > Is this code explicitly related to flushing the cache lines before removing / > > adding CPUs? If I recall correctly, there are several other reasons behind the > > need to issue cache lines flushes. Am I wrong about this? > > > > Can you please say more about what I'm missing here? > > > > > to > > > use CLFLUSHOPT or WBINVD to flush. Since CLFLUSHOPT is global on x86 > > > and WBINVD is called on each cpu in drm_clflush_virt_range(), the flush > > > operation is global and any issue with cpu's being added or removed > > > can be handled safely. > > > > Again your main concern is about CPU hotplug. > > > > Even if I'm missing something, do we really need all these details about the > > inner workings of drm_clflush_virt_range()? > > > > I'm not an expert, so may be that I'm wrong about all I wrote above. > > > > Therefore, can you please elaborate a little more for readers with very little > > knowledge of these kinds of things (like me and perhaps others)? > > > > > 2. Any context switch caused by preemption or sleep (pagefault may > > > cause sleep) doesn't affect the validity of local mapping. > > > > I'd replace "preemption or sleep" with "preemption and page faults" since > > yourself then added that page faults lead to tasks being put to sleep. > > > > > Therefore, i915_gem_object_read_from_page_kmap() is a function where > > > the use of kmap_local_page() in place of kmap_atomic() is correctly > > > suited. > > > > > > Convert the calls of kmap_atomic() / kunmap_atomic() to > > > kmap_local_page() / kunmap_local(). > > > > > > And remove the redundant variable that stores the address of the mapped > > > page since kunmap_local() can accept any pointer within the page. > > > > > > [1]: https://lore.kernel.org/all/20220813220034.806698-1-ira.weiny@intel.com > > > > > > Suggested-by: Dave Hansen <dave.hansen@intel.com> > > > Suggested-by: Ira Weiny <ira.weiny@intel.com> > > > Suggested-by: Fabio M. De Francesco <fmdefrancesco@gmail.com> > > > Signed-off-by: Zhao Liu <zhao1.liu@intel.com> > > > --- > > > > > > Suggested by credits: > > > Dave: Referred to his explanation about cache flush. > > > Ira: Referred to his task document, review comments and explanation about > > > > > > cache flush. > > > > > > Fabio: Referred to his boiler plate commit message. > > > > > > --- > > > > > > drivers/gpu/drm/i915/gem/i915_gem_object.c | 8 +++----- > > > 1 file changed, 3 insertions(+), 5 deletions(-) > > > > > > diff --git a/drivers/gpu/drm/i915/gem/i915_gem_object.c > > > b/drivers/gpu/drm/i915/gem/i915_gem_object.c index > > > > 369006c5317f..a0072abed75e 100644 > > > > > --- a/drivers/gpu/drm/i915/gem/i915_gem_object.c > > > +++ b/drivers/gpu/drm/i915/gem/i915_gem_object.c > > > @@ -413,17 +413,15 @@ void __i915_gem_object_invalidate_frontbuffer(struct > > > drm_i915_gem_object *obj, static void > > > > > > i915_gem_object_read_from_page_kmap(struct drm_i915_gem_object *obj, u64 > > > > offset, void > > > > > *dst, int size) { > > > - void *src_map; > > > > > > void *src_ptr; > > > > > > - src_map = kmap_atomic(i915_gem_object_get_page(obj, offset >> > > > > PAGE_SHIFT)); > > > > > - > > > - src_ptr = src_map + offset_in_page(offset); > > > + src_ptr = kmap_local_page(i915_gem_object_get_page(obj, offset >> > > > > PAGE_SHIFT)) > > > > > + + offset_in_page(offset); > > > > > > if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)) > > > > > > drm_clflush_virt_range(src_ptr, size); > > > > > > memcpy(dst, src_ptr, size); > > > > > > - kunmap_atomic(src_map); > > > + kunmap_local(src_ptr); > > > > > > } > > > > > > static void > > > > The changes look good, but I'd like to better understand the commit message. > > > > Thanks, > > > > Fabio
On Sat, Oct 29, 2022 at 01:17:03PM +0200, Fabio M. De Francesco wrote: > Date: Sat, 29 Oct 2022 13:17:03 +0200 > From: "Fabio M. De Francesco" <fmdefrancesco@gmail.com> > Subject: Re: [PATCH 1/9] drm/i915: Use kmap_local_page() in > gem/i915_gem_object.c > > On luned? 17 ottobre 2022 11:37:17 CEST Zhao Liu wrote: > > From: Zhao Liu <zhao1.liu@intel.com> > > > > The use of kmap_atomic() is being deprecated in favor of > > kmap_local_page()[1]. > > > > The main difference between atomic and local mappings is that local > > mappings doesn't disable page faults or preemption. > > You are right about about page faults which are never disabled by > kmap_local_page(). However kmap_atomic might not disable preemption. It > depends on CONFIG_PREEMPT_RT. > > Please refer to how kmap_atomic_prot() works (this function is called by > kmap_atomic() when kernels have HIGHMEM enabled). Yes, there is some ambiguity here. What about "The main difference between atomic and local mappings is that local mappings never disable page faults or preemption"? > > > > > There're 2 reasons why i915_gem_object_read_from_page_kmap() doesn't > > need to disable pagefaults and preemption for mapping: > > > > 1. The flush operation is safe for CPU hotplug when preemption is not > > disabled. > > I'm confused here. Why are you talking about CPU hotplug? > In any case, developers should never rely on implicit calls of > preempt_disable() for the reasons said above. Therefore, flush operations > should be allowed regardless that kmap_atomic() potential side effect. Sorry, it's my fault, my misunderstanding about the connection between hotplug and flush here. When mapping exists, the cpu cannot be unplugged via CPU-hotplug. But whether plug or unplug, it has nothing to do with flush. I will delete this wrong description. My initial consideration is that this interface of flush may require an atomic context, so I want to explain more from the details of its implementation that cache consistency can be guaranteed without atomic context. Is this consideration redundant? Also, do I need to state that migration is still ok for this flush interface here (since __kmap_local_page_prot() doesn't always disable migration)? > > In drm/i915/gem/i915_gem_object.c, the function > > i915_gem_object_read_from_page_kmap() calls drm_clflush_virt_range() > > If I recall correctly, drm_clflush_virt_range() can always be called with page > faults and preemption enabled. If so, this is enough to say that the > conversion is safe. > > Is this code explicitly related to flushing the cache lines before removing / > adding CPUs? If I recall correctly, there are several other reasons behind the > need to issue cache lines flushes. Am I wrong about this? > > Can you please say more about what I'm missing here? > > > to > > use CLFLUSHOPT or WBINVD to flush. Since CLFLUSHOPT is global on x86 > > and WBINVD is called on each cpu in drm_clflush_virt_range(), the flush > > operation is global and any issue with cpu's being added or removed > > can be handled safely. > > Again your main concern is about CPU hotplug. > > Even if I'm missing something, do we really need all these details about the > inner workings of drm_clflush_virt_range()? > > I'm not an expert, so may be that I'm wrong about all I wrote above. > > Therefore, can you please elaborate a little more for readers with very little > knowledge of these kinds of things (like me and perhaps others)? > > > 2. Any context switch caused by preemption or sleep (pagefault may > > cause sleep) doesn't affect the validity of local mapping. > > I'd replace "preemption or sleep" with "preemption and page faults" since > yourself then added that page faults lead to tasks being put to sleep. Thanks, good advice. Zhao
On Thu, Nov 03, 2022 at 09:51:23AM -0700, Ira Weiny wrote: > Date: Thu, 3 Nov 2022 09:51:23 -0700 > From: Ira Weiny <ira.weiny@intel.com> > Subject: Re: [PATCH 1/9] drm/i915: Use kmap_local_page() in > gem/i915_gem_object.c > > On Sat, Oct 29, 2022 at 01:17:03PM +0200, Fabio M. De Francesco wrote: > > On luned? 17 ottobre 2022 11:37:17 CEST Zhao Liu wrote: > > > From: Zhao Liu <zhao1.liu@intel.com> > > > > > > The use of kmap_atomic() is being deprecated in favor of > > > kmap_local_page()[1]. > > > > > > The main difference between atomic and local mappings is that local > > > mappings doesn't disable page faults or preemption. > > > > You are right about about page faults which are never disabled by > > kmap_local_page(). However kmap_atomic might not disable preemption. It > > depends on CONFIG_PREEMPT_RT. > > > > Please refer to how kmap_atomic_prot() works (this function is called by > > kmap_atomic() when kernels have HIGHMEM enabled). > > > > > > > > There're 2 reasons why i915_gem_object_read_from_page_kmap() doesn't > > > need to disable pagefaults and preemption for mapping: > > > > > > 1. The flush operation is safe for CPU hotplug when preemption is not > > > disabled. > > > > I'm confused here. Why are you talking about CPU hotplug? > > I agree with Fabio here. I'm not making the connection between cpu hotplug and > this code path. Sorry, my misunderstanding. Will delete this wrong explanation. Thanks, Zhao
On Thu, Nov 03, 2022 at 08:22:04PM +0100, Fabio M. De Francesco wrote: > Date: Thu, 03 Nov 2022 20:22:04 +0100 > From: "Fabio M. De Francesco" <fmdefrancesco@gmail.com> > Subject: Re: [PATCH 1/9] drm/i915: Use kmap_local_page() in > gem/i915_gem_object.c > > On gioved? 3 novembre 2022 17:51:23 CET Ira Weiny wrote: > > On Sat, Oct 29, 2022 at 01:17:03PM +0200, Fabio M. De Francesco wrote: > > > On luned? 17 ottobre 2022 11:37:17 CEST Zhao Liu wrote: > > > > From: Zhao Liu <zhao1.liu@intel.com> > > > > > > > > The use of kmap_atomic() is being deprecated in favor of > > > > kmap_local_page()[1]. > > > > > > > > The main difference between atomic and local mappings is that local > > > > mappings doesn't disable page faults or preemption. > > > > > > You are right about about page faults which are never disabled by > > > kmap_local_page(). However kmap_atomic might not disable preemption. It > > > depends on CONFIG_PREEMPT_RT. > > > > > > Please refer to how kmap_atomic_prot() works (this function is called by > > > kmap_atomic() when kernels have HIGHMEM enabled). > > > > > > > There're 2 reasons why i915_gem_object_read_from_page_kmap() doesn't > > > > need to disable pagefaults and preemption for mapping: > > > > > > > > 1. The flush operation is safe for CPU hotplug when preemption is not > > > > disabled. > > > > > > I'm confused here. Why are you talking about CPU hotplug? > > > > I agree with Fabio here. I'm not making the connection between cpu hotplug > and > > this code path. > > > > Ira > > @Zhao, > > I'd like to add that I was about to put my reviewed-by tag. The other things I > objected are minor nits. Please just clarify this connection. Thanks Fabio for your comments! Sorry I missed the mails that day. This connection is my misunderstanding. Other thoughts please refer to my reply to your first email in this thread. Thanks, Zhao
diff --git a/drivers/gpu/drm/i915/gem/i915_gem_object.c b/drivers/gpu/drm/i915/gem/i915_gem_object.c index 369006c5317f..a0072abed75e 100644 --- a/drivers/gpu/drm/i915/gem/i915_gem_object.c +++ b/drivers/gpu/drm/i915/gem/i915_gem_object.c @@ -413,17 +413,15 @@ void __i915_gem_object_invalidate_frontbuffer(struct drm_i915_gem_object *obj, static void i915_gem_object_read_from_page_kmap(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size) { - void *src_map; void *src_ptr; - src_map = kmap_atomic(i915_gem_object_get_page(obj, offset >> PAGE_SHIFT)); - - src_ptr = src_map + offset_in_page(offset); + src_ptr = kmap_local_page(i915_gem_object_get_page(obj, offset >> PAGE_SHIFT)) + + offset_in_page(offset); if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)) drm_clflush_virt_range(src_ptr, size); memcpy(dst, src_ptr, size); - kunmap_atomic(src_map); + kunmap_local(src_ptr); } static void