Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp1301734pxb; Sat, 9 Jan 2021 15:01:52 -0800 (PST) X-Google-Smtp-Source: ABdhPJzQscLlgQ3kq+6stv0kGzEZYL/P+RCPMCu1hm0m2mo9anpfmkWb0D2hv0RCHZHfPcMNZcfp X-Received: by 2002:a50:ccdb:: with SMTP id b27mr9923421edj.20.1610233311933; Sat, 09 Jan 2021 15:01:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610233311; cv=none; d=google.com; s=arc-20160816; b=kq1zO9IsLtpalxXIH/g5nwrHvLUtKCPguP0MqmFH0/coFiuyNE84aA+xD7ZioyKzpH oje7B5aLKv/orlLr3kexpqyaQvEFMDRqbqMfgEtMflBNSmQD9FMFoX1L8axGj7Lfcwja VKYiF8gGQsOgLssYOI6cSkZeKfyQKxdN83ZpLLTLCQLSLxk4XKnwHUkmZW0RflpjcYMR OxXvMtAVy+fr7ZqzflhNAUJtKaN+Hlz1haHqrmAVjySBU1JQdME8rKRXaWRJG0hJOgso CeR/90SzmiJbaCz4iWXQSTN4R9HWPG4uCV0bMI/eRMkUfqNQCqAN9MCONPTaQGFNH0eW PLkw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:reply-to; bh=7bQ85ywvdx0KKXCWfx1IjWXJpAofNmHnC7KzXbTGPS4=; b=D6SJPwG9GuQdu2/1TeGSjH4Tgm4jPy7XxAk7JTbhovVgWIUMOz013r5vAyvT+tLhzd VnJN2UVf6yyq0FNCCpl3DcQGJ3yA3CEh6DpcjKRzIYM24zCa2XFSzog0ueAuqid3to53 4xwVRH05CP1gOVA0n8s/M8HrOTRm0yD+OdmOvUpmtUZGFd9NTOWXw1Cr+NqQqB6et/7q jbX4erIGhcRWO5V4PBfA0zvcX1noEeWGqtnIQ49/3cZb+zAW3DuwQb3A5/RlKfpDoCeM vlNc2tb7jWtRhHbcceD+AWuu9I8/wRtNXwfQ681Vk2OEjZLCbEmF3nvByIB16B3BYy1D MRgQ== ARC-Authentication-Results: i=1; mx.google.com; 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 f25si1865890ejh.688.2021.01.09.15.01.26; Sat, 09 Jan 2021 15:01:51 -0800 (PST) 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; 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 S1726262AbhAIW7e (ORCPT + 99 others); Sat, 9 Jan 2021 17:59:34 -0500 Received: from mail-1.ca.inter.net ([208.85.220.69]:59948 "EHLO mail-1.ca.inter.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726068AbhAIW7e (ORCPT ); Sat, 9 Jan 2021 17:59:34 -0500 Received: from localhost (offload-3.ca.inter.net [208.85.220.70]) by mail-1.ca.inter.net (Postfix) with ESMTP id 3EEB82EA491; Sat, 9 Jan 2021 17:58:52 -0500 (EST) Received: from mail-1.ca.inter.net ([208.85.220.69]) by localhost (offload-3.ca.inter.net [208.85.220.70]) (amavisd-new, port 10024) with ESMTP id leBru8RgDJjg; Sat, 9 Jan 2021 17:45:52 -0500 (EST) Received: from [192.168.48.23] (host-104-157-204-209.dyn.295.ca [104.157.204.209]) (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) (Authenticated sender: dgilbert@interlog.com) by mail-1.ca.inter.net (Postfix) with ESMTPSA id 400562EA023; Sat, 9 Jan 2021 17:58:51 -0500 (EST) Reply-To: dgilbert@interlog.com Subject: Re: [PATCH v5 1/4] sgl_alloc_order: remove 4 GiB limit, sgl_free() warning To: Jason Gunthorpe Cc: linux-scsi@vger.kernel.org, linux-block@vger.kernel.org, target-devel@vger.kernel.org, linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org, martin.petersen@oracle.com, jejb@linux.vnet.ibm.com, bostroesser@gmail.com, bvanassche@acm.org, ddiss@suse.de References: <20201228234955.190858-1-dgilbert@interlog.com> <20201228234955.190858-2-dgilbert@interlog.com> <20210107174410.GB504133@ziepe.ca> From: Douglas Gilbert Message-ID: <76827f07-9484-d2c6-346b-0bdccfdf4a7a@interlog.com> Date: Sat, 9 Jan 2021 17:58:50 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <20210107174410.GB504133@ziepe.ca> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-CA Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2021-01-07 12:44 p.m., Jason Gunthorpe wrote: > On Mon, Dec 28, 2020 at 06:49:52PM -0500, Douglas Gilbert wrote: >> diff --git a/lib/scatterlist.c b/lib/scatterlist.c >> index a59778946404..4986545beef9 100644 >> +++ b/lib/scatterlist.c >> @@ -554,13 +554,15 @@ EXPORT_SYMBOL(sg_alloc_table_from_pages); >> #ifdef CONFIG_SGL_ALLOC >> >> /** >> - * sgl_alloc_order - allocate a scatterlist and its pages >> + * sgl_alloc_order - allocate a scatterlist with equally sized elements >> * @length: Length in bytes of the scatterlist. Must be at least one >> - * @order: Second argument for alloc_pages() >> + * @order: Second argument for alloc_pages(). Each sgl element size will >> + * be (PAGE_SIZE*2^order) bytes >> * @chainable: Whether or not to allocate an extra element in the scatterlist >> - * for scatterlist chaining purposes >> + * for scatterlist chaining purposes >> * @gfp: Memory allocation flags >> - * @nent_p: [out] Number of entries in the scatterlist that have pages >> + * @nent_p: [out] Number of entries in the scatterlist that have pages. >> + * Ignored if NULL is given. >> * >> * Returns: A pointer to an initialized scatterlist or %NULL upon failure. >> */ >> @@ -574,8 +576,8 @@ struct scatterlist *sgl_alloc_order(unsigned long long length, >> u32 elem_len; >> >> nent = round_up(length, PAGE_SIZE << order) >> (PAGE_SHIFT + order); >> - /* Check for integer overflow */ >> - if (length > (nent << (PAGE_SHIFT + order))) >> + /* Integer overflow if: length > nent*2^(PAGE_SHIFT+order) */ >> + if (ilog2(length) > ilog2(nent) + PAGE_SHIFT + order) >> return NULL; >> nalloc = nent; >> if (chainable) { > > This is a little bit too tortured now, how about this: > > if (length >> (PAGE_SHIFT + order) >= UINT_MAX) > return NULL; > nent = length >> (PAGE_SHIFT + order); > if (length & ((1ULL << (PAGE_SHIFT + order)) - 1)) > nent++; > > if (chainable) { > if (check_add_overflow(nent, 1, &nalloc)) > return NULL; > } > else > nalloc = nent; > And your proposal is less <> ? I'm looking at performance, not elegance and I'm betting that two ilog2() calls [which boil down to ffs()] are faster than two right-shift-by-n_s and one left-shift-by-n . Perhaps an extra comment could help my code by noting that mathematically: /* if n > m for positive n and m then: log(n) > log(m) */ My original preference was to drop the check all together but Bart Van Assche (who wrote that function) wanted me to keep it. Any function that takes 'order' (i.e. an exponent) can blow up given a silly value. The chainable check_add_overflow() call is new and an improvement. Doug Gilbert