Comments (5)
Here is the new activeness safety check design that I'm thinking about:
- kpatch_register doesn't use stop_machine any more, and just calls kpatch_apply directly.
- kpatch_apply_patch protects the activeness safety check and updates to the function list with a
write_lock_irqsave(&kpatch_lock)
. - The ftrace handler's reading of the function list will be protected with a
read_lock(&kpatch_lock)
. This means that even if the trace handler is called during the activeness safety check, it will wait for the check to complete and will not cause it to fail. - The activeness safety check includes:
- For tasks not currently running on a CPU, check their backtraces
- For a task currently running on a CPU, run the backtrace check for that task with
stop_one_cpu_nowait()
on that CPU, so that the task is removed from the CPU before the backtrace check. - The tricky part here is ensuring that the entirety of each backtrace check occurs while the task is not running, otherwise we could get a corrupt stack or a false positive. This may need some coordination with the scheduler (or using something like
task_rq_lock()
, which is currently only an internal scheduler function).
The for_each_thread loop doesn't have to be atomic, because any future entries into kpatched functions will be paused by the read lock.
If the write lock is taken, and an NMI happens on a CPU other than the writer, and the NMI handler tries to call a kpatched function, it will spin on the kpatch_lock until the writer releases the lock.
If an NMI happens on the CPU with the writer (i.e. NMI interrupts the activeness safety and func list update), and the NMI handler tries to call a kpatched function, we will have to detect that scenario (in NMI and read_trylock fails) and panic.
The goal of this new design is to avoid using stop_machine if at all possible.
from kpatch.
@jpoimboe Hmm, why would you like to avoid using stop_machine and use lock? I don't think it's a good idea to dance with schedulers.
Instead, here I have another option to support NMI with stop_machine. This will involves CAS(compare and swap) but no lock.
- prepare an atomic conflict_counter. And add an updating flag in kpatch_func.
- when registering the patch, updating flag on each new kpatch_func set true.
- right before stop_machine, clear the conflict_counter.(Actually, a trick is here, run a stop_machine ensures all running interrupt(including NMI) handlers exit until stop_machine callback runs)
- in the stop_machine, after checked activeness and copied new kpatch_func list, do atomic CAS on conflict_counter to set 2 if it is 0.
- if the CAS failed and conflict_counter should be 1, there may be at least one concurrently running NMI handler hits the new kpatch_func, this patch should be failed. Rollback and return -EBUSY
- if the CAS succeeded, clear updating flag of all new kpatch_func and return 0.
On the other hand, the kpatch_ftrace_handler() does;
- search kpatch_func by ip address
- if failed to find kpatch_func, and if in_nmi() is true, goto step 5.
- if we hits an updating kpatch_func and conflict_counter is not 2, goto step 5.
- modify regs->ip and return
- do atomic CAS on conflict_counter to set 1 if it is 0.
- if the CAS is succeeded or the conflict_counter is 1, return without modifying regs->ip
- if the CAS is failed and the conflict_counter is 2, go back to step 1 to retry
With this method, you can avoid using locks and no need to PANIC on patching too. :)
from kpatch.
Oops, I missed push the close button... sorry.
from kpatch.
I want to avoid using stop_machine, if possible, because it can introduce a lot of latency into the system, especially for systems with many CPUs. Also I've heard rumors that stop_machine is planned to be removed from the kernel someday.
However, I agree that my non-stop_machine design has problems: it relies on locking the scheduler, it creates a small possibility of panic on NMI, and it requires modifications to the kernel.
For now, I think your proposal is the way to go. I can work on implementing it, but I'll be at the Collaboration Summit next week, so it might be a few weeks. So feel free to submit a pull request ;-)
If we get a lot of complaints about latencies, or if stop_machine gets removed from the kernel, we'll have to revisit this issue someday.
from kpatch.
Fixed with #108.
from kpatch.
Related Issues (20)
- rhel-7.9/syscall.patch fails to build on 3.10.0-1160.71.1.el7.ppc64le HOT 1
- kpatch-build: .rela.return_sites not selected for inclusion on 5.18.12-rc1+ kernels HOT 2
- kpatch-cc build error when source tree prefix matches substring in changed object file
- kpatch module build could fail when kernel 5.19+ contains dynamic symbols HOT 21
- kpatch_version: Unknown symbol kpatch_unregister HOT 8
- '-'/'_' mismatch for OOT modules HOT 2
- kpatch load can't find core module HOT 8
- Fix possible buffer overflow in ESP transformation error System.map error HOT 3
- integration tests: CONFIG_MODVERSIONS and linux-5.18.0/symvers-disagreement-FAIL.patch HOT 8
- After the kpatch install "xxx. ko" on openeuler, the machine was restarted. The kpatch service failed, resulting in the kernel patch restart not taking effect
- What do I need to do to support riscv64? HOT 11
- ERROR: testmod_drv.o: .return_sites section header details differ from .return_sites HOT 20
- Need suggestions for CONFIG_LTO_CLANG support HOT 40
- New error building patch for 6.2-rc2? HOT 12
- kpatch-build errors for kernel v6.1.4 HOT 4
- ubsan for kpatch HOT 2
- find_local_syms for <modified-file>: found_none HOT 2
- 6.3 failure with setlocalversion HOT 5
- Outstanding clang issues for 6.2 HOT 3
- x86 NOP padded functions without __pfx_ symbol HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kpatch.