From: "Jan Beulich" Subject: [PATCH 2/4] linux-2.6.18/gntdev: fix multi-page slot allocation Date: Thu, 10 May 2012 16:17:46 +0100 Message-ID: <4FABF83A0200007800082C8B@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part5A74B30A.0__=" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part5A74B30A.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Any range with the first slot available would have got considered usable, as range_length never got reset when encountering an in-use slot. Additionally fold the two almost identical loops into a single instance, at once avoiding to go through both loops when start_index was zero even for the first one. Signed-off-by: Jan Beulich --- a/drivers/xen/gntdev/gntdev.c +++ b/drivers/xen/gntdev/gntdev.c @@ -285,35 +285,27 @@ static void compress_free_list(gntdev_fi static int find_contiguous_free_range(gntdev_file_private_data_t = *private_data, uint32_t num_slots)=20 { - uint32_t i, start_index =3D private_data->next_fit_index; - uint32_t range_start =3D 0, range_length; - - /* First search from the start_index to the end of the array. */ - range_length =3D 0; - for (i =3D start_index; i < private_data->grants_size; ++i) { - if (private_data->grants[i].state =3D=3D GNTDEV_SLOT_INVALI= D) { - if (range_length =3D=3D 0) { - range_start =3D i; - } - ++range_length; - if (range_length =3D=3D num_slots) { - return range_start; - } - } - } -=09 - /* Now search from the start of the array to the start_index. */ - range_length =3D 0; - for (i =3D 0; i < start_index; ++i) { - if (private_data->grants[i].state =3D=3D GNTDEV_SLOT_INVALI= D) { - if (range_length =3D=3D 0) { - range_start =3D i; - } - ++range_length; - if (range_length =3D=3D num_slots) { - return range_start; - } - } + /* First search from next_fit_index to the end of the array. */ + uint32_t start_index =3D private_data->next_fit_index; + uint32_t end_index =3D private_data->grants_size; + + for (;;) { + uint32_t i, range_start =3D 0, range_length =3D 0; + + for (i =3D start_index; i < end_index; ++i) { + if (private_data->grants[i].state =3D=3D GNTDEV_SLO= T_INVALID) { + if (range_length =3D=3D 0) + range_start =3D i; + if (++range_length =3D=3D num_slots) + return range_start; + } else + range_length =3D 0; + } + /* Now search from the start of the array to next_fit_index= . */ + if (!start_index) + break; + end_index =3D start_index; + start_index =3D 0; } =09 return -ENOMEM; --=__Part5A74B30A.0__= Content-Type: text/plain; name="xen-gntdev-multi-page.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="xen-gntdev-multi-page.patch" gntdev: fix multi-page slot allocation=0A=0AAny range with the first slot = available would have got considered=0Ausable, as range_length never got = reset when encountering an in-use=0Aslot.=0A=0AAdditionally fold the two = almost identical loops into a single=0Ainstance, at once avoiding to go = through both loops when start_index=0Awas zero even for the first = one.=0A=0ASigned-off-by: Jan Beulich =0A=0A--- = a/drivers/xen/gntdev/gntdev.c=0A+++ b/drivers/xen/gntdev/gntdev.c=0A@@ = -285,35 +285,27 @@ static void compress_free_list(gntdev_fi=0A static int = find_contiguous_free_range(gntdev_file_private_data_t *private_data,=0A = uint32_t num_slots) =0A {=0A- uint32_t = i, start_index =3D private_data->next_fit_index;=0A- uint32_t range_star= t =3D 0, range_length;=0A-=0A- /* First search from the start_index to = the end of the array. */=0A- range_length =3D 0;=0A- for (i =3D = start_index; i < private_data->grants_size; ++i) {=0A- if = (private_data->grants[i].state =3D=3D GNTDEV_SLOT_INVALID) {=0A- = if (range_length =3D=3D 0) {=0A- = range_start =3D i;=0A- }=0A- ++range_len= gth;=0A- if (range_length =3D=3D num_slots) {=0A- = return range_start;=0A- }=0A- = }=0A- }=0A- =0A- /* Now search from the start of the array to the = start_index. */=0A- range_length =3D 0;=0A- for (i =3D 0; i < = start_index; ++i) {=0A- if (private_data->grants[i].state =3D=3D = GNTDEV_SLOT_INVALID) {=0A- if (range_length =3D=3D 0) = {=0A- range_start =3D i;=0A- = }=0A- ++range_length;=0A- if = (range_length =3D=3D num_slots) {=0A- return = range_start;=0A- }=0A- }=0A+ /* First = search from next_fit_index to the end of the array. */=0A+ uint32_t = start_index =3D private_data->next_fit_index;=0A+ uint32_t end_index = =3D private_data->grants_size;=0A+=0A+ for (;;) {=0A+ uint32_t = i, range_start =3D 0, range_length =3D 0;=0A+=0A+ for (i =3D = start_index; i < end_index; ++i) {=0A+ if (private_data->g= rants[i].state =3D=3D GNTDEV_SLOT_INVALID) {=0A+ = if (range_length =3D=3D 0)=0A+ range_start= =3D i;=0A+ if (++range_length =3D=3D = num_slots)=0A+ return range_start;=0A+ = } else=0A+ range_length =3D 0;=0A+ = }=0A+ /* Now search from the start of the array to next_fit_index= . */=0A+ if (!start_index)=0A+ break;=0A+ = end_index =3D start_index;=0A+ start_index =3D 0;=0A = }=0A =0A return -ENOMEM;=0A --=__Part5A74B30A.0__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__Part5A74B30A.0__=--