Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp819187imm; Wed, 26 Sep 2018 07:17:33 -0700 (PDT) X-Google-Smtp-Source: ACcGV61TZ0S/kKdZGnLeY5O65sx0P0eoYvInmI689MwVqr21C/BSsxDg0C70p1IzGq+m1gYJNsZY X-Received: by 2002:a63:ca09:: with SMTP id n9-v6mr5936464pgi.287.1537971453809; Wed, 26 Sep 2018 07:17:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537971453; cv=none; d=google.com; s=arc-20160816; b=IMxEUDjDd+PhoqR7l9OAjgXL8Ep4lDoh9s5m0u7E9JTL4FNExgKUTQAEVW1QnIWR+P zn5zok5XWneLiBDgWXPlGKi7MJ0qZricGo4KtrEgVep1C8DRUtn68nG4TimBhtue36/L 9pm28vMra0mwu0vjkNcjgx5ZKRhSKptuq/xGvLn8/gOJV33SqtoCgUxTB/6yowCHMXVz iKfZsMVESfoEy16RUcC7l70dzP94SqkOrlvI/kia7dpXqd+lwUo2Sa2p7aWvGqdUr4Ff CUVeNqLe3Y+TD6D2u/bHCWypHHaTqPv3VneqDtfSG6oUC1mSUUvXNQE4Dx6TXa5yZNcu wuog== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=6GEgMJo7HbdKLlrJg81sd/Zf0U2XQ8tZJ/V/bTDDrzs=; b=zdhp1NyhcjnCykPFLl3Md3VcX+AfB48Wq1/SZ/BA/eXmA9lBCdugu5q5kWpR4e8C0a /JCPO1K61WNT1MWJ+LyO0eYnzALv4GNl89CGt6j9gRG5LFTQOUQPWMyntd7p++dVPwlY CXqqGzC4Y0WeCBObJ1gMPkqO/MJAHyrAh0qHyYUv54fV8RqF6EI9BQsBXiYVbsSYdg/k LX2T2wELD9aPS1uuqf4dl9pFp/rVLH4hjOPX95npzJV4dIeeU85MysgFsJ8sT8qdSp1a y00cd8uhE/+rWP2XnUHeioqrFtSOcXzUmj6Lxoj8ZHglo+MGuBDbFjyHMotGfPxfbf83 riwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ursulin-net.20150623.gappssmtp.com header.s=20150623 header.b=L6UV7uhZ; 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 u8-v6si5312284plh.253.2018.09.26.07.17.18; Wed, 26 Sep 2018 07:17:33 -0700 (PDT) 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=@ursulin-net.20150623.gappssmtp.com header.s=20150623 header.b=L6UV7uhZ; 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 S1728251AbeIZUaG (ORCPT + 99 others); Wed, 26 Sep 2018 16:30:06 -0400 Received: from mail-wm1-f67.google.com ([209.85.128.67]:53178 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727045AbeIZU3t (ORCPT ); Wed, 26 Sep 2018 16:29:49 -0400 Received: by mail-wm1-f67.google.com with SMTP id l7-v6so2482937wme.2 for ; Wed, 26 Sep 2018 07:16:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ursulin-net.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=6GEgMJo7HbdKLlrJg81sd/Zf0U2XQ8tZJ/V/bTDDrzs=; b=L6UV7uhZAvXSHMWXKay+k1KVVcRT2PwJfMvC21hxUH9GSzMOIqPGpmYelD7opV8Yd4 iFpy9ZQlv416kfILPV+dekfDtR9X/Hvm62tJCUWqxocVFDhi38h10jYND65t5Z0oxwkQ idRh58Pi9yeNigvIH3EiDSvRTsFXkEGfNEoY6bw6Tj44tRsW0bV+YXy9DwuhtB6Xnocp iaQJ/ZdDVhiroQKQGg55PX1uFLOHJdNVn/3MK8Hr3No0+LjRjWElIZ6zdcbFdkPrirPW AfZo3aky+9tIoh+9IVYrJBWkLBh2Q1m+TLDvFwv7nHwh/FhAkbKST5YpzK6paGuTf2WW LW9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=6GEgMJo7HbdKLlrJg81sd/Zf0U2XQ8tZJ/V/bTDDrzs=; b=Qrv+K+5KBORVI9RgpyZtnoj0zjcBMphQVsr7diGNEsRWVx6xJSFd0dmnhZhWGbY2Zo 2ojOdON8ZAtseOOfnUs9AuenSyQqipNkXUKT0a9FC5ypb+vM9Fq7QhlUUbBxfVjihzns 8u4Z1PJoHQsMP0ekVwHukwxrJ2zFDDYHhhoCpzo8jMbsEqNHpBfcRJqLXLLfhnUfV4V9 VfJGtYbnF0VDq8vzpG5+ruVu7z1wI/wwved8wXHAZf9k23QgbF8pTgIuQ+9nVQfaKPAb tPOnEoUvA67X+imT4gcanZtSDZ51j/QBFmlbowu+BpwLSngsakYnzKCy5P04wSJ7yNF3 +whg== X-Gm-Message-State: ABuFfoi6TV8VYTSypozDIqcjuPlZuHHWhlX4eQxaxWwbdhMoxvgdsC+J s90Sat2nMqYaqf6PET1sPHZl5vrORC0= X-Received: by 2002:a1c:88a:: with SMTP id 132-v6mr4772071wmi.9.1537971397936; Wed, 26 Sep 2018 07:16:37 -0700 (PDT) Received: from localhost.localdomain ([95.144.165.37]) by smtp.gmail.com with ESMTPSA id v14-v6sm3209974wmh.41.2018.09.26.07.16.37 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 26 Sep 2018 07:16:37 -0700 (PDT) From: Tvrtko Ursulin X-Google-Original-From: Tvrtko Ursulin To: linux-kernel@vger.kernel.org Cc: tursulin@ursulin.net, tvrtko.ursulin@linux.intel.com, Tvrtko Ursulin , Bart Van Assche , Hannes Reinecke , Johannes Thumshirn , Jens Axboe Subject: [PATCH 4/6] lib/scatterlist: Do not leak pages when high-order allocation fails Date: Wed, 26 Sep 2018 15:16:23 +0100 Message-Id: <20180926141625.17727-5-tvrtko.ursulin@linux.intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180926141625.17727-1-tvrtko.ursulin@linux.intel.com> References: <20180926141625.17727-1-tvrtko.ursulin@linux.intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Tvrtko Ursulin If a higher-order allocation fails, the existing abort and cleanup path would consider all segments allocated so far as 0-order page allocations and would therefore leak memory. Fix this by cleaning up using sgl_free_n_order which allows the correct page order to be passed in. Signed-off-by: Tvrtko Ursulin Cc: Bart Van Assche Cc: Hannes Reinecke Cc: Johannes Thumshirn Cc: Jens Axboe --- lib/scatterlist.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/scatterlist.c b/lib/scatterlist.c index 3cc01cd82242..0caed79d7291 100644 --- a/lib/scatterlist.c +++ b/lib/scatterlist.c @@ -481,7 +481,7 @@ struct scatterlist *sgl_alloc_order(unsigned long length, unsigned int order, { struct scatterlist *sgl, *sg; struct page *page; - unsigned int nent, nalloc; + unsigned int nent, nalloc, i; u32 elem_len; nent = round_up(length, PAGE_SIZE << order) >> (PAGE_SHIFT + order); @@ -501,17 +501,19 @@ struct scatterlist *sgl_alloc_order(unsigned long length, unsigned int order, sg_init_table(sgl, nalloc); sg = sgl; + i = 0; while (length) { elem_len = min_t(u64, length, PAGE_SIZE << order); page = alloc_pages(gfp, order); if (!page) { - sgl_free(sgl); + sgl_free_n_order(sgl, i, order); return NULL; } sg_set_page(sg, page, elem_len, 0); length -= elem_len; sg = sg_next(sg); + i++; } WARN_ONCE(length, "length = %ld\n", length); if (nent_p) -- 2.17.1