Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp687057lqo; Thu, 16 May 2024 20:24:27 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXj5FOPq2qNMWTkRvnUjKWNBp8ilJKWIiksRHhQwy7XaGWrwEjVxETraLOooeNVh3MlBkdvfgyctU/M4XbsS63oI77N7j4FnleDw8YU5A== X-Google-Smtp-Source: AGHT+IHxQM+eTbUNZTmpJfmSi2zfN0fLKRnLjsE049eCi/immOz5oNmtn6zM5UtbMp5SbqvXttL2 X-Received: by 2002:a17:907:12cb:b0:a5a:893a:a73a with SMTP id a640c23a62f3a-a5a893aa833mr851206766b.10.1715916267805; Thu, 16 May 2024 20:24:27 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715916267; cv=pass; d=google.com; s=arc-20160816; b=x3EXJBqgmU6g15ykSPvedlr4lcTUv4NwOliGcNYihp3rofOnW0+SzGBpKb68SbiDcI yxkXAsl1cz2IrrOigWAfndjvbZOHCDstjtpVRociBx9eExbg7lJrW7ebiJusmGmxxKoH j5Tc/VhZqDrCKf/lWc/LITEDVKdioTgD4qWq2v5PIQkcV1HU5fz2eBNeZgdVrQYL/HlY pKNeTjaeDbtHuhPNrEj6DkvHuAMiScXxMyyTnsqgc3G15Y8KmBZDd4F1mOMGm/SJGXSi t1fOZWQUxAnWtKyHFd+XvGG6okW7x3K8FTjVHOEuyjaQ4vYJQJGKHslM/m02PPkRURye ik3A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:message-id:subject:cc:to:from:date:dkim-signature; bh=yTi86NLko9+FJmcjB2JWS3oT0tnePKfTstbRdxRL7Zg=; fh=s31yQ+pC2NeLngXz8SqgUJTzrcWvn2NCb9If5gBV89o=; b=BB5y5SMemxT2P5LH+FLgMgGMeUrSq84bW4nJ8U7DLVjptCOAIa2FxtCwVprEiFx2my NWj+Ywwe0piow9Lo0ZzfeQ8GKc1ZkXfgkmB5rmYk51LAQb6tBcCVcr9bw6LnchQ++i3M qz7quhPzTvQa1lqEVg3vuf9OQmSVtFa+an705P+8CimkNBp4YnxE6g35RgJzfz5MtYYv SUX2r50Jxqw6vt1Per6PQiK9yufheVNFpki1pePZoE7TXJC6OWPsLVCSvfItKjy1H2qb VsdOoUynkVYkbUvIN+7Lq8fmnfuvxcz0/8LhCnLbYUSQxFvQiJ1mbqEnWIWRj4qtcMfV ejcA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="K/bmhaov"; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-181715-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-181715-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a640c23a62f3a-a5a1797cae1si930306566b.252.2024.05.16.20.24.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 May 2024 20:24:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-181715-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="K/bmhaov"; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-181715-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-181715-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 5E6371F2246A for ; Fri, 17 May 2024 03:24:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CD0B1B676; Fri, 17 May 2024 03:24:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="K/bmhaov" Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7EEB79470 for ; Fri, 17 May 2024 03:24:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715916259; cv=none; b=XhdykCb5otzPbvnDDSDEeUT4xfJfg8UVnEfFzv3hjjpmAgzM1FpDmKbWrzt9R18Xl8O9xEkaJCxARWjeCQ2MkGrTRdEbwyfY0WWjY8bgTaknGrs8cuHiFUsuRDTewD7revG9tO24xtP5ghlxZM1MdRsDFeAxqMGcyoi7cMlUjwo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715916259; c=relaxed/simple; bh=IOPutvIpfO2y5V0fCmOVIYS37QKohYrmV2SvxEstUHo=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=YqlfT+vIPk5pXbnjB2kq6HLhAF4STp05gIf9ZBMO5p11sO4kFkND5AtnczolBsxEHvtjGthDZkzjZmWZbHDIP8HAaFdt01NbEZeynCk7b6qZOUquXqOCZEbp2tHNqMcTlNfqH8UPZIRwEXC3B2ILm9R+tNVIgofePqGKT5R9h/0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=K/bmhaov; arc=none smtp.client-ip=209.85.210.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-6f44a2d1e3dso921494b3a.3 for ; Thu, 16 May 2024 20:24:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1715916258; x=1716521058; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=yTi86NLko9+FJmcjB2JWS3oT0tnePKfTstbRdxRL7Zg=; b=K/bmhaovayTx1oNAgoM3J97UQJdwYNuGN4kh9sdwwNdzVuWLe9JO2GMSLpQJpHH1yG qfitfQd38FrD3eBudlIsAhvM3wJShbhukbqRX2ZxmLcTi8kRahnRYW6eZKkBJ7wlcPDj iNZiid0iyuFYY75Mm/mneSZZbF/MD0lGJUS199OnKeAdclXQGCCXoxzBZ0GlBQa5xKdO jT4jb5LQQ3wDbVu7bZhtTab0KMU+9pBF9cKOp0xCy8uOtMe1u36SkPZ+8+UeFE49rKXP Zfo7BuZufdhUSNrzwdV8itLZRweikGzhH6xVYUuJBA+KJH7TddOb1pdX/NtM3FjjPYMJ 9cRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715916258; x=1716521058; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=yTi86NLko9+FJmcjB2JWS3oT0tnePKfTstbRdxRL7Zg=; b=NRw0inml7weK+tI/YstBKJWV+QPaGgRW78Yw/uJxoOMuHN0f0qiDTbQGrqitZ82p3W lL9ZI5rq1TABv2PKNG7IV30nzf06yra66X27A+GKR3+lBbt0/tiCjz6EYvfMyPk5V06B bBrLCKsf3APDqCUtFFYQCRpmWps5Ie25NR5ew/CyqyE1XBlJcXRWxfdZRMlQa0C5ACjE qsQ6RxcgPIxw7sl25coVaF/6uuSPROeWsITTbOZhHCHuK8Yisr97kxVHOFazQjqAzV79 oLOVtnxoCjd7riWTkGP6fOZxx82fIDjYdazrBLLyOlfsgcnXr3Gki+pAcocp0EK2Snwk k1fQ== X-Forwarded-Encrypted: i=1; AJvYcCWa/DKW4O/ivo7Z9P8LPi1Jk5XEOiXnj//1HSbIwI+t+Zm+azchj3brGl6zuLhrzkQ38EqZFc6q1p82fDZPlUkb7ICC97mflWNjiHm0 X-Gm-Message-State: AOJu0YwLD+mMU+M9Qq4weHS0ODl2S1nZ0Mjcfb0mXyhNJ2thUyK1LGOk MZ0+px0aZRB9+UyERvAo59eDs5rrMe0/fahFME5usyBm+fLDml/53t72UyVZYQ== X-Received: by 2002:a05:6a20:551c:b0:1ad:746:b15a with SMTP id adf61e73a8af0-1afde1c5775mr16976882637.47.1715916257519; Thu, 16 May 2024 20:24:17 -0700 (PDT) Received: from google.com (57.92.83.34.bc.googleusercontent.com. [34.83.92.57]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0c2567ccsm146502685ad.301.2024.05.16.20.24.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 May 2024 20:24:16 -0700 (PDT) Date: Fri, 17 May 2024 03:24:13 +0000 From: Carlos Llamas To: Alice Ryhl Cc: Christophe JAILLET , Greg Kroah-Hartman , Arve =?iso-8859-1?B?SGr4bm5lduVn?= , Todd Kjos , Martijn Coenen , Joel Fernandes , Christian Brauner , Suren Baghdasaryan , linux-kernel@vger.kernel.org, kernel-team@android.com, Tim Murray , John Stultz , Steven Moreland , Nick Chen Subject: Re: [PATCH v3] binder: use bitmap for faster descriptor lookup Message-ID: References: <20240516133952.4072309-1-cmllamas@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Thu, May 16, 2024 at 04:10:40PM +0200, Alice Ryhl wrote: > On Thu, May 16, 2024 at 3:39 PM Carlos Llamas wrote: > > > > When creating new binder references, the driver assigns a descriptor id > > that is shared with userspace. Regrettably, the driver needs to keep the > > descriptors small enough to accommodate userspace potentially using them > > as Vector indexes. Currently, the driver performs a linear search on the > > rb-tree of references to find the smallest available descriptor id. This > > approach, however, scales poorly as the number of references grows. > > > > This patch introduces the usage of bitmaps to boost the performance of > > descriptor assignments. This optimization results in notable performance > > gains, particularly in processes with a large number of references. The > > following benchmark with 100,000 references showcases the difference in > > latency between the dbitmap implementation and the legacy approach: > > > > [ 587.145098] get_ref_desc_olocked: 15us (dbitmap on) > > [ 602.788623] get_ref_desc_olocked: 47343us (dbitmap off) > > > > Note the bitmap size is dynamically adjusted in line with the number of > > references, ensuring efficient memory usage. In cases where growing the > > bitmap is not possible, the driver falls back to the slow legacy method. > > > > A previous attempt to solve this issue was proposed in [1]. However, > > such method involved adding new ioctls which isn't great, plus older > > userspace code would not have benefited from the optimizations either. > > > > Link: https://lore.kernel.org/all/20240417191418.1341988-1-cmllamas@google.com/ [1] > > Cc: Tim Murray > > Cc: Arve Hjønnevåg > > Cc: Alice Ryhl > > Cc: Martijn Coenen > > Cc: Todd Kjos > > Cc: John Stultz > > Cc: Steven Moreland > > Suggested-by: Nick Chen > > Signed-off-by: Carlos Llamas > > LGTM. One nit below, but it's not a correctness issue. > > Reviewed-by: Alice Ryhl > > > +static inline unsigned int dbitmap_shrink_nbits(struct dbitmap *dmap) > > +{ > > + unsigned int bit; > > + > > + if (dmap->nbits <= NBITS_MIN) > > + return 0; > > + > > + bit = find_last_bit(dmap->map, dmap->nbits); > > + if (unlikely(bit == dmap->nbits)) > > + return NBITS_MIN; > > + > > + if (unlikely(bit <= (dmap->nbits >> 2))) > > + return dmap->nbits >> 1; > > I think this is intended to say that we only shrink if only the lower > fourth of the bits have any bits set, but for the condition to > actually be that, you need `bit < (map->nbits >> 2)` here instead of > `<=`. True, the range goes [0...n-1] so it should be "bit < n". Let me fix that. Thanks. > > Alice