Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/codestyle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,14 @@ jobs:
run: yamllint -f parsable
-d "$yamllint_config"
--strict .github/workflows/*.yml *.yml

kernel-includes:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
filter: 'tree:0'

- name: no kernel-only headers in user-space code
# see scripts/check_kernel_includes.py
run: python3 scripts/check_kernel_includes.py --sources --strict
4 changes: 0 additions & 4 deletions posix/include/rtos/panic.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include <ipc/trace.h>
#include <stdint.h>
#include <arch/debug/panic.h>

#ifdef __ZEPHYR__
#error "This file should only be included in XTOS builds."
Expand All @@ -30,9 +29,6 @@
"Add it to CMake's target with sof_append_relative_path_definitions."
#endif

void dump_panicinfo(void *addr, struct sof_ipc_panic_info *panic_info);
void panic_dump(uint32_t p, struct sof_ipc_panic_info *panic_info,
uintptr_t *data) SOF_NORETURN;
void __panic(uint32_t p, const char *filename, uint32_t linenum) SOF_NORETURN;

/** panic dump filename and linenumber of the call
Expand Down
68 changes: 67 additions & 1 deletion scripts/check_kernel_includes.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,20 @@
checked builds are reported, so that they can be dropped from the list
once they are clean in every configuration.

With --sources it checks the source tree instead of build directories:
kernel-only headers in src/kernel/include are included as
<sof/kernel/...>, and no user-space source or header may include one.
This covers code that no checked build compiles, and has no baseline.

Usage:
check_kernel_includes.py [--strict] [--summary] <build-dir> [<build-dir>...]
check_kernel_includes.py --sources [--strict]
"""

import argparse
import fnmatch
import os
import re
import subprocess
import sys
from collections import defaultdict
Expand Down Expand Up @@ -122,9 +129,52 @@ def check_build(build_dir, manifest, sof_root, zephyr_base):
return leaks, built


KERNEL_INCLUDE = re.compile(r'^\s*#\s*include\s*[<"](sof/kernel/[^>"]+)[>"]', re.M)


def source_files(sof_root):
"""Return the files of the SOF source tree, relative to it."""
try:
out = subprocess.run(['git', '-C', sof_root, 'ls-files'],
check=True, capture_output=True, text=True).stdout
return out.splitlines()
except (OSError, subprocess.CalledProcessError):
files = []
for top, dirs, names in os.walk(sof_root):
dirs[:] = [d for d in dirs if d != '.git' and not d.startswith('build')]
files += [os.path.relpath(os.path.join(top, n), sof_root).replace(os.sep, '/')
for n in names]
return files
Comment on lines +137 to +147

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: out.split() splits on spaces/tabs if a path ever contains whitespace (out.splitlines() matches ninja_deps() above), and in the os.walk fallback normalizing os.sep (/ on Windows) + pruning .git / build* avoids mismatches or scanning in-tree build directories:

Suggested change
try:
out = subprocess.run(['git', '-C', sof_root, 'ls-files'],
check=True, capture_output=True, text=True).stdout
return out.split()
except (OSError, subprocess.CalledProcessError):
files = []
for top, _, names in os.walk(sof_root):
files += [os.path.relpath(os.path.join(top, n), sof_root) for n in names]
return files
try:
out = subprocess.run(['git', '-C', sof_root, 'ls-files'],
check=True, capture_output=True, text=True).stdout
return out.splitlines()
except (OSError, subprocess.CalledProcessError):
files = []
for top, dirs, names in os.walk(sof_root):
dirs[:] = [d for d in dirs if d != '.git' and not d.startswith('build')]
files += [os.path.relpath(os.path.join(top, n), sof_root).replace(os.sep, '/')
for n in names]
return files

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: git ls-files output is split by lines, and the os.walk() fallback normalizes the separator and skips .git and build directories.



def check_sources(manifest, sof_root):
"""Return {user-space file: [kernel header includes]} from the source tree."""
user_src = manifest['user-sources']
not_user = manifest['not-user-sources']
user_hdr = manifest['user-headers']
kernel_hdr = manifest['kernel-headers']

found = {}
for rel in source_files(sof_root):
if rel.endswith(SOURCE_EXTS):
if not matches(rel, user_src) or matches(rel, not_user):
continue
elif not (rel.endswith(('.h', '.hpp')) and matches(rel, user_hdr)) or \
matches(rel, not_user) or matches(rel, kernel_hdr):
continue
path = os.path.join(sof_root, rel)
if not os.path.isfile(path):
continue
with open(path, encoding='utf-8', errors='replace') as f:
incs = KERNEL_INCLUDE.findall(f.read())
if incs:
found[rel] = incs
return found


def main():
parser = argparse.ArgumentParser(description=__doc__.split('\n', 1)[0])
parser.add_argument('build_dirs', nargs='+', metavar='build-dir')
parser.add_argument('build_dirs', nargs='*', metavar='build-dir')
parser.add_argument('--manifest', default=DEFAULT_MANIFEST)
parser.add_argument('--sof-root', default=SOF_ROOT,
help='SOF source directory the build directories were built from')
Expand All @@ -135,9 +185,25 @@ def main():
'includes a kernel-only header')
parser.add_argument('--summary', action='store_true',
help='only print per-header counts, not every source file')
parser.add_argument('--sources', action='store_true',
help='check the source tree for <sof/kernel/...> includes from '
'user-space code instead of build directories')
args = parser.parse_args()

manifest = parse_manifest(args.manifest)

if args.sources:
if args.build_dirs:
parser.error('--sources cannot be combined with build directories')
found = check_sources(manifest, args.sof_root)
Comment on lines +195 to +198

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Passing both --sources and <build-dir> currently ignores build_dirs silently:

Suggested change
if args.sources:
found = check_sources(manifest, args.sof_root)
if args.sources:
if args.build_dirs:
parser.error('--sources cannot be combined with build directories')
found = check_sources(manifest, args.sof_root)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: --sources with build directories is now an argument error.

for src in sorted(found):
print(f'{src}:')
for inc in found[src]:
print(f'\t{inc}')
print(f'{len(found)} user-space files include headers from src/kernel/include')
return 1 if args.strict and found else 0
if not args.build_dirs:
parser.error('build directories or --sources are required')
zephyr_base = args.zephyr_base if os.path.isdir(args.zephyr_base) else None

leaks = defaultdict(set)
Expand Down
14 changes: 13 additions & 1 deletion scripts/kernel_user_partition.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,23 @@
[user-sources]
src/*

# Kernel sources inside src/: src/kernel/ and the legacy drivers.
# Kernel sources inside src/: src/kernel/, and the legacy drivers and LL
# scheduling domains, which go with the legacy drivers.
[not-user-sources]
src/kernel/*
src/audio/dai-legacy.c
src/audio/host-legacy.c
src/schedule/dma_multi_chan_domain.c
src/schedule/dma_single_chan_domain.c
src/schedule/ll_schedule_xtos.c
src/schedule/zephyr_dma_domain.c

# Headers that user-space code may include, checked with --sources: they
# must not include headers from src/kernel/include. The kernel sources
# above and the [kernel-headers] below are excluded.
[user-headers]
src/*
zephyr/include/*

# SOF headers that only kernel code may include.
# Not listed on purpose: rtos/init.h, which only provides the
Expand Down
150 changes: 0 additions & 150 deletions src/debug/panic.c

This file was deleted.

11 changes: 0 additions & 11 deletions src/include/sof/debug/backtrace.h

This file was deleted.

3 changes: 3 additions & 0 deletions src/kernel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ Kernel-only headers are in [`include/sof/kernel`](include/sof/kernel) and are in
`<sof/kernel/platform.h>`. The directory is on the include path of the firmware, the host
library and the testbench, but not of LLEXT modules. Kernel-only headers that the host builds
replace with their own versions, such as the `rtos/` headers, have not moved yet.
User-space code must not include anything from here: the `codestyle` CI workflow runs
`scripts/check_kernel_includes.py --sources --strict`, which fails when a user-space source or
header includes `<sof/kernel/...>`.

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
Expand Down
45 changes: 0 additions & 45 deletions src/kernel/arch/host/include/arch/debug/backtrace.h

This file was deleted.

Loading
Loading