From patchwork Wed Sep 23 01:43:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shuah Khan X-Patchwork-Id: 255182 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-14.3 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 70D08C4727D for ; Wed, 23 Sep 2020 01:44:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2F4BC238A1 for ; Wed, 23 Sep 2020 01:44:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600825450; bh=7R5tvHOc7eLvVJunlcXykPhWQxm/529QTkvONzDb3XU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=ihTW8URWLEDOx2nKMCY2sDysomL1JbjAzb82VfSqAv1HF113id7lJurABqNuXodj5 CK1D3Ct9UuBp5EIyPAdXAb1y86nsfK7mHtPnUWQkS9C6HHFozAdt2uudqO9Nu78X0M aCFuR0wkQcQEoxzIWQlUj7ahzVHb6thvdfavcZP8= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727094AbgIWBoJ (ORCPT ); Tue, 22 Sep 2020 21:44:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56782 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726589AbgIWBoH (ORCPT ); Tue, 22 Sep 2020 21:44:07 -0400 Received: from mail-ot1-x343.google.com (mail-ot1-x343.google.com [IPv6:2607:f8b0:4864:20::343]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B8BFAC061755 for ; Tue, 22 Sep 2020 18:44:07 -0700 (PDT) Received: by mail-ot1-x343.google.com with SMTP id h17so17529854otr.1 for ; Tue, 22 Sep 2020 18:44:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=SvmexvENPFCpDZAV4u4VwpZEuEEoFBmf9PRfakHEQLI=; b=TErWXIGoBeR/4FJKrVarHeeOsn1hxBitvF8C0LMd0QH48ZiqfNM5HqfMT8lA/3tn+i ccPhTD6MqVvMNzDDTal5vDxMAM0BPduWl/4vsVDH1+6EwgpSr97hXX4XZ0B5HTV05pqm UrIgTW5YEJkvNlgv0cry4D0pThxXtvGrKsz9I= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=SvmexvENPFCpDZAV4u4VwpZEuEEoFBmf9PRfakHEQLI=; b=Y8PcTDFPv7FUmX0kLKR60Cw0URGJf31qvKjVXs+uyHdDXvKhvVRQGFP+w4CzBp/Tv6 gEc+Zx+OOoajpRCEzY5sqxzJTv9McRci4ZX48PdF+yymAZGNnd3xbPwuiT5YjKUVV4po ZFjInAl8diss8bN0khu/J65TMDgNAr0FLmPRTzsfzwALfEz8KCHnR5Ozsd53Hw/Z2Q8V 5dRKc86PjX8xRyec1asgqz0qUAM1tFCgbNTGFHcWsbqt2RBL3HiR79ycd7pO80s+3Rb/ 1NSWZdONCK7ckuAG+eYLPfDjqHyFsMLhCi9bTyQopL6uTZpOlmd+HaUsUER3A2qJkb8G ZSmQ== X-Gm-Message-State: AOAM532K/afOC1Eys/EEHpL1fkqIzSztb8uiWjOO1oV6EvtSBCuJm5Eq C5rcgoH3ddA/qOaC5ys2WHQNfg== X-Google-Smtp-Source: ABdhPJz+KrEps6h530Lhm2rxuhkMYCDO+BBy0lS5xjivBoYJn1VtTFXORcqlOIBKqKJfqyAdfYhytQ== X-Received: by 2002:a9d:7d93:: with SMTP id j19mr4500047otn.49.1600825447158; Tue, 22 Sep 2020 18:44:07 -0700 (PDT) Received: from shuah-t480s.internal (c-24-9-64-241.hsd1.co.comcast.net. [24.9.64.241]) by smtp.gmail.com with ESMTPSA id y23sm8801820ooj.34.2020.09.22.18.44.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Sep 2020 18:44:06 -0700 (PDT) From: Shuah Khan To: shuah@kernel.org, keescook@chromium.org, gregkh@linuxfoundation.org Cc: Shuah Khan , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH 02/11] selftests:lib: add new test for counters Date: Tue, 22 Sep 2020 19:43:31 -0600 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Add a new selftest for testing counter and counter_atomic counters api. This test load test_counters test modules and unloads. The test module runs tests and prints results in dmesg. There are a number of atomic_t usages in the kernel where atomic_t api is used strictly for counting and not for managing object lifetime. In some cases, atomic_t might not even be needed. The purpose of these counters is twofold: 1. clearly differentiate atomic_t counters from atomic_t usages that guard object lifetimes, hence prone to overflow and underflow errors. It allows tools that scan for underflow and overflow on atomic_t usages to detect overflow and underflows to scan just the cases that are prone to errors. 2. provides non-atomic counters for cases where atomic isn't necessary. Simple atomic and non-atomic counters api provides interfaces for simple atomic and non-atomic counters that just count, and don't guard resource lifetimes. Counters will wrap around to 0 when it overflows and should not be used to guard resource lifetimes, device usage and open counts that control state changes, and pm states. Using counter_atomic to guard lifetimes could lead to use-after free when it overflows and undefined behavior when used to manage state changes and device usage/open states. Signed-off-by: Shuah Khan --- MAINTAINERS | 1 + tools/testing/selftests/lib/Makefile | 1 + tools/testing/selftests/lib/config | 1 + tools/testing/selftests/lib/test_counters.sh | 5 +++++ 4 files changed, 8 insertions(+) create mode 100755 tools/testing/selftests/lib/test_counters.sh diff --git a/MAINTAINERS b/MAINTAINERS index 1d3abcfa76ab..fc802ef0ee95 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -15847,6 +15847,7 @@ L: linux-kernel@vger.kernel.org S: Maintained F: include/linux/counters.h F: lib/test_counters.c +F: tools/testing/selftests/lib/test_counters.sh SIMPLE FIRMWARE INTERFACE (SFI) S: Obsolete diff --git a/tools/testing/selftests/lib/Makefile b/tools/testing/selftests/lib/Makefile index a105f094676e..e8960d7934e2 100644 --- a/tools/testing/selftests/lib/Makefile +++ b/tools/testing/selftests/lib/Makefile @@ -5,5 +5,6 @@ all: TEST_PROGS := printf.sh bitmap.sh prime_numbers.sh strscpy.sh +TEST_PROGS += test_counters.sh include ../lib.mk diff --git a/tools/testing/selftests/lib/config b/tools/testing/selftests/lib/config index b80ee3f6e265..6ed25024d371 100644 --- a/tools/testing/selftests/lib/config +++ b/tools/testing/selftests/lib/config @@ -3,3 +3,4 @@ CONFIG_TEST_BITMAP=m CONFIG_PRIME_NUMBERS=m CONFIG_TEST_STRSCPY=m CONFIG_TEST_BITOPS=m +CONFIG_TEST_COUNTERS=m diff --git a/tools/testing/selftests/lib/test_counters.sh b/tools/testing/selftests/lib/test_counters.sh new file mode 100755 index 000000000000..d1a130190e3f --- /dev/null +++ b/tools/testing/selftests/lib/test_counters.sh @@ -0,0 +1,5 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# Tests the Simple Atomic and Non-atomic Counters interfaces using +# test_counters kernel module +$(dirname $0)/../kselftest/module.sh "test_counters" test_counters