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

Submitted by Kirill Tkhai on May 14, 2020, 11:06 a.m.

Details

Message ID 158945439859.457137.12059992210564418293.stgit@localhost.localdomain
State New
Series "Series without cover letter"
Headers show

Commit Message

Kirill Tkhai May 14, 2020, 11:06 a.m.
Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
---
 net/packet/af_packet.c |    9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

Patch hide | download patch | download mbox

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)