Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp14693imm; Tue, 22 May 2018 13:08:38 -0700 (PDT) X-Google-Smtp-Source: AB8JxZoJWwVUgGsb4fwk/M4NgV/5+wTDFgQEtCt6Q9pO7xGtyetD4O6hf95EwHAnm55M6yPv5EHO X-Received: by 2002:a65:5382:: with SMTP id x2-v6mr9299192pgq.160.1527019718686; Tue, 22 May 2018 13:08:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527019718; cv=none; d=google.com; s=arc-20160816; b=j+Wy73UmoVfZAtZY5To1VKbgKIADIDbeFDKKIuLcMiIKnEYrcyCNnYGmh3lSqSnXSp IcRAQcg+nXYUb6ZiPdMlh6kK8OsgMaL5c/Rh0XOadhdB47gxJv0LpjtTOGB1yIdlOpM5 mcZKzg5/azmjHAufxL33lBOXl7M+Tmh5WASUUwe5VnQ0CyxBmpJgYP4DJICnKgc+hcel uTPEorm8r0bTZOLhdgWESqWFVWtMWMlyw7z/njfXrT0uRBbb2xKaZO6i++LykpHRMGzv A2JGWRzQ4M5e8m0DEgowpO2SI+KZLj3GUkR/WHDE00A3TDV73FlwTd9PniRscJI8gWJk S4qg== 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=hQ5uqAUE1QtospW0b+AARZnZpLPYLPomJLRIOXzeteo=; b=oIpIBkksgmTVCS28GwNkJ8zQjACOO8M+JN6TmzXs8swb1OmcxRxnTtWnbDOLkYDZ/5 tL17KM4yclqt49A0vHP/DNp6g3NMwXQxrUazsbfwW6FU2cjSVW+E4r/KxXN/yhk0+PqX 39IyJOBrQm3CyxjCGdcTY3uUXQtSX6fRcYAhZLplrnO0FfNplR1CtOpTP45eGMmEi03l 3mxIxR0ur2KTgwnWpncsRRo5/b41EMeDkvJl8/azJNF3s89g5Jpp76WZt8vPOtxqiD4W CEqDO/9fwrUiCDEE9TS4PJdwUSwDRDDgRHGFjAu8ONC8Q8PmpfmOjr1vJA7OVo2sR1pS QMOA== 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 d9-v6si13231084pga.192.2018.05.22.13.08.10; Tue, 22 May 2018 13:08:38 -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 S1752697AbeEVUGS (ORCPT + 99 others); Tue, 22 May 2018 16:06:18 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:49074 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751317AbeEVUGQ (ORCPT ); Tue, 22 May 2018 16:06:16 -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 w4MK4F3X098668 for ; Tue, 22 May 2018 16:06:16 -0400 Received: from e06smtp10.uk.ibm.com (e06smtp10.uk.ibm.com [195.75.94.106]) by mx0a-001b2d01.pphosted.com with ESMTP id 2j4qqv5nsx-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 22 May 2018 16:06:15 -0400 Received: from localhost by e06smtp10.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 22 May 2018 21:06:13 +0100 Received: from b06cxnps4074.portsmouth.uk.ibm.com (9.149.109.196) by e06smtp10.uk.ibm.com (192.168.101.140) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 22 May 2018 21:06:10 +0100 Received: from d06av23.portsmouth.uk.ibm.com (d06av23.portsmouth.uk.ibm.com [9.149.105.59]) by b06cxnps4074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w4MK69Ma7668084; Tue, 22 May 2018 20:06:09 GMT Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 311A4A404D; Tue, 22 May 2018 20:57:31 +0100 (BST) Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 925D3A4053; Tue, 22 May 2018 20:57:29 +0100 (BST) Received: from rapoport-lnx (unknown [9.148.205.65]) by d06av23.portsmouth.uk.ibm.com (Postfix) with ESMTPS; Tue, 22 May 2018 20:57:29 +0100 (BST) Received: by rapoport-lnx (sSMTP sendmail emulation); Tue, 22 May 2018 23:06:07 +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 v3] selftests: cgroup/memcontrol: add basic test for socket accounting Date: Tue, 22 May 2018 23:06:05 +0300 X-Mailer: git-send-email 2.7.4 X-TM-AS-GCONF: 00 x-cbid: 18052220-0040-0000-0000-0000043CABA7 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18052220-0041-0000-0000-00002641E147 Message-Id: <1527019565-20470-1-git-send-email-rppt@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2018-05-22_06:,, 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=1011 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1709140000 definitions=main-1805220207 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The test verifies that when there is active TCP connection, the memory.stat.sock and memory.current values are close. Signed-off-by: Mike Rapoport Acked-by: Roman Gushchin --- v3 changes: * added changelog 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