Skip to content

Comments

Ctrl refactor#32

Merged
ming1 merged 19 commits intomainfrom
ctrl-refactor
Sep 12, 2025
Merged

Ctrl refactor#32
ming1 merged 19 commits intomainfrom
ctrl-refactor

Conversation

@ming1
Copy link
Collaborator

@ming1 ming1 commented Sep 12, 2025

  • refactor ctrl async vs. sync
  • add one test for showing how to write completely async style io handler, just with some performance loss

Remove redundant OnceCell wrapper from thread-local CTRL_URING since
thread_local! is already lazy. Simplify the structure while keeping
the ublk_init_ctrl_task_ring() API for backward compatibility.

Changes:
- Change CTRL_URING from OnceCell<RefCell<IoUring>> to RefCell<Option<IoUring>>
- Update with_ctrl_ring_internal and with_ctrl_ring_mut_internal macros
- Preserve ublk_init_ctrl_task_ring() function with updated signature
- Update documentation examples to reflect simplified API

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
Implement Phase 2 of io.rs cleanup by consolidating documentation
to reduce redundancy and improve maintainability.

Changes:
- Add comprehensive module-level documentation with complete examples
  for ring initialization (basic/advanced) and unified buffer APIs
- Simplify individual method documentation by removing redundant
  examples and referencing module-level docs instead:
  * ublk_init_task_ring(): Remove 48 lines of duplicate examples
  * submit_io_cmd_unified(): Remove verbose usage examples
  * submit_fetch_commands_unified(): Remove redundant buffer examples
  * complete_io_cmd_unified(): Streamline validation/performance docs
- Clean up outdated comments:
  * Remove historical "Previously had separate TASK_URING" comment
  * Remove outdated "todo: apply io_uring flags" comment

Benefits:
- Single source of truth for documentation examples
- Better discoverability with comprehensive module-level docs
- Reduced maintenance burden from duplicate examples
- Improved readability with focused method documentation
- All functionality preserved with better organization

The documentation now follows Rust best practices with comprehensive
module docs and concise method docs that reference the examples.

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
Update the module-level documentation example for traditional buffer
operations to use IoBuf instead of raw arrays, which is more consistent
with the library's established patterns and best practices.

Changes:
- Replace raw array `[0u8; 4096]` with `IoBuf::<u8>::new(4096)`
- Use `BufDesc::from_io_buf()` helper method for proper conversion
- Add missing `use libublk::helpers::IoBuf;` import

This example now demonstrates the recommended approach for traditional
buffer operations and aligns with how IoBuf is used throughout the
codebase and other examples.

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
Add UblkCtrl::set_thread_affinity() API for setting queue thread
affinity.

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
Export it, which is still helpful for target code.

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
Signed-off-by: Ming Lei <tom.leiming@gmail.com>
Extract common command data preparation logic for ADD_DEV into
prepare_add_cmd() method. Both sync and async variants now use
the same preparation logic, reducing code duplication while
maintaining identical behavior.

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
Extract common command data preparation logic for DEL_DEV into
prepare_del_cmd() method. All three deletion variants now use
the same preparation logic with a force_async parameter,
reducing code duplication while maintaining identical behavior.

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
Extract common command data preparation logic for:
- GET_FEATURES: prepare_get_features_cmd()
- GET_DEV_INFO/GET_DEV_INFO2: prepare_read_dev_info_cmd()

Both sync and async variants now use the same preparation logic,
reducing code duplication while maintaining identical behavior.

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
Extract common command data preparation logic for:
- START_DEV: prepare_start_cmd()
- STOP_DEV: prepare_stop_cmd()
- GET_PARAMS: prepare_get_params_cmd()
- SET_PARAMS: prepare_set_params_cmd()

Both sync and async variants now use the same preparation logic,
reducing code duplication while maintaining identical behavior.

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
Extract common command data preparation logic for:
- GET_QUEUE_AFFINITY: prepare_get_queue_affinity_cmd()
- START_USER_RECOVERY: prepare_start_user_recover_cmd()
- END_USER_RECOVERY: prepare_end_user_recover_cmd()

Both sync and async variants now use the same preparation logic,
reducing code duplication while maintaining identical behavior.

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
Extract common JSON building logic into helper methods:
- build_json_internal() and build_json_internal_async() for data generation
- update_json_queue_tids() for shared TID update logic

Both sync and async versions now share validation and data
preparation logic while maintaining separate execution paths.

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
Extract common parameter validation logic into validate_new_params()
method. Both sync and async constructors now share identical
validation logic, ensuring consistent parameter checking while
maintaining separate initialization paths.

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
Extract common device info printing logic into dump_device_info()
helper method. Both sync and async dump methods now share identical
formatting and display logic while maintaining separate data
collection paths.

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
- async command should run in async environment with UBLK_CTRL_ASYNC_AWAIT
set.

- sync command should run in sync environment without UBLK_CTRL_ASYNC_AWAIT

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
Signed-off-by: Ming Lei <tom.leiming@gmail.com>
smol doesn't belong to libublk, we shouldn't add more such mess.

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
Signed-off-by: Ming Lei <tom.leiming@gmail.com>
Update all smol executor usage to use exe.run(async { ... }) instead of
smol::block_on(async { ... }) for proper executor integration.

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
@ming1 ming1 merged commit ae35fd1 into main Sep 12, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant