Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp6573431ybx; Mon, 11 Nov 2019 11:10:16 -0800 (PST) X-Google-Smtp-Source: APXvYqzqxdTdRWxYo5QHu+sE5oxqE9bwVXXhZM8beHVVcI1AluITopfMxSAmCl76jCJuNaVTajhc X-Received: by 2002:a50:85cb:: with SMTP id q11mr27509951edh.141.1573499415988; Mon, 11 Nov 2019 11:10:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573499415; cv=none; d=google.com; s=arc-20160816; b=yFJAcArL2ukBMXHvV784Rk5yy+3A9d5xIMUy+b+TUaLKZogOMCq6Ckj/1GBYVi1X/D tP3TAMElczpED/1SX4D8DQ/E0oxQfP/rGOYuR9z7IPDXHlMMrDq3LqIe5tnTnd1t7Khz hFl7Hqz4MM5YgaA6N8ahL648FVnSoOg517PQ8J/ZBvsj5N90r3ij21sTUYmHN3oV8Zjn wj/UwV0vFyxLlUYTONS5mC3GStIiEBv9141Z//2x0kYRbeqdOvir9ncGAtcJaxQXNgK1 hw+tHkEqXsR+eKlc42BWnOtkE30S1v+vFQsXNnOdUmedv55N4vooRVZCN6QOLB6wcegX 6xjw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=l+jsL24oxZ2qy6M3ci/59xNO03qIpsKGF57I1pk2vas=; b=SrpCWwFCyKJU51GXqd0P7urQjoRJrseOCmmWyxRFT8Ue/t3lKKrJAKfOJldT78RKEn c1AoEtsi/ZHzJEOzy8lm38IM8OydlxPkxFDbdQUrv16rDWz+SqAX4f5foAC+XkTWCQGq ATzSEoRJAbE5MCzHpPeZhWxM57wHCUhifil57oUIlpuOgfdXGgVw51mwN2asLGYTy+y4 sq4RETtLZNfVHy81pPTwCCOZDEeAzVwFoZu+2nlJtcZkmfYagO0VdUWxjtuPiduSanSc wgJO+xAgHM5dcVJdmG6FL6g8Oyk4qHCB6/NC6MR2zsf/gvDkIjq6ouEv2Y6hIJDWU8Z+ vtIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=HoGrtoxq; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f21si12379482ede.253.2019.11.11.11.09.51; Mon, 11 Nov 2019 11:10:15 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=HoGrtoxq; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727047AbfKKSh4 (ORCPT + 99 others); Mon, 11 Nov 2019 13:37:56 -0500 Received: from mail.kernel.org ([198.145.29.99]:56586 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728904AbfKKSht (ORCPT ); Mon, 11 Nov 2019 13:37:49 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 91CA920659; Mon, 11 Nov 2019 18:37:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1573497467; bh=Mm0ZA3LWz7oDhX/RZfH/rHSFlb9+F76BScTso2kUMWU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HoGrtoxq3bXLAJCPe86B96m3ia3FGyCjvBP4mSOe8P5iG8kVeoCGa3bYJVVx08KFt GXNlqaOd3tWkmDJUHE+XvO1qgod9Bq+iBvD0a7EMPazgsAq8Pjm3GPl3AjgyRM0sI9 9bhVgFEiH9oOPL5fy4Ex2Mf5QQ4EtkTP+J2SlBRs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Bart Van Assche , Hannes Reinecke , Johannes Thumshirn , Jens Axboe Subject: [PATCH 4.14 064/105] lib/scatterlist: Introduce sgl_alloc() and sgl_free() Date: Mon, 11 Nov 2019 19:28:34 +0100 Message-Id: <20191111181445.222913711@linuxfoundation.org> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191111181421.390326245@linuxfoundation.org> References: <20191111181421.390326245@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Bart Van Assche commit e80a0af4759a164214f02da157a3800753ce135f upstream. Many kernel drivers contain code that allocates and frees both a scatterlist and the pages that populate that scatterlist. Introduce functions in lib/scatterlist.c that perform these tasks instead of duplicating this functionality in multiple drivers. Only include these functions in the build if CONFIG_SGL_ALLOC=y to avoid that the kernel size increases if this functionality is not used. Signed-off-by: Bart Van Assche Reviewed-by: Hannes Reinecke Reviewed-by: Johannes Thumshirn Signed-off-by: Jens Axboe Signed-off-by: Greg Kroah-Hartman --- include/linux/scatterlist.h | 10 ++++ lib/Kconfig | 4 + lib/scatterlist.c | 105 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+) --- a/include/linux/scatterlist.h +++ b/include/linux/scatterlist.h @@ -267,6 +267,16 @@ int sg_alloc_table_from_pages(struct sg_ unsigned long offset, unsigned long size, gfp_t gfp_mask); +#ifdef CONFIG_SGL_ALLOC +struct scatterlist *sgl_alloc_order(unsigned long long length, + unsigned int order, bool chainable, + gfp_t gfp, unsigned int *nent_p); +struct scatterlist *sgl_alloc(unsigned long long length, gfp_t gfp, + unsigned int *nent_p); +void sgl_free_order(struct scatterlist *sgl, int order); +void sgl_free(struct scatterlist *sgl); +#endif /* CONFIG_SGL_ALLOC */ + size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, void *buf, size_t buflen, off_t skip, bool to_buffer); --- a/lib/Kconfig +++ b/lib/Kconfig @@ -413,6 +413,10 @@ config HAS_DMA depends on !NO_DMA default y +config SGL_ALLOC + bool + default n + config DMA_NOOP_OPS bool depends on HAS_DMA && (!64BIT || ARCH_DMA_ADDR_T_64BIT) --- a/lib/scatterlist.c +++ b/lib/scatterlist.c @@ -433,6 +433,111 @@ int sg_alloc_table_from_pages(struct sg_ } EXPORT_SYMBOL(sg_alloc_table_from_pages); +#ifdef CONFIG_SGL_ALLOC + +/** + * sgl_alloc_order - allocate a scatterlist and its pages + * @length: Length in bytes of the scatterlist. Must be at least one + * @order: Second argument for alloc_pages() + * @chainable: Whether or not to allocate an extra element in the scatterlist + * for scatterlist chaining purposes + * @gfp: Memory allocation flags + * @nent_p: [out] Number of entries in the scatterlist that have pages + * + * Returns: A pointer to an initialized scatterlist or %NULL upon failure. + */ +struct scatterlist *sgl_alloc_order(unsigned long long length, + unsigned int order, bool chainable, + gfp_t gfp, unsigned int *nent_p) +{ + struct scatterlist *sgl, *sg; + struct page *page; + unsigned int nent, nalloc; + u32 elem_len; + + nent = round_up(length, PAGE_SIZE << order) >> (PAGE_SHIFT + order); + /* Check for integer overflow */ + if (length > (nent << (PAGE_SHIFT + order))) + return NULL; + nalloc = nent; + if (chainable) { + /* Check for integer overflow */ + if (nalloc + 1 < nalloc) + return NULL; + nalloc++; + } + sgl = kmalloc_array(nalloc, sizeof(struct scatterlist), + (gfp & ~GFP_DMA) | __GFP_ZERO); + if (!sgl) + return NULL; + + sg_init_table(sgl, nent); + sg = sgl; + while (length) { + elem_len = min_t(u64, length, PAGE_SIZE << order); + page = alloc_pages(gfp, order); + if (!page) { + sgl_free(sgl); + return NULL; + } + + sg_set_page(sg, page, elem_len, 0); + length -= elem_len; + sg = sg_next(sg); + } + WARN_ON_ONCE(sg); + if (nent_p) + *nent_p = nent; + return sgl; +} +EXPORT_SYMBOL(sgl_alloc_order); + +/** + * sgl_alloc - allocate a scatterlist and its pages + * @length: Length in bytes of the scatterlist + * @gfp: Memory allocation flags + * @nent_p: [out] Number of entries in the scatterlist + * + * Returns: A pointer to an initialized scatterlist or %NULL upon failure. + */ +struct scatterlist *sgl_alloc(unsigned long long length, gfp_t gfp, + unsigned int *nent_p) +{ + return sgl_alloc_order(length, 0, false, gfp, nent_p); +} +EXPORT_SYMBOL(sgl_alloc); + +/** + * sgl_free_order - free a scatterlist and its pages + * @sgl: Scatterlist with one or more elements + * @order: Second argument for __free_pages() + */ +void sgl_free_order(struct scatterlist *sgl, int order) +{ + struct scatterlist *sg; + struct page *page; + + for (sg = sgl; sg; sg = sg_next(sg)) { + page = sg_page(sg); + if (page) + __free_pages(page, order); + } + kfree(sgl); +} +EXPORT_SYMBOL(sgl_free_order); + +/** + * sgl_free - free a scatterlist and its pages + * @sgl: Scatterlist with one or more elements + */ +void sgl_free(struct scatterlist *sgl) +{ + sgl_free_order(sgl, 0); +} +EXPORT_SYMBOL(sgl_free); + +#endif /* CONFIG_SGL_ALLOC */ + void __sg_page_iter_start(struct sg_page_iter *piter, struct scatterlist *sglist, unsigned int nents, unsigned long pgoffset)