Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp1136835pxf; Thu, 18 Mar 2021 22:55:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzODuWDnIKiGdJpyewylYOHYXABBXOGDLSadAWMP5doZ8/YGTtSjkd/u3pNbH0Fk6O+k0La X-Received: by 2002:a17:906:22d2:: with SMTP id q18mr2420871eja.437.1616133309311; Thu, 18 Mar 2021 22:55:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616133309; cv=none; d=google.com; s=arc-20160816; b=ZORNQPMAScfHmy4C0rXXaY1YjW/pZb5sfcE7+EIPGEthmQe2E9J8EAutVAxYD7BGPR seoyNZDgID30mwxYqQ0oulJJa8oUIcbSDM50G0Z/eVwxmuHs4DnsOnqi18e6/cdX8Ex5 /GSx56MwXmw8kkzIOhS0oX3nEoBbcOAvsWaeyzbGbSW8z5KyfHE1Wh0PGIMH0EYKzyWZ 7OFLVwtBnvRvTykXejz/NqACl+cgJ0EY+8mbFBoFrxQwqZYTosawokBnY16d3gQCmkSy /KdaRmHXS6mQMf6F1nfecr+vJJ00EHLaNBG9ABQK9zuaYCbvm0/pSECgKtdf3Ov0lCQ2 vWoQ== 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=HrcS5DvDvEhZWOL4skNMel1B6oLPKZtu3foGo7w1mPI=; b=hUi40iWopEdIY605Yx4plazXDxsdV7JpgYI9pxC1mtExGj29G9XqJbrQvJcldYOtVI SRo1i1u5o29WzTXiRWuMeoP2aWU5KmrhghIW6PyxJNr9CW55HD7rSlJ30geO2LjpXdt5 0/xO/oMDWHrM2YgxoVpHlXPOcyZqlWl8qDxIfChHt7TvH77usou1kWzotaXjUNif2v6Q bnFIu/6GiYxhOwZcjwwhy8WVH61c6mNb+fwE82DO9NSxp40XzATY7LWiI38eP/JkJUOi x7QmA2TeejFqXP1Mk0fdEz4brkfU6W4PiBeZ9WfMYowUhWiwjlTzME52EtENZCcyNhfp M9zg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=G3MdP2vd; 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 p13si3268777edt.516.2021.03.18.22.54.47; Thu, 18 Mar 2021 22:55:09 -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=G3MdP2vd; 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 S233902AbhCSFxk (ORCPT + 99 others); Fri, 19 Mar 2021 01:53:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56988 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233920AbhCSFx0 (ORCPT ); Fri, 19 Mar 2021 01:53:26 -0400 Received: from mail-pg1-x52c.google.com (mail-pg1-x52c.google.com [IPv6:2607:f8b0:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4419DC06175F for ; Thu, 18 Mar 2021 22:53:26 -0700 (PDT) Received: by mail-pg1-x52c.google.com with SMTP id v3so2967871pgq.2 for ; Thu, 18 Mar 2021 22:53:26 -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=HrcS5DvDvEhZWOL4skNMel1B6oLPKZtu3foGo7w1mPI=; b=G3MdP2vdCP3PgfU40CFhK8NZgNnVhfnpT/r7NV68fxNUslrDB5iwcQQ645gMs8itXD XgizU0hLKBRpz9JpJ6LzfA2kAhJIi1WHSRTdbx4sDFXUCjnDAlwNw3oYEWz9oGcksrtq 3BvSzOLh0tP/b9IQRij69/d0ggjjPWNUEligdRGAz8pp6n2SQM6FhMgBNNcPgY38b1zn rWNMrTvSsIO5G7ZvaGzuZp5Emf2+GvhN4uhpPJZEfdBWJ0P1IzktMbB1q1pS+qIyDgGy wfS1gG4NukLqEDdnDybBLcTc9GnL8xs20vieIXPf7YcbqBS1WBky5LObje3kyGq5y8/V wTng== 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=HrcS5DvDvEhZWOL4skNMel1B6oLPKZtu3foGo7w1mPI=; b=r1LcTFi9qlnqICY8lCYKROkdvQRza6gjwKdBo/AAk14AVpPe57mvdhK90EUDULHXy9 4cfeAHQNNwLN7nnJiGBI5ltFZqooXDJP0+61e6G0tbtlPnclIOKXaBUh20wQmx48sc+w iWzJCy/3EWsfaIj/vkw/+yBy9u+ilK6thCgSUecwy/58ldkzmx06unEvG21mP3GckvKg jPgkX84kQQhwVaiTAmtw8OvIlyqi7rShFp1w2ueDbq6o/dUUHZuKmipTPKDAq9LkccTA zJ711FCbLq1R9HTkGZMWj5UtG6otH1XBHEIQ85oHm8JXGuoGsSz+Gjd4PfEsFQSDuzEj VrRg== X-Gm-Message-State: AOAM532fy0qpX6rHVa/4wwhc+DikvVUNrhugOPWa2nOBtaM6IuW8aW67 xVitogUdjUbURa2kAC/+0M8kzw== X-Received: by 2002:a62:1ad0:0:b029:1f2:c7b3:3039 with SMTP id a199-20020a621ad00000b02901f2c7b33039mr7958230pfa.30.1616133205650; Thu, 18 Mar 2021 22:53:25 -0700 (PDT) Received: from localhost ([122.171.124.15]) by smtp.gmail.com with ESMTPSA id b19sm3974164pfo.7.2021.03.18.22.53.24 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 18 Mar 2021 22:53:25 -0700 (PDT) Date: Fri, 19 Mar 2021 11:23:22 +0530 From: Viresh Kumar To: Jie Deng Cc: linux-i2c@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, mst@redhat.com, wsa@kernel.org, jasowang@redhat.com, wsa+renesas@sang-engineering.com, andriy.shevchenko@linux.intel.com, conghui.chen@intel.com, arnd@arndb.de, kblaiech@mellanox.com, jarkko.nikula@linux.intel.com, Sergey.Semin@baikalelectronics.ru, rppt@kernel.org, loic.poulain@linaro.org, tali.perry1@gmail.com, u.kleine-koenig@pengutronix.de, bjorn.andersson@linaro.org, yu1.wang@intel.com, shuo.a.liu@intel.com, stefanha@redhat.com, pbonzini@redhat.com Subject: Re: [PATCH v8] i2c: virtio: add a virtio i2c frontend driver Message-ID: <20210319055322.lw4dhb2kwtrtd3qu@vireshk-i7> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20180716-391-311a52 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 16-03-21, 18:35, Jie Deng wrote: > +++ b/drivers/i2c/busses/i2c-virtio.c > +static int virtio_i2c_send_reqs(struct virtqueue *vq, > + struct virtio_i2c_req *reqs, > + struct i2c_msg *msgs, int nr) > +{ > + struct scatterlist *sgs[3], out_hdr, msg_buf, in_hdr; > + int i, outcnt, incnt, err = 0; > + > + for (i = 0; i < nr; i++) { > + if (!msgs[i].len) > + break; > + > + /* > + * Only 7-bit mode supported for this moment. For the address format, > + * Please check the Virtio I2C Specification. > + */ > + reqs[i].out_hdr.addr = cpu_to_le16(msgs[i].addr << 1); > + > + if (i != nr - 1) > + reqs[i].out_hdr.flags = cpu_to_le32(VIRTIO_I2C_FLAGS_FAIL_NEXT); > + > + outcnt = incnt = 0; > + sg_init_one(&out_hdr, &reqs[i].out_hdr, sizeof(reqs[i].out_hdr)); > + sgs[outcnt++] = &out_hdr; > + > + reqs[i].buf = i2c_get_dma_safe_msg_buf(&msgs[i], 1); You allocate a buffer here, lets see if they are freeing properly or not (I remember that I gave same feedback earlier as well, but anyway). > + if (!reqs[i].buf) > + break; > + > + sg_init_one(&msg_buf, reqs[i].buf, msgs[i].len); > + > + if (msgs[i].flags & I2C_M_RD) > + sgs[outcnt + incnt++] = &msg_buf; > + else > + sgs[outcnt++] = &msg_buf; > + > + sg_init_one(&in_hdr, &reqs[i].in_hdr, sizeof(reqs[i].in_hdr)); > + sgs[outcnt + incnt++] = &in_hdr; > + > + err = virtqueue_add_sgs(vq, sgs, outcnt, incnt, &reqs[i], GFP_KERNEL); > + if (err < 0) { > + pr_err("failed to add msg[%d] to virtqueue.\n", i); > + i2c_put_dma_safe_msg_buf(reqs[i].buf, &msgs[i], false); On failure here, you freed the buffers for request "i" but not others.. > + break; > + } > + } > + > + return i; > +} > + > +static int virtio_i2c_complete_reqs(struct virtqueue *vq, > + struct virtio_i2c_req *reqs, > + struct i2c_msg *msgs, int nr) > +{ > + struct virtio_i2c_req *req; > + unsigned int len; > + int i, j; > + > + for (i = 0; i < nr; i++) { > + req = virtqueue_get_buf(vq, &len); > + if (!(req && req == &reqs[i])) { > + pr_err("msg[%d]: addr=0x%x is out of order.\n", i, msgs[i].addr); > + break; Since you break here, what will happen to the buffer ? I thought virtqueue_get_buf() will return a req only once and then you can't access it ? > + } > + > + if (req->in_hdr.status != VIRTIO_I2C_MSG_OK) { > + pr_err("msg[%d]: addr=0x%x backend error.\n", i, msgs[i].addr); > + break; > + } > + > + i2c_put_dma_safe_msg_buf(req->buf, &msgs[i], true); > + } > + > + /* > + * Detach all the used buffers from the vq and > + * Release unused DMA safe buffer if any. > + */ > + for (j = i; j < nr; j++) { > + req = virtqueue_get_buf(vq, &len); > + if (req) > + i2c_put_dma_safe_msg_buf(req->buf, &msgs[j], false); This will come in play only if something failed in the earlier loop ? Or my understanding incorrect ? Also this should be merged with the above for loop itself, it is just doing part of it. > + } > + > + return i; > +} > + > +static int virtio_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) > +{ > + struct virtio_i2c *vi = i2c_get_adapdata(adap); > + struct virtqueue *vq = vi->vq; > + struct virtio_i2c_req *reqs; > + unsigned long time_left; > + int ret, nr; > + > + reqs = kcalloc(num, sizeof(*reqs), GFP_KERNEL); > + if (!reqs) > + return -ENOMEM; > + > + mutex_lock(&vi->lock); > + > + ret = virtio_i2c_send_reqs(vq, reqs, msgs, num); > + if (ret == 0) > + goto err_unlock_free; > + > + nr = ret; > + reinit_completion(&vi->completion); > + virtqueue_kick(vq); > + > + time_left = wait_for_completion_timeout(&vi->completion, adap->timeout); > + if (!time_left) { On error here, we will surely not free the buffers, isn't it ? > + dev_err(&adap->dev, "virtio i2c backend timeout.\n"); > + ret = -ETIMEDOUT; > + goto err_unlock_free; > + } > + > + ret = virtio_i2c_complete_reqs(vq, reqs, msgs, nr); > + > +err_unlock_free: > + mutex_unlock(&vi->lock); > + kfree(reqs); > + return ret; > +} -- viresh