From patchwork Mon Oct 16 11:04:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matti Vaittinen X-Patchwork-Id: 13423029 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 55844CDB474 for ; Mon, 16 Oct 2023 11:04:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232929AbjJPLEt (ORCPT ); Mon, 16 Oct 2023 07:04:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60778 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230116AbjJPLEs (ORCPT ); Mon, 16 Oct 2023 07:04:48 -0400 Received: from mail-lj1-x234.google.com (mail-lj1-x234.google.com [IPv6:2a00:1450:4864:20::234]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 54AC083; Mon, 16 Oct 2023 04:04:45 -0700 (PDT) Received: by mail-lj1-x234.google.com with SMTP id 38308e7fff4ca-2b9338e4695so56404741fa.2; Mon, 16 Oct 2023 04:04:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697454283; x=1698059083; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=ObaxegcvjsoLKomQ1Ky2UBtwCdZ2TFoAamPpr5FM/xc=; b=GvSIQKBRwcxDTg8KI7Ak0YCJR8s+xyBkq/Oq6xAt2oteibvRKDgYucMzxP82iodYX2 2HOJNNMS2wZCWvzWAoD1z3VxULDHNETbUn9RrTOnQpAfC2OkXzqczOjGxgVbUyGXpHfB sglr0IfZAr+bYXF4drKy19gQQxSYbPpuiouuyeJ8zQ8z19AMWE4kEd2M5GkW4Z/Jdr+I lOULbSl6JQXp4SFfn3voNsE/XFYZP+0dwkhj6dELhnuKWGbpMbfGMeo9OsnRPwNMlCzE sbmTWLojvJO6nm+S0b8ZFd3TfjylgfO4uUAg7z7q1T2kn/gT7HYiMdl9GTYArw8iyOIr ukmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697454283; x=1698059083; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=ObaxegcvjsoLKomQ1Ky2UBtwCdZ2TFoAamPpr5FM/xc=; b=rv/0EiBvXLd+7vo23NAJKiYcTzfwVC/3N/1nKvgoEOz4a+/gn3BbFHPm4Iru/msHnN c8N+STWUd/anCuugSg0aFNewlry0TnSAskDsbdqkNWXaYbKY2MwBEouXvryRSlk71mP4 7uvJCHsnWTxgQUS/y+4qrrJzdCVwFb9+Ixyu1Ljl3lK/SsvtQZf+gvbSWiD0+vtsRNTh 823tbiTUjfj5mz92cQ5tFha6lkjoda+hzeJlsWcaI0SyZVU8qLYjrfuFRZAwz7eS60mY fHwFAky7/gGqEhCT++OerFpARdKoErRnJNjK1cHDK0lgskmhUtl8+rBcSWv4tVuWiK8c DNPg== X-Gm-Message-State: AOJu0Yzwk0CgLGBiSiBCXm/ItUNFCGj+X3MSFp9PEnijSDcK4Nytr537 g++soCWoNQVYGPcrZJDtHvY= X-Google-Smtp-Source: AGHT+IFCkrIRiAWjfbalE3brfGOaKtbBei1dfBcUlLWku+Ie1BCX1DNv3w86cJlE4VM3AL5oa0YWSg== X-Received: by 2002:a2e:3817:0:b0:2c5:2d16:6d94 with SMTP id f23-20020a2e3817000000b002c52d166d94mr829670lja.47.1697454283467; Mon, 16 Oct 2023 04:04:43 -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 q3-20020a2e2a03000000b002c5050d6c75sm1264224ljq.90.2023.10.16.04.04.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Oct 2023 04:04:42 -0700 (PDT) Date: Mon, 16 Oct 2023 14:04:39 +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 v2 1/2] iio: sanity check available_scan_masks array Message-ID: References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org 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 --- Revision History: v1 => v2: - warn if masklength of available_scan_masks is wider than a long - drop unnecessary comment and extra blank line NOTE: the v2 was compile-tested only. The change was suggested by Jonathan here: https://lore.kernel.org/lkml/20230924170726.41443502@jic23-huawei/ --- drivers/iio/industrialio-core.c | 63 +++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c index c77745b594bd..34e1f8d0071c 100644 --- a/drivers/iio/industrialio-core.c +++ b/drivers/iio/industrialio-core.c @@ -1896,6 +1896,66 @@ static int iio_check_extended_name(const struct iio_dev *indio_dev) static const struct iio_buffer_setup_ops noop_ring_setup_ops; +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 = indio_dev->available_scan_masks; + masklength = indio_dev->masklength; + longs_per_mask = BITS_TO_LONGS(masklength); + + /* + * The code determining how many available_scan_masks is in the array + * will be assuming the end of masks when first long with all bits + * zeroed is encountered. This is incorrect for masks where mask + * consists of more than one long, and where some of the available masks + * has long worth of bits zeroed (but has subsequent bit(s) set). This + * is a safety measure against bug where array of masks is terminated by + * a single zero while mask width is greater than width of a long. + */ + if (longs_per_mask > 1) + dev_warn(indio_dev->dev.parent, + "multi long available scan masks not fully supported\n"); + + if (bitmap_empty(av_masks, masklength)) + dev_warn(indio_dev->dev.parent, "empty scan mask\n"); + + for (num_masks = 0; *av_masks; num_masks++) + av_masks += longs_per_mask; + + if (num_masks < 2) + return; + + av_masks = 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 = 0; i < num_masks - 1; i++) { + const unsigned long *mask1; + int j; + + mask1 = av_masks + i * longs_per_mask; + for (j = i + 1; j < num_masks; j++) { + const unsigned long *mask2; + + mask2 = 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_mod) { struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); @@ -1934,6 +1994,9 @@ int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod) goto error_unreg_debugfs; } + if (indio_dev->available_scan_masks) + iio_sanity_check_avail_scan_masks(indio_dev); + ret = iio_device_register_sysfs(indio_dev); if (ret) { dev_err(indio_dev->dev.parent, From patchwork Mon Oct 16 11:04:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matti Vaittinen X-Patchwork-Id: 13423030 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EC23ECDB465 for ; Mon, 16 Oct 2023 11:05:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232090AbjJPLFE (ORCPT ); Mon, 16 Oct 2023 07:05:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46420 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232974AbjJPLFD (ORCPT ); Mon, 16 Oct 2023 07:05:03 -0400 Received: from mail-lj1-x230.google.com (mail-lj1-x230.google.com [IPv6:2a00:1450:4864:20::230]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B22BEB4; Mon, 16 Oct 2023 04:05:01 -0700 (PDT) Received: by mail-lj1-x230.google.com with SMTP id 38308e7fff4ca-2c501bd6ff1so45180711fa.3; Mon, 16 Oct 2023 04:05:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697454300; x=1698059100; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=N6dSXg+vWqlViQZ7pvaQOjS16WN6O2z0EqZFtWA/VVw=; b=X2KZusCIS/61L3auZBDPnqazMPYQlgWOr4h3RnvEFF0rjHiDH0gKK49E06eWLAsJI7 fvyqjn42ZJczR7lHxlxJvMeRTzA6YRuqldAhMOeAbn9sQu5zKa7vMsgbeCwtYx4HbYY+ uGY6JrxfkUSIxPIndoqekbQjiREHUxBuohH661vfKQgsM6hEw1y1/dzeCD2Ak6+Qj57Q X92y0tGjxhe+rM6Yq9FWgu2hoJCxhK4ORqyUula07zQZ0ZTxMgcdV5meXyZCWRjz+aE/ nBifCFEer+6m/Y4BBEk28CFpy5g0LazYr7sF2T1not3aYw9jcUpIcjkIbWEAK37dbG4Z yprQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697454300; x=1698059100; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=N6dSXg+vWqlViQZ7pvaQOjS16WN6O2z0EqZFtWA/VVw=; b=E9hbj05H8ImFtdT/3J6NN2uuMxN1Chnm/bSQ50D91Q/poiANUrAZ2kYVfmMh1RGdqc wfLGGLu6AYFD3Z2QuIn597fRruXLxikM7icr2O1p4bBqAVZasZEcMXb4Z1+ADbfW+k7M SGOLIiVJCvwuuFBEvP2ZW5NQnTLoX7fcKDEQreHoRA/i0LJlDNoSkZPD/s8CDMJOuMkw XqKrjUf8WRDWAwELNt8uNpgfCffuT2+TH2/7tKZVSj+RCCcF48/v9gN1B8itQ1xpHBHt OHXii11twrV2Iihb2H8X4LmW16XHist7rUCL1/MxuU3ITtNCVo3G/DUU/Q5FZue8N5CU sADQ== X-Gm-Message-State: AOJu0YxAyayV+f/RTXLNFngBx66Rboccp+FncrOaBeOL5REpcUGLJQky lIb/v7e1pMIuDpJV9zO9Ye2rRSHJBiIDEQ== X-Google-Smtp-Source: AGHT+IE5WXbg3xeYYdwJOB/JSElE8Ux/VtDghmt9oxK+zws1wRbx2GGjHCCFwAt4t8qFpcOxxDIIiQ== X-Received: by 2002:ac2:4247:0:b0:507:ab66:f118 with SMTP id m7-20020ac24247000000b00507ab66f118mr2549170lfl.68.1697454299865; Mon, 16 Oct 2023 04:04:59 -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 b22-20020ac247f6000000b0050363f87c64sm4519363lfp.36.2023.10.16.04.04.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Oct 2023 04:04:58 -0700 (PDT) Date: Mon, 16 Oct 2023 14:04:55 +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 v2 2/2] iio: buffer: document known issue Message-ID: References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Add documentation explaining why the code which scans all available scan masks is checking only a single long worth of bits even though the code was intended to be supporting masks wider than single long. Signed-off-by: Matti Vaittinen --- drivers/iio/industrialio-buffer.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c index 176d31d9f9d8..09c41e9ccf87 100644 --- a/drivers/iio/industrialio-buffer.c +++ b/drivers/iio/industrialio-buffer.c @@ -413,6 +413,22 @@ static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks, { if (bitmap_empty(mask, masklength)) return NULL; + /* + * The condition here do not handle multi-long masks correctly. + * It only checks the first long to be zero, and will use such mask + * as a terminator even if there was bits set after the first long. + * + * Correct check would require using: + * while (!bitmap_empty(av_masks, masklength)) + * instead. This is potentially hazardous because the + * avaliable_scan_masks is a zero terminated array of longs - and + * using the proper bitmap_empty() check for multi-long wide masks + * would require the array to be terminated with multiple zero longs - + * which is not such an usual pattern. + * + * As writing of this no multi-long wide masks were found in-tree, so + * the simple while (*av_masks) check is working. + */ while (*av_masks) { if (strict) { if (bitmap_equal(mask, av_masks, masklength))