Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp876563pxf; Wed, 7 Apr 2021 13:56:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwiMBiicwCjcQ4z8pbxqOlyAajgc0WbDEbTJJj6AVLMKpCqF8Bra3CYXafZD6tUAjN6p+3I X-Received: by 2002:a17:907:984a:: with SMTP id jj10mr5950130ejc.180.1617828962236; Wed, 07 Apr 2021 13:56:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617828962; cv=none; d=google.com; s=arc-20160816; b=QrHEKFSm/jUmSXsFzsxxOPeYtsvNPKRhHUx14xcdAiEeVnDdOHNmW9OFMsKMJACUPu 31kc2MHTfoNMLYHgXElvMDv9O8GG8koo14jZ8mIDSmBgmFmSnytP04wkrTtwuD7uLXZh ZdPbCH8oMCMDQCKwz4HGhLGrusdNimmi3R8qPIhAHBntsHqVr7h8dFLZOm0EuO4aQWbc lHSpbL5rFqt1WRKtVbLvm7DjQRUas3kbiFua2bn3AXcJ/FSmNTFD1wZBY8fePVJ+KLOd XIowgD4Q+UdHyptOmM+BjYtapyCd7LApJbbu2aCMoIls2UNon2F5Peq/VKheFgUmSIrQ X50w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=kwLQH7itFSm2lWvSOLebZG+fQhoRmoJAI/Qf0ZUUORM=; b=zf7S/DwZ58yuKrjIJcb9VBJ5LapeP65zKXQ+BTKk5DIblkpeQwHsAKrL3etYXibvk9 cqESTSngnjEQzYa5vyoY7/dDyC8ef1eueLSv9edCyZiaHq7LeQeHoUtgpRVvgDcfF4Xx WB4zdfgIR0mg23DBNGcuFZ9sJ7HMGNzE+du36bgq6cMLKktmXBAROAVVzROd55EOlhTt 268B/qsCLp5cGftFXVCAeEjOIlbRbZUI3iuh2f/C4vS1oACXIgK8ipMQV0xo4sPp/Rhe grkbqnbBlYiGrGEFC9oDoBtvq3VhBQg3evz91j5m4F5LB/5nTayk8Yy8SqftZNFmhaHO E3Eg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@colorfullife-com.20150623.gappssmtp.com header.s=20150623 header.b=dREGV77B; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u4si43640edo.136.2021.04.07.13.55.39; Wed, 07 Apr 2021 13:56:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@colorfullife-com.20150623.gappssmtp.com header.s=20150623 header.b=dREGV77B; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245493AbhDGLAP (ORCPT + 99 others); Wed, 7 Apr 2021 07:00:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58236 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351882AbhDGK76 (ORCPT ); Wed, 7 Apr 2021 06:59:58 -0400 Received: from mail-ej1-x634.google.com (mail-ej1-x634.google.com [IPv6:2a00:1450:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 07A7FC061756 for ; Wed, 7 Apr 2021 03:59:48 -0700 (PDT) Received: by mail-ej1-x634.google.com with SMTP id r12so26936633ejr.5 for ; Wed, 07 Apr 2021 03:59:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=colorfullife-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=kwLQH7itFSm2lWvSOLebZG+fQhoRmoJAI/Qf0ZUUORM=; b=dREGV77BLNS5nloGPFiVCgMW2mQcYjeIXB2Nri9Lml4uu4+3Lxjx3GaXWm8uIflOoL 6Soo0N4Q5UPPDDtUC9BIK9O81Tqs9Qv7+RDZJU92KLSuTda69A/gdvT/VGdte/SYSH+u zdZw1PnlXVF097eedirfhZiGkob343J3V7UDKnFQoGdU0vnSKo78HDj53KplQqlX4VpE RpmNt/r+p3eMO3HW7TxvESFC/ACaTYiJGBXNR+OFVHNlfzzAvxYDBvFvjaJmnKMV6F8A Y+iiUkavjIfBlUaRyzL6NaCTSn2yP51blSbrIdX83LMyo9aMMOXiCP2AJz/gyoxSMCt5 yRaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=kwLQH7itFSm2lWvSOLebZG+fQhoRmoJAI/Qf0ZUUORM=; b=kdniJ9Wz7eszj3PHgbfs6lGGTNp9tnR738a7osvEmWSFN2VDsSoUNY2Jm52M1B6ax/ svhqxs9Hl+ll5oNr6wT2sWONe36Kh8fKB3waIA4bON73uO8suRe3bXogya0+v6+MhCFS tnzeV6DBiK6GLF/Fk3hzJqn/GVhIvL8Eh0wjomRUatp4QV4OfqFyTRP8evFbfPRLnsqT kPQMXOikLyqCyp1ZYabz+PDJ1P/DCAnJi1VCvcvCPUt1LmpUG5TEaG0UhdI/VfuKk7sq t9Jcmku4IL9jk8e6k0Ac412J4+OplyFdyhEUSv36OCw8+tVd1Bgm+l7Jgr21cGcr8G0h gBJw== X-Gm-Message-State: AOAM531N2YYVr0lQUR0mGzje7j8fjnkD/maA3Xfq9ObpC1iTih4VZKVO k5Ljj5znJBPxo+TdXLyxQBx4ajSZVBygVncT X-Received: by 2002:a17:906:64f:: with SMTP id t15mr265145ejb.411.1617793186774; Wed, 07 Apr 2021 03:59:46 -0700 (PDT) Received: from localhost.localdomain (p200300d997084700e354cc664d363e54.dip0.t-ipconnect.de. [2003:d9:9708:4700:e354:cc66:4d36:3e54]) by smtp.googlemail.com with ESMTPSA id w2sm1977062eju.71.2021.04.07.03.59.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Apr 2021 03:59:46 -0700 (PDT) From: Manfred Spraul To: LKML , Davidlohr Bueso Cc: Andrew Morton , 1vier1@web.de, Manfred Spraul Subject: [RFC] [PATCH] ipc/util.c: Use binary search for max_idx Date: Wed, 7 Apr 2021 12:59:26 +0200 Message-Id: <20210407105926.198404-1-manfred@colorfullife.com> X-Mailer: git-send-email 2.29.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If semctl(), msgctl() and shmctl() are called with IPC_INFO, SEM_INFO, MSG_INFO or SHM_INFO, then the return value is the index of the highest used entry in the kernel's internal array recording information about all SysV objects of the requested type for the current namespace. (This information can be used with repeated ..._STAT or ..._STAT_ANY operations to obtain information about all SysV objects on the system.) If the current highest used entry is destroyed, then the new highest used entry is determined by looping over all possible values. With the introduction of IPCMNI_EXTEND_SHIFT, this could be a loop over 16 million entries. As there is no get_last() function for idr structures: Implement a "get_last()" using a binary search. As far as I see, ipc is the only user that needs get_last(), thus implement it in ipc/util.c and not in a central location. Signed-off-by: Manfred Spraul --- ipc/util.c | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/ipc/util.c b/ipc/util.c index cfa0045e748d..0121bf6b2617 100644 --- a/ipc/util.c +++ b/ipc/util.c @@ -64,6 +64,7 @@ #include #include #include +#include #include @@ -450,6 +451,40 @@ static void ipc_kht_remove(struct ipc_ids *ids, struct kern_ipc_perm *ipcp) ipc_kht_params); } +/** + * ipc_get_maxusedidx - get highest in-use index + * @ids: ipc identifier set + * @limit: highest possible index. + * + * The function determines the highest in use index value. + * ipc_ids.rwsem needs to be owned by the caller. + * If no ipc object is allocated, then -1 is returned. + */ +static int ipc_get_maxusedidx(struct ipc_ids *ids, int limit) +{ + void *val; + int tmpidx; + int i; + int retval; + + i = ilog2(limit+1); + + retval = 0; + for (; i >= 0; i--) { + tmpidx = retval | (1<ipcs_idr, &tmpidx); + if (val) + retval |= (1<deleted = true; if (unlikely(idx == ids->max_idx)) { - do { - idx--; - if (idx == -1) - break; - } while (!idr_find(&ids->ipcs_idr, idx)); + + idx = ids->max_idx-1; + if (idx >= 0) + idx = ipc_get_maxusedidx(ids, idx); ids->max_idx = idx; } } -- 2.29.2