Path: blob/master/drivers/base/test/test_async_driver_probe.c
26444 views
// SPDX-License-Identifier: GPL-2.01/*2* Copyright (C) 2014 Google, Inc.3*/45#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt67#include <linux/delay.h>8#include <linux/init.h>9#include <linux/hrtimer.h>10#include <linux/module.h>11#include <linux/platform_device.h>12#include <linux/time.h>13#include <linux/numa.h>14#include <linux/nodemask.h>15#include <linux/topology.h>1617#define TEST_PROBE_DELAY (5 * 1000) /* 5 sec */18#define TEST_PROBE_THRESHOLD (TEST_PROBE_DELAY / 2)1920static atomic_t warnings, errors, timeout, async_completed;2122static int test_probe(struct platform_device *pdev)23{24struct device *dev = &pdev->dev;2526/*27* Determine if we have hit the "timeout" limit for the test if we28* have then report it as an error, otherwise we wil sleep for the29* required amount of time and then report completion.30*/31if (atomic_read(&timeout)) {32dev_err(dev, "async probe took too long\n");33atomic_inc(&errors);34} else {35dev_dbg(&pdev->dev, "sleeping for %d msecs in probe\n",36TEST_PROBE_DELAY);37msleep(TEST_PROBE_DELAY);38dev_dbg(&pdev->dev, "done sleeping\n");39}4041/*42* Report NUMA mismatch if device node is set and we are not43* performing an async init on that node.44*/45if (dev->driver->probe_type == PROBE_PREFER_ASYNCHRONOUS) {46if (IS_ENABLED(CONFIG_NUMA) &&47dev_to_node(dev) != numa_node_id()) {48dev_warn(dev, "NUMA node mismatch %d != %d\n",49dev_to_node(dev), numa_node_id());50atomic_inc(&warnings);51}5253atomic_inc(&async_completed);54}5556return 0;57}5859static struct platform_driver async_driver = {60.driver = {61.name = "test_async_driver",62.probe_type = PROBE_PREFER_ASYNCHRONOUS,63},64.probe = test_probe,65};6667static struct platform_driver sync_driver = {68.driver = {69.name = "test_sync_driver",70.probe_type = PROBE_FORCE_SYNCHRONOUS,71},72.probe = test_probe,73};7475static struct platform_device *async_dev[NR_CPUS * 2];76static struct platform_device *sync_dev[2];7778static struct platform_device *79test_platform_device_register_node(char *name, int id, int nid)80{81struct platform_device *pdev;82int ret;8384pdev = platform_device_alloc(name, id);85if (!pdev)86return ERR_PTR(-ENOMEM);8788if (nid != NUMA_NO_NODE)89set_dev_node(&pdev->dev, nid);9091ret = platform_device_add(pdev);92if (ret) {93platform_device_put(pdev);94return ERR_PTR(ret);95}9697return pdev;9899}100101static int __init test_async_probe_init(void)102{103struct platform_device **pdev = NULL;104int async_id = 0, sync_id = 0;105unsigned long long duration;106ktime_t calltime;107int err, nid, cpu;108109pr_info("registering first set of asynchronous devices...\n");110111for_each_online_cpu(cpu) {112nid = cpu_to_node(cpu);113pdev = &async_dev[async_id];114*pdev = test_platform_device_register_node("test_async_driver",115async_id,116nid);117if (IS_ERR(*pdev)) {118err = PTR_ERR(*pdev);119*pdev = NULL;120pr_err("failed to create async_dev: %d\n", err);121goto err_unregister_async_devs;122}123124async_id++;125}126127pr_info("registering asynchronous driver...\n");128calltime = ktime_get();129err = platform_driver_register(&async_driver);130if (err) {131pr_err("Failed to register async_driver: %d\n", err);132goto err_unregister_async_devs;133}134135duration = (unsigned long long)ktime_ms_delta(ktime_get(), calltime);136pr_info("registration took %lld msecs\n", duration);137if (duration > TEST_PROBE_THRESHOLD) {138pr_err("test failed: probe took too long\n");139err = -ETIMEDOUT;140goto err_unregister_async_driver;141}142143pr_info("registering second set of asynchronous devices...\n");144calltime = ktime_get();145for_each_online_cpu(cpu) {146nid = cpu_to_node(cpu);147pdev = &async_dev[async_id];148149*pdev = test_platform_device_register_node("test_async_driver",150async_id,151nid);152if (IS_ERR(*pdev)) {153err = PTR_ERR(*pdev);154*pdev = NULL;155pr_err("failed to create async_dev: %d\n", err);156goto err_unregister_async_driver;157}158159async_id++;160}161162duration = (unsigned long long)ktime_ms_delta(ktime_get(), calltime);163dev_info(&(*pdev)->dev,164"registration took %lld msecs\n", duration);165if (duration > TEST_PROBE_THRESHOLD) {166dev_err(&(*pdev)->dev,167"test failed: probe took too long\n");168err = -ETIMEDOUT;169goto err_unregister_async_driver;170}171172173pr_info("registering first synchronous device...\n");174nid = cpu_to_node(cpu);175pdev = &sync_dev[sync_id];176177*pdev = test_platform_device_register_node("test_sync_driver",178sync_id,179NUMA_NO_NODE);180if (IS_ERR(*pdev)) {181err = PTR_ERR(*pdev);182*pdev = NULL;183pr_err("failed to create sync_dev: %d\n", err);184goto err_unregister_async_driver;185}186187sync_id++;188189pr_info("registering synchronous driver...\n");190calltime = ktime_get();191err = platform_driver_register(&sync_driver);192if (err) {193pr_err("Failed to register async_driver: %d\n", err);194goto err_unregister_sync_devs;195}196197duration = (unsigned long long)ktime_ms_delta(ktime_get(), calltime);198pr_info("registration took %lld msecs\n", duration);199if (duration < TEST_PROBE_THRESHOLD) {200dev_err(&(*pdev)->dev,201"test failed: probe was too quick\n");202err = -ETIMEDOUT;203goto err_unregister_sync_driver;204}205206pr_info("registering second synchronous device...\n");207pdev = &sync_dev[sync_id];208calltime = ktime_get();209210*pdev = test_platform_device_register_node("test_sync_driver",211sync_id,212NUMA_NO_NODE);213if (IS_ERR(*pdev)) {214err = PTR_ERR(*pdev);215*pdev = NULL;216pr_err("failed to create sync_dev: %d\n", err);217goto err_unregister_sync_driver;218}219220sync_id++;221222duration = (unsigned long long)ktime_ms_delta(ktime_get(), calltime);223dev_info(&(*pdev)->dev,224"registration took %lld msecs\n", duration);225if (duration < TEST_PROBE_THRESHOLD) {226dev_err(&(*pdev)->dev,227"test failed: probe was too quick\n");228err = -ETIMEDOUT;229goto err_unregister_sync_driver;230}231232/*233* The async events should have completed while we were taking care234* of the synchronous events. We will now terminate any outstanding235* asynchronous probe calls remaining by forcing timeout and remove236* the driver before we return which should force the flush of the237* pending asynchronous probe calls.238*239* Otherwise if they completed without errors or warnings then240* report successful completion.241*/242if (atomic_read(&async_completed) != async_id) {243pr_err("async events still pending, forcing timeout\n");244atomic_inc(&timeout);245err = -ETIMEDOUT;246} else if (!atomic_read(&errors) && !atomic_read(&warnings)) {247pr_info("completed successfully\n");248return 0;249}250251err_unregister_sync_driver:252platform_driver_unregister(&sync_driver);253err_unregister_sync_devs:254while (sync_id--)255platform_device_unregister(sync_dev[sync_id]);256err_unregister_async_driver:257platform_driver_unregister(&async_driver);258err_unregister_async_devs:259while (async_id--)260platform_device_unregister(async_dev[async_id]);261262/*263* If err is already set then count that as an additional error for264* the test. Otherwise we will report an invalid argument error and265* not count that as we should have reached here as a result of266* errors or warnings being reported by the probe routine.267*/268if (err)269atomic_inc(&errors);270else271err = -EINVAL;272273pr_err("Test failed with %d errors and %d warnings\n",274atomic_read(&errors), atomic_read(&warnings));275276return err;277}278module_init(test_async_probe_init);279280static void __exit test_async_probe_exit(void)281{282int id = 2;283284platform_driver_unregister(&async_driver);285platform_driver_unregister(&sync_driver);286287while (id--)288platform_device_unregister(sync_dev[id]);289290id = NR_CPUS * 2;291while (id--)292platform_device_unregister(async_dev[id]);293}294module_exit(test_async_probe_exit);295296MODULE_DESCRIPTION("Test module for asynchronous driver probing");297MODULE_AUTHOR("Dmitry Torokhov <[email protected]>");298MODULE_LICENSE("GPL");299300301