Skip to content

posix: options: add custom Zephyr POSIX subprofile #88547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: main
Choose a base branch
from

Conversation

cfriedt
Copy link
Member

@cfriedt cfriedt commented Apr 12, 2025

  • eventfd: bring config to top-level of posix dir, since it is not posix
  • posix: separate option groups into c library ext and system interfaces
  • posix: profiles: add custom Zephyr POSIX subprofile
  • posix: profiles: make POSIX_AEP_CHOICE_ZEPHYR the default
  • posix: options: add keep-sorted-start and -stop

Fixes #75843

Should only be merged after

Note

The sources used when CONFIG_POSIX_C_LANG_SUPPORT_R=y and CONFIG_POSIX_C_LIB_EXT=y can still be
overridden with CONFIG_TC_PROVIDES_POSIX_C_LANG_SUPPORT_R=y an CONFIG_TC_PROVIDES_POSIX_C_LIB_EXT=y (which must be used with select <option> in Kconfig), as they are primarily meant for toolchain maintainers.

A comparison when building samples/hello_world for native_sim.

Before:

wc -l build-main/zephyr/.config
   1295 build-main/zephyr/.config

After:

wc -l build-branch/zephyr/.config
   1220 build-branch/zephyr/.config
diff -Nua build-{main,branch}/zephyr/.config
--- build-main/zephyr/.config   2025-05-24 11:33:53.697011215 -0400
+++ build-branch/zephyr/.config 2025-05-24 11:34:27.140913656 -0400
@@ -900,98 +900,23 @@
 #
 # POSIX API Support
 #
-
-#
-# POSIX Options
-#
 # CONFIG_POSIX_API is not set
-CONFIG_POSIX_AEP_CHOICE_NONE=y
-# CONFIG_POSIX_AEP_CHOICE_BASE is not set
-# CONFIG_POSIX_AEP_CHOICE_PSE51 is not set
-# CONFIG_POSIX_AEP_CHOICE_PSE52 is not set
-# CONFIG_POSIX_AEP_CHOICE_PSE53 is not set
-# CONFIG_POSIX_ASYNCHRONOUS_IO is not set
-# CONFIG_POSIX_BARRIERS is not set
-# CONFIG_POSIX_C_LANG_SUPPORT_R is not set
-# CONFIG_POSIX_C_LIB_EXT is not set
-
-#
-# POSIX device I/O
-#
-# CONFIG_POSIX_DEVICE_IO is not set
-CONFIG_POSIX_OPEN_MAX=4
-# end of POSIX device I/O
-
-# CONFIG_POSIX_FD_MGMT is not set
-# CONFIG_POSIX_FILE_SYSTEM_R is not set
-# CONFIG_POSIX_FILE_SYSTEM is not set
-
-#
-# POSIX memory
-#
-CONFIG_POSIX_PAGE_SIZE=0x40
-# CONFIG_POSIX_SHARED_MEMORY_OBJECTS is not set
-# CONFIG_POSIX_MAPPED_FILES is not set
-# CONFIG_POSIX_MEMORY_PROTECTION is not set
-# end of POSIX memory
-
-# CONFIG_POSIX_MESSAGE_PASSING is not set
-# CONFIG_POSIX_SINGLE_PROCESS is not set
-# CONFIG_POSIX_MULTI_PROCESS is not set
-# CONFIG_POSIX_THREADS is not set
-# CONFIG_POSIX_READER_WRITER_LOCKS is not set
-
-#
-# POSIX scheduler options
-#
-# CONFIG_POSIX_PRIORITY_SCHEDULING is not set
-# end of POSIX scheduler options
-
-# CONFIG_POSIX_SEMAPHORES is not set
-
-#
-# POSIX signals
-#
-# CONFIG_POSIX_REALTIME_SIGNALS is not set
-# CONFIG_POSIX_SIGNALS is not set
-# end of POSIX signals
-
-# CONFIG_POSIX_SPIN_LOCKS is not set
-
-#
-# POSIX synchronized I/O
-#
-# CONFIG_POSIX_FSYNC is not set
-# CONFIG_POSIX_SYNCHRONIZED_IO is not set
-# end of POSIX synchronized I/O
-
-# CONFIG_POSIX_TIMERS is not set
-
-#
-# X/Open system interfaces
-#
-# CONFIG_XSI_REALTIME is not set
-# CONFIG_XSI_SINGLE_PROCESS is not set
-# CONFIG_XOPEN_STREAMS is not set
-# CONFIG_XSI_SYSTEM_LOGGING is not set
-# CONFIG_XSI_THREADS_EXT is not set
-# end of X/Open system interfaces
-
-#
-# Miscellaneous POSIX-related options
-#
-# CONFIG_EVENTFD is not set
-# end of Miscellaneous POSIX-related options
-
+# CONFIG_POSIX_AEP_CHOICE_NONE is not set
+CONFIG_POSIX_AEP_CHOICE_ZEPHYR=y
 CONFIG_TC_PROVIDES_POSIX_C_LANG_SUPPORT_R=y
