Received: by 2002:a25:86ce:0:0:0:0:0 with SMTP id y14csp2188921ybm; Thu, 23 May 2019 12:50:08 -0700 (PDT) X-Google-Smtp-Source: APXvYqzVb14G0laERjxg8MvadBOvHsdiqrGiqjqKCZyJbmgFcFfraxlOe+XTToNruM/SuREzeQ5F X-Received: by 2002:a17:90a:378b:: with SMTP id v11mr3788393pjb.10.1558641007975; Thu, 23 May 2019 12:50:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558641007; cv=none; d=google.com; s=arc-20160816; b=iuP0zuS1mHJwYA1Tp60SVblIg65+7KK0xGmjkPldWp8QEnviH3mw+Pb2vCDgLPzoXV 2EfZuo9Uvp+rbK6buzLd/lDdyGiAWf6WBFyjN8seQtUunZ8xAFXG7ZC9QjWc1l0xv0nN tiE0JHzoakdm7p+g++8rHsyVIjz2gq/+a+G8f9C3C28/0p8RhSbcrvOzESgLeZSHYyWm Z8UGZWEVUcEcQ/fRf/9r/sVDip0s2PappcoZElfSMZcQC+dnDoY4A/VwKBlI5PKPJRwR nfvwTZRoBqofdxASEkzg+m8z/vqnX4/Ua3lap7OvzfvDDzMlLhoZt5vuyo7VKBgJbJRT wlpg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:smtp-origin-cluster:cc:to :smtp-origin-hostname:from:smtp-origin-hostprefix:dkim-signature; bh=dM9+BVyTbQ+UpQVar9OuJ9mOa6aG8ajNryPCO9+TC84=; b=KK5dp0XZ4XonzUjnROJR6LN0gOE61CkinHROtI3sTKlZVqE4njVN5rLqs9uWJ4xfy8 FftMiqFpuYmrM8uORsKbgwC0WD617I7nTwCYTFWM/BlkkgQNmJGvO3v7wMYqDdjA8ugE kYdbhdjAtpVjSA9iDvTFq0UWWrwrjTnjulzT2mkBu1tMphKWbopuA7WP3XvIdtRJ9AmZ hosz67f+PEfPGKPup68sHrncC3/XCOlPg52pjrs1nfS/74bSboQllVI8uKVHOIitNtVX qjsKoMlajhsWYY+CRtw/UppAemUvtWRzw/Gp0RCLEDbV1FGhsykFWg4ZQrnFWrkbjIz/ jXig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=cH5YpDqL; 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=pass (p=NONE sp=NONE dis=NONE) header.from=fb.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k193si520319pge.306.2019.05.23.12.49.52; Thu, 23 May 2019 12:50:07 -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; dkim=pass header.i=@fb.com header.s=facebook header.b=cH5YpDqL; 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=pass (p=NONE sp=NONE dis=NONE) header.from=fb.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731727AbfEWTsr (ORCPT + 99 others); Thu, 23 May 2019 15:48:47 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:47922 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731730AbfEWTsl (ORCPT ); Thu, 23 May 2019 15:48:41 -0400 Received: from pps.filterd (m0109331.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x4NJhBAD002717 for ; Thu, 23 May 2019 12:48:40 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=facebook; bh=dM9+BVyTbQ+UpQVar9OuJ9mOa6aG8ajNryPCO9+TC84=; b=cH5YpDqLi+CHixLBKT4SUEC1kS0buGjmaHN3UnNbhX7V5irhEmIlTHU2ITVG7r4ZZSpg rTkUi1Q5TngrhHekFu3CNKDQT8mvcrmvLIpvvgnrJZs42PGzpPh9X0IO8f2bQYFApaR/ hIgNplupE5tuAxxFwTPwxGK2khnmOsJp52M= Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com with ESMTP id 2snx7s8wf2-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Thu, 23 May 2019 12:48:40 -0700 Received: from mx-out.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:82::f) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1713.5; Thu, 23 May 2019 12:48:37 -0700 Received: by devvm2643.prn2.facebook.com (Postfix, from userid 111017) id 57E5E125B048B; Thu, 23 May 2019 12:45:33 -0700 (PDT) Smtp-Origin-Hostprefix: devvm From: Roman Gushchin Smtp-Origin-Hostname: devvm2643.prn2.facebook.com To: Alexei Starovoitov , CC: Daniel Borkmann , , Tejun Heo , , , Stanislav Fomichev , Yonghong Song , , Roman Gushchin Smtp-Origin-Cluster: prn2c23 Subject: [PATCH v3 bpf-next 4/4] selftests/bpf: add auto-detach test Date: Thu, 23 May 2019 12:45:32 -0700 Message-ID: <20190523194532.2376233-5-guro@fb.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190523194532.2376233-1-guro@fb.com> References: <20190523194532.2376233-1-guro@fb.com> X-FB-Internal: Safe MIME-Version: 1.0 Content-Type: text/plain X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-05-23_16:,, signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 priorityscore=1501 malwarescore=0 suspectscore=2 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=489 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1905230128 X-FB-Internal: deliver Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add a kselftest to cover bpf auto-detachment functionality. The test creates a cgroup, associates some resources with it, attaches a couple of bpf programs and deletes the cgroup. Then it checks that bpf programs are going away in 5 seconds. Expected output: $ ./test_cgroup_attach #override:PASS #multi:PASS #autodetach:PASS test_cgroup_attach:PASS On a kernel without auto-detaching: $ ./test_cgroup_attach #override:PASS #multi:PASS #autodetach:FAIL test_cgroup_attach:FAIL Signed-off-by: Roman Gushchin --- .../selftests/bpf/test_cgroup_attach.c | 98 ++++++++++++++++++- 1 file changed, 97 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/test_cgroup_attach.c b/tools/testing/selftests/bpf/test_cgroup_attach.c index 2d6d57f50e10..7671909ee1cb 100644 --- a/tools/testing/selftests/bpf/test_cgroup_attach.c +++ b/tools/testing/selftests/bpf/test_cgroup_attach.c @@ -456,9 +456,105 @@ static int test_multiprog(void) return rc; } +static int test_autodetach(void) +{ + __u32 prog_cnt = 4, attach_flags; + int allow_prog[2] = {0}; + __u32 prog_ids[2] = {0}; + int cg = 0, i, rc = -1; + void *ptr = NULL; + int attempts; + + for (i = 0; i < ARRAY_SIZE(allow_prog); i++) { + allow_prog[i] = prog_load_cnt(1, 1 << i); + if (!allow_prog[i]) + goto err; + } + + if (setup_cgroup_environment()) + goto err; + + /* create a cgroup, attach two programs and remember their ids */ + cg = create_and_get_cgroup("/cg_autodetach"); + if (cg < 0) + goto err; + + if (join_cgroup("/cg_autodetach")) + goto err; + + for (i = 0; i < ARRAY_SIZE(allow_prog); i++) { + if (bpf_prog_attach(allow_prog[i], cg, BPF_CGROUP_INET_EGRESS, + BPF_F_ALLOW_MULTI)) { + log_err("Attaching prog[%d] to cg:egress", i); + goto err; + } + } + + /* make sure that programs are attached and run some traffic */ + assert(bpf_prog_query(cg, BPF_CGROUP_INET_EGRESS, 0, &attach_flags, + prog_ids, &prog_cnt) == 0); + assert(system(PING_CMD) == 0); + + /* allocate some memory (4Mb) to pin the original cgroup */ + ptr = malloc(4 * (1 << 20)); + if (!ptr) + goto err; + + /* close programs and cgroup fd */ + for (i = 0; i < ARRAY_SIZE(allow_prog); i++) { + close(allow_prog[i]); + allow_prog[i] = 0; + } + + close(cg); + cg = 0; + + /* leave the cgroup and remove it. don't detach programs */ + cleanup_cgroup_environment(); + + /* wait for the asynchronous auto-detachment. + * wait for no more than 5 sec and give up. + */ + for (i = 0; i < ARRAY_SIZE(prog_ids); i++) { + for (attempts = 5; attempts >= 0; attempts--) { + int fd = bpf_prog_get_fd_by_id(prog_ids[i]); + + if (fd < 0) + break; + + /* don't leave the fd open */ + close(fd); + + if (!attempts) + goto err; + + sleep(1); + } + } + + rc = 0; +err: + for (i = 0; i < ARRAY_SIZE(allow_prog); i++) + if (allow_prog[i] > 0) + close(allow_prog[i]); + if (cg) + close(cg); + free(ptr); + cleanup_cgroup_environment(); + if (!rc) + printf("#autodetach:PASS\n"); + else + printf("#autodetach:FAIL\n"); + return rc; +} + int main(void) { - int (*tests[])(void) = {test_foo_bar, test_multiprog}; + int (*tests[])(void) = { + test_foo_bar, + test_multiprog, + test_autodetach, + }; int errors = 0; int i; -- 2.20.1