Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp3263006imm; Thu, 17 May 2018 06:08:47 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpWrj0SzXrRjo5Xoqv/YP5k+X1pqPFtASyyzJaXqOiT36gDU62xtnWSDRNoDoJvZiI282uw X-Received: by 2002:a62:6642:: with SMTP id a63-v6mr5130925pfc.162.1526562527016; Thu, 17 May 2018 06:08:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526562526; cv=none; d=google.com; s=arc-20160816; b=DGCwD97GeUCrx40etVZZUoUmpZQvcTb6BXo7cKy24K7h3AStPaGokiU3oHGhkVHTWg 1rKXc7ZVRsHGOfesyn35Us2Oa/ty9KWr88Ju8/aKLpThzcJCZ1viRxCfdVpSHLLB4C2i rDtkVmCZ8H/elwhExKkyr/uFXfs3mumplDBAr790pqRlMbJSjYoxJmenpiJXt+mK0dz8 wxEkOhNTICCTeA+Px4eXJ7/aUcpZqGYOC4/NY117HYM0ZUxLCOUN5j5Q6eul/XXiD1vu WBMECDpvkaB8uQMDmOg9Xb2W2Xnb3dKzGo7y5LQIuR0CMIVOHVZBPzz2yqx2EmClyACJ HQiw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :arc-authentication-results; bh=CHchpLqEbl5ni4lyNoZQKrK/5V/RM6TCSC1mE17c/60=; b=WESRZx2YAi8UrAOULc0iakA0zYlqXSxu9+XRamQhhG7F0u3H1t1XQJmuqmL4Y8wHzi ScN+RXKfS9iJ5VErOYH37atb5xwJACe+2KmrtXRXcPks8mgFglaX20b+2riNb5nPh+Aj rJLxpeoQVsAl3buBFQq3bPBGgzONxzRknuvyLcdQLm1OgMNUK73loNGQnbxWo1zEqdYt M5QwQKRMxN4K7OQMALemccWOZT2bbiibbONXONXVEhMbUBxAs9wukNkMgylOsHsIAjyE hPipNMBHyZsZrO7Cvi58VF30mFaJCcQn7aWOlQDY11zmqfjBg0GK2mNKayDSYUKYOOff pVPg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a7-v6si2707176pgu.26.2018.05.17.06.08.32; Thu, 17 May 2018 06:08:46 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752276AbeEQNGE (ORCPT + 99 others); Thu, 17 May 2018 09:06:04 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:44454 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751619AbeEQNF7 (ORCPT ); Thu, 17 May 2018 09:05:59 -0400 Received: from pps.filterd (m0098404.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w4HD55ml047846 for ; Thu, 17 May 2018 09:05:58 -0400 Received: from e06smtp15.uk.ibm.com (e06smtp15.uk.ibm.com [195.75.94.111]) by mx0a-001b2d01.pphosted.com with ESMTP id 2j16wn2h3t-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Thu, 17 May 2018 09:05:57 -0400 Received: from localhost by e06smtp15.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 17 May 2018 14:05:53 +0100 Received: from b06cxnps4074.portsmouth.uk.ibm.com (9.149.109.196) by e06smtp15.uk.ibm.com (192.168.101.145) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 17 May 2018 14:05:50 +0100 Received: from d06av26.portsmouth.uk.ibm.com (d06av26.portsmouth.uk.ibm.com [9.149.105.62]) by b06cxnps4074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w4HD5nrQ66322482; Thu, 17 May 2018 13:05:49 GMT Received: from d06av26.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id ED6AFAE05D; Thu, 17 May 2018 13:55:08 +0100 (BST) Received: from d06av26.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 7702EAE056; Thu, 17 May 2018 13:55:07 +0100 (BST) Received: from rapoport-lnx (unknown [9.148.8.77]) by d06av26.portsmouth.uk.ibm.com (Postfix) with ESMTPS; Thu, 17 May 2018 13:55:07 +0100 (BST) Received: by rapoport-lnx (sSMTP sendmail emulation); Thu, 17 May 2018 16:05:47 +0300 From: Mike Rapoport To: Shuah Khan Cc: Roman Gushchin , Tejun Heo , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Mike Rapoport Subject: [PATCH v2] selftests: cgroup/memcontrol: add basic test for socket accounting Date: Thu, 17 May 2018 16:05:46 +0300 X-Mailer: git-send-email 2.7.4 X-TM-AS-GCONF: 00 x-cbid: 18051713-0020-0000-0000-0000041E4144 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18051713-0021-0000-0000-000042B3641D Message-Id: <1526562346-27178-1-git-send-email-rppt@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2018-05-17_07:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=2 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1709140000 definitions=main-1805170121 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Signed-off-by: Mike Rapoport Acked-by: Roman Gushchin --- v2 changes: * bumped error percentage for comparing memory.current with memory.stat.sock * added checks that memory.stat.sock == 0 and memory.current >= 0 after TCP server exits * simplified error handling tools/testing/selftests/cgroup/test_memcontrol.c | 193 +++++++++++++++++++++++ 1 file changed, 193 insertions(+) diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testing/selftests/cgroup/test_memcontrol.c index beae06c9c899..cf0bddc9d271 100644 --- a/tools/testing/selftests/cgroup/test_memcontrol.c +++ b/tools/testing/selftests/cgroup/test_memcontrol.c @@ -9,6 +9,12 @@ #include #include #include +#include +#include +#include +#include +#include +#include #include "../kselftest.h" #include "cgroup_util.h" @@ -772,6 +778,192 @@ static int test_memcg_oom_events(const char *root) return ret; } +struct tcp_server_args { + unsigned short port; + int ctl[2]; +}; + +static int tcp_server(const char *cgroup, void *arg) +{ + struct tcp_server_args *srv_args = arg; + struct sockaddr_in6 saddr = { 0 }; + socklen_t slen = sizeof(saddr); + int sk, client_sk, ctl_fd, yes = 1, ret = -1; + + close(srv_args->ctl[0]); + ctl_fd = srv_args->ctl[1]; + + saddr.sin6_family = AF_INET6; + saddr.sin6_addr = in6addr_any; + saddr.sin6_port = htons(srv_args->port); + + sk = socket(AF_INET6, SOCK_STREAM, 0); + if (sk < 0) + return ret; + + if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) + goto cleanup; + + if (bind(sk, (struct sockaddr *)&saddr, slen)) { + write(ctl_fd, &errno, sizeof(errno)); + goto cleanup; + } + + if (listen(sk, 1)) + goto cleanup; + + ret = 0; + if (write(ctl_fd, &ret, sizeof(ret)) != sizeof(ret)) { + ret = -1; + goto cleanup; + } + + client_sk = accept(sk, NULL, NULL); + if (client_sk < 0) + goto cleanup; + + ret = -1; + for (;;) { + uint8_t buf[0x100000]; + + if (write(client_sk, buf, sizeof(buf)) <= 0) { + if (errno == ECONNRESET) + ret = 0; + break; + } + } + + close(client_sk); + +cleanup: + close(sk); + return ret; +} + +static int tcp_client(const char *cgroup, unsigned short port) +{ + const char server[] = "localhost"; + struct addrinfo *ai; + char servport[6]; + int retries = 0x10; /* nice round number */ + int sk, ret; + + snprintf(servport, sizeof(servport), "%hd", port); + ret = getaddrinfo(server, servport, NULL, &ai); + if (ret) + return ret; + + sk = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); + if (sk < 0) + goto free_ainfo; + + ret = connect(sk, ai->ai_addr, ai->ai_addrlen); + if (ret < 0) + goto close_sk; + + ret = KSFT_FAIL; + while (retries--) { + uint8_t buf[0x100000]; + long current, sock; + + if (read(sk, buf, sizeof(buf)) <= 0) + goto close_sk; + + current = cg_read_long(cgroup, "memory.current"); + sock = cg_read_key_long(cgroup, "memory.stat", "sock "); + + if (current < 0 || sock < 0) + goto close_sk; + + if (current < sock) + goto close_sk; + + if (values_close(current, sock, 10)) { + ret = KSFT_PASS; + break; + } + } + +close_sk: + close(sk); +free_ainfo: + freeaddrinfo(ai); + return ret; +} + +/* + * This test checks socket memory accounting. + * The test forks a TCP server listens on a random port between 1000 + * and 61000. Once it gets a client connection, it starts writing to + * its socket. + * The TCP client interleaves reads from the socket with check whether + * memory.current and memory.stat.sock are similar. + */ +static int test_memcg_sock(const char *root) +{ + int bind_retries = 5, ret = KSFT_FAIL, pid, err; + unsigned short port; + char *memcg; + + memcg = cg_name(root, "memcg_test"); + if (!memcg) + goto cleanup; + + if (cg_create(memcg)) + goto cleanup; + + while (bind_retries--) { + struct tcp_server_args args; + + if (pipe(args.ctl)) + goto cleanup; + + port = args.port = 1000 + rand() % 60000; + + pid = cg_run_nowait(memcg, tcp_server, &args); + if (pid < 0) + goto cleanup; + + close(args.ctl[1]); + if (read(args.ctl[0], &err, sizeof(err)) != sizeof(err)) + goto cleanup; + close(args.ctl[0]); + + if (!err) + break; + if (err != EADDRINUSE) + goto cleanup; + + waitpid(pid, NULL, 0); + } + + if (err == EADDRINUSE) { + ret = KSFT_SKIP; + goto cleanup; + } + + if (tcp_client(memcg, port) != KSFT_PASS) + goto cleanup; + + waitpid(pid, &err, 0); + if (WEXITSTATUS(err)) + goto cleanup; + + if (cg_read_long(memcg, "memory.current") < 0) + goto cleanup; + + if (cg_read_key_long(memcg, "memory.stat", "sock ")) + goto cleanup; + + ret = KSFT_PASS; + +cleanup: + cg_destroy(memcg); + free(memcg); + + return ret; +} + #define T(x) { x, #x } struct memcg_test { int (*fn)(const char *root); @@ -785,6 +977,7 @@ struct memcg_test { T(test_memcg_max), T(test_memcg_oom_events), T(test_memcg_swap_max), + T(test_memcg_sock), }; #undef T -- 2.7.4