-
Notifications
You must be signed in to change notification settings - Fork 92
[Deepin-Kernel-SIG] [linux 6.6-y] [Upstream] kfence: save freeing stack trace at calling time instead of freeing time #906
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
Conversation
Reviewer's GuideThis PR adds a new RCU-freeing state to kfence, captures the freeing backtrace at the moment of kfree() for SLAB_TYPESAFE_BY_RCU caches instead of at deferred RCU callback, centralizes state checks via a helper, and updates reporting and metadata logic to support the new state. Entity relationship diagram for kfence object states and transitionserDiagram
KFENCE_METADATA {
state enum
addr ulong
cache pointer
}
KFENCE_OBJECT_STATE {
KFENCE_OBJECT_UNUSED
KFENCE_OBJECT_ALLOCATED
KFENCE_OBJECT_RCU_FREEING
KFENCE_OBJECT_FREED
}
KFENCE_METADATA ||--o{ KFENCE_OBJECT_STATE : has
Class diagram for new helper and state transitions in kfenceclassDiagram
class kfence_metadata {
+enum kfence_object_state state
+unsigned long addr
+struct kmem_cache* cache
+spinlock_t lock
}
class kfence {
+bool kfence_obj_allocated(const kfence_metadata *meta)
+void metadata_update_state(kfence_metadata *meta, kfence_object_state next, ...)
+void __kfence_free(void *addr)
+void kfence_guarded_free(void *addr, kfence_metadata *meta, bool zero)
}
kfence_metadata <.. kfence : uses
Flow diagram for reporting object state in kfence_print_stackflowchart TD
A[kfence_print_stack called] --> B{show_alloc?}
B -- Yes --> C["allocated" label]
B -- No --> D{meta->state == RCU_FREEING?}
D -- Yes --> E["rcu freeing" label]
D -- No --> F["freed" label]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
commit c36be0c upstream. For kmem_cache with SLAB_TYPESAFE_BY_RCU, the freeing trace stack at calling kmem_cache_free() is more useful. While the following stack is meaningless and provides no help: freed by task 46 on cpu 0 at 656.840729s: rcu_do_batch+0x1ab/0x540 nocb_cb_wait+0x8f/0x260 rcu_nocb_cb_kthread+0x25/0x80 kthread+0xd2/0x100 ret_from_fork+0x34/0x50 ret_from_fork_asm+0x1a/0x30 Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Tianchen Ding <[email protected]> Reviewed-by: Marco Elver <[email protected]> Cc: Alexander Potapenko <[email protected]> Cc: Dmitry Vyukov <[email protected]> Signed-off-by: Andrew Morton <[email protected]> [ Backport from v6.12-rc1 ] Link: https://gitee.com/anolis/cloud-kernel/pulls/3901 Signed-off-by: WangYuli <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adjusts the KFENCE reporting and state management to record the freeing stack trace at the time of freeing invocation for kmem_caches with SLAB_TYPESAFE_BY_RCU.
- Update printed messages in kfence_print_stack to show a "rcu freeing" state.
- Introduce the KFENCE_OBJECT_RCU_FREEING state and update related state checks throughout the core logic.
- Factor out state checking into a helper (kfence_obj_allocated) to cover both allocated and rcu freeing cases.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
File | Description |
---|---|
mm/kfence/report.c | Updated print message to distinguish "rcu freeing" state. |
mm/kfence/kfence.h | Added the KFENCE_OBJECT_RCU_FREEING enum value. |
mm/kfence/core.c | Updated state transitions and helper function usage. |
Comments suppressed due to low confidence (3)
mm/kfence/report.c:116
- Consider adding an inline comment explaining the distinction of the 'rcu freeing' message compared to the regular 'freed' state for improved clarity in debugging outputs.
track->cpu, (unsigned long)ts_sec, rem_nsec / 1000);
mm/kfence/core.c:277
- [nitpick] The function name kfence_obj_allocated may be misleading as it returns true even when the state is KFENCE_OBJECT_RCU_FREEING. Consider renaming it to kfence_obj_active or similar to reflect that the object is still active.
return state == KFENCE_OBJECT_ALLOCATED || state == KFENCE_OBJECT_RCU_FREEING;
mm/kfence/core.c:294
- [nitpick] Consider adding a comment explaining why the stack trace update is skipped when meta->state is KFENCE_OBJECT_RCU_FREEING to clarify the rationale behind this branch.
if (READ_ONCE(meta->state) == KFENCE_OBJECT_RCU_FREEING)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @Avenger-285714 - I've reviewed your changes - here's some feedback:
- Consider refactoring the goto-based skip logic in metadata_update_state into an explicit early return to make it clearer that only the state update occurs for RCU_FREEING transitions.
- Document in kfence.h that KFENCE_OBJECT_RCU_FREEING transitions to KFENCE_OBJECT_FREED after the RCU callback to clarify the object's lifecycle for future readers.
- The nested ternary in kfence_print_stack for choosing the "allocated"/"freed"/"rcu freeing" label is a bit hard to follow; extracting it into a small helper or named variable could improve readability.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider refactoring the goto-based skip logic in metadata_update_state into an explicit early return to make it clearer that only the state update occurs for RCU_FREEING transitions.
- Document in kfence.h that KFENCE_OBJECT_RCU_FREEING transitions to KFENCE_OBJECT_FREED after the RCU callback to clarify the object's lifecycle for future readers.
- The nested ternary in kfence_print_stack for choosing the "allocated"/"freed"/"rcu freeing" label is a bit hard to follow; extracting it into a small helper or named variable could improve readability.
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
deepin pr auto review代码审查意见:
以上是代码审查的初步意见,具体的改进措施需要根据代码的上下文和业务逻辑来决定。 |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: opsiff The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
commit c36be0c upstream.
For kmem_cache with SLAB_TYPESAFE_BY_RCU, the freeing trace stack at calling kmem_cache_free() is more useful. While the following stack is meaningless and provides no help:
freed by task 46 on cpu 0 at 656.840729s:
rcu_do_batch+0x1ab/0x540
nocb_cb_wait+0x8f/0x260
rcu_nocb_cb_kthread+0x25/0x80
kthread+0xd2/0x100
ret_from_fork+0x34/0x50
ret_from_fork_asm+0x1a/0x30
Link: https://lkml.kernel.org/r/[email protected]
Reviewed-by: Marco Elver [email protected]
Cc: Alexander Potapenko [email protected]
Cc: Dmitry Vyukov [email protected]
Link: https://gitee.com/anolis/cloud-kernel/pulls/3901
Summary by Sourcery
Introduce a new KFENCE object state to capture and report free stack traces at the time of kmem_cache_free for RCU-safe caches rather than at deferred RCU callback time
Enhancements: