Skip to content

Add FreeBSD posix_fadvise support #17379

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

Merged
merged 2 commits into from
May 29, 2025
Merged

Conversation

amotin
Copy link
Member

@amotin amotin commented May 26, 2025

This is a rebase/refresh of #13958 by @macdice.

As commit 320f0c6 did for Linux, connect POSIX_FADV_WILLNEED up to dmu_prefetch() on FreeBSD.

Signed-off-by: Thomas Munro [email protected]

Motivation and Context

See #13694 (née #9807) for motivation and context. The present PR is for feature parity on FreeBSD.

Description

Connects posix_fadvise() to dmu_prefetch().

Quite similar to Linux, but:

  • ESPIPE case is already handled by kern_posix_fadvise() function before we get here.
  • (offset, length) has already been converted to (start, end), and if length was 0 then end is OFF_MAX. Is it OK to call dmu_prefetch() with a very large size, or should we also try to clamp it to zp->z_size? It will be clamped by max prefetch size internally anyway.
  • This is only a draft because I haven't yet figured out what, if anything, needs to be done about mapped files (I don't understand why for Linux something was done before dmu_prefetch() and not after it completes, and whether that is done for correctness or performance).

How Has This Been Tested?

I have tested on top of the main branch of FreeBSD, which most recently merged OpenZFS master a few days ago (openzfs/zfs commit c629f0b). I've tested with simple programs, and PostgreSQL which generates a WILLNEED advice in various circumstances.

I wonder if we should implement POSIX_FADV_DONTNEED to evict ranges from ARC to facilitate testing, but that'd be another PR.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Performance enhancement (non-breaking change which improves efficiency)
  • Code cleanup (non-breaking change which makes code smaller or more readable)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Library ABI change (libzfs, libzfs_core, libnvpair, libuutil and libzfsbootenv)
  • Documentation (a change to man pages or other documentation)

Checklist:

@amotin amotin added the Status: Code Review Needed Ready for review and testing label May 26, 2025
@robn
Copy link
Member

robn commented May 26, 2025

I've been reading the Linux side a bit this evening, and I am almost certain that the call to generic_fadvise() is not for correctness. Maybe performance (though it looks tenuous), but not correctness.

First, generic_fadvise() wasn't available until Linux 5.4, so if it was for correctness, we're missing something there.

As for the behaviour, it seems the call does some error checks (which we also do), then gets into a switch on advice. For SEQUENTIAL, it just increases the "ideal amount of pages to readahead" (ra_pages), so it'll try and get more next time.

For WILLNEED, it calls force_page_cache_readahead(), which ends up calling zpl_readahead() to get pages in. But since we're already pulling those in, the best I think it can be is a sort of prefault - dmu_prefetch() will get stuff into the ARC, but the page readahead should also inflate it into the mapped pages, ready for use. How much does it help though, hard to say, because I guess they're going to be fighting with each other a little?

In any case, I don't think you're gonna break anything by leaving that shape out on the FreeBSD side.

@amotin amotin requested a review from behlendorf May 26, 2025 16:19
@amotin
Copy link
Member Author

amotin commented May 27, 2025

@fuporovvStack Please take a look.

1.  Instead of relying on the numerical values of POSIX_FADV_XXX macros,
    accept macro names as arguments to the file_fadvise program.  (The
    numbers happen to match on Linux and FreeBSD, but future systems may
    vary and it seems a little strange/raw to count on that.)

2.  For implementation reasons, SEQUENTIAL doesn't reach ZFS via FreeBSD
    VFS currently (perhaps something that should be investigated in
    FreeBSD).  Since on Linux we're treating SEQUENTIAL and WILLNEED the
    same, it doesn't really matter which one we use, so switch the test
    over to WILLNEED exercise the new prefetch code on both OSes the
    same way.

Reviewed-by: Brian Behlendorf <[email protected]>
Signed-off-by: Thomas Munro <[email protected]>
@amotin amotin force-pushed the fadvise-freebsd branch from 8342c2f to 69eae05 Compare May 28, 2025 18:56
As commit 320f0c6 did for Linux, connect POSIX_FADV_WILLNEED
up to dmu_prefetch() on FreeBSD.

Reviewed-by: Mateusz Guzik <[email protected]>
Reviewed-by: Alexander Motin <[email protected]>
Reviewed-by: Brian Behlendorf <[email protected]>
Signed-off-by: Thomas Munro <[email protected]>
@amotin amotin force-pushed the fadvise-freebsd branch from 69eae05 to 87dfd68 Compare May 28, 2025 19:37
Copy link
Contributor

@behlendorf behlendorf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I wondered about the call to generic_fadvise() myself, going back to the comments in the original issue it was added as a performance optimization to force populate the page cache. Which makes some sense.

If the file is mmaped, generic_fadvise is also called for page cache read-ahead besides dmu_prefetch.

So there is no correctness issue there.

@behlendorf behlendorf added Status: Accepted Ready to integrate (reviewed, tested) and removed Status: Code Review Needed Ready for review and testing labels May 28, 2025
@amotin amotin merged commit fa697b9 into openzfs:master May 29, 2025
24 checks passed
@amotin amotin deleted the fadvise-freebsd branch May 29, 2025 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Accepted Ready to integrate (reviewed, tested)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants