From mboxrd@z Thu Jan 1 00:00:00 1970 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9A564C433EF for ; Wed, 23 Feb 2022 19:31:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243251AbiBWTcD (ORCPT ); Wed, 23 Feb 2022 14:32:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50244 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236818AbiBWTb7 (ORCPT ); Wed, 23 Feb 2022 14:31:59 -0500 Received: from mail-lj1-x22a.google.com (mail-lj1-x22a.google.com [IPv6:2a00:1450:4864:20::22a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5D8194833C for ; Wed, 23 Feb 2022 11:31:30 -0800 (PST) Received: by mail-lj1-x22a.google.com with SMTP id r20so26221477ljj.1 for ; Wed, 23 Feb 2022 11:31:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=sEyhUpG5h+UzsnkwaWDI2eM3wTHuE75grcoHH/zJYag=; b=P06RhFQPqHMaTnXXTqQiWy7i3ZvuvEnaxcFwUNwLKfDh834mazjKFAOBMkuBI31nYY LkL6nIoyNjJTId+oTv5veateVEDhsfGxHdM9k4hMUFpmTxShWxiuxBPNZCrZqiQZADzX oTmBvX8/Yd2JbxYqu3ZOPPHBH72oYvgZQX2s8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=sEyhUpG5h+UzsnkwaWDI2eM3wTHuE75grcoHH/zJYag=; b=UA4udenLnFvOlDAj/VQeN8K+gI8mtSC+mUnh17bmKsB5ECp2u/V0uUqRN4jLZoRwbd W4owNOaB+h+pMXMP8Q+nV18hkVzpPBYv9XbN4l6LQFDFs0ACt0vsX70gqko4gKs2mpH5 KwUZsAWACy/6BpkjTqBuoTwuk+k7dL9loZeg5iYMmPi32vwzCcdc1gvoFdFosQZqY/mB NLTdIHdWC+xQjkcQTIWtSOwAx5cCiEGYYEo/nI1ylsT3sbkz5VeVgmYrETwezCTpMfP8 PcaIvEDVbtFybOpogBF4Blvwv8vs2LSu1i7Jj2obMltlLKmZ6M3aMbMnGWbdR42yJsV5 wD1w== X-Gm-Message-State: AOAM533vYmt0SOW2VD1C0BnQJ4Q2OdP0B1krsD8MQhn+qV5uswJnt8NH 3g4x4BmoxYTElIpp7gHqxYc2YoEZ704gLIsE8BQ= X-Google-Smtp-Source: ABdhPJxHDlo+gYx0KHFw2JpDM3MNdPVTDXyDt1F+KURrB9p/upjwLiig0FmaZOPDqRT+KDNbIgRfaA== X-Received: by 2002:a2e:7a1a:0:b0:246:210:65cd with SMTP id v26-20020a2e7a1a000000b00246021065cdmr584115ljc.99.1645644688476; Wed, 23 Feb 2022 11:31:28 -0800 (PST) Received: from mail-lf1-f54.google.com (mail-lf1-f54.google.com. [209.85.167.54]) by smtp.gmail.com with ESMTPSA id r2sm35646lfm.19.2022.02.23.11.31.26 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 23 Feb 2022 11:31:27 -0800 (PST) Received: by mail-lf1-f54.google.com with SMTP id f37so31257lfv.8 for ; Wed, 23 Feb 2022 11:31:26 -0800 (PST) X-Received: by 2002:a05:6512:130b:b0:443:c2eb:399d with SMTP id x11-20020a056512130b00b00443c2eb399dmr745790lfu.27.1645644686317; Wed, 23 Feb 2022 11:31:26 -0800 (PST) MIME-Version: 1.0 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> In-Reply-To: <20220223191222.GC10361@ziepe.ca> From: Linus Torvalds Date: Wed, 23 Feb 2022 11:31:10 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [RFC PATCH 04/13] vfio/mdev: remove the usage of the list iterator after the loop To: Jason Gunthorpe Cc: Jakob , 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-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Feb 23, 2022 at 11:12 AM Jason Gunthorpe wrote: > > Yes, this is what I had put together as well about this patch, and I > think it is OK as-is. In this case the list head is in the .bss of a > module so I don't think it is very likely that the type confused > container_of() will alias a kalloc result, but it is certainly > technically wrong as-is. I think that the pattern we should strive to use is not top use a 'bool' with the - initialize to false, and then in loop: do 'found = true;' if found - use the iterator variable if 'found'. but instead strive to - either only use the iterator variable inside the loop - if you want to use it after the loop, have a externally visible separate pointer initialized to NULL, and set it to the iterator variable inside the loop IOW, instead of having a variable that is a 'bool', just make that variable _be_ the pointer you want. It's clearer, and it avoids using the iterator variable outside the loop. It also is likely to generate better code, because there are fewer live variables - outside the loop now only uses that one variable, rather than using the 'bool' variable _and_ the iterator variable. Linus