diff --git a/.github/workflows/zephyr.yml b/.github/workflows/zephyr.yml index 9751b628a3c3..53defeebd1dc 100644 --- a/.github/workflows/zephyr.yml +++ b/.github/workflows/zephyr.yml @@ -235,18 +235,22 @@ jobs: --no-tarball \ ${{ matrix.build_opts }} ${{ matrix.IPC_platforms }} - # Informational only for now, see scripts/check_kernel_includes.py - - name: kernel/user-space include report + # Fails when a user-space source outside of the [leak-baseline] in + # scripts/kernel_user_partition.txt includes a kernel-only header, + # see scripts/check_kernel_includes.py + - name: kernel/user-space include check if: always() env: PLATFORMS: ${{ matrix.IPC_platforms }} run: | + rc=0 for p in $PLATFORMS; do test -f build-"$p"/build.ninja || continue printf '\n==== %s ====\n' "$p" ZEPHYR_BASE="$(pwd)"/zephyr \ - python3 sof/scripts/check_kernel_includes.py --summary build-"$p" || true + python3 sof/scripts/check_kernel_includes.py --strict --summary build-"$p" || rc=1 done + exit $rc - name: Upload build artifacts uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 diff --git a/scripts/check_kernel_includes.py b/scripts/check_kernel_includes.py index 3db354bbfd7d..d4e9a639043e 100755 --- a/scripts/check_kernel_includes.py +++ b/scripts/check_kernel_includes.py @@ -16,9 +16,13 @@ source against the kernel-only header patterns in scripts/kernel_user_partition.txt. -By default it only reports. With --strict it exits with an error when any -user-space object includes a kernel-only header; that is meant for the -later phases of the partition work, once the leaks have been removed. +By default it only reports. With --strict it exits with an error when a +user-space object includes a kernel-only header, unless its source is +listed in the manifest's [leak-baseline] section. The baseline lists the +sources that still have leaks to remove; it may only shrink, so new +leaks fail the build. Baseline sources that no longer leak in the +checked builds are reported, so that they can be dropped from the list +once they are clean in every configuration. Usage: check_kernel_includes.py [--strict] [--summary] [...] @@ -88,13 +92,15 @@ def relative_to(path, root): def check_build(build_dir, manifest, sof_root, zephyr_base): - """Return {user source: set(kernel headers)} for one build directory.""" + """Return ({user source: set(kernel headers)}, set(user sources)) for one + build directory.""" user_src = manifest['user-sources'] not_user = manifest['not-user-sources'] kernel_hdr = manifest['kernel-headers'] zephyr_hdr = manifest['zephyr-kernel-headers'] leaks = defaultdict(set) + built = set() if not os.path.exists(os.path.join(build_dir, 'build.ninja')): sys.exit(f'{build_dir}: not a ninja build directory') @@ -103,6 +109,7 @@ def check_build(build_dir, manifest, sof_root, zephyr_base): rel_src = relative_to(src, sof_root) if src else None if rel_src is None or not matches(rel_src, user_src) or matches(rel_src, not_user): continue + built.add(rel_src) for dep in deps: rel = relative_to(dep, sof_root) if rel is not None and matches(rel, kernel_hdr): @@ -112,7 +119,7 @@ def check_build(build_dir, manifest, sof_root, zephyr_base): rel = relative_to(dep, zephyr_base) if rel is not None and matches(rel, zephyr_hdr): leaks[rel_src].add('zephyr/' + rel) - return leaks + return leaks, built def main(): @@ -124,7 +131,8 @@ def main(): parser.add_argument('--zephyr-base', default=os.environ.get('ZEPHYR_BASE', os.path.join(os.path.dirname(SOF_ROOT), 'zephyr'))) parser.add_argument('--strict', action='store_true', - help='exit with an error if any kernel-only header is included') + help='exit with an error if a source outside of the baseline ' + 'includes a kernel-only header') parser.add_argument('--summary', action='store_true', help='only print per-header counts, not every source file') args = parser.parse_args() @@ -133,10 +141,13 @@ def main(): zephyr_base = args.zephyr_base if os.path.isdir(args.zephyr_base) else None leaks = defaultdict(set) + built = set() for build_dir in args.build_dirs: - for src, hdrs in check_build(build_dir, manifest, args.sof_root, - zephyr_base).items(): + build_leaks, build_srcs = check_build(build_dir, manifest, args.sof_root, + zephyr_base) + for src, hdrs in build_leaks.items(): leaks[src] |= hdrs + built |= build_srcs by_header = defaultdict(set) for src, hdrs in leaks.items(): @@ -156,7 +167,25 @@ def main(): print(f'{len(leaks)} user-space source files include {len(by_header)} ' f'kernel-only headers') - if args.strict and leaks: + baseline = manifest['leak-baseline'] + new_leaks = sorted(src for src in leaks if not matches(src, baseline)) + fixed = sorted(src for src in built - leaks.keys() if matches(src, baseline)) + + if fixed: + print('\nBaseline sources built without leaks (drop them from ' + '[leak-baseline] once they are clean in every configuration):') + for src in fixed: + print(f'\t{src}') + + if new_leaks: + print('\nUser-space sources including kernel-only headers that are not in ' + '[leak-baseline]:') + for src in new_leaks: + print(f'{src}:') + for hdr in sorted(leaks[src]): + print(f'\t{hdr}') + + if args.strict and new_leaks: return 1 return 0 diff --git a/scripts/kernel_user_partition.txt b/scripts/kernel_user_partition.txt index 95b4bc0ae99f..fc3894b9e9c9 100644 --- a/scripts/kernel_user_partition.txt +++ b/scripts/kernel_user_partition.txt @@ -14,20 +14,14 @@ # [zephyr-kernel-headers] which is relative to ZEPHYR_BASE. "*" also # matches "/". -# Sources that must be user-space safe. +# Sources that must be user-space safe: everything in src/ that is not +# kernel code. Kernel code is in src/kernel/ and the zephyr/ directory. [user-sources] -src/audio/* -src/math/* -src/module/* -src/samples/* -src/ipc/ipc-helper.c -src/ipc/ipc4/ams_helpers.c -src/ipc/ipc4/dai.c -src/ipc/ipc4/notification-user.c -src/lib/objpool.c +src/* -# Kernel sources inside the directories above. +# Kernel sources inside src/: src/kernel/ and the legacy drivers. [not-user-sources] +src/kernel/* src/audio/dai-legacy.c src/audio/host-legacy.c @@ -65,3 +59,50 @@ zephyr/include/sof/lib/vpage.h kernel/include/* include/zephyr/drivers/dma.h include/zephyr/pm/device_runtime.h + +# User-space sources that still include kernel-only headers, removed one +# by one by the partition work (#11306). With --strict, a leak in any +# other user-space source is an error, so this list must only shrink. +# It covers the CI build configurations and the user-space overlays. +[leak-baseline] +src/audio/asrc/asrc.c +src/audio/chain_dma.c +src/audio/component.c +src/audio/copier/copier.c +src/audio/copier/copier_dai.c +src/audio/copier/copier_gain.c +src/audio/copier/copier_host.c +src/audio/copier/copier_ipcgtw.c +src/audio/dai-zephyr.c +src/audio/data_blob.c +src/audio/host-zephyr.c +src/audio/intel_uaol.c +src/audio/kpb.c +src/audio/module_adapter/iadk/kpb_service.cpp +src/audio/module_adapter/iadk/system_agent.cpp +src/audio/module_adapter/iadk/timestamping_service_provider.cpp +src/audio/module_adapter/library/native_system_service.c +src/audio/module_adapter/module_adapter_ipc3.c +src/audio/module_adapter/module_adapter_ipc4.c +src/audio/pipeline/pipeline-graph.c +src/audio/pipeline/pipeline-params.c +src/audio/pipeline/pipeline-schedule.c +src/audio/pipeline/pipeline-stream.c +src/audio/pipeline/pipeline-xrun.c +src/audio/volume/volume.c +src/audio/volume/volume_ipc4.c +src/debug/tester/tester_simple_dram_test.c +src/ipc/ipc-common.c +src/ipc/ipc-helper.c +src/ipc/ipc4/dai.c +src/ipc/ipc4/handler-user.c +src/ipc/ipc4/helper.c +src/lib/dai.c +src/lib/dma.c +src/lib/notifier.c +src/samples/audio/detect_test.c +src/schedule/zephyr_domain.c +src/schedule/zephyr_dp_schedule.c +src/schedule/zephyr_dp_schedule_application.c +src/schedule/zephyr_dp_schedule_thread.c +src/schedule/zephyr_ll.c diff --git a/src/include/sof/debug/telemetry/telemetry.h b/src/include/sof/debug/telemetry/telemetry.h index 109916414781..7f4fea9a3e72 100644 --- a/src/include/sof/debug/telemetry/telemetry.h +++ b/src/include/sof/debug/telemetry/telemetry.h @@ -17,7 +17,6 @@ #include #include #ifdef __ZEPHYR__ -#include #include #include #endif diff --git a/src/kernel/README.md b/src/kernel/README.md index fd92a6369716..4c7c01de4be0 100644 --- a/src/kernel/README.md +++ b/src/kernel/README.md @@ -23,6 +23,8 @@ Zephyr builds everything below this directory into the `sof_kernel` library inst library for this directory tree, so the CMake files of moved directories do not change. Their Kconfig files are sourced from [`Kconfig`](Kconfig). -Code here may use kernel-only headers. User-space code must not call into it directly; the +Code here may use kernel-only headers. User-space code must not call into it directly. The checker in `scripts/check_kernel_includes.py` reports kernel-only includes from user-space -sources. +sources, and CI fails when one is added. The user-space sources that still include +kernel-only headers are listed in the `[leak-baseline]` section of +`scripts/kernel_user_partition.txt`; that list must only shrink.