Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp1844339rdb; Tue, 3 Oct 2023 02:50:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEMY5kIBdyKa6nRup+PDtePKlBsfTm7upT6Yxc8Da/jGM3RLlw9a9nPolRB/wiwDmqOLQJc X-Received: by 2002:a05:6358:8812:b0:142:fd2b:d165 with SMTP id hv18-20020a056358881200b00142fd2bd165mr14038050rwb.13.1696326624799; Tue, 03 Oct 2023 02:50:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696326624; cv=none; d=google.com; s=arc-20160816; b=F65a8G4W1eiQRd9f394NU6LUW1u2MG/opfcFcFQdX4fO9MVekXyaPDv71G4JYeZci6 NsUaIYV+8+rB7dCyF04SDKcm05PMt3+AXtDPXDaaXKawKfPpGt5rJSRjDmAxYX76tgt4 by0HS1xPnWL6Q6yNd+XAsHITyd+ZT8EMKRGpomBlURy7ZMN1kQeLkOUgodEGWvhHXSjv 0gaKwVZY3iNGv2v018oYUnc5Is+eiGoRwxN9SpQ9B83rY2d6KgZH7QXJrQA0JLVe1Roq CcWINDir3NIOn8Ox3KKIxg0TyR2Cr05w4D6oDyO6a0TAPMSZCRxcNrF+wm6+LqCYKQGT jk5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:dkim-signature; bh=PeiBiJEI2N9fhcYpDpgV3rBn3MtISs9Q/BTF3zD9mvU=; fh=jC3Zr14IfogOQHFLVGA9DtjKypPgomeJbjRJgp8dR94=; b=c8vHDYShwXe+1iMhy+QZskAe1WX7dCC+bysfzagDkyWbYTLuCo3B5d7xteT3Dq3Rbv JEvY1YkHgdlmMm9XaR+kl90kdVVtp9BPx3sIf4GjSJhEaZTp3oK8QQ7F6NYin7757YpI hTWsjzi8qIn6tTlevCqxqx3YYL+IkAKfWksJK9sfVe2uzcKbJd+gzqfRMtx69ZJhs4wK VWZ3wqZw0wySJz0d5ndAbhmdSo7Q5UWwaHDg78z7qNKMQlqp+mlFCSN3h9qcvrW8RbdU i7wL5ZNWM7N/nOhb3nWj4/53Hre652AQCtC/yJOmdVoIlGCWFsJxeFBZ9cFfy1zWT/qU as7g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=BDiD+2CK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id ck13-20020a056a00328d00b0069338feae40si1090165pfb.149.2023.10.03.02.50.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 02:50:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=BDiD+2CK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 838BB8125FF6; Tue, 3 Oct 2023 02:50:21 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239782AbjJCJuE (ORCPT + 99 others); Tue, 3 Oct 2023 05:50:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58590 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231968AbjJCJuC (ORCPT ); Tue, 3 Oct 2023 05:50:02 -0400 Received: from mail-lf1-x12d.google.com (mail-lf1-x12d.google.com [IPv6:2a00:1450:4864:20::12d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 21E64A6; Tue, 3 Oct 2023 02:49:59 -0700 (PDT) Received: by mail-lf1-x12d.google.com with SMTP id 2adb3069b0e04-50306b2920dso834340e87.0; Tue, 03 Oct 2023 02:49:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696326597; x=1696931397; darn=vger.kernel.org; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=PeiBiJEI2N9fhcYpDpgV3rBn3MtISs9Q/BTF3zD9mvU=; b=BDiD+2CK57wAOfhJD05q4J36vsQZ/MnegMz1NuA7lx1+6Rx2TP3XIdQZSGd0xQeaRr +4UTGSf017405TvWI7mE6tosayrh2mccodwMcl+cTNrhJqBzvJk8jmJEfgUgp/UIBkzc JQLiYp1JIV+Min8NEpsADuyYMRXrXVmwRiZyYc4nhdv38H9sCV9Lw8qmooSbZcahXIJE dYX7olvcE0ih8HGxWOliqYCqOYKbmrOCwz/BJBaIKAS7p7e482NPP2KNWtgwDr8O7+F1 XpjB5kWY4qL0TM8Za1w6b47DNpEHX+9PD/WYrK9fiAOh/t3QLb9n0jf5h4t2f14cJP3W k7bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696326597; x=1696931397; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=PeiBiJEI2N9fhcYpDpgV3rBn3MtISs9Q/BTF3zD9mvU=; b=Sn6/l7iEW7Lcwtthh2LmszNwza/jehAy2+PyjMLaQlmq79qs1sNjUqm1/zzvVAG8Ll +RAVCDKY7RfWyBk2wcgyK6pXDJOE3HEVOWjGDLjXwxxPfJcFa0MSifa+S3B2HPZkI+5i MJ5q0BR9GVuA02sQBNtV90QH7zqmcBrlRtmq5BL1tMhf5ZhbeFDo56CxMZHoxXqOomKT ppqfZNcnSgyk0akZPldtMqF76AQ/F0lMKaZQ/V9CM97mr97v+2w2MRACLlv8GqQ/qURK n8TpHT988ydmLFPgmlin+8ktsibpPSaUVI0Qa0ofjToXUVJHoJ3nWorGA2y7oNCWSqXI I4rA== X-Gm-Message-State: AOJu0YyOxPmnS3zOcNXMMkaF4/wRLHjXQY8gF6i0JMaMJsDX5jC9eMqh GxLk/QN+Ee3mvbD+wB3OvIc= X-Received: by 2002:a05:6512:3189:b0:504:33cd:ad1 with SMTP id i9-20020a056512318900b0050433cd0ad1mr1542420lfe.27.1696326596437; Tue, 03 Oct 2023 02:49:56 -0700 (PDT) Received: from dc78bmyyyyyyyyyyyyydt-3.rev.dnainternet.fi (dc78bmyyyyyyyyyyyyydt-3.rev.dnainternet.fi. [2001:14ba:16f8:1500::7]) by smtp.gmail.com with ESMTPSA id j9-20020a2e8509000000b002c021b6e82bsm170151lji.80.2023.10.03.02.49.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 02:49:55 -0700 (PDT) Date: Tue, 3 Oct 2023 12:49:45 +0300 From: Matti Vaittinen To: Matti Vaittinen , Matti Vaittinen Cc: Jonathan Cameron , Lars-Peter Clausen , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] iio: sanity check available_scan_masks array Message-ID: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="Xt8lz5EDqjSsd/g8" Content-Disposition: inline X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Tue, 03 Oct 2023 02:50:21 -0700 (PDT) --Xt8lz5EDqjSsd/g8 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable When IIO goes through the available scan masks in order to select the best suiting one, it will just accept the first listed subset of channels which meets the user's requirements. If driver lists a mask which is a subset of some of the masks previously in the array of avaliable_scan_masks, then the latter one will never be selected. Add a warning if driver registers masks which can't be used due to the available_scan_masks-array ordering. Suggested-by: Jonathan Cameron Signed-off-by: Matti Vaittinen --- The change was suggested by Jonathan here: https://lore.kernel.org/lkml/20230924170726.41443502@jic23-huawei/ --- drivers/iio/industrialio-core.c | 57 +++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-cor= e.c index c77745b594bd..d4f37f4eeec0 100644 --- a/drivers/iio/industrialio-core.c +++ b/drivers/iio/industrialio-core.c @@ -1896,6 +1896,53 @@ static int iio_check_extended_name(const struct iio_= dev *indio_dev) =20 static const struct iio_buffer_setup_ops noop_ring_setup_ops; =20 +static void iio_sanity_check_avail_scan_masks(struct iio_dev *indio_dev) +{ + unsigned int num_masks, masklength, longs_per_mask; + const unsigned long *av_masks; + int i; + + av_masks =3D indio_dev->available_scan_masks; + masklength =3D indio_dev->masklength; + longs_per_mask =3D BITS_TO_LONGS(masklength); + + if (bitmap_empty(av_masks, masklength)) + dev_warn(indio_dev->dev.parent, "empty scan mask\n"); + + for (num_masks =3D 0; *av_masks; num_masks++) + av_masks +=3D longs_per_mask; + + if (num_masks < 2) + return; + + av_masks =3D indio_dev->available_scan_masks; + + /* + * Go through all the masks from first to one before the last, and see + * that no mask found later from the available_scan_masks array is a + * subset of mask found earlier. If this happens, then the mask found + * later will never get used because scanning the array is stopped when + * the first suitable mask is found. Drivers should order the array of + * available masks in the order of preference (presumably the least + * costy to access masks first). + */ + for (i =3D 0; i < num_masks - 1; i++) { + const unsigned long *mask1; + int j; + + mask1 =3D av_masks + i * longs_per_mask; + for (j =3D i + 1; j < num_masks; j++) { + const unsigned long *mask2; + + mask2 =3D av_masks + j * longs_per_mask; + if (bitmap_subset(mask2, mask1, masklength)) + dev_warn(indio_dev->dev.parent, + "available_scan_mask %d subset of %d. Never used\n", + j, i); + } + } +} + int __iio_device_register(struct iio_dev *indio_dev, struct module *this_m= od) { struct iio_dev_opaque *iio_dev_opaque =3D to_iio_dev_opaque(indio_dev); @@ -1934,6 +1981,16 @@ int __iio_device_register(struct iio_dev *indio_dev,= struct module *this_mod) goto error_unreg_debugfs; } =20 + /* + * In order to not wreck utter havoc we just warn for now. Might want + * to convert this to a failure after people have had time to act upon + * the warning. It'd be nice to check this earlier, but we need the + * iio_buffers_alloc_sysfs_and_mask() to have the masklength set. + */ + if (indio_dev->available_scan_masks) + iio_sanity_check_avail_scan_masks(indio_dev); + + ret =3D iio_device_register_sysfs(indio_dev); if (ret) { dev_err(indio_dev->dev.parent, base-commit: 5e99f692d4e32e3250ab18d511894ca797407aec --=20 2.41.0 --=20 Matti Vaittinen, Linux device drivers ROHM Semiconductors, Finland SWDC Kiviharjunlenkki 1E 90220 OULU FINLAND ~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~ Simon says - in Latin please. ~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~ Thanks to Simon Glass for the translation =3D]=20 --Xt8lz5EDqjSsd/g8 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAEBCAAdFiEEIx+f8wZb28fLKEhTeFA3/03aocUFAmUb47QACgkQeFA3/03a ocXgIQf6A9Djw/SAEiLG/3DWYU0p1hGdcGhu+XZbbg6ISuMkZeieWldgG59OyENe iyoksQO0RM8h3C+TIF5vw193iEh5EzR4h3Fz1uK1CbG3RWzNbhPwTnWQ5+3VbCrK c5i+OUUncmXEltSoAbSj1e9J+ft88zr9r6wvxmXLQCLTVHN8QnFupPUA77rl3IAw qmOKLf6E7EfxEyTzAFoEHcu1VvdQKbR8HNKIiHO/UoEdraIgekwfG8gLf/nwXJzB RekS65AfP8sWQR2emyYsWuXDarpUXgvgU0S0wggpoREC+OdwuP6/jIBCDsulpxZk KDwHQnfBAJT0y0LZjwQzNuGvUl5itw== =sXlv -----END PGP SIGNATURE----- --Xt8lz5EDqjSsd/g8--