From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752040AbdJYUHz (ORCPT ); Wed, 25 Oct 2017 16:07:55 -0400 Received: from mga05.intel.com ([192.55.52.43]:65430 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751793AbdJYUHx (ORCPT ); Wed, 25 Oct 2017 16:07:53 -0400 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.43,433,1503385200"; d="scan'208";a="1235275851" Date: Wed, 25 Oct 2017 22:07:46 +0200 From: Jarkko Sakkinen To: Jason Gunthorpe Cc: PrasannaKumar Muralidharan , linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org, Matt Mackall , Herbert Xu , Peter Huewe , Marcel Selhorst , Mimi Zohar , Dmitry Kasatkin , James Morris , "Serge E. Hallyn" , David Safford , David Howells , Jerry Snitselaar , "open list:HARDWARE RANDOM NUMBER GENERATOR CORE" , open list , "moderated list:TPM DEVICE DRIVER" , "open list:INTEGRITY MEASUREMENT ARCHITECTURE (IMA)" , "open list:INTEGRITY MEASUREMENT ARCHITECTURE (IMA)" , "open list:KEYS-TRUSTED" Subject: Re: [PATCH v2] tpm: use struct tpm_chip for tpm_chip_find_get() Message-ID: <20171025200746.svsraubdotjyzt2i@linux.intel.com> References: <20171025115508.5682-1-jarkko.sakkinen@linux.intel.com> <20171025193452.d4qa4dhacfgqejk7@linux.intel.com> <20171025194633.GB998@obsidianresearch.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20171025194633.GB998@obsidianresearch.com> Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo User-Agent: NeoMutt/20170609 (1.8.3) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Oct 25, 2017 at 01:46:33PM -0600, Jason Gunthorpe wrote: > > 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 The id has a nice feature that it is unique for one boot cycle you can even try to get a chip that has been deleted. It has the most stable properties in the long run. Address is a reusable identifier in one boot cycle. /Jarkko