-# end of POSIX Options
 
 #
-# POSIX Shell Utilities
+# POSIX C Library Extensions
 #
-# end of POSIX Shell Utilities
+CONFIG_POSIX_C_LANG_SUPPORT_R=y
+CONFIG_POSIX_C_LIB_EXT=y
+# CONFIG_GETOPT_LONG is not set
+# end of POSIX C Library Extensions
+
+# CONFIG_POSIX_SYSTEM_INTERFACES is not set
 # end of POSIX API Support
 
+# CONFIG_EVENTFD is not set
 # CONFIG_OPENAMP_RSC_TABLE is not set
 # CONFIG_SMF is not set
 CONFIG_LIBGCC_RTLIB=y

@cfriedt cfriedt force-pushed the add-zephyr-posix-aep-choice branch from 0d193e5 to f68b0fb Compare April 12, 2025 12:31
@cfriedt cfriedt requested review from nashif, JordanYates and ycsin April 12, 2025 12:37
@cfriedt cfriedt added the area: POSIX POSIX API Library label Apr 12, 2025
@cfriedt
Copy link
Member Author

cfriedt commented Apr 12, 2025

Probably one or two follow-up PRs can be made to ensure that the POSIX_C_LIB_EXT Option Group is complete. I think there are some string manipulation functions missing (not ISO C).

Currently, all of the implementation of POSIX_C_LANG_SUPPORT_R is done in libc/common, which ... doesn't matter too much, but that could be corrected later if necessary.

@cfriedt
Copy link
Member Author

cfriedt commented Apr 14, 2025

Shell utilities should probably also be gated by the new Kconfig.

Copy link
Member

@jukkar jukkar left a comment

Choose a reason for hiding this comment

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

Thanks Chris, this helps to understand what is going on with the POSIX options.


These functions are divided into two standalone Option Groups that may be enabled
independently of the remainder of the POSIX API implementation; namely POSIX_C_LIB_EXT and
POSIX_C_LANG_SUPPORT_R. If not referenced by the Zephyr kernel or application, There are no
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
POSIX_C_LANG_SUPPORT_R. If not referenced by the Zephyr kernel or application, There are no
POSIX_C_LANG_SUPPORT_R. If not referenced by the Zephyr kernel or application, there are no

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed!

@cfriedt cfriedt force-pushed the add-zephyr-posix-aep-choice branch 5 times, most recently from db9804b to 2ca347b Compare April 25, 2025 03:31
@cfriedt cfriedt force-pushed the add-zephyr-posix-aep-choice branch 2 times, most recently from aeb78be to 7ac5797 Compare April 29, 2025 01:15
@cfriedt
Copy link
Member Author

cfriedt commented May 14, 2025

I'll give this a rebase shorty

@cfriedt cfriedt force-pushed the add-zephyr-posix-aep-choice branch 3 times, most recently from 04b8fa8 to df478b2 Compare May 18, 2025 03:52
@cfriedt cfriedt dismissed JordanYates’s stale review May 24, 2025 15:47

