Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp3500276pxv; Sun, 4 Jul 2021 21:41:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzvnA9OOmsyXUnwJMspGIGSJb1sNMJPYYGvJmh/L2njyrY7YqHBb11Z+HN82O423uqWQIpN X-Received: by 2002:aa7:cd85:: with SMTP id x5mr13938578edv.115.1625460063631; Sun, 04 Jul 2021 21:41:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625460063; cv=none; d=google.com; s=arc-20160816; b=oAVADEoOxCXhtz9qTqrn9Vkow98/TVY/qUG8VS9xs8+bVn3FoYhFy+ojFpqq30dFXl G574pXV+KySG7x/XC3q5UmWQUYEZAudRa4u435N6jhUHEEjcyfh1GThrbU8PtjG6pgud NPQcj7Y09TAAAEU3DPzFRjydz7pdn2NxNNOQl5URFPaO/ch9UelJyDZtCnROwZBdhlvA /J07ba+Bx1cRhHTDhrYVAYw51tb09jVpljM9sfVDPQOYbLSVLK3E4LODO9PPBaSgRTce BTV5KI83pYBOvYtMzcIbMCpCf/ynq/MTgRneIkBe5Xs+D7HexyrymGYxSfIPIqQ0fMNY LvSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=9P2iB/sAd1xmJMgbCZUej24RLFJsagtt1uS0tyR4q8M=; b=iDeWt5DGqKSFjf9/NU4t/jxFLKonpBDTBlkv7FCwXRmF5QyncZhXBgLJf2TbBwZYan uu47+tdVIxmGaWYrA2a//HGGfr9A01foIDMwNtu6ifvBZgT2PAaMY/8kH5uUsFMWOoE8 ndI7R94tE4Snm+Br6rOKL0Pw0z5eKs0jWDs95i/5QUJ3LPnSmTVCOVUfGumeWrLZ5u7p TkZwlh3BO6zbDmD/2397cNbZXMIlR3eWlJsnM7peRivtz0ADcAeb8hStyrBLyRPv8DE0 v3+yTn5DjSqXBXrLm8jYeauc8bkQYN0u5G03c6bpdw14BHlppV2yTk2xG1XyjNf/vkK1 5+ZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=w2USAZ+I; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v22si10819968ejc.505.2021.07.04.21.40.40; Sun, 04 Jul 2021 21:41:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=w2USAZ+I; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229666AbhGEElV (ORCPT + 99 others); Mon, 5 Jul 2021 00:41:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33178 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229495AbhGEElU (ORCPT ); Mon, 5 Jul 2021 00:41:20 -0400 Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AE42DC061762 for ; Sun, 4 Jul 2021 21:38:44 -0700 (PDT) Received: by mail-pj1-x1036.google.com with SMTP id n11so10989008pjo.1 for ; Sun, 04 Jul 2021 21:38:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=9P2iB/sAd1xmJMgbCZUej24RLFJsagtt1uS0tyR4q8M=; b=w2USAZ+ILD7DA3N5p2ftBJv8i0VXUjV8GESTg1sd/GdNGz+szkvOBjCkqy4uieeacq yuHsKRxPBIfIuNdpT9RHjEcwAlVMrrBeDSXZOcXLwoYnQ2q00DBngXc8dKIP/tE7R+Cl ql5k6+AyoWcCAEa/PdZWeI4ReVBr8J0bXlZBpZaChM3YI3jQ00sU3WRyU5xwSWAUPdv8 FA0cuLoHurAmzUXeKsIqWjlcR3Z3mSYDgM1+TaHxgmc7ZQK3su9Y2A9/SNzG1b7283hi 8+WcSc0ow4mNoffPvJFFgyvCVhdMqpBdp8np/3HfPnVBWqCgU8GxOuvBf/53g2p9QeOD LWxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=9P2iB/sAd1xmJMgbCZUej24RLFJsagtt1uS0tyR4q8M=; b=oLVJ1KJmLTzUp1Hxiht/R128GcRUUCpWP7CrVzBtsYiw9msuz32iDVz5MsN5incxE5 HrL0pEfdFWm7la40e5Cta3HzNvrhCF3C2sOZS4+AOSyZ98sl0V5d8pM4FAN9ZVftrwGZ lnJdifG5lEW3JXahBqhBnfH3cqxaMiB+sLQGnxjyfPa0DnmT+d3kTJ7gfImsIUrHq9hz rykq9QrAAaemWrphZj+rQepz5Ul2dqOJd8k6b8Hh4utosI+PbApQWOuYgzIcbymMZTfY B1I5NZ8b3vOc3T217qqbIH5KZ9EDaMxmY0qUYzcopdMwyx1iNRCG4aHhlYLiOYpAwFxj CONg== X-Gm-Message-State: AOAM531sg1zsrYhm1AysKKXMAeneLT7pfFxoKRhRYVJO9I2U2revg7mk QJRfhZhnLfbE2eagkL6ImrTArQ== X-Received: by 2002:a17:90a:8e82:: with SMTP id f2mr13568598pjo.177.1625459924145; Sun, 04 Jul 2021 21:38:44 -0700 (PDT) Received: from localhost ([106.201.108.2]) by smtp.gmail.com with ESMTPSA id l12sm10902491pff.105.2021.07.04.21.38.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Jul 2021 21:38:43 -0700 (PDT) Date: Mon, 5 Jul 2021 10:08:41 +0530 From: Viresh Kumar To: Jie Deng Cc: linux-i2c@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, wsa@kernel.org, wsa+renesas@sang-engineering.com, mst@redhat.com, arnd@arndb.de, jasowang@redhat.com, andriy.shevchenko@linux.intel.com, yu1.wang@intel.com, shuo.a.liu@intel.com, conghui.chen@intel.com, stefanha@redhat.com Subject: Re: [PATCH v12] i2c: virtio: add a virtio i2c frontend driver Message-ID: <20210705043841.zujwo672nfdndpg2@vireshk-i7> References: <20210705024056.ndth2bwn2itii5g3@vireshk-i7> <332af2be-0fb0-a846-8092-49d496fe8b6b@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <332af2be-0fb0-a846-8092-49d496fe8b6b@intel.com> User-Agent: NeoMutt/20180716-391-311a52 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 05-07-21, 11:45, Jie Deng wrote: > On 2021/7/5 10:40, Viresh Kumar wrote: > > On 02-07-21, 16:46, Jie Deng wrote: > > The right way of doing this is is making this function return - Error on failure > > and 0 on success. There is no point returning number of successful additions > > here. > > > We need the number for virtio_i2c_complete_reqs to do cleanup. We don't have > to > > do cleanup "num" times every time. Just do it as needed. If you do full cleanup here, then you won't required that at the caller site. > > Moreover, on failures this needs to clean up (free the dmabufs) itself, just > > like you did i2c_put_dma_safe_msg_buf() at the end. The caller shouldn't be > > required to handle the error cases by freeing up resources. > > > This function will return the number of requests being successfully prepared > and make sure > > resources of the failed request being freed. And virtio_i2c_complete_reqs > will free the > > resources of those successful request. It just looks cleaner to give such responsibility to each and every function, i.e. if they fail, they should clean stuff up instead of the caller. That's the normal philosophy you will find across kernel in most of the cases. > > > +static int virtio_i2c_complete_reqs(struct virtqueue *vq, > > > + struct virtio_i2c_req *reqs, > > > + struct i2c_msg *msgs, int nr, > > > + bool fail) > > > +{ > > > + struct virtio_i2c_req *req; > > > + bool failed = fail; > > > + unsigned int len; > > > + int i, j = 0; > > > + > > > + for (i = 0; i < nr; i++) { > > > + /* Detach the ith request from the vq */ > > > + req = virtqueue_get_buf(vq, &len); > > > + > > > + /* > > > + * Condition (req && req == &reqs[i]) should always meet since > > > + * we have total nr requests in the vq. > > > + */ > > > + if (!failed && (WARN_ON(!(req && req == &reqs[i])) || > > > + (req->in_hdr.status != VIRTIO_I2C_MSG_OK))) > > What about writing this as: > > > > if (!failed && (WARN_ON(req != &reqs[i]) || > > (req->in_hdr.status != VIRTIO_I2C_MSG_OK))) > > > > We don't need to check req here since if req is NULL, we will not do req->in_hdr > > at all. > > > It's right here just because the &reqs[i] will never be NULL in our case. > But if you see > > "virtio_i2c_complete_reqs" as an independent function, you need to check the > > req. From the perspective of the callee, you can't ask the caller always > give you > > the non-NULL parameters. We need to keep this driver optimized in its current form. If you see your own argument here, then why don't you test vq or msgs for a valid pointer ? And even reqs. If we know for certain that this will never happen, then it should be optimized. But if you see a case where reqs[i] can be NULL here, then it would be fine. > And some tools may give warnings. I don't see why a tool will raise an error here and if it does, then the tool is buggy and not the driver. And we don't need to take care of that. -- viresh