Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp1052697pxm; Wed, 23 Feb 2022 16:56:12 -0800 (PST) X-Google-Smtp-Source: ABdhPJxBk817qlpbNiXbHqdSlztQJeUptvU0yfLw9XNbo2beDWWmC2kzrOjOvUKZzqmaVUww0CcP X-Received: by 2002:a17:902:724a:b0:14f:bb5d:3d72 with SMTP id c10-20020a170902724a00b0014fbb5d3d72mr256729pll.79.1645664172228; Wed, 23 Feb 2022 16:56:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645664172; cv=none; d=google.com; s=arc-20160816; b=yebHUgM7QVX1YWrMZGbGCYsbWH2RdtEJB03z/oH48t6S/maYyuuVSt0AuSy8W4xgZv G/WDh95ihqlCLyuvnWEtRw8+RjX5Eo6sOBmwlYiVhVxC0mKW7fLBuQOxMwWU0Huu2DJs jJDKTSfg3AuLyn5+Sy7nhZkpGHPOztKZpon/xubpuBRvBnPlqLNP3ugvPaS2TavjYPI9 3jTECC9kF6c8PxKl0puHWlYYBp56jUdALhWf1IVO4lqnLcO0f8bFLSheZgevbb1eIK7C n5oIMCpyWLu2J/nW41uIjdh1wYbQq8uDff9iA5BpTAC3BHzGm6+6opdpYqpiwgvMTNnv UH5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:references:message-id :content-transfer-encoding:cc:date:in-reply-to:from:subject :mime-version:dkim-signature; bh=Heow4hDDMSZ2DF+IFY7uzO/bwjvFxqCGzwmmOWLjSLo=; b=KAfi92idMi3ySgjcSNqB5YiaoZmZWZy9nPnyWLsyhqijHTY/B8ocFKB8rKcjswBBdC OWUQ33kvHtU531rhoNMSb5jt8J9uKa/AWq68I0Qg/Hc9ZMiO2yNV7VNmJ8itxvrIOEKl 2G/9HFsfSA+tqjxiGtg/9AQpy5is2WQzTBe4WdefZZvV0y4rRj/wxsI3Keobb2NP4ObX chvZndW2OmVoXkxv5sGc1pkfGzEz4wqvWwXxxyvC34ftXeLRLXdwCOuVY5BOQK7kq7ME nsXh+yVettyINSIpj5ays9Mgx+YeqwwgJBtzWq084Y1hqt5gSeTryDCPbFIlG5mkRq5e v9ZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=aqNZfy51; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id s12si1020555pji.31.2022.02.23.16.56.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Feb 2022 16:56:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=aqNZfy51; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id C544E1111B2; Wed, 23 Feb 2022 16:49:23 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238003AbiBWWJc (ORCPT + 99 others); Wed, 23 Feb 2022 17:09:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230527AbiBWWJa (ORCPT ); Wed, 23 Feb 2022 17:09:30 -0500 Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [IPv6:2a00:1450:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 832F94A927; Wed, 23 Feb 2022 14:09:00 -0800 (PST) Received: by mail-ej1-x62f.google.com with SMTP id vz16so412010ejb.0; Wed, 23 Feb 2022 14:09:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=Heow4hDDMSZ2DF+IFY7uzO/bwjvFxqCGzwmmOWLjSLo=; b=aqNZfy51Zoj3GU0fSlQHALzjGJHEntuIJ9qOIf0wqhxeHCmhk2IVi2giP2LbjJUL/o pxUKWShoqp2O4G5gGgDaXqjCO4xFxrAnQJ4GgSk+hFyqAE+doGi1BXte0NDdNwyq/E0S 5oIKRWV0htuhNqkiQ8SS8r4PtK+BClJQTYfYDS2TfY9ZoHySIMpSwd8ONoNi3PaXkbBU oPVMDCZXlCHD2vin5ljlJ4s4j9F7fvge82e8Ei523Epj0j+I7Vbw0KhztDSjPvCMh6Px yb2Jw4dcRjn8tKJWL3bWTnNmZ99EGJAOrFAAKqrXIe3ggPcAbqRdhsLVq0tHfxZIB4l8 MC/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=Heow4hDDMSZ2DF+IFY7uzO/bwjvFxqCGzwmmOWLjSLo=; b=Wd+7SYimqb+lEuASOI/BeXJt/5nOj+GzfIqU0msaNDrAi8K8FN1O50GR9GJVyumMEA /mkad1r86xp2HWSTjYYzpbSkLdQ3KkPyIiW2W100tvOKTRDYcfBxwBWa1arVti18Q9R+ cKeeKD8E8Rf1ju3CrWFVDxr5G8Ik3wU/QdyPTiNHThSVYw1mYlGWp4mbaUCunnwH/Kjg K6RREKJAueDrLnFTYlMHl0HMCyJlPZYUPMB8vWTu/QPlarQcDfHntosVMaO8LxNAmwte VuxSQpieBEe3HEDVr4xgHww2g30u77qyemZ7qcrBpu2K/DDnhfvv4NBng9W9CEXsL5Bg AqPw== X-Gm-Message-State: AOAM531vzp5wN9Z16NSJpVvdFAM1EfaGlkhYbJBfzF29lckh8boaHcAH P4ZheY4NzNFBxdW2NRLQAwU= X-Received: by 2002:a17:906:abca:b0:6d0:9ebc:b9cf with SMTP id kq10-20020a170906abca00b006d09ebcb9cfmr1301096ejb.288.1645654138940; Wed, 23 Feb 2022 14:08:58 -0800 (PST) Received: from smtpclient.apple (dhcp-077-250-038-153.chello.nl. [77.250.38.153]) by smtp.gmail.com with ESMTPSA id w4sm469075edc.73.2022.02.23.14.08.57 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 23 Feb 2022 14:08:58 -0800 (PST) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 15.0 \(3693.60.0.1.1\)) Subject: Re: [RFC PATCH 04/13] vfio/mdev: remove the usage of the list iterator after the loop From: Jakob In-Reply-To: Date: Wed, 23 Feb 2022 23:08:53 +0100 Cc: Jason Gunthorpe , Linux Kernel Mailing List , linux-arch , Greg Kroah-Hartman , Thomas Gleixner , Arnd Bergman , Andy Shevchenko , Andrew Morton , Kees Cook , Mike Rapoport , "Gustavo A. R. Silva" , Brian Johannesmeyer , Cristiano Giuffrida , "Bos, H.J." Content-Transfer-Encoding: quoted-printable Message-Id: References: <20220217184829.1991035-1-jakobkoschel@gmail.com> <20220217184829.1991035-5-jakobkoschel@gmail.com> <20220218151216.GE1037534@ziepe.ca> <6BA40980-554F-45E2-914D-5E4CD02FF21C@gmail.com> <20220223191222.GC10361@ziepe.ca> To: Linus Torvalds X-Mailer: Apple Mail (2.3693.60.0.1.1) X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, 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 23. Feb 2022, at 21:22, Linus Torvalds = wrote: >=20 > On Wed, Feb 23, 2022 at 12:15 PM Jakob wrote: >>=20 >> in such a case you would still have to set the iterator value to >> NULL when reaching the terminating condition or am I missing = something? >=20 > No. >=20 > Make the rule be "you never use the iterator outside the loop". >=20 > IOW, the code sequence is >=20 > some_struct *ptr, *iter; with C99 iter would be defined within the loop instead right? >=20 > ptr =3D NULL; > list_for_each_entry(iter, ...) { > if (iter_matches_condition(iter)) { > ptr =3D iter; > break; > } > } >=20 > .. never use 'iter' here - you use 'ptr' and check it for NULL = .. >=20 > See? Same number of variables as using a separate 'bool found' flag, > but simpler code, and it matches the rule of 'don't use iter outside > the loop'. ah yes this does make sense. I missed the part of using a separate 'ptr' variable. Thanks for clarifying. I think this is a great idea. There are cases where pos->member is used (the only legitimate way to use it right now). I suppose those turn into something like this (this example is inspired by dev_add_offload() (net/core/gro.c:38)): some_struct *ptr, *iter; list_head *list_ptr; ptr =3D NULL; list_for_each_entry(iter, head, list) { if (iter_matches_condition(iter)) { ptr =3D iter; break; } } =20 if (ptr) list_ptr =3D head->prev; else list_ptr =3D iter->list.prev; list_add(..., list_ptr); before it was simply list_add(..., iter->list.prev); The other possibility I suppose would be: if (!ptr) ptr =3D container_of(head, typeof(*ptr), list) list_add(..., ptr->list.prev); which leaves you with the same type confusion as before, being far from ideal. > This is how you'd have to do it anyway if we start using a C99 style > 'declare iter _in_ the loop' model. >=20 > And as mentioned, it actually tends to lead to better code, since the > code outside the loop only has one variable live, not two. >=20 > Of course, compilers can do a lot of optimizations, so a 'found' > variable can be made to generate good code too - if the compiler just > tracks it and notices, and turns the 'break' into a 'goto found', and > the fallthrough into the 'goto not_found'. >=20 > So 'better code generation' is debatable, but even if the compiler can > do as good a job with a separate 'bool' variable and some cleverness, > I think we should strive for code where we make it easy for the > compiler to DTRT - and where the generated code is easier to match up > with what we wrote. >=20 > Linus If there is interest, I'm happy to send a new patch set once the fixes = are clear. Jakob