Indentation was fixed

@cfriedt cfriedt force-pushed the add-zephyr-posix-aep-choice branch from 02a72f0 to b1e87fc Compare May 25, 2025 03:25
@cfriedt
Copy link
Member Author

cfriedt commented May 25, 2025

  • Removed commits that should be part of a different PR.

@cfriedt
Copy link
Member Author

cfriedt commented May 25, 2025

Hmm... this was passing. Now SonarQube seems to be refusing to run. Maybe if I force push again?

@cfriedt cfriedt force-pushed the add-zephyr-posix-aep-choice branch 2 times, most recently from 6f20e45 to 3b2d0af Compare May 26, 2025 22:51
@ycsin
Copy link
Member

ycsin commented May 30, 2025

Hmm... this was passing. Now SonarQube seems to be refusing to run. Maybe if I force push again?

The commit message for tests: libc: thrd: use pthread barriers for qemu_x86_64/atom is too long

Commit dc81ee98cc:
35: UC4 Commit message body line exceeds max length (100>75): " twister -i --test-only -p qemu_x86_64/atom -s libraries.libc.c11_threads.picolibc >/dev/null 2>&1"

@cfriedt
Copy link
Member Author

cfriedt commented May 30, 2025

I fixed it locally, just haven't pushed. Thought I would give CI a break for a while

cfriedt added 20 commits May 31, 2025 08:22
Previously, log messages would generate warnings (escalated to errors
in CI) when CONFIG_TIMEOUT_64BIT=n.

For example,

```
west build -p auto -b qemu_cortex_m0 -t run tests/posix/timers/ \
  -- -DCONFIG_TIMEOUT_64BIT=n
..
warning: format '%llx' expects argument of type \
  'long long unsigned int', but argument 3 has type 'k_ticks_t' \
  {aka 'unsigned int'}
```

Use portable print specifiers and cast the argument to int64_t.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
Additional entries for the sys_clock API, comprised of:

* sys_clock_gettime()
* sys_clock_settime()
* sys_clock_nanosleep()

along with the constants

* SYS_CLOCK_REALTIME
* SYS_CLOCK_MONOTONIC
* SYS_TIMER_ABSTIME

The primary motivation for this API is so that libc and other
libraries have a familiar-enough API to reach to when POSIX
is not available, since POSIX is optional in Zephyr.

By adding this API to lib/os, we also eliminate dependency cycles
between libc and posix, as lib/os is a mutual dependency.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
Add bullets to release notes for sys_clock_gettime(),
sys_clock_settime(), and sys_clock_nanosleep().

These changes were significant, as they make timekeeping dependencies
more independent between libc and posix, and remove several unnecessary
dependencies on posix for ISO C library routines.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
Use the newly added sys_clock_gettime() to avoid the unnecessary
dependency on POSIX APIs.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
Remove POSIX clock_gettime() from the common libc time implementation,
since POSIX should not be a dependency for ISO C.

Instead, use the newly added lib/os sys_clock API.

Specifically, sys_clock_gettime().

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
Use the implementation of time() from the common libc, since there
it no longer pulls in POSIX.

Use is implied for minimal, newlib, and picolibc, and selected
for IAR.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
Reduce the dependency on POSIX by taking advantage of the newly added
sys_clock_nanosleep().

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
The C standard does not guarantee that `thrd_success` is equal to
zero, so ensure that the test takes that into account.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
The specification does not say that the first struct timespec passed to
thrd_sleep() may be NULL.

Preserving the testpoint so that a possible future testsuite can be
run that expects faults to occur when undefined behaviour is used.

Faults are one possible solution to handling undefined behaviour, but
it would be good to come to a concensus about how that should be
handled in Zephyr for standard function calls belonging to ISO C or
POSIX.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
Use `timespec_from_timeout(K_MSEC(msec), &ts)` instead of leaning on
lazily-crafted timespecs with invalid tv_nsec fields.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
A bit of a race condition seems to exist, but only for
`qemu_x86_64/atom`.

