記一次misc_register註冊失敗

        在看內核log時,發現有一個misc_register註冊失敗的warning。一開始以爲配置的參數有誤,看代碼也使用了參數MISC_DYNAMIC_MINOR,不太可能出現註冊失敗的情況。

int misc_register(struct miscdevice * misc)
{
	dev_t dev;
	int err = 0;
	bool is_dynamic = (misc->minor == MISC_DYNAMIC_MINOR);

	if (is_dynamic) {
		struct miscdevice *c;
		int i = find_first_zero_bit(misc_minors, DYNAMIC_MINORS);
		if (i >= DYNAMIC_MINORS) {
			err = -EBUSY;
			goto out;
		}
		list_for_each_entry(c, &misc_list, list) {
			if (c == misc) {
				err = -EEXIST;
				goto out;
			}
		}
		misc->minor = DYNAMIC_MINORS - i - 1;
		set_bit(i, misc_minors);
	} else {
		
	    ...
	}

	...
}

加打印發現is_dynamic爲0,但有使用參數MISC_DYNAMIC_MINOR,不應該爲0。出問題的代碼如下,能看出哪裏有問題沒

static int xxx_img_probe(struct platform_device *pdev)
{
	int ret = 0;

	ret = misc_register(&image_dev);
	if (ret) {
		pr_err("cannot register miscdev on minor=%d (%d).\n",IMAGE_MINOR, ret);
		ret = -EACCES;
		goto exit;
	}
}

static const struct of_device_id  of_match_table_dcam[] = {
	{ .compatible = "xxx,dcam"},
};

static struct platform_driver xxx_img_driver = {
	.probe = xxx_img_probe,
	.driver = {
		.of_match_table = of_match_ptr(of_match_table_dcam),
		},
};

發現xxx_img_probe跑了兩遍,第一次註冊成功了,導致misc->minor爲固定值(非255),導致不能動態註冊該設備,使用靜態註冊時,該設備號已經被使用了,從而註冊失敗。

但xxx_img_probe爲什麼會跑兩遍的,設備只有一個。一開始以爲xxx_img_probe返回值爲EPROBE_DEFER,導致xxx_img_probe再次運行。但打印發現xxx_img_probe返回值爲0。只能加打印進行跟蹤了。

const struct of_device_id *__of_match_node(const struct of_device_id *matches,
					   const struct device_node *node)
{
	const struct of_device_id *best_match = NULL;
	int score, best_score = 0;

	if (!matches)
		return NULL;

	for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
		score = __of_device_is_compatible(node, matches->compatible,
						  matches->type, matches->name);
		if (score > best_score) {
			best_match = matches;
			best_score = score;
		}
	}

	return best_match;
}

發現matches指向了其他的非法數據結構,導致probe函數第二次運行。

static const struct of_device_id  of_match_table_dcam[] = {
	{ .compatible = "xxx,dcam"},
+       {                         },
};

罪魁禍首就是少了一個括號,導致野指針出現,出現了非法probe。這是某cpu廠商代碼的bug,已提給他們修復。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章