lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20230810202413.1780286-2-nfraprado@collabora.com>
Date:   Thu, 10 Aug 2023 16:23:50 -0400
From:   Nícolas F. R. A. Prado 
        <nfraprado@...labora.com>
To:     Rob Herring <robh+dt@...nel.org>,
        Frank Rowand <frowand.list@...il.com>,
        Shuah Khan <shuah@...nel.org>
Cc:     cocci@...ia.fr, Mark Brown <broonie@...nel.org>,
        Nicolas Palix <nicolas.palix@...g.fr>,
        kernelci@...ts.linux.dev, Julia Lawall <Julia.Lawall@...ia.fr>,
        Bjorn Andersson <andersson@...nel.org>, kernel@...labora.com,
        Guenter Roeck <groeck@...omium.org>,
        Nícolas F. R. A. Prado 
        <nfraprado@...labora.com>, devicetree@...r.kernel.org,
        linux-kernel@...r.kernel.org
Subject: [RFC PATCH 1/2] scripts/dtc: Add script to extract matchable DT compatibles

Introduce a script to extract all compatibles that can match a device
described on the Devicetree to a driver.

The compatible extraction is done by running Coccinelle with a newly
introduced Coccinelle file that detects compatibles listed inside a
of_device_id table which is referenced by the of_match_table of a
driver struct.

Signed-off-by: Nícolas F. R. A. Prado <nfraprado@...labora.com>

---

 scripts/dtc/extract-matchable-dt-compatibles | 33 +++++++++++
 scripts/dtc/matchable_dt_compatibles.cocci   | 58 ++++++++++++++++++++
 2 files changed, 91 insertions(+)
 create mode 100755 scripts/dtc/extract-matchable-dt-compatibles
 create mode 100644 scripts/dtc/matchable_dt_compatibles.cocci

diff --git a/scripts/dtc/extract-matchable-dt-compatibles b/scripts/dtc/extract-matchable-dt-compatibles
new file mode 100755
index 000000000000..b5e96c7ba42e
--- /dev/null
+++ b/scripts/dtc/extract-matchable-dt-compatibles
@@ -0,0 +1,33 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Copyright (c) 2023 Collabora Ltd
+#
+# Based on coccicheck script.
+#
+# This script uses Coccinelle to extract all compatibles that can be matched by
+# Devicetree devices from the kernel source.
+
+DIR="$(dirname $(readlink -f $0))"
+SPATCH=`which ${SPATCH:=spatch}`
+COCCI_FILE=$DIR/matchable_dt_compatibles.cocci
+
+if [ ! -x "$SPATCH" ]; then
+    echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
+    exit 1
+fi
+
+# Use only one thread per core by default if hyperthreading is enabled
+THREADS_PER_CORE=$(LANG=C lscpu | grep "Thread(s) per core: " | tr -cd "[:digit:]")
+if [ -z "$J" ]; then
+	NPROC=$(getconf _NPROCESSORS_ONLN)
+	if [ $THREADS_PER_CORE -gt 1 -a $NPROC -gt 4 ] ; then
+		NPROC=$((NPROC/2))
+	fi
+else
+	NPROC="$J"
+fi
+
+"$SPATCH" --cocci-file "$COCCI_FILE" --dir .  --jobs "$NPROC" --chunksize 1 \
+	--no-show-diff --no-includes --include-headers --very-quiet \
+	| grep '"' | sed -e 's/"//g;s/^ *\([^ ]*\) *$/\1/'
diff --git a/scripts/dtc/matchable_dt_compatibles.cocci b/scripts/dtc/matchable_dt_compatibles.cocci
new file mode 100644
index 000000000000..ecd3705681aa
--- /dev/null
+++ b/scripts/dtc/matchable_dt_compatibles.cocci
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// Copyright: (C) 2023 Collabora Ltd
+@...e1@
+identifier table;
+type drv;
+identifier drvname;
+identifier drvfield;
+identifier drvfield1;
+identifier drvfield2;
+@@
+
+(
+drv drvname = {
+	.drvfield = {
+		.of_match_table = table,
+	},
+};
+|
+drv drvname = {
+	.drvfield = {
+		.of_match_table = of_match_ptr(table),
+	},
+};
+|
+// Accounts for mdio and spimem drivers
+drv drvname = {
+	.drvfield1 = {
+		.drvfield2 = {
+			.of_match_table = table,
+		},
+	},
+};
+|
+drv drvname = {
+	.drvfield1 = {
+		.drvfield2 = {
+			.of_match_table = of_match_ptr(table),
+		},
+	},
+};
+)
+
+@...e2@
+identifier rule1.table;
+expression compat;
+@@
+
+struct of_device_id table[] = {
+...,
+{ .compatible = compat, },
+...
+};
+
+@...ipt:python@
+compat << rule2.compat;
+@@
+
+print("%s" % compat)
-- 
2.41.0

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