Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F167937914
D25787.id74866.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
5 KB
Referenced Files
None
Subscribers
None
D25787.id74866.diff
View Options
Index: sys/vm/swap_pager.c
===================================================================
--- sys/vm/swap_pager.c
+++ sys/vm/swap_pager.c
@@ -202,101 +202,141 @@
return (sysctl_handle_64(oidp, &newval, 0, req));
}
-int
+static bool
+swap_reserve_by_cred_rlimit(u_long pincr, struct ucred *cred, int oc)
+{
+ struct uidinfo *uip;
+ u_long prev;
+
+ uip = cred->cr_ruidinfo;
+
+ prev = atomic_fetchadd_long(&uip->ui_vmsize, pincr);
+ if ((oc & SWAP_RESERVE_RLIMIT_ON) != 0 &&
+ prev + pincr > lim_cur(curthread, RLIMIT_SWAP) &&
+ priv_check(curthread, PRIV_VM_SWAP_NORLIMIT)) {
+ prev = atomic_fetchadd_long(&uip->ui_vmsize, -pincr);
+ if (prev < pincr)
+ panic("uip->ui_vmsize < incr on overcommit fail");
+ return (false);
+ }
+ return (true);
+}
+
+static void
+swap_release_by_cred_rlimit(u_long pdecr, struct ucred *cred)
+{
+ struct uidinfo *uip;
+ u_long prev;
+
+ uip = cred->cr_ruidinfo;
+
+ prev = atomic_fetchadd_long(&uip->ui_vmsize, -pdecr);
+ if (prev < pdecr)
+ printf("negative vmsize for uid = %d\n", uip->ui_uid);
+}
+
+static void
+swap_reserve_force_rlimit(u_long pincr, struct ucred *cred)
+{
+ struct uidinfo *uip;
+
+ uip = cred->cr_ruidinfo;
+ atomic_add_long(&uip->ui_vmsize, pincr);
+}
+
+bool
swap_reserve(vm_ooffset_t incr)
{
return (swap_reserve_by_cred(incr, curthread->td_ucred));
}
-int
+bool
swap_reserve_by_cred(vm_ooffset_t incr, struct ucred *cred)
{
u_long r, s, prev, pincr;
- int res, error;
+#ifdef RACCT
+ int error;
+#endif
static int curfail;
static struct timeval lastfail;
- struct uidinfo *uip;
-
- uip = cred->cr_ruidinfo;
+ int oc;
KASSERT((incr & PAGE_MASK) == 0, ("%s: incr: %ju & PAGE_MASK", __func__,
(uintmax_t)incr));
#ifdef RACCT
- if (racct_enable) {
+ if (RACCT_ENABLED()) {
PROC_LOCK(curproc);
error = racct_add(curproc, RACCT_SWAP, incr);
PROC_UNLOCK(curproc);
if (error != 0)
- return (0);
+ return (false);
}
#endif
pincr = atop(incr);
- res = 0;
prev = atomic_fetchadd_long(&swap_reserved, pincr);
r = prev + pincr;
- if (overcommit & SWAP_RESERVE_ALLOW_NONWIRED) {
- s = vm_cnt.v_page_count - vm_cnt.v_free_reserved -
- vm_wire_count();
- } else
- s = 0;
- s += swap_total;
- if ((overcommit & SWAP_RESERVE_FORCE_ON) == 0 || r <= s ||
- (error = priv_check(curthread, PRIV_VM_SWAP_NOQUOTA)) == 0) {
- res = 1;
- } else {
+ s = swap_total;
+ oc = atomic_load_int(&overcommit);
+ if (r > s) {
+ if (oc & SWAP_RESERVE_ALLOW_NONWIRED) {
+ s += vm_cnt.v_page_count - vm_cnt.v_free_reserved -
+ vm_wire_count();
+ }
+ }
+ if ((oc & SWAP_RESERVE_FORCE_ON) != 0 && r > s &&
+ priv_check(curthread, PRIV_VM_SWAP_NOQUOTA) != 0) {
prev = atomic_fetchadd_long(&swap_reserved, -pincr);
if (prev < pincr)
panic("swap_reserved < incr on overcommit fail");
+ goto out_error;
}
- if (res) {
- prev = atomic_fetchadd_long(&uip->ui_vmsize, pincr);
- if ((overcommit & SWAP_RESERVE_RLIMIT_ON) != 0 &&
- prev + pincr > lim_cur(curthread, RLIMIT_SWAP) &&
- priv_check(curthread, PRIV_VM_SWAP_NORLIMIT)) {
- res = 0;
- prev = atomic_fetchadd_long(&uip->ui_vmsize, -pincr);
- if (prev < pincr)
- panic("uip->ui_vmsize < incr on overcommit fail");
- }
+
+ if (!swap_reserve_by_cred_rlimit(pincr, cred, oc)) {
+ prev = atomic_fetchadd_long(&swap_reserved, -pincr);
+ if (prev < pincr)
+ panic("swap_reserved < incr on overcommit fail");
+ goto out_error;
}
- if (!res && ppsratecheck(&lastfail, &curfail, 1)) {
+
+ return (true);
+
+out_error:
+ if (ppsratecheck(&lastfail, &curfail, 1)) {
printf("uid %d, pid %d: swap reservation for %jd bytes failed\n",
- uip->ui_uid, curproc->p_pid, incr);
+ cred->cr_ruidinfo->ui_uid, curproc->p_pid, incr);
}
-
#ifdef RACCT
- if (racct_enable && !res) {
+ if (RACCT_ENABLED()) {
PROC_LOCK(curproc);
racct_sub(curproc, RACCT_SWAP, incr);
PROC_UNLOCK(curproc);
}
#endif
- return (res);
+ return (false);
}
void
swap_reserve_force(vm_ooffset_t incr)
{
- struct uidinfo *uip;
u_long pincr;
KASSERT((incr & PAGE_MASK) == 0, ("%s: incr: %ju & PAGE_MASK", __func__,
(uintmax_t)incr));
- PROC_LOCK(curproc);
#ifdef RACCT
- if (racct_enable)
+ if (RACCT_ENABLED()) {
+ PROC_LOCK(curproc);
racct_add_force(curproc, RACCT_SWAP, incr);
+ PROC_UNLOCK(curproc);
+ }
#endif
pincr = atop(incr);
atomic_add_long(&swap_reserved, pincr);
- uip = curproc->p_ucred->cr_ruidinfo;
- atomic_add_long(&uip->ui_vmsize, pincr);
- PROC_UNLOCK(curproc);
+ swap_reserve_force_rlimit(pincr, curthread->td_ucred);
}
void
@@ -314,9 +354,6 @@
swap_release_by_cred(vm_ooffset_t decr, struct ucred *cred)
{
u_long prev, pdecr;
- struct uidinfo *uip;
-
- uip = cred->cr_ruidinfo;
KASSERT((decr & PAGE_MASK) == 0, ("%s: decr: %ju & PAGE_MASK", __func__,
(uintmax_t)decr));
@@ -326,9 +363,7 @@
if (prev < pdecr)
panic("swap_reserved < decr");
- prev = atomic_fetchadd_long(&uip->ui_vmsize, -pdecr);
- if (prev < pdecr)
- printf("negative vmsize for uid = %d\n", uip->ui_uid);
+ swap_release_by_cred_rlimit(pdecr, cred);
#ifdef RACCT
if (racct_enable)
racct_sub_cred(cred, RACCT_SWAP, decr);
Index: sys/vm/vm.h
===================================================================
--- sys/vm/vm.h
+++ sys/vm/vm.h
@@ -151,8 +151,8 @@
extern int vm_ndomains;
struct ucred;
-int swap_reserve(vm_ooffset_t incr);
-int swap_reserve_by_cred(vm_ooffset_t incr, struct ucred *cred);
+bool swap_reserve(vm_ooffset_t incr);
+bool swap_reserve_by_cred(vm_ooffset_t incr, struct ucred *cred);
void swap_reserve_force(vm_ooffset_t incr);
void swap_release(vm_ooffset_t decr);
void swap_release_by_cred(vm_ooffset_t decr, struct ucred *cred);
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Wed, Aug 26, 12:48 PM (1 m, 22 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
37297220
Default Alt Text
D25787.id74866.diff (5 KB)
Attached To
Mode
D25787: vm: fix swap reservation leak
Attached
Detach File
Event Timeline
Log In to Comment