[kernel][ktrace] Fix race condition in ktrace record creation (reland #2)

This change fixes a bug in ktrace that could manifest as a failed
debug assert.  See https://fxbug.dev/120711 for details.

Remove unused PendingCommit::observed_grpmask_ field.

Replace grpmask_and_inflight_writes_ with a new field, write_state_
that encodes both the in-flight count and a writes-enabled bit.
Because we emit group-less name records at trace initialization, group
mask alone is insufficient to control whether writes are enabled.

Move the group mask into its own dedicated atomic field that's checked
by writers before attempting to increment.

Change KTraceState::IncPendingWrite to only increment if writes are
enabled and return false otherwise.  KTraceState::Reserve will now
fail with ZX_ERR_BAD_STATE if called when tracing is stopped.

This is a reland.  The earlier two versions were:

 * https://fuchsia-review.googlesource.com/c/fuchsia/+/801688

   The change from that version is to fix an assertion in
   DecPendingWrite() (and to add a test for that).

 * https://fuchsia-review.googlesource.com/c/fuchsia/+/804268

   The change from that version is to do IncPendingWrite() before
   ReserveRaw() so that we don't create trace records with zero
   headers when writing to the trace buffer has been disabled (and to
   add a test for that).

Those original versions were authored by Nick Maniscalco.

Bug: 120711
Bug: 122181
Change-Id: Iab89c17e06a3987e83e4fac4c0d425a4085cdf8b
Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/810689
Commit-Queue: Mark Seaborn <mseaborn@google.com>
Reviewed-by: John Grossman <johngro@google.com>
3 files changed
tree: 07afd74728db93e3dd020c668cb60036e430d3c0
  1. boards/
  2. build/
  3. bundles/
  4. docs/
  5. examples/
  6. products/
  7. scripts/
  8. sdk/
  9. src/
  10. third_party/
  11. tools/
  12. zircon/
  13. .clang-format
  14. .clang-tidy
  15. .editorconfig
  16. .git-blame-ignore-revs
  17. .gitattributes
  18. .gitignore
  19. .gitmodules
  20. .gn
  21. .ignore
  22. .style.yapf
  23. analysis_options.yaml
  24. AUTHORS
  25. BUILD.gn
  26. CODE_OF_CONDUCT.md
  27. CONTRIBUTING.md
  28. fuchsia.code-workspace
  29. LICENSE
  30. OWNERS
  31. PATENTS
  32. pyrightconfig.json
  33. README.md
  34. rustfmt.toml
README.md

Fuchsia

What is Fuchsia?

Fuchsia is an open source, general purpose operating system supporting modern 64-bit Intel and ARM processors.

We expect everyone interacting with our project to respect our code of conduct.

Read more about Fuchsia's principles.

How can I build and run Fuchsia?

See Getting Started.

Where can I learn more about Fuchsia?

See fuchsia.dev.