From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4AAB23C8732 for ; Mon, 27 Apr 2026 13:31:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777296702; cv=none; b=eXAJyBHj7oF/1CvSrDeLJLCDKilUV6LDHgpy0OxMI44qGbWWnQgNJKUzthmOyJrHzcy+YnuG4aiwd6f4Wk9MFuqkYmdxzuINQEmfN42MJPCOKw5B/XEoo1ETj+gzss/9OM1UcasFmA6NyEChdYo0UAy/v7FI1dQ/F+YCwclhQac= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777296702; c=relaxed/simple; bh=5A7d0xEr6UZg3zALUZ6HYgjPXriLWdN9GklYjVNl/f4=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=fO3R2LNOWvAjEwj0RJElVpYpqHToXW//4fveCpJz1QCK9S2xDfcKSjiiClrcgDcVlgnSkIQl82dEE6woBIlpP3AVkH7K+5PNgoAZeIUIBUNHmIVwhte6yOMIiMq4WdYrcITqR5YrEaGhUuwakuoXiKXg3wt0vcS3Pb+s8OwsyP8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=HIcqQ1Gq; arc=none smtp.client-ip=209.85.221.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="HIcqQ1Gq" Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-43d75312379so7364547f8f.1 for ; Mon, 27 Apr 2026 06:31:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777296700; x=1777901500; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=mRnlVjG0EUtLNoWywJoZdaf/5UhwPl/dJQnQyyvjvgs=; b=HIcqQ1GqCciey2E+G4aEiH7DZasxs3wPBkbpyKmbK7UMtrHieJTgosn8Qoqv2Bje4q Og/3rhMPHVBdJKqSkl4SYZTg3nRuZL8AaldjB/CJQzaiI3LGhb++GGjg0Y6sdgCAeemZ wjWkkW/nGb+LoDDYA+PGCT9MtOm3Oas9MBJ2GTTdf6K3ow+Npu6+RtzpB7aRGkbLqtuf avaM7WtfbM4QvRfYQ+U7P05/VAFYZGEbuyGr4fUfCeAnyCdLR+XKHJUWvmiWgVeJyIO1 Xkhdd7BKwqTiheKBDsKDZEy2ZD8ovRgDMUfQrTxLELHHCrJQbFqVdbgKwr68sonPEoWK wGrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777296700; x=1777901500; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=mRnlVjG0EUtLNoWywJoZdaf/5UhwPl/dJQnQyyvjvgs=; b=U3CY3/bQtca39goAVXfGJ1DBAx7J08ueWOFJMP3kAeRKEDTHny221bprSSXZ0yNqFK b6YMUnukAEXlhGLmMGSbbD7drnEXvNDKnm1bfF4PKtz1ZweE1NkCj23xIIKyFJ1NIlv6 dKEffyp3wUbam/WtoWb5crTcwExlXDJCFCBZKB+BIW2axLrs0P96dFJ9jQ8utRuUqkeg QzTifNSjWL/GHj4f7ofgpGsPOHm7hXDfQrfA3EBf8jr8qWAJs8JFx2AuIcgCEnT5oBlr XDgmgqnuMAv4IRBVj4FbsZ5BQiEAy5WgvN/ape8R1mLUAkP1fW5UdmtusgfZ/XjNn+p9 eSCg== X-Forwarded-Encrypted: i=1; AFNElJ+JZx23TUwQioRp6YhE7cZ9VbNupwFgynnW25TL1A1zRJ8XOURV9V1oXHj5IopzUYaW8w2QGa1eqdlmI/o=@vger.kernel.org X-Gm-Message-State: AOJu0Yzw5RrciheTWzRcBZVZzcMCnHhUQICwONhUTyNQmTLq8Hh/HKT7 7odHHidV9ewQ/SHVcgQEHWt9wBnF2psdgVsy/KB4Wjn4gMJB+vIeUoy3 X-Gm-Gg: AeBDiesfqRXzZ7mLbme60fAYlymCPD7F3S63vsYOd+KamRbT6S2zINFam5cDOa3KtNS PqxNhzrKfENJvMDzu/TUiMXi0sgauX4JkeUsX92IsqHz6y7dFctKql1RMlj9GPFRKZhbmar/zsg nAt+Pu1M8Iq+QJXp8Bv27NxT+fcIb4Tv9lZ/a2wLWCdZTtZWHY/eHc7fdhbcpoVcSjmJkoUWXsN os2mHV5A5px80iP5jQbt7lhU4vYy1oS9WYABn9gufwJtKKBCfc4XjJmSB9btSIwxu7BzJUAdk0a NNr5/Qyqg98eR+P8OBgC/CwbIXejGn7cWqkgnluYEk8UdmJ+k/XYbG8QYzYasN/NEjIP4GVsGWT leucymaOaougQ1NIlm0PWlFqIKNZFNY0VgxzY7+OEfRRDBFmQVtRmoVZ0M8kFCuoxVXHpWN/zIu 4i8zR5MfL7NEtLEFZkudquo3RXogt1qxnqVUp9MCsG5Wpj3R39U9LtfJzzz7fOmbk0a4Ql3YY1A qA= X-Received: by 2002:a5d:5848:0:b0:439:bee4:8a93 with SMTP id ffacd0b85a97d-43fe4073758mr67744932f8f.12.1777296699402; Mon, 27 Apr 2026 06:31:39 -0700 (PDT) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43fe4cc0f31sm81263613f8f.12.2026.04.27.06.31.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Apr 2026 06:31:39 -0700 (PDT) Date: Mon, 27 Apr 2026 14:31:37 +0100 From: David Laight To: Giorgi Tchankvetadze Cc: antoniu.miclaus@analog.com, lars@metafoo.de, Michael.Hennerich@analog.com, jic23@kernel.org, dlechner@baylibre.com, nuno.sa@analog.com, andy@kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Jonathan Cameron , Andy Shevchenko Subject: Re: [PATCH v2] iio: adc: ti-ads7138: replace kmalloc() with stack allocation in i2c_write_block Message-ID: <20260427143137.27bc4552@pumpkin> In-Reply-To: <20260427112705.71138-3-giorgitchankvetadze1997@gmail.com> References: <20260427112705.71138-3-giorgitchankvetadze1997@gmail.com> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Mon, 27 Apr 2026 15:27:07 +0400 Giorgi Tchankvetadze wrote: > The ads7138_i2c_write_block() function currently utilizes kmalloc() > to allocate a buffer for I2C transfers. However, the length > parameter passed to this function is strictly 2 bytes across all > driver invocations, making the total payload buffer size exactly 4 bytes. > Invoking the heap allocator for a 4-byte buffer introduces > unnecessary SLUB overhead. Have you confirmed that the buffer is never used for DMA? Provided the lock that blocks concurrent access from two threads is actually outside this code, a buffer for short transfers could be allocated within 'struct i2c_client'. David > > Replace the kmalloc() call with a statically sized 4-byte stack array. > Add a boundary check returning -EINVAL to ensure future driver > modifications do not overflow the stack buffer if a length > greater than 2 is requested. > Furthermore, dropping the dynamic allocation entirely removes the need to > include (which was the original motivation for this patch) > and streamlines the PIO transfer path. > > Suggested-by: Jonathan Cameron > Suggested-by: Andy Shevchenko > Signed-off-by: Giorgi Tchankvetadze > --- > drivers/iio/adc/ti-ads7138.c | 11 +++++------ > 1 file changed, 5 insertions(+), 6 deletions(-) > > diff --git a/drivers/iio/adc/ti-ads7138.c b/drivers/iio/adc/ti-ads7138.c > index ee5c1b8e3a8e..172893cbc17d 100644 > --- a/drivers/iio/adc/ti-ads7138.c > +++ b/drivers/iio/adc/ti-ads7138.c > @@ -102,21 +102,20 @@ static const int ads7138_oversampling_ratios[] = { > static int ads7138_i2c_write_block(const struct i2c_client *client, u8 reg, > u8 *values, u8 length) > { > + u8 buf[4]; > int ret; > - int len = length + 2; /* "+ 2" for OPCODE and reg */ > > - u8 *buf __free(kfree) = kmalloc(len, GFP_KERNEL); > - if (!buf) > - return -ENOMEM; > + if (length != 2) > + return -EINVAL; > > buf[0] = ADS7138_OPCODE_BLOCK_WRITE; > buf[1] = reg; > memcpy(&buf[2], values, length); > > - ret = i2c_master_send(client, buf, len); > + ret = i2c_master_send(client, buf, ARRAY_SIZE(buf)); > if (ret < 0) > return ret; > - if (ret != len) > + if (ret != ARRAY_SIZE(buf)) > return -EIO; > > return 0;