Skip to content

Commit 7cb0240

Browse files
Mel Gormantorvalds
authored andcommitted
netvm: allow the use of __GFP_MEMALLOC by specific sockets
Allow specific sockets to be tagged SOCK_MEMALLOC and use __GFP_MEMALLOC for their allocations. These sockets will be able to go below watermarks and allocate from the emergency reserve. Such sockets are to be used to service the VM (iow. to swap over). They must be handled kernel side, exposing such a socket to user-space is a bug. There is a risk that the reserves be depleted so for now, the administrator is responsible for increasing min_free_kbytes as necessary to prevent deadlock for their workloads. [a.p.zijlstra@chello.nl: Original patches] Signed-off-by: Mel Gorman <mgorman@suse.de> Acked-by: David S. Miller <davem@davemloft.net> Cc: Neil Brown <neilb@suse.de> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Mike Christie <michaelc@cs.wisc.edu> Cc: Eric B Munson <emunson@mgebm.net> Cc: Eric Dumazet <eric.dumazet@gmail.com> Cc: Sebastian Andrzej Siewior <sebastian@breakpoint.cc> Cc: Mel Gorman <mgorman@suse.de> Cc: Christoph Lameter <cl@linux.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 99a1dec commit 7cb0240

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

include/net/sock.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,7 @@ enum sock_flags {
621621
SOCK_RCVTSTAMPNS, /* %SO_TIMESTAMPNS setting */
622622
SOCK_LOCALROUTE, /* route locally only, %SO_DONTROUTE setting */
623623
SOCK_QUEUE_SHRUNK, /* write queue has been shrunk recently */
624+
SOCK_MEMALLOC, /* VM depends on this socket for swapping */
624625
SOCK_TIMESTAMPING_TX_HARDWARE, /* %SOF_TIMESTAMPING_TX_HARDWARE */
625626
SOCK_TIMESTAMPING_TX_SOFTWARE, /* %SOF_TIMESTAMPING_TX_SOFTWARE */
626627
SOCK_TIMESTAMPING_RX_HARDWARE, /* %SOF_TIMESTAMPING_RX_HARDWARE */
@@ -660,7 +661,7 @@ static inline bool sock_flag(const struct sock *sk, enum sock_flags flag)
660661

661662
static inline gfp_t sk_gfp_atomic(struct sock *sk, gfp_t gfp_mask)
662663
{
663-
return GFP_ATOMIC;
664+
return GFP_ATOMIC | (sk->sk_allocation & __GFP_MEMALLOC);
664665
}
665666

666667
static inline void sk_acceptq_removed(struct sock *sk)
@@ -803,6 +804,8 @@ extern int sk_stream_wait_memory(struct sock *sk, long *timeo_p);
803804
extern void sk_stream_wait_close(struct sock *sk, long timeo_p);
804805
extern int sk_stream_error(struct sock *sk, int flags, int err);
805806
extern void sk_stream_kill_queues(struct sock *sk);
807+
extern void sk_set_memalloc(struct sock *sk);
808+
extern void sk_clear_memalloc(struct sock *sk);
806809

807810
extern int sk_wait_data(struct sock *sk, long *timeo);
808811

net/core/sock.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,28 @@ __u32 sysctl_rmem_default __read_mostly = SK_RMEM_MAX;
271271
int sysctl_optmem_max __read_mostly = sizeof(unsigned long)*(2*UIO_MAXIOV+512);
272272
EXPORT_SYMBOL(sysctl_optmem_max);
273273

274+
/**
275+
* sk_set_memalloc - sets %SOCK_MEMALLOC
276+
* @sk: socket to set it on
277+
*
278+
* Set %SOCK_MEMALLOC on a socket for access to emergency reserves.
279+
* It's the responsibility of the admin to adjust min_free_kbytes
280+
* to meet the requirements
281+
*/
282+
void sk_set_memalloc(struct sock *sk)
283+
{
284+
sock_set_flag(sk, SOCK_MEMALLOC);
285+
sk->sk_allocation |= __GFP_MEMALLOC;
286+
}
287+
EXPORT_SYMBOL_GPL(sk_set_memalloc);
288+
289+
void sk_clear_memalloc(struct sock *sk)
290+
{
291+
sock_reset_flag(sk, SOCK_MEMALLOC);
292+
sk->sk_allocation &= ~__GFP_MEMALLOC;
293+
}
294+
EXPORT_SYMBOL_GPL(sk_clear_memalloc);
295+
274296
#if defined(CONFIG_CGROUPS)
275297
#if !defined(CONFIG_NET_CLS_CGROUP)
276298
int net_cls_subsys_id = -1;

0 commit comments

Comments
 (0)