It's the same race that was attributed to scheduler noise or something
along those lines when running under qemu.

Most other architectures no longer see this issue, and it has not been
required for some time in the `tests/benchmark/posix/threads` app, but
here it seems to be pronounced again.

Testing done:

Before:

We would see failures very reproducible, but only on this
platform. Also, the failure has existed in `main` for some time, so is
independent of the current PR.

https://github.com/zephyrproject-rtos/zephyr/actions/runs/\
15238463423/job/42862652065

After:
```shell
COUNT=100
NOKAY=0
NFAIL=0

for i in $(seq 0 $((COUNT - 1))); do
  if [ $i -ne 0 ] && [ $((i % 10)) -eq 0 ]; then
    echo "Running test iteration $i"
  fi

  twister -i --test-only -p qemu_x86_64/atom  -s \
    libraries.libc.c11_threads.picolibc >/dev/null 2>&1
  if [ $? -eq 0 ]; then
    NOKAY=$((NOKAY + 1))
  else
    NFAIL=$((NFAIL + 1))
    echo "Test failed on iteration $i"
  fi
done

PCT="$(echo "scale=0; ((100 * $NFAIL) / $COUNT) / 1" | bc)"

echo "$NFAIL / $COUNT = $PCT % test failure rate"

Running test iteration 10
Running test iteration 20
Running test iteration 30
Running test iteration 40
Running test iteration 50
Running test iteration 60
Running test iteration 70
Running test iteration 80
Running test iteration 90
0 / 100 = 0 % test failure rate
```

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
Presumably the time testsuite was separate from the c library set of
testsuites because it had a depedency on POSIX.

Since that dependency no longer exists, colocate the time testsuite
with the other c library testsuites.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
Use the newly added sys_clock API in lib/os for

* clock_gettime()
* clock_settime()
* clock_nanosleep() and nanosleep()
* gettimeofday()

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
The ISO C function time() should not depend on POSIX and this was
corrected recently via the common libc time() implementation.

Remove this dependency from the network subsystem where it has
been unduly needed for some time.

Similarly, XSI_SINGLE_PROCESS was a dependency for time() via
picolibc, because the time() implementation in picolibc relies
on the POSIX gettimeofday() call.

However, since Zephyr's common libc time() implementation no longer
depends on that, it can be removed as a dependency in the network
subsystem as well.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
Remove POSIX_TIMERS and XSI_SINGLE_PROCESS dependencies from the aws
cloud sample and the lwm2m client sample, as they are no longer
required.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
The eventfd configuration does not need to be so deeply nested within
POSIX since it does not depend on POSIX completely.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
Separate the POSIX implementation into two categories:

- Extensions to ISO C
- System Interfaces

The first category include standalone functions that generally do not
require OS support or depend on any other features within the POSIX
specification. The Option Groups that comprise this category include

- POSIX_C_LIB_EXT: e.g. strnlen(), fnmatch()
- POSIX_C_LANG_SUPPORT_R: e.g. gmtime_r(), strtok_r()

The second category includes the majority of other POSIX Option Groups
that do require OS support. The latter group may also be categorized
generally as being NATIVE_LIBC_INCOMPATIBLE, although that might
eventually become more granular.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
Add a custom Zephyr POSIX subprofile specifically for enabling the
default features that Zephyr requires as per the coding guidelines.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
Default POSIX_AEP_CHOICE to POSIX_AEP_CHOICE_ZEPHYR

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
Add zephyr-keep-sorted-start and zephyr-keep-sorted-stop
comments.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
@cfriedt cfriedt force-pushed the add-zephyr-posix-aep-choice branch from 3b2d0af to 764da0b Compare May 31, 2025 12:22
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Base OS Base OS Library (lib/os) area: C Library C Standard Library area: LWM2M area: Networking area: POSIX POSIX API Library area: Process area: Samples Samples area: Utilities Release Notes To be mentioned in the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

POSIX kconfigs show up although POSIX APIs is not being used
4 participants