Received: by 2002:ac0:950c:0:0:0:0:0 with SMTP id f12csp3230351imc; Wed, 13 Mar 2019 12:12:21 -0700 (PDT) X-Google-Smtp-Source: APXvYqwutwPxEbUKbSf+G6WulAt+BdB4t2HtR0bhb9B+t8D2t6nxaqakFrsIca8D1Ulubq6MzjPd X-Received: by 2002:a17:902:403:: with SMTP id 3mr47393450ple.48.1552504341404; Wed, 13 Mar 2019 12:12:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552504341; cv=none; d=google.com; s=arc-20160816; b=Gmjz4ipAfbhUjUqExK/iR1gx7pQKU4n6ryjz009+klLW0Zg5seexTFE7w9ZG4FuZQa Nx38P0Hg2lZER6yTqVDyeHwSjnpCrJAaIH60M0m3sw0Ridg7oQOzyd/A7GCrxnfpu5jZ /dU9439i+biOtiCXT3hRJmGVUapAQSSa3itaX+HKC+9ZYOipkPzkm5OqEFeuwf/F/lvr iiPvKYubKnI4oWLrWY3mk4cNHD1POCTb08o4iIGav8YXvWCQLkcyGyjgxPBjU+wwpdtI 21kMg9sRCGcO6tA1b+sWtwJ2RTDjRp5z6hXRrhQ1LcLZ4RfRrYDgh9d8s2NSowHRcmcz ehBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=tEJCTQMDJ5O4LehRhLk/iCUn+qC2lLwT3pj9E2TZSy4=; b=BcsreYyXk2VXgiGTHs5t6U2yo3+KKF3Iuv7eeuAl7460WXxrA8hk6Nn/yY3hZVUrYn G8xVCRDCZKnG1hma/13GXKaRmVfxkzY38cl0nWIM+Q0JiBocMY0grj65p+kwpxJppOUX AmruCfHkWg56ZP/8Tlch8XnYWiSmoHfl8L1eqDCf/rmrLM8kb87Os6D2YdvCUJtUC2Vy n4pN5X7fERuNvh1kzuBSXx9pabAsJ3TXAdErmsbC5rFEm3mingaFiO0sfuCvXQkYYxIW losTR/+ST8wVsQS8aAGva8ckmI4ylSFI4D396pB1d6SpYnUaNtv6o2Eb3QvWBYsU190Y 8IAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=MCwZKRwM; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u62si3426336pgc.204.2019.03.13.12.12.05; Wed, 13 Mar 2019 12:12:21 -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=@kernel.org header.s=default header.b=MCwZKRwM; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727395AbfCMTLO (ORCPT + 99 others); Wed, 13 Mar 2019 15:11:14 -0400 Received: from mail.kernel.org ([198.145.29.99]:41754 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727364AbfCMTLM (ORCPT ); Wed, 13 Mar 2019 15:11:12 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 7912D2075C; Wed, 13 Mar 2019 19:11:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1552504271; bh=M90tyzwbXJdW7WZmz/ARiMvMIIwtoaKSJk56xlcEmX0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MCwZKRwMP3ijMCTt/rAYjAeMC9tBlxyv7QT3udv9L0J0rIDIVvTADTeQVLjeX1/q+ qKmzhYm5ljcmFgMTu/U02jwG5oWublriWNi6OugRPfkBlb2M5kDd592r2vNxkK13G7 76U6zQtcNP1d3/sEHMaa/zZQGQ4KAPv5KdGXLAAg= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Stanislav Fomichev , Daniel Borkmann , Sasha Levin , netdev@vger.kernel.org, bpf@vger.kernel.org Subject: [PATCH AUTOSEL 4.20 24/60] bpf/test_run: fix unkillable BPF_PROG_TEST_RUN Date: Wed, 13 Mar 2019 15:09:45 -0400 Message-Id: <20190313191021.158171-24-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190313191021.158171-1-sashal@kernel.org> References: <20190313191021.158171-1-sashal@kernel.org> MIME-Version: 1.0 X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Stanislav Fomichev [ Upstream commit df1a2cb7c74b3d3abc8d8c2d690f82c8ebc3490a ] Syzbot found out that running BPF_PROG_TEST_RUN with repeat=0xffffffff makes process unkillable. The problem is that when CONFIG_PREEMPT is enabled, we never see need_resched() return true. This is due to the fact that preempt_enable() (which we do in bpf_test_run_one on each iteration) now handles resched if it's needed. Let's disable preemption for the whole run, not per test. In this case we can properly see whether resched is needed. Let's also properly return -EINTR to the userspace in case of a signal interrupt. See recent discussion: http://lore.kernel.org/netdev/CAH3MdRWHr4N8jei8jxDppXjmw-Nw=puNDLbu1dQOFQHxfU2onA@mail.gmail.com I'll follow up with the same fix bpf_prog_test_run_flow_dissector in bpf-next. Reported-by: syzbot Signed-off-by: Stanislav Fomichev Signed-off-by: Daniel Borkmann Signed-off-by: Sasha Levin --- net/bpf/test_run.c | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c index 25001913d03b..14a631817e12 100644 --- a/net/bpf/test_run.c +++ b/net/bpf/test_run.c @@ -13,27 +13,13 @@ #include #include -static __always_inline u32 bpf_test_run_one(struct bpf_prog *prog, void *ctx, - struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE]) -{ - u32 ret; - - preempt_disable(); - rcu_read_lock(); - bpf_cgroup_storage_set(storage); - ret = BPF_PROG_RUN(prog, ctx); - rcu_read_unlock(); - preempt_enable(); - - return ret; -} - -static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, u32 *ret, - u32 *time) +static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, + u32 *retval, u32 *time) { struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] = { 0 }; enum bpf_cgroup_storage_type stype; u64 time_start, time_spent = 0; + int ret = 0; u32 i; for_each_cgroup_storage_type(stype) { @@ -48,25 +34,42 @@ static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, u32 *ret, if (!repeat) repeat = 1; + + rcu_read_lock(); + preempt_disable(); time_start = ktime_get_ns(); for (i = 0; i < repeat; i++) { - *ret = bpf_test_run_one(prog, ctx, storage); + bpf_cgroup_storage_set(storage); + *retval = BPF_PROG_RUN(prog, ctx); + + if (signal_pending(current)) { + ret = -EINTR; + break; + } + if (need_resched()) { - if (signal_pending(current)) - break; time_spent += ktime_get_ns() - time_start; + preempt_enable(); + rcu_read_unlock(); + cond_resched(); + + rcu_read_lock(); + preempt_disable(); time_start = ktime_get_ns(); } } time_spent += ktime_get_ns() - time_start; + preempt_enable(); + rcu_read_unlock(); + do_div(time_spent, repeat); *time = time_spent > U32_MAX ? U32_MAX : (u32)time_spent; for_each_cgroup_storage_type(stype) bpf_cgroup_storage_free(storage[stype]); - return 0; + return ret; } static int bpf_test_finish(const union bpf_attr *kattr, -- 2.19.1