Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp1204339pxb; Sat, 4 Sep 2021 03:17:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxbtTHr8JvljvXemhtXpMB+3TZ7Z/QAuNDzgtyC3gZyb3ftsk9bCvQdiF1XThxUy4ywUkmt X-Received: by 2002:aa7:ccd1:: with SMTP id y17mr3594655edt.314.1630750627158; Sat, 04 Sep 2021 03:17:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630750627; cv=none; d=google.com; s=arc-20160816; b=CVf83xxEkSiQLOJ392QqtcQRFxjCIXauNp9IIF/VlN1T5gQo5B3PCLzMWLes5S2aYx jOtnsHLJmdYfbxA936IkkB8KREkWg3JDQ0X7p7yzlXE6PyTPVFdjD9zJ3MwgQCPG3PPU tnpMLvHwi4F+MQZcTlygKIJzHoqeVLbWcdY5sz5qyji+5nHLYYQEPtu+PeR4H7u+YAAz SkmXoFxQh/LwunobkXMhZkUf/UsG90onT7haWARTDf3cPbvPUfdQYebJ6YxGmHF9Srqq am9dfDu12jwlnnFT0YAK/Qh/nYzmHNsKuGoCnB9Gj6yTpOL3D5EllKaED051igU/OQZ4 WfZQ== 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 :references:in-reply-to:date:to:from:subject:message-id; bh=hhisDwLFWnbTVfvivaIPuqYTvixK1O5IohD6c8fNUVY=; b=IZco984ceOVC6ueIEj5pDxBFBU3qpz9SaIUUOI/+JjecVS+1YbrfIH2d4QsPRfqDiJ EFM+OSh6Qx0A5/40cR19DEAnbNdYcTOwzt8h/mTueT+fexEpFF9kG1AbCXbT3RS0x+Gi hxKQky6T6H5X2FZXPYZvDFCGel/KY+7pbcVbC2afrVFjk2jAP1I9peM6vSANkHCXfSw2 Oka8EokCwzcUfWQT1+NDD4OkW07UHU8vSoNgHfdQDHetr1jox9gVclwXQyTEYk8XNldB LoTmJaL6fwIwrq9qVz94SSz2f9UufUK5p1PkxICKQ59xjHe3kYHtdFegPyJvHawnQQE1 i1qw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-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 v3si2094828edc.618.2021.09.04.03.16.16; Sat, 04 Sep 2021 03:17:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350852AbhIDKDJ (ORCPT + 99 others); Sat, 4 Sep 2021 06:03:09 -0400 Received: from mout01.posteo.de ([185.67.36.141]:40523 "EHLO mout01.posteo.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235973AbhIDKDI (ORCPT ); Sat, 4 Sep 2021 06:03:08 -0400 X-Greylist: delayed 429 seconds by postgrey-1.27 at vger.kernel.org; Sat, 04 Sep 2021 06:03:08 EDT Received: from submission (posteo.de [89.146.220.130]) by mout01.posteo.de (Postfix) with ESMTPS id 44DB8240027 for ; Sat, 4 Sep 2021 11:54:57 +0200 (CEST) Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4H1qlN5NfJz9rxT for ; Sat, 4 Sep 2021 11:54:56 +0200 (CEST) Message-ID: Subject: Re: [PATCH BlueZ 1/2] shared/util: use 64-bit bitmap in util_get/clear_uid From: Pauli Virtanen To: "linux-bluetooth@vger.kernel.org" Date: Sat, 04 Sep 2021 09:54:56 +0000 In-Reply-To: References: <20210829155012.164880-1-pav@iki.fi> <20210829155012.164880-2-pav@iki.fi> Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org Hi Luiz, pe, 2021-09-03 kello 15:59 -0700, Luiz Augusto von Dentz kirjoitti: > Hi Pauli, > > On Sun, Aug 29, 2021 at 8:52 AM Pauli Virtanen wrote: > > > > The util_get/clear_uid functions use int type for bitmap, and are used > > e.g. for SEID allocation. However, valid SEIDs are in range 1 to 0x3E > > (AVDTP spec v1.3, 8.20.1), and 8*sizeof(int) is often smaller than 0x3E. > > > > The function is also used in src/advertising.c, but an explicit maximum > > value is always provided, so growing the bitmap size is safe there. > > > > Use 64-bit bitmap instead, to be able to cover the valid range. > > --- > > android/avdtp.c | 2 +- > > profiles/audio/avdtp.c | 2 +- > > src/advertising.c | 2 +- > > src/shared/util.c | 27 +++++++++++++++------------ > > src/shared/util.h | 4 ++-- > > unit/test-avdtp.c | 2 +- > > 6 files changed, 21 insertions(+), 18 deletions(-) > > > > diff --git a/android/avdtp.c b/android/avdtp.c > > index 8c2930ec1..a261a8e5f 100644 > > --- a/android/avdtp.c > > +++ b/android/avdtp.c > > @@ -34,7 +34,7 @@ > > #include "../profiles/audio/a2dp-codecs.h" > > > > #define MAX_SEID 0x3E > > -static unsigned int seids; > > +static uint64_t seids; > > > > #ifndef MAX > > # define MAX(x, y) ((x) > (y) ? (x) : (y)) > > diff --git a/profiles/audio/avdtp.c b/profiles/audio/avdtp.c > > index 946231b71..25520ceec 100644 > > --- a/profiles/audio/avdtp.c > > +++ b/profiles/audio/avdtp.c > > @@ -44,7 +44,7 @@ > > #define AVDTP_PSM 25 > > > > #define MAX_SEID 0x3E > > -static unsigned int seids; > > +static uint64_t seids; > > > > #ifndef MAX > > # define MAX(x, y) ((x) > (y) ? (x) : (y)) > > diff --git a/src/advertising.c b/src/advertising.c > > index bd79454d5..41b818650 100644 > > --- a/src/advertising.c > > +++ b/src/advertising.c > > @@ -48,7 +48,7 @@ struct btd_adv_manager { > > uint8_t max_scan_rsp_len; > > uint8_t max_ads; > > uint32_t supported_flags; > > - unsigned int instance_bitmap; > > + uint64_t instance_bitmap; > > bool extended_add_cmds; > > int8_t min_tx_power; > > int8_t max_tx_power; > > diff --git a/src/shared/util.c b/src/shared/util.c > > index 244756456..723dedd75 100644 > > --- a/src/shared/util.c > > +++ b/src/shared/util.c > > @@ -124,30 +124,33 @@ unsigned char util_get_dt(const char *parent, const char *name) > > > > /* Helpers for bitfield operations */ > > > > -/* Find unique id in range from 1 to max but no bigger then > > - * sizeof(int) * 8. ffs() is used since it is POSIX standard > > - */ > > -uint8_t util_get_uid(unsigned int *bitmap, uint8_t max) > > +/* Find unique id in range from 1 to max but no bigger than 64. */ > > +uint8_t util_get_uid(uint64_t *bitmap, uint8_t max) > > { > > uint8_t id; > > > > - id = ffs(~*bitmap); > > Can't we use ffsll instead of using a for loop testing every bit? > Afaik long long should be at least 64 bits. Ok, I now see GNU extensions are fine here. I'll use ffsll to make it simpler. > > + if (max > 64) > > + max = 64; > > > > - if (!id || id > max) > > - return 0; > > + for (id = 1; id <= max; ++id) { > > + uint64_t mask = ((uint64_t)1) << (id - 1); > > > > - *bitmap |= 1u << (id - 1); > > + if (!(*bitmap & mask)) { > > + *bitmap |= mask; > > + return id; > > + } > > + } > > > > - return id; > > + return 0; > >  } > > > >  /* Clear id bit in bitmap */ > > -void util_clear_uid(unsigned int *bitmap, uint8_t id) > > +void util_clear_uid(uint64_t *bitmap, uint8_t id) > >  { > > - if (!id) > > + if (id == 0 || id > 64) > >                 return; > > > > - *bitmap &= ~(1u << (id - 1)); > > + *bitmap &= ~(((uint64_t)1) << (id - 1)); > >  } > > > >  static const struct { > > diff --git a/src/shared/util.h b/src/shared/util.h > > index 9920b7f76..60908371d 100644 > > --- a/src/shared/util.h > > +++ b/src/shared/util.h > > @@ -102,8 +102,8 @@ void util_hexdump(const char dir, const > > unsigned char *buf, size_t len, > > > >  unsigned char util_get_dt(const char *parent, const char *name); > > > > -uint8_t util_get_uid(unsigned int *bitmap, uint8_t max); > > -void util_clear_uid(unsigned int *bitmap, uint8_t id); > > +uint8_t util_get_uid(uint64_t *bitmap, uint8_t max); > > +void util_clear_uid(uint64_t *bitmap, uint8_t id); > > > >  const char *bt_uuid16_to_str(uint16_t uuid); > >  const char *bt_uuid32_to_str(uint32_t uuid); > > diff --git a/unit/test-avdtp.c b/unit/test-avdtp.c > > index f5340d6f3..4e8a68c6b 100644 > > --- a/unit/test-avdtp.c > > +++ b/unit/test-avdtp.c > > @@ -550,7 +550,7 @@ static void test_server_seid(gconstpointer > > data) > >         struct avdtp_local_sep *sep; > >         unsigned int i; > > > > - for (i = 0; i < sizeof(int) * 8; i++) { > > + for (i = 0; i < MAX_SEID; i++) { > >                 sep = avdtp_register_sep(context->lseps, > > AVDTP_SEP_TYPE_SINK, > >                                                 AVDTP_MEDIA_TYPE_AU > > DIO, > >                                                 0x00, TRUE, > > &sep_ind, NULL, > > -- > > 2.31.1 > > > >