Skip to content
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

dtrace: fix fbt regression for aarch64 #855

Closed
wants to merge 1 commit into from

Conversation

ihoro
Copy link
Contributor

@ihoro ihoro commented Oct 3, 2023

I was working with pf and faced the issue with dtrace on main@aarch64. The steps:

  • $ kldload pf
  • $ dtrace -n 'fbt:pf:hook_pf:entry'
  • $ pfctl -e # it's expected to hit the probe once

As a result, dtrace was in a loop. Its output tail after interruption was like:

  4  59199                    hook_pf:entry
  4  59199                    hook_pf:entry
  4  59199                    hook_pf:entry
dtrace: 8893079 drops on CPU 4

I did some first comparisons:

  • 13.2-RELEASE: works as expected -- a single probe hit only
  • 14-BETA4: loops

I git bisect'ed it down to 980746e as the root cause.
The instr gets an extra increment -- a wrong instruction is targeted for patching.

This is the proposed variant of the fix.
I guess it would be good to fix it for 14.0 as well.

@emaste
Copy link
Member

emaste commented Oct 3, 2023

CC @markjdb

@markjdb
Copy link
Member

markjdb commented Oct 3, 2023

Thanks, I think your analysis is correct.

I'd prefer to fix it by getting rid of found entirely. Instead of setting found = true;, just break;, then replace the if (!found) check with if (instr >= limit). That is simpler and more consistent with some other FBT implementations. The commit log message should also explain the bug that was fixed.

Would you make these changes and re-push? If not I am happy to, just let me know.

ihoro added a commit to ihoro/freebsd-src that referenced this pull request Oct 3, 2023
fbt computes incorrect instruction position upon AArch64 kernel module load.

A reproduction example:
  $ kldload pf
  $ dtrace -n 'fbt:pf:hook_pf:entry'
  $ pfctl -e # it's expected to hit the probe once
As a result dtrace is stuck in a loop, e.g. its output after SIGINT:
  ...
  4  59199                    hook_pf:entry
  4  59199                    hook_pf:entry
  4  59199                    hook_pf:entry
dtrace: 8893079 drops on CPU 4

The issue is with the for loop, it does an extra increment of instr pointer
after the required instruction is found. Hence, a wrong instruction is
targeted for patching.

Fixes:          980746e ("fbt: simplify arm64 function-prologue parsing")
Pull-request:   freebsd#855
Signed-off-by:  Igor Ostapenko <pm@igoro.pro>
@ihoro ihoro force-pushed the 0002-dtrace-fbt-regression-aarch64 branch from c223cbf to cf72f1d Compare October 3, 2023 15:59
@ihoro
Copy link
Contributor Author

ihoro commented Oct 3, 2023

Would you make these changes and re-push? If not I am happy to, just let me know.

Sure. Please, consider another commit pushed. Let me know if a bit different style or way was expected.

@markjdb
Copy link
Member

markjdb commented Oct 3, 2023

Two more nits: first, the found label should come before the if (instr >= limit) check, not after. In practice it probably doesn't matter, but it's better not to make assumptions about the ELF symbol (i.e., that it's at least as large as a BTI and a NOP instruction). Second, the commit log message doesn't need the reproduction example, the last paragraph is enough ("The loop does an extra increment...").

With those fixed, I'll push the patch. Thanks again.

fbt computes incorrect instruction position for AArch64 kernel module symbol.

The issue is with the for loop, it does an extra increment of instr pointer
after the required instruction is found. Hence, a wrong instruction is
targeted for patching.

Fixes:          980746e ("fbt: simplify arm64 function-prologue parsing")
Pull-request:   freebsd#855
Signed-off-by:  Igor Ostapenko <pm@igoro.pro>
@ihoro ihoro force-pushed the 0002-dtrace-fbt-regression-aarch64 branch from cf72f1d to 4c06a3d Compare October 3, 2023 16:50
@ihoro
Copy link
Contributor Author

ihoro commented Oct 3, 2023

In practice it probably doesn't matter, but it's better not to make assumptions about the ELF symbol (i.e., that it's at least as large as a BTI and a NOP instruction).

Agree, good point.

freebsd-git pushed a commit that referenced this pull request Oct 3, 2023
fbt computes incorrect instruction position for AArch64 kernel module symbol.

The issue is with the for loop, it does an extra increment of instr pointer
after the required instruction is found. Hence, a wrong instruction is
targeted for patching.

Signed-off-by:  Igor Ostapenko <pm@igoro.pro>

Fixes:		980746e ("fbt: simplify arm64 function-prologue parsing")
Reviewed by:	markj
Pull Request:	#855
MFC after:	1 week
@markjdb
Copy link
Member

markjdb commented Oct 3, 2023

Merged, thank you.

@markjdb markjdb closed this Oct 3, 2023
@emaste emaste added the merged label Oct 3, 2023
marksisson pushed a commit to vendored/freebsd-src that referenced this pull request Oct 3, 2023
freebsd-git pushed a commit that referenced this pull request Oct 10, 2023
fbt computes incorrect instruction position for AArch64 kernel module symbol.

The issue is with the for loop, it does an extra increment of instr pointer
after the required instruction is found. Hence, a wrong instruction is
targeted for patching.

Signed-off-by:  Igor Ostapenko <pm@igoro.pro>

Fixes:		980746e ("fbt: simplify arm64 function-prologue parsing")
Reviewed by:	markj
Pull Request:	#855
MFC after:	1 week

(cherry picked from commit b4db386)
freebsd-git pushed a commit that referenced this pull request Oct 10, 2023
fbt computes incorrect instruction position for AArch64 kernel module symbol.

The issue is with the for loop, it does an extra increment of instr pointer
after the required instruction is found. Hence, a wrong instruction is
targeted for patching.

Signed-off-by:  Igor Ostapenko <pm@igoro.pro>

Approved by:	re (gjb)
Fixes:		980746e ("fbt: simplify arm64 function-prologue parsing")
Reviewed by:	markj
Pull Request:	#855
MFC after:	1 week

(cherry picked from commit b4db386)
(cherry picked from commit 2ba605f)
emaste pushed a commit to emaste/freebsd that referenced this pull request Oct 13, 2023
gmshake pushed a commit to gmshake/freebsd-src that referenced this pull request Oct 19, 2023
emaste pushed a commit to emaste/freebsd that referenced this pull request Oct 25, 2023
…d#855)

Approved by:	re (gjb)

(cherry picked from commit ef3a764)
(cherry picked from commit 579455e)
bsdjhb pushed a commit to bsdjhb/cheribsd that referenced this pull request Feb 7, 2024
fbt computes incorrect instruction position for AArch64 kernel module symbol.

The issue is with the for loop, it does an extra increment of instr pointer
after the required instruction is found. Hence, a wrong instruction is
targeted for patching.

Signed-off-by:  Igor Ostapenko <pm@igoro.pro>

Fixes:		980746e ("fbt: simplify arm64 function-prologue parsing")
Reviewed by:	markj
Pull Request:	freebsd/freebsd-src#855
MFC after:	1 week
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants