Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp2419187pxp; Mon, 21 Mar 2022 19:50:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzK4j2A+44pvAjgeoX3wCwgLaJUOu8nQ7YVNKPunp8DhWpZLaXdrGn+VKLqk99As2uZMsGa X-Received: by 2002:a17:902:cecb:b0:154:6f46:a5d6 with SMTP id d11-20020a170902cecb00b001546f46a5d6mr4917442plg.92.1647917459422; Mon, 21 Mar 2022 19:50:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647917459; cv=none; d=google.com; s=arc-20160816; b=cDB6J61fmbpk0MK39DTl9TcE2/1dR+0yCkTeRiS22ePioWp3zNTT7r9XTs8GkE4fp/ wP6EDDssEaElq0CSC86XTc78VnXvPTFd9o5OFAN3XzEwFIEt3S8+ci59oT6ktVKmmHde uYDr5RsLWt1DOSyU1/YFdl54ejHO12yASHHS0YVKG1o7YdHZrhlnZdwfT65H21BIcjyz uV4LApMRQFx7CBafyCoP2itTwEmrJfV/KQuUwkG5LWzuh7ENWqXYia60gw4HqyZt3mvW pq9cTwm3tF86JKTUPx7EDtmSSfzTa3BX5MXXAK5BP8GiXxcSCWMV8Xt/3erktwAinTru z06Q== 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:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=D27+pHP45UuZJ+EYW9UUxs10xhWRqxsJMCnmkdB0+ao=; b=UNOOQhGXUXaKLYtmIKoljXP+vUhQiKRVDikUEnCu5KYw2A1XJTTf/G+nFn83ryCp1Z GIFCggLxTj0FGzPl7JPmJFVQz6UnNVQT7sQi7dl+tWdjTiQ+EGeVwuRP8BrthEZMtQ/c sgy6nFtZV0EAcp3Aufk5wHIOvWxH1p66m5sRFnVXgyE3NxyDYtyJuzZKasBloi9qyLnA p/qwLMM/UKsWUxNYBQOXTz2qXXAy0zFTpM/CCy/xNLQDRymIYldCwxMxiYt+lDB4g0iG yePfmSZvTIxyNl6HAX6sxB1cVkyBm/LwDsNBXabnk9C4EeBVcGtRaJ38cu++b8JVlFDS VWbg== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id hi18-20020a17090b30d200b001bf80eb1bcdsi954576pjb.120.2022.03.21.19.50.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Mar 2022 19:50:59 -0700 (PDT) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B43023997D3; Mon, 21 Mar 2022 19:23:22 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235476AbiCVCR5 (ORCPT + 99 others); Mon, 21 Mar 2022 22:17:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235395AbiCVCRv (ORCPT ); Mon, 21 Mar 2022 22:17:51 -0400 Received: from out199-12.us.a.mail.aliyun.com (out199-12.us.a.mail.aliyun.com [47.90.199.12]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 271D325C6A for ; Mon, 21 Mar 2022 19:15:39 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R161e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04426;MF=joseph.qi@linux.alibaba.com;NM=1;PH=DS;RN=14;SR=0;TI=SMTPD_---0V7tYAbz_1647915334; Received: from 30.225.24.67(mailfrom:joseph.qi@linux.alibaba.com fp:SMTPD_---0V7tYAbz_1647915334) by smtp.aliyun-inc.com(127.0.0.1); Tue, 22 Mar 2022 10:15:35 +0800 Message-ID: <5aefc78f-9a75-9b44-9471-87f42011b7c2@linux.alibaba.com> Date: Tue, 22 Mar 2022 10:15:34 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.6.0 Subject: Re: [PATCH] ocfs2: fix check if list iterator did find an element Content-Language: en-US To: Jakob Koschel Cc: Mark Fasheh , Linux Kernel Mailing List , ocfs2-devel@oss.oracle.com, Joel Becker , Andrew Morton , Geert Uytterhoeven , Masahiro Yamada , Miguel Ojeda , Mike Rapoport , Brian Johannesmeyer , Cristiano Giuffrida , "Bos, H.J." , Dan Carpenter References: <20220319203106.2541700-1-jakobkoschel@gmail.com> From: Joseph Qi In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,NICE_REPLY_A, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY 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 3/21/22 9:34 PM, Jakob Koschel wrote: > >> On 21. Mar 2022, at 02:50, Joseph Qi wrote: >> >> >> >> On 3/20/22 4:31 AM, Jakob Koschel wrote: >>> Instead of setting 'res' to NULL, it should only be set if >>> the suitable element was found. >>> >>> In the original code 'res' would have been set to an incorrect pointer >>> if the list is empty. >>> >> The logic before iteration can make sure track_list won't be empty. >> Please refer the discussion via: >> https://lore.kernel.org/ocfs2-devel/bd0ec87e-b490-83dc-2363-5e5342c59fa4@linux.alibaba.com/T/#m96d4397930201d83d68677c33a9721ae8dbd8f15 > > ah yes, I just read up on the discussion there, sorry for having duplicated it > here. > > Was any conclusion reached there which fixes can/should be merged? > > This code obviously can always be safe if the list cannot be empty. > That's also not necessarily the reason I'm fixing this. The reason is that > we want to get rid of any use of the list iterator variable after the loop > ('res' in this case). This will allow moving the list iterator variable > into the scope of the list iterator macro to forbid any invalid use of it > at compile time. Like this you don't have to rely on assumptions that are > hard to validate (e.g. that a certain list is never empty). > > The patch here is the minimal change to simply do that but looking at > Dan Carpenter patch there might be more things in this code that can > be simplified. > Agree, so I'm fine with this change. So could you please update the description and send v2? Thanks, Joseph > [CC'd Dan Carpenter] > > See [1] for changes that have already been merged: > > [1] https://lore.kernel.org/linux-kernel/20220308171818.384491-3-jakobkoschel@gmail.com/ > >> >> Thanks, >> Joseph >> >>> In preparation to limit the scope of the list iterator to the list >>> traversal loop, use a dedicated pointer pointing to the found element [1]. >>> >>> Link: https://lore.kernel.org/all/YhdfEIwI4EdtHdym@kroah.com/ >>> Signed-off-by: Jakob Koschel >>> --- >>> fs/ocfs2/dlm/dlmdebug.c | 12 ++++++------ >>> 1 file changed, 6 insertions(+), 6 deletions(-) >>> >>> diff --git a/fs/ocfs2/dlm/dlmdebug.c b/fs/ocfs2/dlm/dlmdebug.c >>> index d442cf5dda8a..be5e9ed7da8d 100644 >>> --- a/fs/ocfs2/dlm/dlmdebug.c >>> +++ b/fs/ocfs2/dlm/dlmdebug.c >>> @@ -541,7 +541,7 @@ static void *lockres_seq_start(struct seq_file *m, loff_t *pos) >>> struct debug_lockres *dl = m->private; >>> struct dlm_ctxt *dlm = dl->dl_ctxt; >>> struct dlm_lock_resource *oldres = dl->dl_res; >>> - struct dlm_lock_resource *res = NULL; >>> + struct dlm_lock_resource *res = NULL, *iter; >>> struct list_head *track_list; >>> >>> spin_lock(&dlm->track_lock); >>> @@ -556,11 +556,11 @@ static void *lockres_seq_start(struct seq_file *m, loff_t *pos) >>> } >>> } >>> >>> - list_for_each_entry(res, track_list, tracking) { >>> - if (&res->tracking == &dlm->tracking_list) >>> - res = NULL; >>> - else >>> - dlm_lockres_get(res); >>> + list_for_each_entry(iter, track_list, tracking) { >>> + if (&iter->tracking != &dlm->tracking_list) { >>> + dlm_lockres_get(iter); >>> + res = iter; >>> + } >>> break; >>> } >>> spin_unlock(&dlm->track_lock); >>> >>> base-commit: 34e047aa16c0123bbae8e2f6df33e5ecc1f56601 >>> -- >>> 2.25.1 > > Jakob