Re: [PATCH v2] tpm: use struct tpm_chip for tpm_chip_find_get()

Jason Gunthorpe <[email protected]>
Newsgroups net.sourceforge.lists.tpmdd-devel,org.kernel.vger.keyrings,org.kernel.vger.linux-crypto,org.kernel.vger.linux-integrity,org.kernel.vger.linux-kernel,org.kernel.vger.linux-security-module
Message-ID <[email protected]>
> struct tpm_chip *tpm_chip_find_get(u64 id)
> {
> 	struct tpm_chup *chip;
> 	struct tpm_chip *res = NULL;
> 	int chip_num = 0;
> 	int chip_prev;
> 
> 	mutex_lock(&idr_lock);
> 
> 	do {
> 		chip_prev = chip_num;
> 
> 		chip = idr_get_next(&dev_nums_idr, &chip_num);
> 
> 		if (chip && (!id || id == chip->id) && !tpm_try_get_ops(chip)) {
> 			res = chip;
> 			break;
> 		}
> 	} while (chip_prev != chip_num);
> 
> 	mutex_unlock(&idr_lock);
> 
> 	return res;
> }

?? The old version was correct, idr_find_slowpath is better than an
idr_get_next serach if you already know id.

PrasannaKumar's solution seems right, if we already have chip, then we
just need to lock it again:

struct tpm_chip *tpm_chip_find_get(struct tpm_chip *chip)
{
	struct tpm_chip *res = NULL;

	mutex_lock(&idr_lock);

	if (!chip) {
		int chip_num = 0;
		int chip_prev;

		do {
			chip_prev = chip_num;
			chip = idr_get_next(&dev_nums_idr, &chip_num);
			if (chip && !tpm_try_get_ops(chip)) {
				res = chip;
				break;
			}
		} while (chip_prev != chip_num);
	} else {
		if (!tpm_try_get_ops(chip))
			res = chip;
	}

	mutex_unlock(&idr_lock);

	return res;
}

Jason
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.