Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp78015pxm; Wed, 2 Mar 2022 10:44:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJwYfEO4jD+F5z7EV5Df2pk7dNp3+THxWMUtdYOrYFvv4dQEoDi0fNZM2LrnVIHgw35orpBD X-Received: by 2002:a63:88c6:0:b0:37c:4e10:1a1f with SMTP id l189-20020a6388c6000000b0037c4e101a1fmr854966pgd.162.1646246678563; Wed, 02 Mar 2022 10:44:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646246678; cv=none; d=google.com; s=arc-20160816; b=FY3uaYDpMWOLnac0xE65NXM1/hUtbD67uDt8CBHUklfvMZZqXKase57a5CqQGZ+gpW VeNovNi21yxts39UKnZzc2D8y0OuSFIHixcG5fpGdO6+OoWPdVMEMTMcvukYYKFQU3sE g9OrIFIWg93Jw5rC+KtPuPrMaBMbq7nANlNNp/PDWa3RR/z5DsI0OXmBpyXOQBTMJqft fDAalUaiI5XJTEVoVA9LDVNtZLNMIAI+smPx+M//ZcFC5IgRsq1TzaZg2/FdyOCNa/Ow 4xArBktrwS9b0uRwIvgT9bj6o6yylZucmausXCiZIOYe0DJRNXTj9vVUcM5lXhsfJ/xr 7lRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :organization:from:references:cc:to:content-language:subject :user-agent:mime-version:date:message-id:dkim-signature; bh=GNUJTdx/CXqG0F6Gi3f79BsUb8LRzy8a1Ziwnr6YbJk=; b=dxBEmiA/4kViN7f3dthQsif5huw5PaR+HrOM1BQyYNN3WXETBbwxMBOZYYuCevo/iL FNlsehTBPuPs7Z1oVe+8xIQuxp6Ab1YivbxaYuiW1jReRDRxsvr9JxL27CHs7XVO0HBk o4xcf8U1krkTjIH+bX+x+YDodR4DWN8Ih+NnikwxOVFnDRq+SFfgTriR6TbXKGnnx7Di 0uGqtSxqR6YdS4SmTC6X3fGR8PPaGjwxd/jHRvTfeZRrzqctxiKUn29ZlFmC0gNnslzJ G97ox3CKnJr4XdXleo/yRTO9b9rhhzMnrfao7Xsaz3IEgGZ1OHZH9/1cWkQmy/c6H7qk eo0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=EgFtrQTE; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-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 u34-20020a632362000000b00378a8d6b24fsi8959254pgm.730.2022.03.02.10.43.59; Wed, 02 Mar 2022 10:44:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-crypto-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=EgFtrQTE; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-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 S242407AbiCBRPv (ORCPT + 99 others); Wed, 2 Mar 2022 12:15:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38762 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237817AbiCBRPu (ORCPT ); Wed, 2 Mar 2022 12:15:50 -0500 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A19AA58E45; Wed, 2 Mar 2022 09:15:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1646241306; x=1677777306; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=quZJORXDoBJnmtXWeY1UEG0LMErZNbKWG4uR8GxuM8A=; b=EgFtrQTE2RuXhvw9sRIr3IT+pA0Ncb8f9tiCc0SC60P5NxN5IIJ1lIbG qRXhfI2wJDlspyrIGC0iL/9/1vb/Lqq8nOVou0CScelNSeM2zFz5ZYwQd vRCxbYiuOYMHLgn2p868GOLIzIRhmd1d7Ftp+C+uIAO1rF5yphwxH1nTK BZo8QGgTZGobcAwOhtAkimiecLreyGJPEymFRGwK29ytkPQQTUw1YAXwn 7s74MTXIjfdUqEKiu1QGwymv12Ru01fVqM/H0rIJHbiNJzFQB0n0mysA7 4izUGN7Nj88HSoNqykEmQlZt68TbXwAcpy2jJWkt8gcliob3VHMqMtNgg A==; X-IronPort-AV: E=McAfee;i="6200,9189,10274"; a="339886271" X-IronPort-AV: E=Sophos;i="5.90,149,1643702400"; d="scan'208";a="339886271" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Mar 2022 09:15:05 -0800 X-IronPort-AV: E=Sophos;i="5.90,149,1643702400"; d="scan'208";a="551343768" Received: from jbuller-mobl1.ger.corp.intel.com (HELO [10.213.194.231]) ([10.213.194.231]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Mar 2022 09:14:53 -0800 Message-ID: Date: Wed, 2 Mar 2022 17:14:50 +0000 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Subject: Re: [Intel-gfx] [PATCH 6/6] treewide: remove check of list iterator against head past the loop body Content-Language: en-US To: Jakob Koschel , Linus Torvalds Cc: alsa-devel@alsa-project.org, linux-aspeed@lists.ozlabs.org, "Gustavo A. R. Silva" , linux-iio@vger.kernel.org, nouveau@lists.freedesktop.org, Rasmus Villemoes , dri-devel@lists.freedesktop.org, Cristiano Giuffrida , amd-gfx@lists.freedesktop.org, samba-technical@lists.samba.org, linux1394-devel@lists.sourceforge.net, drbd-dev@lists.linbit.com, linux-arch , linux-cifs@vger.kernel.org, kvm@vger.kernel.org, linux-scsi@vger.kernel.org, linux-rdma@vger.kernel.org, linux-staging@lists.linux.dev, "Bos, H.J." , Jason Gunthorpe , intel-wired-lan@lists.osuosl.org, kgdb-bugreport@lists.sourceforge.net, bcm-kernel-feedback-list@broadcom.com, Dan Carpenter , linux-media@vger.kernel.org, Kees Cook , Arnd Bergman , linux-pm@vger.kernel.org, intel-gfx@lists.freedesktop.org, Brian Johannesmeyer , Nathan Chancellor , linux-fsdevel@vger.kernel.org, Christophe JAILLET , v9fs-developer@lists.sourceforge.net, linux-tegra@vger.kernel.org, Thomas Gleixner , Andy Shevchenko , linux-arm-kernel@lists.infradead.org, linux-sgx@vger.kernel.org, linux-block@vger.kernel.org, netdev@vger.kernel.org, linux-usb@vger.kernel.org, linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, tipc-discussion@lists.sourceforge.net, linux-crypto@vger.kernel.org, dmaengine@vger.kernel.org, linux-mediatek@lists.infradead.org, Andrew Morton , linuxppc-dev@lists.ozlabs.org, Mike Rapoport References: <20220228110822.491923-1-jakobkoschel@gmail.com> <20220228110822.491923-7-jakobkoschel@gmail.com> From: Tvrtko Ursulin Organization: Intel Corporation UK Plc In-Reply-To: <20220228110822.491923-7-jakobkoschel@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,HK_RANDOM_ENVFROM,HK_RANDOM_FROM, NICE_REPLY_A,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE 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-crypto@vger.kernel.org On 28/02/2022 11:08, Jakob Koschel wrote: > When list_for_each_entry() completes the iteration over the whole list > without breaking the loop, the iterator value will be a bogus pointer > computed based on the head element. > > While it is safe to use the pointer to determine if it was computed > based on the head element, either with list_entry_is_head() or > &pos->member == head, using the iterator variable after the loop should > be avoided. > > In preparation to limiting the scope of a list iterator to the list > traversal loop, use a dedicated pointer to point to the found element. > > Signed-off-by: Jakob Koschel [snip until i915 parts] > drivers/gpu/drm/i915/gem/i915_gem_context.c | 14 +++--- > .../gpu/drm/i915/gem/i915_gem_execbuffer.c | 15 ++++--- > drivers/gpu/drm/i915/gt/intel_ring.c | 15 ++++--- [snip] > diff --git a/drivers/gpu/drm/i915/gem/i915_gem_context.c b/drivers/gpu/drm/i915/gem/i915_gem_context.c > index 00327b750fbb..80c79028901a 100644 > --- a/drivers/gpu/drm/i915/gem/i915_gem_context.c > +++ b/drivers/gpu/drm/i915/gem/i915_gem_context.c > @@ -107,25 +107,27 @@ static void lut_close(struct i915_gem_context *ctx) > radix_tree_for_each_slot(slot, &ctx->handles_vma, &iter, 0) { > struct i915_vma *vma = rcu_dereference_raw(*slot); > struct drm_i915_gem_object *obj = vma->obj; > - struct i915_lut_handle *lut; > + struct i915_lut_handle *lut = NULL; > + struct i915_lut_handle *tmp; > > if (!kref_get_unless_zero(&obj->base.refcount)) > continue; > > spin_lock(&obj->lut_lock); > - list_for_each_entry(lut, &obj->lut_list, obj_link) { > - if (lut->ctx != ctx) > + list_for_each_entry(tmp, &obj->lut_list, obj_link) { > + if (tmp->ctx != ctx) > continue; > > - if (lut->handle != iter.index) > + if (tmp->handle != iter.index) > continue; > > - list_del(&lut->obj_link); > + list_del(&tmp->obj_link); > + lut = tmp; > break; > } > spin_unlock(&obj->lut_lock); > > - if (&lut->obj_link != &obj->lut_list) { > + if (lut) { > i915_lut_handle_free(lut); > radix_tree_iter_delete(&ctx->handles_vma, &iter, slot); Looks okay although personally I would have left lut as is for a smaller diff and introduced a new local like 'found' or 'unlinked'. > i915_vma_close(vma); > diff --git a/drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c b/drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c > index 1736efa43339..fda9e3685ad2 100644 > --- a/drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c > +++ b/drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c > @@ -2444,7 +2444,8 @@ static struct i915_request *eb_throttle(struct i915_execbuffer *eb, struct intel > { > struct intel_ring *ring = ce->ring; > struct intel_timeline *tl = ce->timeline; > - struct i915_request *rq; > + struct i915_request *rq = NULL; > + struct i915_request *tmp; > > /* > * Completely unscientific finger-in-the-air estimates for suitable > @@ -2460,15 +2461,17 @@ static struct i915_request *eb_throttle(struct i915_execbuffer *eb, struct intel > * claiming our resources, but not so long that the ring completely > * drains before we can submit our next request. > */ > - list_for_each_entry(rq, &tl->requests, link) { > - if (rq->ring != ring) > + list_for_each_entry(tmp, &tl->requests, link) { > + if (tmp->ring != ring) > continue; > > - if (__intel_ring_space(rq->postfix, > - ring->emit, ring->size) > ring->size / 2) > + if (__intel_ring_space(tmp->postfix, > + ring->emit, ring->size) > ring->size / 2) { > + rq = tmp; > break; > + } > } > - if (&rq->link == &tl->requests) > + if (!rq) > return NULL; /* weird, we will check again later for real */ Alternatively, instead of break could simply do "return i915_request_get(rq);" and replace the end of the function after the loop with "return NULL;". A bit smaller diff, or at least less "spread out" over the function, so might be easier to backport stuff touching this area in the future. But looks correct as is. > > return i915_request_get(rq); > diff --git a/drivers/gpu/drm/i915/gt/intel_ring.c b/drivers/gpu/drm/i915/gt/intel_ring.c > index 2fdd52b62092..4881c4e0c407 100644 > --- a/drivers/gpu/drm/i915/gt/intel_ring.c > +++ b/drivers/gpu/drm/i915/gt/intel_ring.c > @@ -191,24 +191,27 @@ wait_for_space(struct intel_ring *ring, > struct intel_timeline *tl, > unsigned int bytes) > { > - struct i915_request *target; > + struct i915_request *target = NULL; > + struct i915_request *tmp; > long timeout; > > if (intel_ring_update_space(ring) >= bytes) > return 0; > > GEM_BUG_ON(list_empty(&tl->requests)); > - list_for_each_entry(target, &tl->requests, link) { > - if (target->ring != ring) > + list_for_each_entry(tmp, &tl->requests, link) { > + if (tmp->ring != ring) > continue; > > /* Would completion of this request free enough space? */ > - if (bytes <= __intel_ring_space(target->postfix, > - ring->emit, ring->size)) > + if (bytes <= __intel_ring_space(tmp->postfix, > + ring->emit, ring->size)) { > + target = tmp; > break; > + } > } > > - if (GEM_WARN_ON(&target->link == &tl->requests)) > + if (GEM_WARN_ON(!target)) > return -ENOSPC; > > timeout = i915_request_wait(target, Looks okay as well. Less clear here if there is a clean solution to make the diff smaller so no suggestions. I mean do I dare mention "goto found;" from inside the loop, where the break is, instead of the variable renames.. risky.. :) (And ofc "return -ENOSPC" immediately after the loop.) As a summary changes looks okay, up to you if you want to try to make the diffs smaller or not. It doesn't matter hugely really, all I have is a vague and uncertain "maybe it makes backporting of something, someday easier". So for i915 it is good either way. Reviewed-by: Tvrtko Ursulin # i915 bits only Regards, Tvrtko