Page MenuHomeFreeBSD

libuvmem: usermode port of vmem(9)
ClosedPublic

Authored by kib on Nov 14 2020, 10:13 PM.
Tags
None
Referenced Files
Unknown Object (File)
Tue, Nov 18, 10:13 AM
Unknown Object (File)
Mon, Nov 17, 2:03 PM
Unknown Object (File)
Mon, Nov 17, 1:22 PM
Unknown Object (File)
Mon, Nov 17, 12:51 PM
Unknown Object (File)
Mon, Nov 17, 9:19 AM
Unknown Object (File)
Mon, Nov 17, 8:23 AM
Unknown Object (File)
Mon, Nov 17, 8:07 AM
Unknown Object (File)
Mon, Nov 17, 7:58 AM

Details

Summary

The quantum cache is disabled, there is no uma.

Intent is to use this for resource allocation in bhyve(8), for start. Addition of -luvmem to bhyve linking was done to test changes to share/mk.

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

There are a very large number of changes, so older changes are hidden. Show Older Changes
sys/kern/subr_vmem.c
1467

Same comment as above wrt local variables.

1573

This block should probably be formally protected with ifdef _KERNEL as well.

This revision is now accepted and ready to land.Nov 16 2020, 4:41 PM
sys/kern/subr_vmem.c
1329

I agree in general, and if this were some special case (e.g. INVARIANTS) instead of the usual case I'd argue strongly for markj's take.

kib marked 7 inline comments as done.Nov 17 2020, 1:32 AM
kib added inline comments.
sys/kern/subr_vmem.c
1258

To not add __unused declaration, I think it is logically wrong.

1458

There were only two of them so I did not bothered.

This revision was automatically updated to reflect the committed changes.
kib marked 2 inline comments as done.
kib updated this revision to Diff 79634.

Rebase after commit of warning fixes.
Handle Mark notes.

