From owner-svn-src-head@freebsd.org Wed May 11 10:35:16 2016 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id EB7B7B37342; Wed, 11 May 2016 10:35:16 +0000 (UTC) (envelope-from hselasky@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 9F7991A84; Wed, 11 May 2016 10:35:16 +0000 (UTC) (envelope-from hselasky@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id u4BAZF6D017932; Wed, 11 May 2016 10:35:15 GMT (envelope-from hselasky@FreeBSD.org) Received: (from hselasky@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id u4BAZFkk017931; Wed, 11 May 2016 10:35:15 GMT (envelope-from hselasky@FreeBSD.org) Message-Id: <201605111035.u4BAZFkk017931@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: hselasky set sender to hselasky@FreeBSD.org using -f From: Hans Petter Selasky Date: Wed, 11 May 2016 10:35:15 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r299426 - head/sys/compat/linuxkpi/common/src X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 11 May 2016 10:35:17 -0000 Author: hselasky Date: Wed May 11 10:35:15 2016 New Revision: 299426 URL: https://svnweb.freebsd.org/changeset/base/299426 Log: Factor out common code into "idr_find_layer_locked()" and fix inverted bitmap test for free entry in "idr_replace()". Obtained from: kmacy @ MFC after: 1 week Sponsored by: Mellanox Technologies Modified: head/sys/compat/linuxkpi/common/src/linux_idr.c Modified: head/sys/compat/linuxkpi/common/src/linux_idr.c ============================================================================== --- head/sys/compat/linuxkpi/common/src/linux_idr.c Wed May 11 10:19:44 2016 (r299425) +++ head/sys/compat/linuxkpi/common/src/linux_idr.c Wed May 11 10:35:15 2016 (r299426) @@ -160,34 +160,43 @@ idr_remove(struct idr *idr, int id) mtx_unlock(&idr->lock); } -void * -idr_replace(struct idr *idr, void *ptr, int id) + +static inline struct idr_layer * +idr_find_layer_locked(struct idr *idr, int id) { struct idr_layer *il; - void *res; int layer; - int idx; - res = ERR_PTR(-EINVAL); id &= MAX_ID_MASK; - mtx_lock(&idr->lock); il = idr->top; layer = idr->layers - 1; if (il == NULL || id > idr_max(idr)) - goto out; + return (NULL); while (layer && il) { il = il->ary[idr_pos(id, layer)]; layer--; } + return (il); +} + +void * +idr_replace(struct idr *idr, void *ptr, int id) +{ + struct idr_layer *il; + void *res; + int idx; + + mtx_lock(&idr->lock); + il = idr_find_layer_locked(idr, id); idx = id & IDR_MASK; - /* - * Replace still returns an error if the item was not allocated. - */ - if (il != NULL && (il->bitmap & (1 << idx)) != 0) { + + /* Replace still returns an error if the item was not allocated. */ + if (il == NULL || (il->bitmap & (1 << idx))) { + res = ERR_PTR(-ENOENT); + } else { res = il->ary[idx]; il->ary[idx] = ptr; } -out: mtx_unlock(&idr->lock); return (res); } @@ -197,22 +206,13 @@ idr_find_locked(struct idr *idr, int id) { struct idr_layer *il; void *res; - int layer; mtx_assert(&idr->lock, MA_OWNED); - - id &= MAX_ID_MASK; - res = NULL; - il = idr->top; - layer = idr->layers - 1; - if (il == NULL || id > idr_max(idr)) - return (NULL); - while (layer && il) { - il = il->ary[idr_pos(id, layer)]; - layer--; - } + il = idr_find_layer_locked(idr, id); if (il != NULL) res = il->ary[id & IDR_MASK]; + else + res = NULL; return (res); }