Received: by 2002:a19:f614:0:0:0:0:0 with SMTP id x20csp61609lfe; Fri, 15 Apr 2022 19:33:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwY975hJ6ogiSK2AWSXADv4jkUnBnGme0y9Yjw5XcCT9XrrDazVDiJmr9lMHrazq4+fLWiL X-Received: by 2002:a17:903:2052:b0:158:c6aa:b96 with SMTP id q18-20020a170903205200b00158c6aa0b96mr1811211pla.140.1650076431865; Fri, 15 Apr 2022 19:33:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650076431; cv=none; d=google.com; s=arc-20160816; b=Hlr6fjPBe3Vr9xOSmziYgHe28l5Yo8FfGDNb0IkOxoTvkq3A3VQZm5NCqRM8UoedFU laWxqE8NQ2/Dc1IC/Gl6MDvXjtGlg9K9O4JgfUOjW8Z3HjKSJvss9sVtnGKSPeLQmy4h zwCHp9KrFrHU6IbKRgTYoWrDPTP0GpyR/vDQtZjhGGAHnKwClGR2DCBqMx/10XyAqTSj YHQLW188JqNarsvOUjL52X2iPcrlfx/XpqxJIpSnCklgdsHjcUhwpbypOC8BC/JmZLKO 5xNfYFzhXpZaB90XDe5X/NHcOb00o1JXPP4uHgvIxszDJTm202yRybF3PRh435hfGoxd qiiw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=xTEBdUo+G1c4jXJbeG4KCIlZUemXkj8RFa/eMZVe0ko=; b=H5xLdaWUinp234MoA6ZOqsxYoZoTMXxUrIDJjkb3XEdsmp5bus55N2SPDJK6CD8Dj/ Xg8B7LAdtn1gpP43jtnPoqTyZroFQ+yBZhnrZXRudReENaJU/SYFhBxVAmJrVoz16WVt EVgAbfj3nvD40GMRF4XuNexYAneBI7XOfGhA4l121859YS7cmtI2uJ5ui18u/ggT91+a zwHPvdHaXo7TC2/6UKXFA5hmiFk9qYXp2In+J9Wxn0UPBUjFyG8hJ3BFz8xruJgtd/We i/8uoauhWKyXM2O/VImN7UR5IJDvvF//lhHL6ghIHC/mEgMNBO9vFEIk2zM9QDP9/hry mk6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=BaRgvRQx; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id e30-20020a056a0000de00b005061f41c548si3252266pfj.352.2022.04.15.19.33.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Apr 2022 19:33:51 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=BaRgvRQx; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6D457129842; Fri, 15 Apr 2022 18:43:40 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347245AbiDOAj6 (ORCPT + 99 others); Thu, 14 Apr 2022 20:39:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240902AbiDOAj4 (ORCPT ); Thu, 14 Apr 2022 20:39:56 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 770C93BBF8; Thu, 14 Apr 2022 17:37:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1649983050; x=1681519050; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=NqIg0JIeL+aLq0L6edg0YyzM2lgFxJdLKtyUthSAtWE=; b=BaRgvRQxv0PisaUB6y8yT0YtMva/5s3fPNOsAoJgXtYi0UzYRGd1HsJD 8nzevtt/ySmfkkqKlrrVhIIHKfqrWi8Q6WDXr0VV1X6A1M7Rcu/nux5n8 ci5jvTn2Wtv8Iu8MIye+5HaN9+CPOvb3J/Da4KADv0+dMAvo/CmINUMnA DGyZSSK4ebP5QpgVhbbQlOBScwWUge3WA1jAlmXfLkdz3gZJA8XlcGcGu gMpKNcPHuPquQl0x9JFBmn/VzIEHhoPntJyS/sencQHDqOec0cWq36AdZ 02jq9v7O6fMeMb8PJ+OvniSyVMqP7Bd9BlbTTRE7cfk2ROTOPWKxRaubx w==; X-IronPort-AV: E=McAfee;i="6400,9594,10317"; a="349500063" X-IronPort-AV: E=Sophos;i="5.90,261,1643702400"; d="scan'208";a="349500063" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Apr 2022 17:37:29 -0700 X-IronPort-AV: E=Sophos;i="5.90,261,1643702400"; d="scan'208";a="508609395" Received: from aimeehax-mobl1.amr.corp.intel.com (HELO localhost) ([10.212.113.132]) by orsmga003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Apr 2022 17:37:28 -0700 Date: Thu, 14 Apr 2022 17:37:27 -0700 From: Ira Weiny To: "Fabio M. De Francesco" Cc: Mauro Carvalho Chehab , Sakari Ailus , Greg Kroah-Hartman , Hans Verkuil , Tsuchiya Yuto , Martiros Shakhzadyan , Hans de Goede , linux-media@vger.kernel.org, linux-staging@lists.linux.dev, linux-kernel@vger.kernel.org, outreachy@lists.linux.dev Subject: Re: [PATCH] staging: media: atomisp: Use kmap_local_page() in hmm_store() Message-ID: References: <20220413225531.9425-1-fmdefrancesco@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220413225531.9425-1-fmdefrancesco@gmail.com> X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 On Thu, Apr 14, 2022 at 12:55:31AM +0200, Fabio M. De Francesco wrote: > The use of kmap() is being deprecated in favor of kmap_local_page() > where it is feasible. The same is true for kmap_atomic(). > > In file pci/hmm/hmm.c, function hmm_store() test if we are in atomic > context and, if so, it calls kmap_atomic(), if not, it calls kmap(). > > First of all, in_atomic() shouldn't be used in drivers. This macro > cannot always detect atomic context; in particular, it cannot know > about held spinlocks in non-preemptible kernels. > > Notwithstanding what it is said above, this code doesn't need to care > whether or not it is executing in atomic context. It can simply use > kmap_local_page() / kunmap_local() that can instead do the mapping / > unmapping regardless of the context. > > With kmap_local_page(), the mapping is per thread, CPU local and not > globally visible. Therefore, hmm_store()() is a function where the use > of kmap_local_page() in place of both kmap() and kmap_atomic() is > correctly suited. > > Convert the calls of kmap() / kunmap() and kmap_atomic() / > kunmap_atomic() to kmap_local_page() / kunmap_local() and drop the > unnecessary tests which test if the code is in atomic context. > > Signed-off-by: Fabio M. De Francesco I've had to research this a bit myself because I've not seen this pattern before. kmap_atomic() had 2 uses: 1) a situation where the operation on the page requires pagefaults and/or preemption to be disabled 2) in a situation where one knows the code can't sleep kmap_local_page() removes the second use case because kmap() is no longer the only alternative; from the kdoc for kmap_atomic(): ... * kmap_atomic - Atomically map a page for temporary usage - Deprecated! ... * Effectively a wrapper around kmap_local_page() which disables pagefaults * and preemption. ... The deprecation is because any pagefault/preemption disabling should probably be done explicitly from now on but allows for existing kmap_atomic() callers to live on. In this case, I suspect the original driver writer wanted to use kmap() but hmm_store() was called from various contexts. So they incorrectly tried to protect against the (potentially) sleeping kmap() call. In reality, I think they could have simply called kmap_atomic() and skipped 'in_atomic()' check altogether. Regardless now that kmap_local_page() exists, this is correct. Reviewed-by: Ira Weiny > --- > drivers/staging/media/atomisp/pci/hmm/hmm.c | 14 ++------------ > 1 file changed, 2 insertions(+), 12 deletions(-) > > diff --git a/drivers/staging/media/atomisp/pci/hmm/hmm.c b/drivers/staging/media/atomisp/pci/hmm/hmm.c > index 46ac082cd3f1..54188197c3dc 100644 > --- a/drivers/staging/media/atomisp/pci/hmm/hmm.c > +++ b/drivers/staging/media/atomisp/pci/hmm/hmm.c > @@ -482,10 +482,7 @@ int hmm_store(ia_css_ptr virt, const void *data, unsigned int bytes) > idx = (virt - bo->start) >> PAGE_SHIFT; > offset = (virt - bo->start) - (idx << PAGE_SHIFT); > > - if (in_atomic()) > - des = (char *)kmap_atomic(bo->page_obj[idx].page); > - else > - des = (char *)kmap(bo->page_obj[idx].page); > + des = (char *)kmap_local_page(bo->page_obj[idx].page); > > if (!des) { > dev_err(atomisp_dev, > @@ -512,14 +509,7 @@ int hmm_store(ia_css_ptr virt, const void *data, unsigned int bytes) > > clflush_cache_range(des, len); > > - if (in_atomic()) > - /* > - * Note: kunmap_atomic requires return addr from > - * kmap_atomic, not the page. See linux/highmem.h > - */ > - kunmap_atomic(des - offset); > - else > - kunmap(bo->page_obj[idx].page); > + kunmap_local(des); > } > > return 0; > -- > 2.34.1 >