markj added inline comments.
sys/contrib/openzfs/include/sys/zfs_context.h
53 ↗(On Diff #79634)

Is this just a reminder to upstream the change? Or is there another problem?

sys/kern/subr_vmem.c
1258

I think (void)qcache_max; is more common for this scenario.

This revision is now accepted and ready to land.Nov 17 2020, 2:38 PM

Add M_*FIT bits to sys/vmem.h for userspace.

This revision now requires review to proceed.Nov 19 2020, 5:37 PM

Hi, is there a concrete reason why this revision never landed or has it just slipped through the cracks?

I would really like to use libuvmem for one of my ongoing bhyve projects and would like to see this land, let me know if there a way I could help with that.

Hi, is there a concrete reason why this revision never landed or has it just slipped through the cracks?

I would really like to use libuvmem for one of my ongoing bhyve projects and would like to see this land, let me know if there a way I could help with that.

The intent of the libuvmem port was to use vmem allocator for bhyve to handle physical address space. That stalled, and I do not see a reason to add unused library to base.
If you need it, I am more than happy to push the commit.

Meantime I rebased the branch.

Rebase. Use FreeBSD 16.0 symbol version for the new lib.

Remove undefined symbols from the version script. The library should be buildable, at least.

Use standard way to reference libc src dir.
Silence compiler warning in _bt_fill() about flags.

sys/contrib/openzfs/include/sys/zfs_context.h
53 ↗(On Diff #79634)

AFAIR it did not work, there was some conflict because solaris shims also provided sys/vmem.h.

sys/kern/subr_vmem.c
1312

Maybe add macros for these list locks, like we have for other locks?

kib marked an inline comment as done.

VMEM_LIST_LOCK()

kib marked 6 inline comments as done.Oct 14 2025, 5:23 PM
In D27220#1212532, @kib wrote:

Hi, is there a concrete reason why this revision never landed or has it just slipped through the cracks?

I would really like to use libuvmem for one of my ongoing bhyve projects and would like to see this land, let me know if there a way I could help with that.

The intent of the libuvmem port was to use vmem allocator for bhyve to handle physical address space. That stalled, and I do not see a reason to add unused library to base.

Is there a revision with the stalled patches somewhere? I'd be interested in taking a look.

If you need it, I am more than happy to push the commit.

That would be amazing, thank you!
It would really help with managing PCI BAR allocations for my ongoing device hotplugging work.

In D27220#1212532, @kib wrote:

Hi, is there a concrete reason why this revision never landed or has it just slipped through the cracks?

I would really like to use libuvmem for one of my ongoing bhyve projects and would like to see this land, let me know if there a way I could help with that.

The intent of the libuvmem port was to use vmem allocator for bhyve to handle physical address space. That stalled, and I do not see a reason to add unused library to base.

Is there a revision with the stalled patches somewhere? I'd be interested in taking a look.

I suspect none survived, since I only found this libuvmem diff itself on that branch.

If you need it, I am more than happy to push the commit.

That would be amazing, thank you!
It would really help with managing PCI BAR allocations for my ongoing device hotplugging work.

Yes this is exactly the place where libuvmem should fit perfectly.

Looks ok to me. Perhaps this should be a private library instead?

lib/libuvmem/Makefile
1 ↗(On Diff #164155)

Extra newline.

7 ↗(On Diff #164155)
This revision is now accepted and ready to land.Oct 15 2025, 1:18 PM
kib marked 2 inline comments as done.Oct 15 2025, 1:45 PM

Looks ok to me. Perhaps this should be a private library instead?

IMO it is generally useful API. Also, I do provide symbol versioning, which does not make sense for a private library: the intent is to keep stable interfaces.

This revision now requires review to proceed.Oct 15 2025, 1:45 PM
In D27220#1213306, @kib wrote:

Looks ok to me. Perhaps this should be a private library instead?

IMO it is generally useful API. Also, I do provide symbol versioning, which does not make sense for a private library: the intent is to keep stable interfaces.

Well, now the kernel's internal interface must be kept stable as well. I don't object, but I'm worried it'll be easy for someone modifying vmem to forget about this.

If the library is public, it should get a man page IMO, even if it just points at vmem(9).

In D27220#1213306, @kib wrote:

Looks ok to me. Perhaps this should be a private library instead?

IMO it is generally useful API. Also, I do provide symbol versioning, which does not make sense for a private library: the intent is to keep stable interfaces.

Well, now the kernel's internal interface must be kept stable as well. I don't object, but I'm worried it'll be easy for someone modifying vmem to forget about this.

Yes, I noted the same when parts of msdosfs were used in makefs (AFAIR). But there it is much simpler interface, and if the interface for kernel or userspace starts drifting, the code can be easily forked to the residual part.

In D27220#1213582, @kib wrote:
In D27220#1213306, @kib wrote:

Looks ok to me. Perhaps this should be a private library instead?

IMO it is generally useful API. Also, I do provide symbol versioning, which does not make sense for a private library: the intent is to keep stable interfaces.

Well, now the kernel's internal interface must be kept stable as well. I don't object, but I'm worried it'll be easy for someone modifying vmem to forget about this.

Yes, I noted the same when parts of msdosfs were used in makefs (AFAIR). But there it is much simpler interface, and if the interface for kernel or userspace starts drifting, the code can be easily forked to the residual part.

My point is that no automation (e.g., tests) will catch this drift until after it is a problem for some hypothetical users. If makefs -t msdos is broken somehow, we will notice fairly quickly.

If Bojan's work lands in bhyve, maybe that will be enough.

lib/libuvmem/libuvmem.3
47 ↗(On Diff #164268)
63 ↗(On Diff #164268)

You might note that the allocator is thread-safe.

This revision is now accepted and ready to land.Oct 16 2025, 1:27 PM

One minor thing I've noticed is that we don't export M_NOWAIT.
I think it would be very useful to have a way of controlling whether we wait or exit on failure, but I'm not sure if accomplishing that is as straightforward as defining M_NOWAIT in sys/vmem.h.

sys/kern/subr_vmem.c
1250–1251

I hit an error caused by this pretty early on.

Because the assertion above enforces that the quantum should always be 0, the resulting quantum mask will be set to -1 (all F's) which will cause vmem_roundup_size to always return 0.
This locks vmem_xalloc into either an infinite loop or causes it to block indefinitely.

kib marked 2 inline comments as done.Oct 17 2025, 12:01 AM

One minor thing I've noticed is that we don't export M_NOWAIT.
I think it would be very useful to have a way of controlling whether we wait or exit on failure, but I'm not sure if accomplishing that is as straightforward as defining M_NOWAIT in sys/vmem.h.

It should be, at least lets try it that way. I added M_NOWAIT into !_KERNEL block of sys/vmem.h. Also I inspected subr_vmem.c and M_WAITOK seems to be only used by the kernel blocks.

sys/kern/subr_vmem.c
1250–1251

Thanks. I handled it differently, forcing quantum = 1 in the initial ifdef block in vmem_init().

Set quantum to 1.
Allow M_NOWAIT
List flags for vmem_xalloc() in man page.
Mention thread-safety in man page.
Remove unused VMEM_TRYLOCK()

This revision now requires review to proceed.Oct 17 2025, 12:02 AM
kib marked an inline comment as done.Sat, Nov 29, 6:36 PM

Any progress with testing?

lib/libuvmem/libuvmem.3
9 ↗(On Diff #164354)

You can omit this license text now if you want.

35 ↗(On Diff #164354)

This doesn't need to be quoted.

36 ↗(On Diff #164354)

The library section is deprecated, current advice is to put the Lb macro line immediately after the Sh SYNOPSIS line.

kib marked 2 inline comments as done.Sat, Nov 29, 7:09 PM
kib added inline comments.
lib/libuvmem/libuvmem.3
36 ↗(On Diff #164354)

The result of this looks extremely weird. Where this advise came from?

lib/libuvmem/libuvmem.3
36 ↗(On Diff #164354)

It comes from the mdoc(7) manual [^0]. I imported this change from upstream mdoc.7 version 1.298 [^1] which says:

LIBRARY
	     The name of the library containing	the documented functions.  Us-
	     ing this section is no longer recommended.	 If any	 Lb  macro  is
	     needed, put it at the beginning of	the SYNOPSIS section instead.

It seems to have first been proposed on the groff mailing list [^2].

I tried to notify everyone with a mail to freebsd-current@ [^3] in June when when I imported the change in commit 4c07abdbacf4 [^4], but nobody said anything.

[^0]: https://man.freebsd.org/cgi/man.cgi?query=mdoc&apropos=0&sektion=0&manpath=FreeBSD+16.0-CURRENT&arch=default&format=html
[^1]: https://cvsweb.bsd.lv/mandoc/mdoc.7?rev=1.298&content-type=text/x-cvsweb-markup&sortby=date
[^2]: https://lists.gnu.org/archive/html/groff/2025-06/msg00004.html
[^3]: https://lists.freebsd.org/archives/freebsd-current/2025-June/007907.html
[^4]: https://cgit.freebsd.org/src/log/?qt=range&q=4c07abdbacf4&showmsg=1

kib marked 2 inline comments as done.Sat, Nov 29, 11:21 PM
kib added inline comments.
lib/libuvmem/libuvmem.3
36 ↗(On Diff #164354)

Not that I like it.

kib marked an inline comment as done.

Some tweaks to the man page

In D27220#1233531, @kib wrote:

Any progress with testing?

Yes, I have reworked bhyve's PCI BAR allocator to use libuvmem and it works flawlessly.
I did not run into any vmem-related issues over the last couple of weeks while developing and testing my PCI hotplugging patches, so as far as I'm concerned this revision is good to go.

This revision is now accepted and ready to land.Mon, Dec 1, 8:38 PM