From patchwork Fri Oct 20 01:42:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hou Tao X-Patchwork-Id: 13429964 Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C9D40648 for ; Fri, 20 Oct 2023 01:41:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=none Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2FF5A18E for ; Thu, 19 Oct 2023 18:41:08 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.153]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4SBS3K28yDz4f3kkW for ; Fri, 20 Oct 2023 09:41:01 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.124.27]) by APP4 (Coremail) with SMTP id gCh0CgAnt9ar2jFlqF+2DQ--.48939S4; Fri, 20 Oct 2023 09:41:01 +0800 (CST) From: Hou Tao To: bpf@vger.kernel.org Cc: Martin KaFai Lau , Alexei Starovoitov , Andrii Nakryiko , Song Liu , Hao Luo , Yonghong Song , Daniel Borkmann , KP Singh , Stanislav Fomichev , Jiri Olsa , John Fastabend , Hsin-Wei Hung , houtao1@huawei.com Subject: [PATCH bpf v2 0/2] bpf: Fix bpf timer kmemleak Date: Fri, 20 Oct 2023 09:42:12 +0800 Message-Id: <20231020014214.2471419-1-houtao@huaweicloud.com> X-Mailer: git-send-email 2.29.2 Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-CM-TRANSID: gCh0CgAnt9ar2jFlqF+2DQ--.48939S4 X-Coremail-Antispam: 1UD129KBjvJXoW7Cry8uF4UZw4xXw43CF1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net From: Hou Tao Hi, The patchset aims to fix the kmemleak problem reported by Hsin-Wei Hung [0]. Patch #1 fixes the kmemleak problem by re-checking map->usercnt after timer->timer is assigned. Patch #2 adds a selftest for the kmemleak problem. But it is a bit hard to reproduce the kmemleak by only running the test and I managed to reproduce the problem by both running the test and injecting delay before timer->timer is assigned in bpf_timer_init(). Please see individual patches for more details. And comments are always welcome. Change Log: v2: * patch #1: use smp_mb() instead of smp_mb__before_atomic() * patch #2: use WRITE_ONCE(timer->timer, x) to match the lockless read of timer->timer v1: https://lore.kernel.org/bpf/20231017125717.241101-1-houtao@huaweicloud.com Hou Tao (2): bpf: Check map->usercnt again after timer->timer is assigned selftests/bpf: Test race between map uref release and bpf timer init kernel/bpf/helpers.c | 18 ++- .../bpf/prog_tests/timer_init_race.c | 138 ++++++++++++++++++ .../selftests/bpf/progs/timer_init_race.c | 56 +++++++ 3 files changed, 209 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/timer_init_race.c create mode 100644 tools/testing/selftests/bpf/progs/timer_init_race.c