Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp314661lqo; Thu, 16 May 2024 07:11:17 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUlZcMxtmB2W7A0ic0xY6J64WDyQRfE2xgRtwJuQL4q/CBGM0MBfyLssF1C5o+yYyYWQGOu2dpLck7BjfFE1HrzkX62mBXHqEh8WA78aQ== X-Google-Smtp-Source: AGHT+IE0PckE6dC40740CjKFU+wMoADKusEX1Q9XCHJOzSmJUJhsQJZiwXJoV6/H0VWgvAoabyN5 X-Received: by 2002:a05:620a:294b:b0:792:e3a3:f924 with SMTP id af79cd13be357-792e3a4040cmr1476147685a.65.1715868676952; Thu, 16 May 2024 07:11:16 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715868676; cv=pass; d=google.com; s=arc-20160816; b=hbgmCeemBnn551/txuuL0mjDrQMGnxFpo6Q0xmY9S5Jnh0V4XNd6BNh6Q+KeAfP5Z4 2bCukuXSsa0hkOAvoZmy4wuqQ4geQaaFVFw5jBeJy8KTOxlIZfn1ztO1qLbEVvy3MSjl 7C3LlePvTf8A0Ii4yDDq83ltHYsQ3Otvl4AMYmX6cbaBWelrLAm+sXvieweT4NQKyzOz wWUlqFpOCR9yrXjckXchGSd35tAP7OrzneWZElFaAPx3FQsVs++Szp3ccS0hQlBwMTFM Wb/vEX0DXv4GOycLkTqWoj1MtsOF7DAHlBW5H94HYN0fr3VCHfW6D9Qlczp0TDfpwx0w mObA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=EmERywOLItQdbPqV7TU/hQijQ/77khKWhpW6wK/l15M=; fh=27SEuZLAG/6OtdPoy95oERriq/NLbgsFf/aeXN5oXaA=; b=0laRpEnPi/TirFPJOsku6fCIiCsBCzrQgSoqZ4ijPeFXZe87gLW48ypPcXYsKDfJJs h8CSY1J3roSGPccEm/98LFTlHaMMTWzWXJPK6CBuzXM37X/nqZ+VlksYjPsmKtRRqdNp mERc9tiLsfePoFmH0IMT8jw37/pewMCBKZxr/NlQK+Bd1Wnmpv7FSf8gL6pqWcx1C1uY QfJJVBEko+7AUOePBMmrEm1HmTu0uerOk5HPMuY/Rfqh7tz1xgU8Y2mvEdTfftpX4qyK GFSV+cGgpDeMOmNjoc6UgsCGQw0sCRxH5HSojSQ79g6RDwzC0+97nnSOCJH/dhkw3wc8 nChw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=sc4HhVUG; 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-181157-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-181157-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id af79cd13be357-793082bb91bsi128208885a.327.2024.05.16.07.11.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 May 2024 07:11:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-181157-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=sc4HhVUG; 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-181157-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-181157-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 8065B1C21366 for ; Thu, 16 May 2024 14:11:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 75B8714A4D1; Thu, 16 May 2024 14:10:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="sc4HhVUG" Received: from mail-ot1-f46.google.com (mail-ot1-f46.google.com [209.85.210.46]) (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 13E591487CD for ; Thu, 16 May 2024 14:10:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715868654; cv=none; b=BXrsx5pXZ0OjCA4Mayn3O0NmdGie20AeDsPVe4QthM4oYXtpy9F6iSHDsBaEQWmM9qwDupIcIsZDVWwOS6vU3evikxDyi9AhIvdVjl+dwnxMqb5UO8EL1crjdgtr8BGJL+Jg/CXrFuEaLOrcoaneMPo+3uG+B1S2nsm7E0C8En4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715868654; c=relaxed/simple; bh=mnvj+qh6fnc2dz1OtvSl8U5zGPGDwak9VJs7OHFHwSo=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=SL5MQvhrvPq+bFYJ1ozVajCD9AFIRXDYTLHahJqsBByrl2ON7OACkSX2lGgCPwUoeR2J/4sFrfyFJC4ex4sRj+IeKpZNqHNJC+0lqZGmOpOcPeLpxVbGUK0p74CkmDzNQ09nK1G0h2hvZXKLyS+f/rROxgWLZz4LsoAShyin7FI= 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=sc4HhVUG; arc=none smtp.client-ip=209.85.210.46 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-ot1-f46.google.com with SMTP id 46e09a7af769-6f1239a2e83so140432a34.3 for ; Thu, 16 May 2024 07:10:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1715868652; x=1716473452; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=EmERywOLItQdbPqV7TU/hQijQ/77khKWhpW6wK/l15M=; b=sc4HhVUGHBC5Bf/ZrXv13UlbuTvC0BT649BUOxXGIGV14Aq/PEVdKvjYo3jAAL+NiR i5K21sjE9aZSKQHVyGhVIkzO40Z4Qn2rKIt6DbPTeZsKFJXcVpdciUodf/tPykJ42jKQ +JpkO5T/+gddGavX1i7kn8nBa7CR6t2gtInI04INYp1vAthfYbCqrDUxbME/8yBQy8r/ +Rs3RJ62MF3vVFycP6xRNpUmG6sPXkeOOUiJ6ZnBYpLYe6YOwQMmC4OOnn+EbD3yb5vQ tdGIGPAhDJwf0NIkc/dd4NvbbTYk6igfWmd9MiW6hdhuKna36hqu0UOhEA3WeVp8uZRy IeQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715868652; x=1716473452; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=EmERywOLItQdbPqV7TU/hQijQ/77khKWhpW6wK/l15M=; b=hHy9HTqkugiETnvPka5Te7DlqoB+nMJL8OSxWQVCCCg2haasxONcWASerdrHJcEmdX deFtazsTgIPlbxRMHq/IQq9lplfLmMIfnAxuhS3OVmLi0+O5jiwhFRcXL2pcPGwkiQJJ hMpa+RADwV2wwVrdzn/BFvYvXaler0m/UAU55iJ4R71CO9bRI2PJUy6rR4e34UnAB9PR AYjDAlF5DASK43PT9CU/IAUc2HlLGYEb0iWwh4eJb0Xf6asUF304nnwGPbzoVPTWi+FY KIbqy25UfzD5wiUF5o1ezyV56xjDltCmv3cVUeWpGfJJIG4sp2iJn2EkG61vckCACJZK Vvhg== X-Forwarded-Encrypted: i=1; AJvYcCXpOOP2kjI8AuEVIMOw6rJVnlmqei1nG+xJTQmOxHIVEHbgIUxFFTUJohsedNQIEiv/n1Uv2CzCFPXrhuSzFVdQekx5gdqKGSKz0y3R X-Gm-Message-State: AOJu0Yxgqw0UlG1XXEAteMciBsLc+a5/WmQRxIXnm0Nrdjw3F2rVz3Qv lS/ReyVXWYIOwAoYF78DFt11Qc6NcXgJPN82URSHeDj1ddoyH5eOBfIYWrOt5hKe9bErj0xxSOD pZ2zT+8DlGPJT4G56hBP2O+j3GvVMvKwF99g3 X-Received: by 2002:a05:6808:1206:b0:3c9:930c:1c9 with SMTP id 5614622812f47-3c9970cfad9mr29242397b6e.40.1715868651918; Thu, 16 May 2024 07:10:51 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240516133952.4072309-1-cmllamas@google.com> In-Reply-To: <20240516133952.4072309-1-cmllamas@google.com> From: Alice Ryhl Date: Thu, 16 May 2024 16:10:40 +0200 Message-ID: Subject: Re: [PATCH v3] binder: use bitmap for faster descriptor lookup To: Carlos Llamas Cc: Christophe JAILLET , Greg Kroah-Hartman , =?UTF-8?B?QXJ2ZSBIasO4bm5ldsOlZw==?= , 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, May 16, 2024 at 3:39=E2=80=AFPM 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@googl= e.com/ [1] > Cc: Tim Murray > Cc: Arve Hj=C3=B8nnev=C3=A5g > 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 <=3D NBITS_MIN) > + return 0; > + > + bit =3D find_last_bit(dmap->map, dmap->nbits); > + if (unlikely(bit =3D=3D dmap->nbits)) > + return NBITS_MIN; > + > + if (unlikely(bit <=3D (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 `<=3D`. Alice