[Devel] [PATCH rh7 1/2] packet: Never charge 0 kmem in packet_sk_charge()

Kirill Tkhai ktkhai at virtuozzo.com
Thu May 14 14:06:40 MSK 2020


Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
---
 net/packet/af_packet.c |    9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
index 30b8e900e936..1e4d017d851e 100644
--- a/net/packet/af_packet.c
+++ b/net/packet/af_packet.c
@@ -2547,8 +2547,15 @@ struct packet_sk_charge {
 static struct cg_proto *packet_sk_charge(void)
 {
 	struct packet_sk_charge *psc;
+	unsigned int nr_pages;
 	int err = -ENOMEM;
 
+	nr_pages = DIV_ROUND_UP(ACCESS_ONCE(sysctl_rmem_max), PAGE_SIZE);
+	if (!nr_pages) {
+		/* Caller will assign NULL memcg to socket */
+		return NULL;
+	}
+
 	psc = kmalloc(sizeof(*psc), GFP_KERNEL);
 	if (!psc)
 		goto out;
@@ -2565,7 +2572,7 @@ static struct cg_proto *packet_sk_charge(void)
 	 * It's typically not huge and packet sockets are rare guests in
 	 * containers, so we don't disturb the memory consumption much.
 	 */
-	psc->nr_pages = ACCESS_ONCE(sysctl_rmem_max)/PAGE_SIZE;
+	psc->nr_pages = nr_pages;
 
 	err = memcg_charge_kmem(psc->memcg, GFP_KERNEL, psc->nr_pages);
 	if (!err)




More information about the Devel mailing list