From patchwork Mon Oct 7 08:37:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Martelli X-Patchwork-Id: 13824318 Received: from mail-ej1-f51.google.com (mail-ej1-f51.google.com [209.85.218.51]) (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 F16DA189BAA; Mon, 7 Oct 2024 08:38:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728290282; cv=none; b=tF43dN2LqoTGoQ9uLY/nEAQ75gUAyMTEcARib/447peeL3LH7tPvKOcNA9/URZ5ld0Iyq7QPtIWHwr/wAsuuXIOBdESnvNezcokYoCHh194GhdBp8IOg8LvhpnpVtb0P8SEeNZF2IVEWe5oyNLIOunMsKilL1g0YedrIvKei/gk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728290282; c=relaxed/simple; bh=7Qd7pConDOask/BNXfSwFweuyl/4yUcBzrozpeWuKdI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=EEAbYcNUuTDzD7atiMTjoV2Eyi1UiHghB50dEQ1rFN0aSuUrg2IS9kjWqgEtldnQdo5rp8nvkTuZMW5+GeQB9WkJSBb57oNPPqmkSq3eC3JwrqhVQfE0q9o+LoItmlF1cz3/SIxWpWzZJoaRy/z2wFr02Us4fpp50BWqoi+akmM= 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=jO37YK/o; arc=none smtp.client-ip=209.85.218.51 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="jO37YK/o" Received: by mail-ej1-f51.google.com with SMTP id a640c23a62f3a-a7aa086b077so477773666b.0; Mon, 07 Oct 2024 01:38:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728290279; x=1728895079; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=7h4jXiLdPt0pxESQJDinZHqz2wgnSDllGc714nCDCUQ=; b=jO37YK/o+SfXJwkfwmiPKTlqtSgjYDVQNQKH/cVQjFZ05Jijn9z60tguAEsKXb0BhO /Hp6mZMojsJ+URIZwOWySqHKJqpl0x30b6/7rBS7hT3E+Wegl3xGVq+qe3odxgvXV0+v eHXIeNqJ9th5HyKu0cJwsVxrcVg0dN2NN5TJ3MoxIzmFBBlkNs8zalgPiRE621AI6x+l 0rgZBhHZiKISTTidpVO+bYrpDDI7fJlaQ66CQFOUsZfJeNYhBvzhSBldezs6zHzpmavw uWio26sdYHNO0SM2rIbifMMDYOEfOjlyu11Q7fgeof0Wxyqj/KIXw8EC9tU/C0Hz/+BF ZdnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728290279; x=1728895079; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7h4jXiLdPt0pxESQJDinZHqz2wgnSDllGc714nCDCUQ=; b=ZZ9nO7D9TrzLRuLstMW/d2kp3qV8TvTz4tdNjBQIRppV3PLW1sHX2ggjkR041vHoq2 AyuDNEM7+Lj9pvkSKQ5T8TcH5/oDaEDDvPaN/bVbXo1uwxJMm31vDTZ7PM/c96Js0t6h ADxZDiAu/WD+1C6mWswyy9Oas9i8qGwMlRP4JYmkLroy9HtlGPYhv3iu4noS0XbAsSZW u9+JXHGXZ5vF/VG9zRnk16dvsGqQYa67X4olPSq9pYSNbDA7y9mw6//Q7OFnQUCWHT3b OKCDwJ40xWtZQ5y7FeT02llPZxXiMIwUhd9wEZJuSN7HScwB35R22xkCOW6TMLFCawRb 3z3Q== X-Forwarded-Encrypted: i=1; AJvYcCURHKsRGAZTXU24KfIK8/b7dq45YxI7C0aAotgy7t5yzfqDoHd29Qu3WsobbYazZoyM0Fep9AwdiAc5gQ==@vger.kernel.org, AJvYcCV3UBtaYxY33Fv3XE1gp8VtA74q4IahFaqAMpWQpkaLDgBxjNYhVwVs1txxqmLwijxyfuWYwP9TPuc=@vger.kernel.org, AJvYcCWiw9eL+lFAnX6QglithZYmphWpftfO7O4QwZOPLGmG6WyhfZmTZF+gsra2NdNAdcB4/I0L5Ie56wekrSo=@vger.kernel.org X-Gm-Message-State: AOJu0YylHcZpW9itvaMVhqvLVlufG8thY7ALIj3zpTN14RRW/E3H1dMv iQ5AgLhZpKwT/UwxwFextNTdM1uKMprJ2o2ZD0lilllGDZgkDD36 X-Google-Smtp-Source: AGHT+IHb8QJqDDbymdlp6OK+wtmSQbGb4sEYnd+tjiMcvWc7eKwscGGJU9MQkIPuOEI4D7OO5FB7Kw== X-Received: by 2002:a17:906:fd89:b0:a86:9c41:cfc1 with SMTP id a640c23a62f3a-a991bce40demr1243863866b.8.1728290279222; Mon, 07 Oct 2024 01:37:59 -0700 (PDT) Received: from localhost (host-79-19-52-27.retail.telecomitalia.it. [79.19.52.27]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a992e625a7dsm350698666b.84.2024.10.07.01.37.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 01:37:58 -0700 (PDT) From: Matteo Martelli Date: Mon, 07 Oct 2024 10:37:10 +0200 Subject: [PATCH v2 1/7] iio: core: add read_avail_release_resource callback to fix race Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241007-iio-read-avail-release-v2-1-245002d5869e@gmail.com> References: <20241007-iio-read-avail-release-v2-0-245002d5869e@gmail.com> In-Reply-To: <20241007-iio-read-avail-release-v2-0-245002d5869e@gmail.com> To: Jonathan Cameron , Lars-Peter Clausen , Michael Hennerich , Alisa-Dariana Roman , Christian Eggers , Peter Rosin , Paul Cercueil , Sebastian Reichel Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org, linux-pm@vger.kernel.org, Matteo Martelli X-Mailer: b4 0.14.2 Some iio drivers currently share an available info buffer that might be changed while iio core prints it to sysfs. To avoid the buffer corruption, add a release callback to let iio drivers allocate a copy of the available info buffer and later free it in the release callback. Such control is kept in the driver logic so that some driver that needs a big available info buffer might also perform some check to keep the copied buffer around in case no race has occurred. Signed-off-by: Matteo Martelli --- drivers/iio/industrialio-core.c | 14 +++++++++++--- include/linux/iio/iio.h | 4 ++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c index 6a6568d4a2cb3a3f63381d5a6f25a2881b3ba2ed..4aea9de9f15a4d70f9d02fb3d47df49eef8c8423 100644 --- a/drivers/iio/industrialio-core.c +++ b/drivers/iio/industrialio-core.c @@ -859,12 +859,20 @@ static ssize_t iio_read_channel_info_avail(struct device *dev, return ret; switch (ret) { case IIO_AVAIL_LIST: - return iio_format_avail_list(buf, vals, type, length); + ret = iio_format_avail_list(buf, vals, type, length); + break; case IIO_AVAIL_RANGE: - return iio_format_avail_range(buf, vals, type); + ret = iio_format_avail_range(buf, vals, type); + break; default: - return -EINVAL; + ret = -EINVAL; } + + if (indio_dev->info->read_avail_release_resource) + indio_dev->info->read_avail_release_resource( + indio_dev, this_attr->c, vals, this_attr->address); + + return ret; } /** diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h index 18779b631e9071801958fc9d50b941b548fab940..55326f0310b2e9b3fa561c4728e7eabe1d7a5e78 100644 --- a/include/linux/iio/iio.h +++ b/include/linux/iio/iio.h @@ -491,6 +491,10 @@ struct iio_info { int *length, long mask); + void (*read_avail_release_resource)(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + const int *vals, long mask); + int (*write_raw)(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int val, From patchwork Mon Oct 7 08:37:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Martelli X-Patchwork-Id: 13824319 Received: from mail-ed1-f54.google.com (mail-ed1-f54.google.com [209.85.208.54]) (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 6CD7018BC1A; Mon, 7 Oct 2024 08:38:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728290284; cv=none; b=r4RfYt2itcsdTDB1tlTujaTuPPycHdGlNULFKOBo5BgBSCcL2u5K0t5vpZu+LJ3WiA5v0J41Sp1PzuHuc3VE0VaHkjLgzW4yrb2D9T/pN9vWn98aDlc7lJ4kyY5wE0bw2022KAwvw0gTumquIlUkK6xnCfOjZ7WpYZQDZx1rMLw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728290284; c=relaxed/simple; bh=cNg176BYetOGo2MpF+NBbDgUNyJwivGqccecDk2ywTU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ov42G4VVlMkEhy93hc936yWsyDjzFzgL+ORu8ZLzPcCxuiHW78Cmwz3BhzNqtai0h1NlTDCfMz9ShfLSNfHRKVL2JLNC6oEl2wq/rP7zDacVNvDN+6DFbWymYhqVqkR98j+FXE5MUj36yJ4y5PQxTMRSfgzRH71DBnO04GR2j0I= 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=aDGTSUeM; arc=none smtp.client-ip=209.85.208.54 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="aDGTSUeM" Received: by mail-ed1-f54.google.com with SMTP id 4fb4d7f45d1cf-5c718bb04a3so5823297a12.3; Mon, 07 Oct 2024 01:38:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728290281; x=1728895081; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=gYEu7vJAEzzRAf4hNdfGGtBas1po8e0YLUji+g4IAZE=; b=aDGTSUeMYmxen5dnpZb+UNFGj46i1jYAt8Q8w36p3VKFydSph0Q0TpbPSgamvL9RL4 22OB46LHvxDaWHxI+Oq5luWdon0rOHievZRQUhr0XfxzVRGLB0GMYhlwzSDdsQiudyZ9 YQj92Z61BypPEfzRLoMR1cVsHUR3TBlFfx/v7mnSrDYLG/JLurm9d+LfUp4yTW6bdUJn WVyCwt04uraXZH/mUe+gb/+JOKo9fuLveIW+dzB0X2yZV51ABVca1Xg7pK3XCBjZJPvG VChJeeh5sjD5nD9MQEXroZioj9Nx8jE6sfGXAzlqDuGL1Mu0MgaFWUSCYA8lwYPLorux m7Tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728290281; x=1728895081; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gYEu7vJAEzzRAf4hNdfGGtBas1po8e0YLUji+g4IAZE=; b=nc6/bEi+NSU4sVkDsoKfkPWwgvQjX/7+nLHPOZvvQxSI9YL1DoNK0mt6Hw2Fs793ZP 1CTBqocZc6xZ6q+QbBKM2Jh/1P46roO/nlRsEj/Z8GY5imkfPynjNl538ku9uU+OWMe6 GsS9gIb0npjB9bP+fzbfwUTEEAZuIhdTrB6/zOVt4bqjA2k3QKThGvXYaeBl8bWfd6Cm 84wHUNSgoS8uLG3aE3x9iR/7YbIZUjFEz3yhWr2sHNvV0LsYudt0AWOQRIiYd8DmdGcr fSeF/fo5gL11F2fCw6CQTs2k9QZKCsWBlpue3qYUhOks8zWrRdSsom637B7wnXPb0PSP AHOQ== X-Forwarded-Encrypted: i=1; AJvYcCUsIjPalc0fvDUlv6ns2LmxO3CUr1fVY51Ycz2y8PAT3HOs+G//ENjfPr+xhly5/7257vH/8oFpYB8zz7Q=@vger.kernel.org, AJvYcCVx0iS/6GTSuKBcaQ+Cm4xuN29Ls1uVQfQ4nbBvmp0t42Jl1Ylan9o02D4EjdsxagqFAfJEgBMmGNZgcA==@vger.kernel.org, AJvYcCW0u9umG/Ehpo7JgA9AQkR+08nlCvMlG7xaE0ju66JfffL6BHlNJwx+YnDcNbn2QbqBcheZsAGp4fw=@vger.kernel.org X-Gm-Message-State: AOJu0Yxi9Bj5At8aat4Efn1Di6yP826jKJ1MukaGr2a4aVT3ZFAraf1b 6Uja3HmpIVQMehJ4h2wp5Y+ytUzI3aya5iTocTvBA2bSak3X7QKv X-Google-Smtp-Source: AGHT+IEUmlMfRDe3veYeB4Xt1to9N5WazOnYhbrVmiLPifEvgtZ6WsZt65nVRvQalMjBfDO6SH7M5Q== X-Received: by 2002:a05:6402:34d4:b0:5c5:b8bd:c873 with SMTP id 4fb4d7f45d1cf-5c8d2e3004emr7792145a12.16.1728290280460; Mon, 07 Oct 2024 01:38:00 -0700 (PDT) Received: from localhost (host-79-19-52-27.retail.telecomitalia.it. [79.19.52.27]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5c8e05a9297sm2917925a12.33.2024.10.07.01.37.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 01:38:00 -0700 (PDT) From: Matteo Martelli Date: Mon, 07 Oct 2024 10:37:11 +0200 Subject: [PATCH v2 2/7] iio: pac1921: use read_avail+release APIs instead of custom ext_info Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241007-iio-read-avail-release-v2-2-245002d5869e@gmail.com> References: <20241007-iio-read-avail-release-v2-0-245002d5869e@gmail.com> In-Reply-To: <20241007-iio-read-avail-release-v2-0-245002d5869e@gmail.com> To: Jonathan Cameron , Lars-Peter Clausen , Michael Hennerich , Alisa-Dariana Roman , Christian Eggers , Peter Rosin , Paul Cercueil , Sebastian Reichel Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org, linux-pm@vger.kernel.org, Matteo Martelli X-Mailer: b4 0.14.2 The pac1921 driver was exposing the available scale info via an ad-hoc ext_info attribute instead of using the read_avail API. This to avoid a possible race condition: while the available current scales were being printed to sysfs by iio core (iio_read_channel_info_avail), the shunt resistor might have been changed concurrently. Switch to the read_avail+release APIs now that the race condition has been addressed. Signed-off-by: Matteo Martelli --- drivers/iio/adc/pac1921.c | 128 ++++++++++++++++------------------------------ 1 file changed, 45 insertions(+), 83 deletions(-) diff --git a/drivers/iio/adc/pac1921.c b/drivers/iio/adc/pac1921.c index 4c2a1c07bc399028f0334885fc9cd4552d5892b1..f491a4923380ef9b406a68f8cc413df873869e3e 100644 --- a/drivers/iio/adc/pac1921.c +++ b/drivers/iio/adc/pac1921.c @@ -445,11 +445,52 @@ static int pac1921_read_avail(struct iio_dev *indio_dev, *vals = pac1921_int_num_samples; *length = ARRAY_SIZE(pac1921_int_num_samples); return IIO_AVAIL_LIST; + case IIO_CHAN_INFO_SCALE: + switch (chan->channel) { + case PAC1921_CHAN_VBUS: + *vals = (const int *)pac1921_vbus_scales; + *length = ARRAY_SIZE(pac1921_vbus_scales) * 2; + *type = IIO_VAL_INT_PLUS_NANO; + return IIO_AVAIL_LIST; + + case PAC1921_CHAN_VSENSE: + *vals = (const int *)pac1921_vsense_scales; + *length = ARRAY_SIZE(pac1921_vsense_scales) * 2; + *type = IIO_VAL_INT_PLUS_NANO; + return IIO_AVAIL_LIST; + + case PAC1921_CHAN_CURRENT: { + struct pac1921_priv *priv = iio_priv(indio_dev); + + *length = ARRAY_SIZE(priv->current_scales) * 2; + *type = IIO_VAL_INT_PLUS_NANO; + + guard(mutex)(&priv->lock); + + *vals = kmemdup_array((int *)priv->current_scales, + *length, sizeof(int), GFP_KERNEL); + if (!*vals) + return -ENOMEM; + + return IIO_AVAIL_LIST; + } + default: + return -EINVAL; + } default: return -EINVAL; } } +static void pac1921_read_avail_release_res(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + const int *vals, long mask) +{ + if (mask == IIO_CHAN_INFO_SCALE && + chan->channel == PAC1921_CHAN_CURRENT) + kfree(vals); +} + /* * Perform configuration update sequence: set the device into read state, then * write the config register and set the device back into integration state. @@ -749,6 +790,7 @@ static int pac1921_read_event_value(struct iio_dev *indio_dev, static const struct iio_info pac1921_iio = { .read_raw = pac1921_read_raw, .read_avail = pac1921_read_avail, + .read_avail_release_resource = pac1921_read_avail_release_res, .write_raw = pac1921_write_raw, .write_raw_get_fmt = pac1921_write_raw_get_fmt, .read_label = pac1921_read_label, @@ -806,88 +848,7 @@ static ssize_t pac1921_write_shunt_resistor(struct iio_dev *indio_dev, return len; } -/* - * Emit on sysfs the list of available scales contained in scales_tbl - * - * TODO:: this function can be replaced with iio_format_avail_list() if the - * latter will ever be exported. - * - * Must be called with lock held if the scales_tbl can change runtime (e.g. for - * the current scales table) - */ -static ssize_t pac1921_format_scale_avail(const int (*const scales_tbl)[2], - size_t size, char *buf) -{ - ssize_t len = 0; - - for (unsigned int i = 0; i < size; i++) { - if (i != 0) { - len += sysfs_emit_at(buf, len, " "); - if (len >= PAGE_SIZE) - return -EFBIG; - } - len += sysfs_emit_at(buf, len, "%d.%09d", scales_tbl[i][0], - scales_tbl[i][1]); - if (len >= PAGE_SIZE) - return -EFBIG; - } - - len += sysfs_emit_at(buf, len, "\n"); - return len; -} - -/* - * Read available scales for a specific channel - * - * NOTE: using extended info insted of iio.read_avail() because access to - * current scales must be locked as they depend on shunt resistor which may - * change runtime. Caller of iio.read_avail() would access the table unlocked - * instead. - */ -static ssize_t pac1921_read_scale_avail(struct iio_dev *indio_dev, - uintptr_t private, - const struct iio_chan_spec *chan, - char *buf) -{ - struct pac1921_priv *priv = iio_priv(indio_dev); - const int (*scales_tbl)[2]; - size_t size; - - switch (chan->channel) { - case PAC1921_CHAN_VBUS: - scales_tbl = pac1921_vbus_scales; - size = ARRAY_SIZE(pac1921_vbus_scales); - return pac1921_format_scale_avail(scales_tbl, size, buf); - - case PAC1921_CHAN_VSENSE: - scales_tbl = pac1921_vsense_scales; - size = ARRAY_SIZE(pac1921_vsense_scales); - return pac1921_format_scale_avail(scales_tbl, size, buf); - - case PAC1921_CHAN_CURRENT: { - guard(mutex)(&priv->lock); - scales_tbl = priv->current_scales; - size = ARRAY_SIZE(priv->current_scales); - return pac1921_format_scale_avail(scales_tbl, size, buf); - } - default: - return -EINVAL; - } -} - -#define PAC1921_EXT_INFO_SCALE_AVAIL { \ - .name = "scale_available", \ - .read = pac1921_read_scale_avail, \ - .shared = IIO_SEPARATE, \ -} - -static const struct iio_chan_spec_ext_info pac1921_ext_info_voltage[] = { - PAC1921_EXT_INFO_SCALE_AVAIL, - {} -}; - static const struct iio_chan_spec_ext_info pac1921_ext_info_current[] = { - PAC1921_EXT_INFO_SCALE_AVAIL, { .name = "shunt_resistor", .read = pac1921_read_shunt_resistor, @@ -911,6 +872,7 @@ static const struct iio_chan_spec pac1921_channels[] = { .type = IIO_VOLTAGE, .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), + .info_mask_separate_available = BIT(IIO_CHAN_INFO_SCALE), .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) | BIT(IIO_CHAN_INFO_SAMP_FREQ), @@ -928,12 +890,12 @@ static const struct iio_chan_spec pac1921_channels[] = { .indexed = 1, .event_spec = pac1921_overflow_event, .num_event_specs = ARRAY_SIZE(pac1921_overflow_event), - .ext_info = pac1921_ext_info_voltage, }, { .type = IIO_VOLTAGE, .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), + .info_mask_separate_available = BIT(IIO_CHAN_INFO_SCALE), .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) | BIT(IIO_CHAN_INFO_SAMP_FREQ), @@ -951,12 +913,12 @@ static const struct iio_chan_spec pac1921_channels[] = { .indexed = 1, .event_spec = pac1921_overflow_event, .num_event_specs = ARRAY_SIZE(pac1921_overflow_event), - .ext_info = pac1921_ext_info_voltage, }, { .type = IIO_CURRENT, .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), + .info_mask_separate_available = BIT(IIO_CHAN_INFO_SCALE), .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) | BIT(IIO_CHAN_INFO_SAMP_FREQ), From patchwork Mon Oct 7 08:37:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Martelli X-Patchwork-Id: 13824320 Received: from mail-ej1-f42.google.com (mail-ej1-f42.google.com [209.85.218.42]) (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 A37C018C010; Mon, 7 Oct 2024 08:38:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728290285; cv=none; b=t1QFe1AgUVvQDSJbhy61HO8W+0il8JCjne7HsS8mljZmHw2pZAYuvr9w/hduJufvhm4KuA0Fua6wldO+nMAl+zUE27MtnYzwyw0Joet6a2DdK1GhYNejEppS/L2+HTWLBG/CbxqHkNvuBROJQ8K477+YVKczKSAzldhstIAH/1w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728290285; c=relaxed/simple; bh=2/hMSftv+3dvVX2Wa2Ap1k9j9lysmFSfimPuu4XRnro=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=AflMu8lrq97c5kKRORIEr9OWMAZGs1eWZ73J60vyrw00MMlERGhnF3XxLmk2bJ/pfy9+Ckt3ftqM1hPEza6ByGPWtC4LQDsFzFbOx/Uwu44IpUmKCcdXsOc1eH/2S13pn05YL+N0frcB0aNen6iIsZL9prYnDLqX7sFwx+dM2+k= 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=DHq2kuUU; arc=none smtp.client-ip=209.85.218.42 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="DHq2kuUU" Received: by mail-ej1-f42.google.com with SMTP id a640c23a62f3a-a995ec65c35so44586966b.1; Mon, 07 Oct 2024 01:38:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728290282; x=1728895082; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=MTXdcjBlauvr7yxzI4HI4Dpu0RqCMP0lDL0VjxoSP1k=; b=DHq2kuUUV3BZFac8NoBYfwmYd42aEAswQeUbhvEMGmfV5TSt7UIRWFeu8ZzH+wEjuD qD+SGuaoh4+s3ML9r0LB5sY0p68KChRy7jf/AQWk12yYdQJcW9w7xWs8K4zmLCHh1dSI 8Ne43nc/IyXLk53VvQXIYW41Zlq9HhCMoXhsLNHROs1OjTPBUWUTxPuaN/ex9Vrb/6HZ MLAkrLy7jQyehRShYXzhmal3oUyUp5xN2fho5gW04o0K7CZlg7q8IMsErEVx5PVQqcl6 PTIWCYSIhnHJlb9IVX5sct8CrOpnpTGjXs4QMTf87CSaf93x/GSoES+PzLD4XGbwcNWo 0FaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728290282; x=1728895082; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MTXdcjBlauvr7yxzI4HI4Dpu0RqCMP0lDL0VjxoSP1k=; b=FtqDoA7UIkYaQ9BE5xqTur7AeetFWXIDN4ew/XyHila2Nmr7uMat1rqNMLIHcy22Jc iai0XeTxffsJAnMTAmfZTS1pE0BsqAhFaBS4NeJLJcQfAwvtDmwri0vPS82MefQVtmcq 4q+Ir/1QShzPCRRC7OLfRvoWXQd8CoXaGCWwaDfrwf/B0wDxk2NVcj0hbE/V2T0gDlRM b4sg9dqyQXdHEp5r63+C86uWf2xuINTGBkACifDoFp/ebnLdiJUiJNtb37Qw9NN7DrbX Ebl9TmOYVdYwX1RW6XMffhFKWeULMvbJVZvkxO1BGnr0QLhKGW5Tg375CuHmoVes0LkE +xgA== X-Forwarded-Encrypted: i=1; AJvYcCU1OBRG6yM1SffIe2kslSo9+jmwRjT1kaUYeiL0ioiVYsudhsQdCJI+ChRlVSQLfEPgcHRKkdj/dYc=@vger.kernel.org, AJvYcCUrYlAuBR9SCRjyIZSGz12kEZa4xTCWhIeGhoh9x+MYtLS8lmggQq3Iv9O4MPz7fHcfA+53sq37/LhzMzU=@vger.kernel.org, AJvYcCV9wXJqz0UWZTLLU6wjm6uIQrrvDBesCzXWcuJhyDDhahUeQqLbfSqqjE6CDB2z/y44gd+7er02YmVHYg==@vger.kernel.org X-Gm-Message-State: AOJu0YyIHJzOJJZlaBi/JeNkRFsN1PWzZr+A8usu2LrcluxEiBIg7Gbk hYKHNlUVPAyuryxx1NVKvaBk3ZFqKlnYy703vN3j8JMpFNZ3jtRq X-Google-Smtp-Source: AGHT+IELlVxbtYI0i+pPyXAVWrLkI+Xdhk8y8wiB6riSCOvuDysWcJUQjy87E4cTcQRY6Whlrv7dqA== X-Received: by 2002:a17:907:7e92:b0:a99:4c71:704 with SMTP id a640c23a62f3a-a994c7108famr517619166b.26.1728290281692; Mon, 07 Oct 2024 01:38:01 -0700 (PDT) Received: from localhost (host-79-19-52-27.retail.telecomitalia.it. [79.19.52.27]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a99420bcc1esm261387766b.199.2024.10.07.01.38.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 01:38:01 -0700 (PDT) From: Matteo Martelli Date: Mon, 07 Oct 2024 10:37:12 +0200 Subject: [PATCH v2 3/7] iio: ad7192: copy/release available filter frequencies to fix race Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241007-iio-read-avail-release-v2-3-245002d5869e@gmail.com> References: <20241007-iio-read-avail-release-v2-0-245002d5869e@gmail.com> In-Reply-To: <20241007-iio-read-avail-release-v2-0-245002d5869e@gmail.com> To: Jonathan Cameron , Lars-Peter Clausen , Michael Hennerich , Alisa-Dariana Roman , Christian Eggers , Peter Rosin , Paul Cercueil , Sebastian Reichel Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org, linux-pm@vger.kernel.org, Matteo Martelli X-Mailer: b4 0.14.2 While available filter frequencies are being printed to sysfs by iio core (iio_read_channel_info_avail), the sampling frequency might be changed. This could cause the buffer shared with iio core to be corrupted. To prevent it, make a copy of the filter frequencies buffer and free it in the read_avail_release_resource callback. Signed-off-by: Matteo Martelli --- drivers/iio/adc/ad7192.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/drivers/iio/adc/ad7192.c b/drivers/iio/adc/ad7192.c index 7042ddfdfc03ee5ea58ca07fb1943feb6538175b..acf625ced0b21db8d44f77929e8a875b3c10e1b1 100644 --- a/drivers/iio/adc/ad7192.c +++ b/drivers/iio/adc/ad7192.c @@ -1056,12 +1056,19 @@ static int ad7192_read_avail(struct iio_dev *indio_dev, *length = ARRAY_SIZE(st->scale_avail) * 2; return IIO_AVAIL_LIST; - case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: - *vals = (int *)st->filter_freq_avail; + case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: { *type = IIO_VAL_FRACTIONAL; *length = ARRAY_SIZE(st->filter_freq_avail) * 2; + guard(mutex)(&st->lock); + + *vals = kmemdup_array((int *)st->filter_freq_avail, *length, + sizeof(int), GFP_KERNEL); + if (!*vals) + return -ENOMEM; + return IIO_AVAIL_LIST; + } case IIO_CHAN_INFO_OVERSAMPLING_RATIO: *vals = (int *)st->oversampling_ratio_avail; *type = IIO_VAL_INT; @@ -1073,6 +1080,14 @@ static int ad7192_read_avail(struct iio_dev *indio_dev, return -EINVAL; } +static void ad7192_read_avail_release_res(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + const int *vals, long mask) +{ + if (mask == IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) + kfree(vals); +} + static int ad7192_update_scan_mode(struct iio_dev *indio_dev, const unsigned long *scan_mask) { struct ad7192_state *st = iio_priv(indio_dev); @@ -1098,6 +1113,7 @@ static const struct iio_info ad7192_info = { .write_raw = ad7192_write_raw, .write_raw_get_fmt = ad7192_write_raw_get_fmt, .read_avail = ad7192_read_avail, + .read_avail_release_resource = ad7192_read_avail_release_res, .attrs = &ad7192_attribute_group, .validate_trigger = ad_sd_validate_trigger, .update_scan_mode = ad7192_update_scan_mode, @@ -1108,6 +1124,7 @@ static const struct iio_info ad7194_info = { .write_raw = ad7192_write_raw, .write_raw_get_fmt = ad7192_write_raw_get_fmt, .read_avail = ad7192_read_avail, + .read_avail_release_resource = ad7192_read_avail_release_res, .validate_trigger = ad_sd_validate_trigger, }; @@ -1116,6 +1133,7 @@ static const struct iio_info ad7195_info = { .write_raw = ad7192_write_raw, .write_raw_get_fmt = ad7192_write_raw_get_fmt, .read_avail = ad7192_read_avail, + .read_avail_release_resource = ad7192_read_avail_release_res, .attrs = &ad7195_attribute_group, .validate_trigger = ad_sd_validate_trigger, .update_scan_mode = ad7192_update_scan_mode, From patchwork Mon Oct 7 08:37:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Martelli X-Patchwork-Id: 13824321 Received: from mail-ed1-f53.google.com (mail-ed1-f53.google.com [209.85.208.53]) (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 D01FF18C326; Mon, 7 Oct 2024 08:38:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728290286; cv=none; b=A6TmDeoZRJCCd+DgB1yV3b7zs6mFqBaYHCI8Nr87K96ITTQ7UuyXScHh/gHRpRkzeWRyZVU5Riuz/XeQHojiiDxS88ZYvaTjwfhkdrj23dQh8EejUXx213W085/AGbre5K+hIaori8ks/phc2UoDaM0+P1X/gM8Ai6mHC+hB8Q0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728290286; c=relaxed/simple; bh=dfQ1cow3unMNifLlLnZ2v6pTkxjaTVb2Cs8MDMVYs38=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=IHcg5W7aGIYjNRCgihcG4fxkeaL5JAMsqFmv0HKEcDq4wyKVstWXBQSepW2NkjjMw0ONYCr2gDZvtXHo3pBH4dYUKuNUqJKS98CCHTmVNxQpvip/ulcZafdztSl/UnNJEnvVWqgTpPT6zioPu5LLr3MwJY005N+oWTLl803SCMI= 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=ZDJ9YdMp; arc=none smtp.client-ip=209.85.208.53 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="ZDJ9YdMp" Received: by mail-ed1-f53.google.com with SMTP id 4fb4d7f45d1cf-5c8952f7f95so4949851a12.0; Mon, 07 Oct 2024 01:38:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728290283; x=1728895083; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=1Iupx0UMz9bSEUnxJeQl9iuLZkh4H5XILO0ivcFTJMY=; b=ZDJ9YdMpe0mBjl1CwtzwgYGRPSzbiyUpNtwBq/LIxeNgloJTqsgcrH3EG4geBnvycO X6Kl27lo/MZ84lt+rJtFJkOQpWFV/CNHBkkCxCe8pTa8jccTINIvwA+dNdpJ9TvpppOR +m6isui38jIB9wIEKfI4tQ3QBIDP9+O3h0JRXNnKbRRz616mtR9xHKF8k0F/IjMtuZTa krSfoPjXrN/a/zI40DNbdio7Z81rbXZXUrWI7tJH8emFBGzVoji+tyrqEdZTx7HM7RzS IFU8q5Svh+thr3RUqYcrFHecmpZj5yybKxvHG+TrmXLlLv+xOG/l1Z5yEWzeuOQ15uPF 0cUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728290283; x=1728895083; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1Iupx0UMz9bSEUnxJeQl9iuLZkh4H5XILO0ivcFTJMY=; b=qHkuRmwWBTpFNmz7sC+lU1hHvV0OJnnt++gUHFA6t8P8Xlx1RYD1uD6TZNCn3y/F+L H84GraE8aC8yTslo00W+PfObfNZzJ4ytrZhBpP3YUgIvjuFuM76mIjH0gIJDZWU/T/v1 D58Q2HJPCJPQ14BjPqEmaA/HWeAxrWonUo3CkV051ZH9m9OyYyUQ8JxaJpCw8LCFoAQx YG4mbN09hTIGbmUCcjMJwRL5O/0HOFXklHUv5LYW/ft7/V+4qf3Nc5fa0IxYdvlKkfe/ a7V4WCbFHlDunR5y0rpOn6mTbcmua33CHWOw1LO69amPCElhLf84Bo/DC3yEeNNFl7vf E5wg== X-Forwarded-Encrypted: i=1; AJvYcCVS0+r8W7RVLxXhuu+BDHvBElZajv/DQDOj3dJgO230MOUm0LcxNlAyfo7vAxvfJSYhQ2IpiI3/GEs=@vger.kernel.org, AJvYcCWVrRpakMmHkxi+pwdB1vSTCAEth8I4ROgdzp3AuB5+Og1n6lfDv0tbUaMPDaDQMJFylEZN+I0Y/byY1no=@vger.kernel.org, AJvYcCWvUOH4V5Sl1BHYup1v7iiS7cyoaGn9tVLu/ePEF0gzZg6sxQOc4fJrXHjn4E4SuCzmEYLxN4F7YJ1tjg==@vger.kernel.org X-Gm-Message-State: AOJu0Yx9qxJMWz8fKdZLlGu1sR4IhB2NnlXAmuLmXIABHnBWAp5qup80 fWUijgnYGVFTLL6ln306uAoWWpSMjQnbynkUG8IryIkrI7+SIh7W X-Google-Smtp-Source: AGHT+IEO6T0vt8G1kvvCGBqXo4YiEkRYS5FqCX1sucevcI/s4sUILTJIb9ajf21mmHsJZu8JoacNeg== X-Received: by 2002:a17:907:1b98:b0:a99:422a:dee5 with SMTP id a640c23a62f3a-a99422adfffmr406402566b.57.1728290282955; Mon, 07 Oct 2024 01:38:02 -0700 (PDT) Received: from localhost (host-79-19-52-27.retail.telecomitalia.it. [79.19.52.27]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a99414b675esm272651666b.10.2024.10.07.01.38.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 01:38:02 -0700 (PDT) From: Matteo Martelli Date: Mon, 07 Oct 2024 10:37:13 +0200 Subject: [PATCH v2 4/7] iio: as73211: copy/release available integration times to fix race Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241007-iio-read-avail-release-v2-4-245002d5869e@gmail.com> References: <20241007-iio-read-avail-release-v2-0-245002d5869e@gmail.com> In-Reply-To: <20241007-iio-read-avail-release-v2-0-245002d5869e@gmail.com> To: Jonathan Cameron , Lars-Peter Clausen , Michael Hennerich , Alisa-Dariana Roman , Christian Eggers , Peter Rosin , Paul Cercueil , Sebastian Reichel Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org, linux-pm@vger.kernel.org, Matteo Martelli X-Mailer: b4 0.14.2 While available integration times are being printed to sysfs by iio core (iio_read_channel_info_avail), the sampling frequency might be changed. This could cause the buffer shared with iio core to be corrupted. To prevent it, make a copy of the integration times buffer and free it in the read_avail_release_resource callback. Signed-off-by: Matteo Martelli --- drivers/iio/light/as73211.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/drivers/iio/light/as73211.c b/drivers/iio/light/as73211.c index be0068081ebbbb37fdfb252b67a77b302ff725f6..27bc8cb791039944662a74fc72f09e2c3642cfa6 100644 --- a/drivers/iio/light/as73211.c +++ b/drivers/iio/light/as73211.c @@ -493,17 +493,32 @@ static int as73211_read_avail(struct iio_dev *indio_dev, struct iio_chan_spec co *type = IIO_VAL_INT; return IIO_AVAIL_LIST; - case IIO_CHAN_INFO_INT_TIME: + case IIO_CHAN_INFO_INT_TIME: { *length = ARRAY_SIZE(data->int_time_avail); - *vals = data->int_time_avail; *type = IIO_VAL_INT_PLUS_MICRO; - return IIO_AVAIL_LIST; + guard(mutex)(&data->mutex); + + *vals = kmemdup_array(data->int_time_avail, *length, + sizeof(int), GFP_KERNEL); + if (!*vals) + return -ENOMEM; + + return IIO_AVAIL_LIST; + } default: return -EINVAL; } } +static void as73211_read_avail_release_res(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + const int *vals, long mask) +{ + if (mask == IIO_CHAN_INFO_INT_TIME) + kfree(vals); +} + static int _as73211_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan __always_unused, int val, int val2, long mask) @@ -699,6 +714,7 @@ static irqreturn_t as73211_trigger_handler(int irq __always_unused, void *p) static const struct iio_info as73211_info = { .read_raw = as73211_read_raw, .read_avail = as73211_read_avail, + .read_avail_release_resource = as73211_read_avail_release_res, .write_raw = as73211_write_raw, }; From patchwork Mon Oct 7 08:37:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Martelli X-Patchwork-Id: 13824322 Received: from mail-ej1-f44.google.com (mail-ej1-f44.google.com [209.85.218.44]) (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 202F138F97; Mon, 7 Oct 2024 08:38:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728290287; cv=none; b=aQVodw8nYbmaiDh8xbYyS85rIE1i0X64hiWgevDZRJNhefx6aqFdBz1yISjAm1FTq/3ycHnfgCWKkOF4Qhy0NTUlvymxr4yj6efEuBufbCmD/BOwlGrQ7+NruV0DQIviMpZ2wwlkLVSv+/Nix9sWSkHIjIonVLv0QTWuE8mxQ1c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728290287; c=relaxed/simple; bh=JI696ir0QITrxWEfRcF3ZHdp8+XMqxpwlGo9q4ElXuU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=K3UzMh4CeZk9oOzOY4X/Sq2qbjjbTBZobDXXiXdvxaHCyIAjCPI/WBf4A6gFYJbtBVAO+pY4BowySp+Pk9TG/SlSwUQfwpjy/OMreNFHmVQzbcBW+U//L0dZ6FdepuWysb+AKMvn6arGdE/km8vg/Ep9AXYXYH2qDh9hvh+1s5s= 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=k8Gfnctv; arc=none smtp.client-ip=209.85.218.44 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="k8Gfnctv" Received: by mail-ej1-f44.google.com with SMTP id a640c23a62f3a-a8ce5db8668so670035366b.1; Mon, 07 Oct 2024 01:38:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728290284; x=1728895084; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=lQLemq3ZBEgFeRrStV/dpS5btl0G+M2whUJOsHCfVSQ=; b=k8GfnctvGMYETRPOrT+Mv0ki7Et7bOdKepIsBmx34nnYrciAM8YNdKsiPTtP4dej6y HbcimZa+jWSB/JjxJL4TzzQPJR2Z1WacOIyHVLBq71zAHCxZBy8zkDq1sKlmjxGb6HAM XnCBHc82AYB3bHYwMv5Oo5ApoF0m1pfLIUcWPaO9xEcjyFnICHtQBRHcPwdR+9C2vDmh I5XTfUDYjpIxrCj6NVmG+CrwmRlfIwj+gtESt8y1JPEM5phjFUObDMM7uLP7lNaZ+rXy 2cPWQhtq1jMZZQG3pd+NFJJ8z9+zr8OfOPVPA79qdjdHHsALf9a2h3S/ifpLBTs/PODq 9wAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728290284; x=1728895084; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lQLemq3ZBEgFeRrStV/dpS5btl0G+M2whUJOsHCfVSQ=; b=hbejM0LpW9JKpFxz2d+kAjouBms2K+zlg61KqhJQySxZ1cAXo3ejhgcIEZoH6b7XU2 6ShzOOXS1HWPBCUnU56eHFSBagS+FESM7w0AZx7/QgmBLknfmBVmYJ3nBlbB4oZy8y24 1osy+SfGzTQPaVwqLbmTecoJQcWyyejuCOHAi0DwJiz9OybXpMPTWjtSAXO4bKkvs74E oPG+QKf8HeJ8O83z+g5deL7+gND3PWSAlSZy9WseA1YVfjhu2h4lhguMjxbsciBeqceM //sTZI7tXcvDN1f7W5ygcNJWtM96Ptreb9TvtjIDreQ/rjy19XHV1BIdnwN1RmDZQ93u p/NA== X-Forwarded-Encrypted: i=1; AJvYcCUiatJ88ZbGoWIknKK32vP2AuTGCZs9NSctyjdus/PT3j5rYHKiQpmNgwkj/fzeh+AA3Qa1/Wtoncc=@vger.kernel.org, AJvYcCUuvAGsv/6xjY5k5Pp7fQDh21BJTKAotBjAjci8CRzANz2Xn87jcXEIlXMvFKLwzBvV5NQw4/lnb7MyDA==@vger.kernel.org, AJvYcCVlsBkCG5zJ7gHa/d+K7MXD8Xz/QchbUlvgN+t7isW7BLc30/GoEqNWTe80BhQEmOBfXbDyfvzJDt+STbI=@vger.kernel.org X-Gm-Message-State: AOJu0YxnemP82FHtULEQRo2lMZYL+uOMy2el2Q74RLQZ1OARhDC0hmvb VUYQB19bFw+2pZ1Uo+SxTiXnUaR92WIVuIwyYusVSEyZUpXPtE/g X-Google-Smtp-Source: AGHT+IG4CpNC97e+DUqXVQ1IeBS1KYLVurpgEriMmojss7ag+04Zkv78n3EMC8a4tHvLxDox3WcUkQ== X-Received: by 2002:a17:906:ee87:b0:a8b:ddf4:46f1 with SMTP id a640c23a62f3a-a991c077d4amr1336736266b.63.1728290284134; Mon, 07 Oct 2024 01:38:04 -0700 (PDT) Received: from localhost (host-79-19-52-27.retail.telecomitalia.it. [79.19.52.27]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a992e7864besm349494666b.113.2024.10.07.01.38.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 01:38:03 -0700 (PDT) From: Matteo Martelli Date: Mon, 07 Oct 2024 10:37:14 +0200 Subject: [PATCH v2 5/7] iio: inkern: copy/release available info from producer Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241007-iio-read-avail-release-v2-5-245002d5869e@gmail.com> References: <20241007-iio-read-avail-release-v2-0-245002d5869e@gmail.com> In-Reply-To: <20241007-iio-read-avail-release-v2-0-245002d5869e@gmail.com> To: Jonathan Cameron , Lars-Peter Clausen , Michael Hennerich , Alisa-Dariana Roman , Christian Eggers , Peter Rosin , Paul Cercueil , Sebastian Reichel Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org, linux-pm@vger.kernel.org, Matteo Martelli X-Mailer: b4 0.14.2 Consumers need to call the read_avail_release_resource after reading the available info. To call the release with info_exists locked, copy the available info from the producer and immediately call its release callback. With this change, users of iio_read_avail_channel_raw() and iio_read_avail_channel_attribute() must free the copied avail info after calling them. Signed-off-by: Matteo Martelli --- drivers/iio/inkern.c | 64 +++++++++++++++++++++++++++++++++----------- include/linux/iio/consumer.h | 4 +-- 2 files changed, 50 insertions(+), 18 deletions(-) diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c index 7f325b3ed08fae6674245312cf8f57bb151006c0..cc65ef79451e5aa2cea447e168007a447ffc0d91 100644 --- a/drivers/iio/inkern.c +++ b/drivers/iio/inkern.c @@ -760,9 +760,25 @@ static int iio_channel_read_avail(struct iio_channel *chan, if (!iio_channel_has_available(chan->channel, info)) return -EINVAL; - if (iio_info->read_avail) - return iio_info->read_avail(chan->indio_dev, chan->channel, - vals, type, length, info); + if (iio_info->read_avail) { + const int *vals_tmp; + int ret; + + ret = iio_info->read_avail(chan->indio_dev, chan->channel, + &vals_tmp, type, length, info); + if (ret < 0) + return ret; + + *vals = kmemdup_array(vals_tmp, *length, sizeof(int), GFP_KERNEL); + if (!*vals) + return -ENOMEM; + + if (iio_info->read_avail_release_resource) + iio_info->read_avail_release_resource( + chan->indio_dev, chan->channel, vals_tmp, info); + + return ret; + } return -EINVAL; } @@ -789,9 +805,11 @@ int iio_read_avail_channel_raw(struct iio_channel *chan, ret = iio_read_avail_channel_attribute(chan, vals, &type, length, IIO_CHAN_INFO_RAW); - if (ret >= 0 && type != IIO_VAL_INT) + if (ret >= 0 && type != IIO_VAL_INT) { /* raw values are assumed to be IIO_VAL_INT */ + kfree(*vals); ret = -EINVAL; + } return ret; } @@ -820,24 +838,31 @@ static int iio_channel_read_max(struct iio_channel *chan, if (val2) *val2 = vals[5]; } - return 0; + ret = 0; + break; case IIO_AVAIL_LIST: - if (length <= 0) - return -EINVAL; + if (length <= 0) { + ret = -EINVAL; + goto out; + } switch (*type) { case IIO_VAL_INT: *val = max_array(vals, length); + ret = 0; break; default: /* TODO: learn about max for other iio values */ - return -EINVAL; + ret = -EINVAL; } - return 0; + break; default: - return -EINVAL; + ret = -EINVAL; } +out: + kfree(vals); + return ret; } int iio_read_max_channel_raw(struct iio_channel *chan, int *val) @@ -876,24 +901,31 @@ static int iio_channel_read_min(struct iio_channel *chan, if (val2) *val2 = vals[1]; } - return 0; + ret = 0; + break; case IIO_AVAIL_LIST: - if (length <= 0) - return -EINVAL; + if (length <= 0) { + ret = -EINVAL; + goto out; + } switch (*type) { case IIO_VAL_INT: *val = min_array(vals, length); + ret = 0; break; default: /* TODO: learn about min for other iio values */ - return -EINVAL; + ret = -EINVAL; } - return 0; + break; default: - return -EINVAL; + ret = -EINVAL; } +out: + kfree(vals); + return ret; } int iio_read_min_channel_raw(struct iio_channel *chan, int *val) diff --git a/include/linux/iio/consumer.h b/include/linux/iio/consumer.h index 333d1d8ccb37f387fe531577ac5e0bfc7f752cec..e3e268d2574b3e01c9412449d90d627de7efcd84 100644 --- a/include/linux/iio/consumer.h +++ b/include/linux/iio/consumer.h @@ -316,7 +316,7 @@ int iio_read_min_channel_raw(struct iio_channel *chan, int *val); /** * iio_read_avail_channel_raw() - read available raw values from a given channel * @chan: The channel being queried. - * @vals: Available values read back. + * @vals: Available values read back. Must be freed after use. * @length: Number of entries in vals. * * Returns an error code, IIO_AVAIL_RANGE or IIO_AVAIL_LIST. @@ -334,7 +334,7 @@ int iio_read_avail_channel_raw(struct iio_channel *chan, /** * iio_read_avail_channel_attribute() - read available channel attribute values * @chan: The channel being queried. - * @vals: Available values read back. + * @vals: Available values read back. Must be freed after use. * @type: Type of values read back. * @length: Number of entries in vals. * @attribute: info attribute to be read back. From patchwork Mon Oct 7 08:37:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Martelli X-Patchwork-Id: 13824323 Received: from mail-lf1-f52.google.com (mail-lf1-f52.google.com [209.85.167.52]) (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 7C4E118CBED; Mon, 7 Oct 2024 08:38:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728290289; cv=none; b=r/ULRZrgydR3pGBDgVveKNJ9KQp7CnHyH0AUuXRO/CjytybwxZCWsZORpZXl/QwrPki7n7dOg8BwIAyi42CP09URS+6HMl03MgNKwH47B8TGhYLPrQqteh2qmJZqCYbyg7o4/R0CC/Eto88jYj2R1FIEC54dRa9+Dz4q/KTUvUw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728290289; c=relaxed/simple; bh=nRhnNaDZCEdFpfuQY+d3PHGabxDhnUnJnKU7JXNt19E=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=LukWZePddPl5OrWDahB2Hl4HpGcgD7QYh14rq+ZLlPYI4jaE3Z5Cw2KYmUDIMaKSJGwmHLLLmOmMvFMekcEa/qkbqXQrMhp8uy7RKtJVyVi0jhmZpFYg1dR7hvJedB14AY4aNba1TknRU5uVuSC43YfKQE2W4IzqOalsZi33/m0= 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=DhEex6IZ; arc=none smtp.client-ip=209.85.167.52 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="DhEex6IZ" Received: by mail-lf1-f52.google.com with SMTP id 2adb3069b0e04-5398df2c871so4286580e87.1; Mon, 07 Oct 2024 01:38:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728290286; x=1728895086; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=gd1iJo2Ae4EEednioqk/xw+7U1ML6e6KimnctnEpL5U=; b=DhEex6IZTBuoL/h0TVrtYH5BkcLgUVB09RbBq6fIZ4KDw4YN3uNVWbd01xgxH8LgXn FZy53g9T4XifEtRYpzFDMYag/A0xafyEPYhRxNwVw8EhsEjOG8baYBEOam2ooWRIudlt 9h8M+5QOR9gnM4Ui2dYU7zj2YcH6K43SePnkpvLYrkYQHBTMEZmg5mParisjggko99Xh N2AjWZHWGPExRbYNK6Tk3RpbSrBfObubno4T+OmBtIWSzPFBay7G3a6/w27XOz6OPPlI Gmt1Y0vHA5AzYwA43AVLL4Fj5t9XU+HjxvXNAD7oSOGjTb2b8q6CQBSEtGTzMoKImSQ1 mK4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728290286; x=1728895086; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gd1iJo2Ae4EEednioqk/xw+7U1ML6e6KimnctnEpL5U=; b=kRG/59G1AWffZtTPh5fqWjPcUEYON4pnrZvu5jCCsM9/muypz8SuZMeHph+pte1IDl sJTrebAvr+RBHuqYRvt3IOfo3N/vZC6ylDVAtG0XEwHiHY9RxdGqg++F5ik8+0RM5OVt WAqSh9xvST4gTZVLd8/sTAB8FctWdtyfCb7RDDUKf8S6H4AD+oM6FIj3DXO4i/v6XHy5 +foy8pOEldbOhOGeAW1PPGW1jjFDyZHsxGyCPzuvO2RauPHu150uIFYrNfgF973WjQGc xVtEBCsp0OVLjnxzwTc7rcHLEsitVhcnUWFfXGAECqdRwNJkHEzemvO5v0kQDWUFmvZk FbCQ== X-Forwarded-Encrypted: i=1; AJvYcCU5GL6wZ4htqKVvRbShwiZIr4UsevKlPwPH8eukiUXHm2gHNWZ0ZE/+/2rR6VqFIVLxdGFxAA3aTv8=@vger.kernel.org, AJvYcCUmKVSqXbnT46ub9dSmt+Z8ZT8wU4/4MkhSOOL4udpFYKKAiDKdCEsXeFAi0yaJBUptpnP+f7mBmL1k9g==@vger.kernel.org, AJvYcCUy2tdtKNxM+fFTZx8uiIh3s6hDx4RZ3EMcNlzfov/9SyQf9JDphxQPpXt0LhMOdmc8g3HGeta60IaCf60=@vger.kernel.org X-Gm-Message-State: AOJu0Yz6e+/9zmlkO2MLaS7l92XjzCcGQViunjkNihToxTixT1nZrtia yJeMiTxk4XLx3lhiWorma0FUUoAH6ME2HYvDpUeX3SoBD/S9y3Dw X-Google-Smtp-Source: AGHT+IGnQ5d4feQXUX2RxM5Jec9R7d5ot/8bGk0KvZl8H4Q1K3oqOrca4l99vrGw2QCjarcKNAgaXQ== X-Received: by 2002:a05:6512:12d3:b0:535:69ee:9717 with SMTP id 2adb3069b0e04-539ab84a7cfmr5334883e87.3.1728290285315; Mon, 07 Oct 2024 01:38:05 -0700 (PDT) Received: from localhost (host-79-19-52-27.retail.telecomitalia.it. [79.19.52.27]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5c8e05a7e87sm2921949a12.24.2024.10.07.01.38.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 01:38:04 -0700 (PDT) From: Matteo Martelli Date: Mon, 07 Oct 2024 10:37:15 +0200 Subject: [PATCH v2 6/7] iio: consumers: release available info buffer copied from producer Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241007-iio-read-avail-release-v2-6-245002d5869e@gmail.com> References: <20241007-iio-read-avail-release-v2-0-245002d5869e@gmail.com> In-Reply-To: <20241007-iio-read-avail-release-v2-0-245002d5869e@gmail.com> To: Jonathan Cameron , Lars-Peter Clausen , Michael Hennerich , Alisa-Dariana Roman , Christian Eggers , Peter Rosin , Paul Cercueil , Sebastian Reichel Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org, linux-pm@vger.kernel.org, Matteo Martelli X-Mailer: b4 0.14.2 The iio_read_avail_channel_raw() inkern interface now allocates a copy of the available info buffer that must be freed after use. To do so, free the buffer in the consumers read_avail_release_resource callback. Signed-off-by: Matteo Martelli --- drivers/iio/afe/iio-rescale.c | 8 ++++++++ drivers/iio/dac/dpot-dac.c | 8 ++++++++ drivers/iio/multiplexer/iio-mux.c | 8 ++++++++ 3 files changed, 24 insertions(+) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index 56e5913ab82d1c045c9ca27012008a4495502cbf..78bb86c291706748b4072a484532ad20c415ff9f 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -249,9 +249,17 @@ static int rescale_read_avail(struct iio_dev *indio_dev, } } +static void rescale_read_avail_release_res(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + const int *vals, long mask) +{ + kfree(vals); +} + static const struct iio_info rescale_info = { .read_raw = rescale_read_raw, .read_avail = rescale_read_avail, + .read_avail_release_resource = rescale_read_avail_release_res, }; static ssize_t rescale_read_ext_info(struct iio_dev *indio_dev, diff --git a/drivers/iio/dac/dpot-dac.c b/drivers/iio/dac/dpot-dac.c index 7332064d0852d979620f90066fb027f6f68f8c95..beec76247acb5170b81058d28a71ed17c831c905 100644 --- a/drivers/iio/dac/dpot-dac.c +++ b/drivers/iio/dac/dpot-dac.c @@ -108,6 +108,13 @@ static int dpot_dac_read_avail(struct iio_dev *indio_dev, return -EINVAL; } +static void dpot_dac_read_avail_release_res(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + const int *vals, long mask) +{ + kfree(vals); +} + static int dpot_dac_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int val, int val2, long mask) @@ -125,6 +132,7 @@ static int dpot_dac_write_raw(struct iio_dev *indio_dev, static const struct iio_info dpot_dac_info = { .read_raw = dpot_dac_read_raw, .read_avail = dpot_dac_read_avail, + .read_avail_release_resource = dpot_dac_read_avail_release_res, .write_raw = dpot_dac_write_raw, }; diff --git a/drivers/iio/multiplexer/iio-mux.c b/drivers/iio/multiplexer/iio-mux.c index 2953403bef53bbe47a97a8ab1c475ed88d7f86d2..31345437784b01c5d6f8ea70263f4c2574388e7a 100644 --- a/drivers/iio/multiplexer/iio-mux.c +++ b/drivers/iio/multiplexer/iio-mux.c @@ -142,6 +142,13 @@ static int mux_read_avail(struct iio_dev *indio_dev, return ret; } +static void mux_read_avail_release_res(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + const int *vals, long mask) +{ + kfree(vals); +} + static int mux_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int val, int val2, long mask) @@ -171,6 +178,7 @@ static int mux_write_raw(struct iio_dev *indio_dev, static const struct iio_info mux_info = { .read_raw = mux_read_raw, .read_avail = mux_read_avail, + .read_avail_release_resource = mux_read_avail_release_res, .write_raw = mux_write_raw, }; From patchwork Mon Oct 7 08:37:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Martelli X-Patchwork-Id: 13824324 Received: from mail-ej1-f42.google.com (mail-ej1-f42.google.com [209.85.218.42]) (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 D060818D637; Mon, 7 Oct 2024 08:38:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728290290; cv=none; b=EU8cZFdDWgRL0tmXbUqVn3NoMpZSAML0vQzBkuzHiyByOrIhU39uU5jtejZ0+47AL2gQ0XhRGh8o+haVdFAlHC4KwlXu1oNSe90JZlaCumAbGw79LRzRHyFksIjvlBgc8odq9TkOCbx9n2zn62lsTv1e9SVyOBM2otlqLjJDD+E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728290290; c=relaxed/simple; bh=mIojD9CDR7umMqrpnB2F3ciGt1tvIMi9E/u0hIezCWA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ieCjfClBfOvrlr9me1uxntAHThSoRjjQ2Oi6XVDHKRKkpo2XnmhmnLFTh3ZUhohPKXGoOGynUNNAO0vfMwiY6u2YKuref+Awknrab9/O2GDyvsmp/7+GuGYsfekTWFPHH4Xj31KQm52uXdlyRgNBNW3pP6y8LkMGgHzY/1QGb2g= 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=CyPjUdb6; arc=none smtp.client-ip=209.85.218.42 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="CyPjUdb6" Received: by mail-ej1-f42.google.com with SMTP id a640c23a62f3a-a9951fba3b4so123672666b.1; Mon, 07 Oct 2024 01:38:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728290287; x=1728895087; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=QKltXJ63QuedW8DsSUo0BYVkH+UQGKq0Hkk8QNeBeDg=; b=CyPjUdb6oqCmnPR35JuiJMG3UmNbrRH7HBcULvu+Srkgb1mlRVQVDFAre3UAC1DQLE l0NOS7w0LHhvRu6+pDb/m+tsaykZlLtFUaxG+8S+u65nyQWrftIAv6vyMr7irpC9b2Dx fMowwhyfVWkwJB+ZpsOE9YV7L3jB+b98LN4Jhbr2LqKipXToORl2yGx6dJyMDd9Sn3Yd EHfm3+hqxCmSpb+VrkS0MtGJGqLKcbRzzvuAlv6axbkyNwT+YJXkUFZUZivs2Iw6PGMi pRPJ/VzixsrDvXxs4bAP5UdRgsMgixDeFnx5FRgIbmxpWWlM2WWbrYpEDN/j8wxFt2UK ItZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728290287; x=1728895087; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QKltXJ63QuedW8DsSUo0BYVkH+UQGKq0Hkk8QNeBeDg=; b=hv514cm/ZdHHrYmf95HEBKqx6weTTLsOQuN8iQYic3Z6j4AfEfSLzQBVwpOvdcNQVq mEn7kby0ZqjM9r/TLGKCS/AIsqmTJ93O1IPp1LV+DUh3f0XkbcO8SO0qaLsRP0nES4Xx bhYWXih1r1Yp5ke7H9hQtP0nMUnaXWpVAN0JklFkatx41ESxrlKL8TFozcT2Yr+OR6UL UbscwqhcYj1+RgSZrbuRyccTyuyWH16vLlInRyLKOPx6xP4ZxRw3VEJRIm0vl6a1WybQ Pq7aVUPDSz+AS/2pqxN86jhyxhy+6VhnmpwRRVPQnZHyPPjk+xQixLHpXJjVnLn4eEkX 5MaQ== X-Forwarded-Encrypted: i=1; AJvYcCU6fspWrwknXCaqmFkb4/6hueYXrDBNBIIjW9BF28dgkBR16zNNGudkAdCDU5aEMszqwPOQF/ndeZ1DYg==@vger.kernel.org, AJvYcCV8hOfBpb9HDU1dIyjB4QpMnxVBKoYd5VTW4KUp9v73eHHvo6FrprSFiqSNTegZRFoFh3HZiY6hkro=@vger.kernel.org, AJvYcCXj98uW2+EboeIpBcL6MuzEOmGQRo/LAAL9trFxCkrNyddkz2cFTRgGoeg/20EPyNMXsHBaNat1TUjN1RQ=@vger.kernel.org X-Gm-Message-State: AOJu0Ywnsjc1Tu3B+gKRlS9MFb6UJrRzo9hYiCqW7o3qktCBGmVEbMOX y3KPy41fbqX6aIUeRI20zTbi3H6EG2Zs1PzHmpTqwR4xCsVSuNLX X-Google-Smtp-Source: AGHT+IHowlj9xEF88dnf54Y6o+cqTSCDpvredrMjpaDbjhyspMpJEWwL7BumMGw6mcaRzgRxFQfWig== X-Received: by 2002:a17:907:70a:b0:a86:7ba9:b061 with SMTP id a640c23a62f3a-a991c02397amr1249862866b.64.1728290287045; Mon, 07 Oct 2024 01:38:07 -0700 (PDT) Received: from localhost (host-79-19-52-27.retail.telecomitalia.it. [79.19.52.27]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a9953d48594sm121254166b.176.2024.10.07.01.38.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 01:38:06 -0700 (PDT) From: Matteo Martelli Date: Mon, 07 Oct 2024 10:37:16 +0200 Subject: [PATCH v2 7/7] power: supply: ingenic-battery: free scale buffer after use Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241007-iio-read-avail-release-v2-7-245002d5869e@gmail.com> References: <20241007-iio-read-avail-release-v2-0-245002d5869e@gmail.com> In-Reply-To: <20241007-iio-read-avail-release-v2-0-245002d5869e@gmail.com> To: Jonathan Cameron , Lars-Peter Clausen , Michael Hennerich , Alisa-Dariana Roman , Christian Eggers , Peter Rosin , Paul Cercueil , Sebastian Reichel Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org, linux-pm@vger.kernel.org, Matteo Martelli X-Mailer: b4 0.14.2 The iio_read_avail_channel_attribute() iio interface now allocates a copy of the available info buffer that must be freed after use. Signed-off-by: Matteo Martelli --- drivers/power/supply/ingenic-battery.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/drivers/power/supply/ingenic-battery.c b/drivers/power/supply/ingenic-battery.c index 0a40f425c27723ccec49985b8b5e14a737b6a7eb..3db000d9fff9a7a6819631314547b3d16db7f967 100644 --- a/drivers/power/supply/ingenic-battery.c +++ b/drivers/power/supply/ingenic-battery.c @@ -12,6 +12,7 @@ #include #include #include +#include struct ingenic_battery { struct device *dev; @@ -79,8 +80,10 @@ static int ingenic_battery_set_scale(struct ingenic_battery *bat) dev_err(bat->dev, "Unable to read channel avail scale\n"); return ret; } - if (ret != IIO_AVAIL_LIST || scale_type != IIO_VAL_FRACTIONAL_LOG2) - return -EINVAL; + if (ret != IIO_AVAIL_LIST || scale_type != IIO_VAL_FRACTIONAL_LOG2) { + ret = -EINVAL; + goto out; + } max_mV = bat->info->voltage_max_design_uv / 1000; @@ -99,7 +102,8 @@ static int ingenic_battery_set_scale(struct ingenic_battery *bat) if (best_idx < 0) { dev_err(bat->dev, "Unable to find matching voltage scale\n"); - return -EINVAL; + ret = -EINVAL; + goto out; } /* Only set scale if there is more than one (fractional) entry */ @@ -109,10 +113,13 @@ static int ingenic_battery_set_scale(struct ingenic_battery *bat) scale_raw[best_idx + 1], IIO_CHAN_INFO_SCALE); if (ret) - return ret; + goto out; } - return 0; + ret = 0; +out: + kfree(scale_raw); + return ret; } static enum power_supply_property ingenic_battery_properties[] = {