Skip to content

Commit

Permalink
kernel: add RLIMIT_PIPEBUF
Browse files Browse the repository at this point in the history
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D46619
  • Loading branch information
kostikbel committed Sep 20, 2024
1 parent 0ecbb28 commit 3458bbd
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
7 changes: 7 additions & 0 deletions sys/kern/kern_resource.c
Original file line number Diff line number Diff line change
Expand Up @@ -1607,3 +1607,10 @@ chgumtxcnt(struct uidinfo *uip, int diff, rlim_t max)

return (chglimit(uip, &uip->ui_umtxcnt, diff, max, "umtxcnt"));
}

int
chgpipecnt(struct uidinfo *uip, int diff, rlim_t max)
{

return (chglimit(uip, &uip->ui_pipecnt, diff, max, "pipecnt"));
}
16 changes: 16 additions & 0 deletions sys/kern/sys_pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ pipe_paircreate(struct thread *td, struct pipepair **p_pp)
#endif
rpipe = &pp->pp_rpipe;
wpipe = &pp->pp_wpipe;
pp->pp_owner = crhold(td->td_ucred);

knlist_init_mtx(&rpipe->pipe_sel.si_note, PIPE_MTX(rpipe));
knlist_init_mtx(&wpipe->pipe_sel.si_note, PIPE_MTX(wpipe));
Expand Down Expand Up @@ -408,6 +409,7 @@ pipe_paircreate(struct thread *td, struct pipepair **p_pp)
fail:
knlist_destroy(&rpipe->pipe_sel.si_note);
knlist_destroy(&wpipe->pipe_sel.si_note);
crfree(pp->pp_owner);
#ifdef MAC
mac_pipe_destroy(pp);
#endif
Expand Down Expand Up @@ -574,9 +576,20 @@ pipespace_new(struct pipe *cpipe, int size)
size = round_page(size);
buffer = (caddr_t) vm_map_min(pipe_map);

if (!chgpipecnt(cpipe->pipe_pair->pp_owner->cr_ruidinfo,
size, lim_cur(curthread, RLIMIT_PIPEBUF))) {
if (cpipe->pipe_buffer.buffer == NULL &&
size > SMALL_PIPE_SIZE) {
size = SMALL_PIPE_SIZE;
goto retry;
}
return (ENOMEM);
}

error = vm_map_find(pipe_map, NULL, 0, (vm_offset_t *)&buffer, size, 0,
VMFS_ANY_SPACE, VM_PROT_RW, VM_PROT_RW, 0);
if (error != KERN_SUCCESS) {
chgpipecnt(cpipe->pipe_pair->pp_owner->cr_ruidinfo, -size, 0);
if (cpipe->pipe_buffer.buffer == NULL &&
size > SMALL_PIPE_SIZE) {
size = SMALL_PIPE_SIZE;
Expand Down Expand Up @@ -1645,6 +1658,8 @@ pipe_free_kmem(struct pipe *cpipe)

if (cpipe->pipe_buffer.buffer != NULL) {
atomic_subtract_long(&amountpipekva, cpipe->pipe_buffer.size);
chgpipecnt(cpipe->pipe_pair->pp_owner->cr_uidinfo,
-cpipe->pipe_buffer.size, 0);
vm_map_remove(pipe_map,
(vm_offset_t)cpipe->pipe_buffer.buffer,
(vm_offset_t)cpipe->pipe_buffer.buffer + cpipe->pipe_buffer.size);
Expand Down Expand Up @@ -1731,6 +1746,7 @@ pipeclose(struct pipe *cpipe)
*/
if (ppipe->pipe_present == PIPE_FINALIZED) {
PIPE_UNLOCK(cpipe);
crfree(cpipe->pipe_pair->pp_owner);
#ifdef MAC
mac_pipe_destroy(pp);
#endif
Expand Down
1 change: 1 addition & 0 deletions sys/sys/pipe.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ struct pipepair {
struct pipe pp_wpipe;
struct mtx pp_mtx;
struct label *pp_label;
struct ucred *pp_owner; /* to dec pipe usage count */
};

#define PIPE_MTX(pipe) (&(pipe)->pipe_pair->pp_mtx)
Expand Down
3 changes: 2 additions & 1 deletion sys/sys/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ struct __wrusage {
#define RLIMIT_SWAP 12 /* swap used */
#define RLIMIT_KQUEUES 13 /* kqueues allocated */
#define RLIMIT_UMTXP 14 /* process-shared umtx */
#define RLIMIT_PIPEBUF 15 /* pipes/fifos buffers */

#define RLIM_NLIMITS 15 /* number of resource limits */
#define RLIM_NLIMITS 16 /* number of resource limits */

#define RLIM_INFINITY ((rlim_t)(((__uint64_t)1 << 63) - 1))
#define RLIM_SAVED_MAX RLIM_INFINITY
Expand Down
2 changes: 2 additions & 0 deletions sys/sys/resourcevar.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ struct uidinfo {
long ui_ptscnt; /* (b) number of pseudo-terminals */
long ui_kqcnt; /* (b) number of kqueues */
long ui_umtxcnt; /* (b) number of shared umtxs */
long ui_pipecnt; /* (b) consumption of pipe buffers */
uid_t ui_uid; /* (a) uid */
u_int ui_ref; /* (b) reference count */
#ifdef RACCT
Expand All @@ -142,6 +143,7 @@ int chgsbsize(struct uidinfo *uip, u_int *hiwat, u_int to,
rlim_t maxval);
int chgptscnt(struct uidinfo *uip, int diff, rlim_t maxval);
int chgumtxcnt(struct uidinfo *uip, int diff, rlim_t maxval);
int chgpipecnt(struct uidinfo *uip, int diff, rlim_t max);
int kern_proc_setrlimit(struct thread *td, struct proc *p, u_int which,
struct rlimit *limp);
struct plimit
Expand Down

0 comments on commit 3458bbd

Please sign in to comment.