Received: by 10.223.185.116 with SMTP id b49csp4996525wrg; Wed, 7 Mar 2018 04:48:53 -0800 (PST) X-Google-Smtp-Source: AG47ELtB7R+pU5AHB0IsFVnnggPuPnSAQmjUPSJ3aUecGApRQ2K9IX7MqULpEBnF3twRO31QTRkv X-Received: by 10.98.202.138 with SMTP id y10mr22408784pfk.184.1520426933647; Wed, 07 Mar 2018 04:48:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520426933; cv=none; d=google.com; s=arc-20160816; b=fMMfFiOOpkgPjjBVvWa0fQZqi8k3jv3cGggNI5C36CIcNRe73sKhbCx/lL7hSQwCc2 UpkbhAv21zKkdtJUQxAfsm8nyRjVV8LgRoFth3yvrOYYJgNReKYjj8NlflF0cN4kNODr J5q8VIabU7H6Q/3a116MbgDBR4misWRoQ5QwPfyioQF5xNThtrkLsr24cvwCUzKH2138 omcgfRX6RvUgXRbaRQ+f1WuZ912zZZXZC+IUZ2rYkTvmoB/kjHN9O2l9UbNlTeP7eOZt FEqcNOWbVBkPEcxOjbVc3bd9UREQFnMdQr1PQ+eWYrKBaoWLDXJ/ZLQkk8CJtcJXVrv2 gtUw== 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:arc-authentication-results; bh=NC1P9VFW6XLQeGQ3JPIJu9/ngWEEvBvkm19G8hNl1YU=; b=HC1efVHo8HpW5zw/X+UJig+6asBRbOz1nL49mU1Akxey+8T7Bx9D4PEm77Qg9KZRSb uV1pGpmcALwmoSxHbSV6cnl7yq2ML6hK6TlFigKIvpxmVsPz9qISgTu6PnYMP0rE+Y1G ebyTIimImB7LCMJz1z3CPLSBJ2isP/XcS/fkF0SOG/1ICqrxCeBRR8GwLi6DTbrRLMtj 7NXmM88kn+xFh+eoU5LDH6CMurOBlkK1OxeFDujL78+R1HgbvNyjj2KHC2gc7DKF4FO/ d1+bmS/WOG9ew8hQx99fxpVUshE8jWHPs3ln/8RwRJDr/2J05bKRuTI5JGugzk4IY2DH amcg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ursulin-net.20150623.gappssmtp.com header.s=20150623 header.b=l4yRWgaA; 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 r17si11437737pgo.179.2018.03.07.04.48.39; Wed, 07 Mar 2018 04:48:53 -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=@ursulin-net.20150623.gappssmtp.com header.s=20150623 header.b=l4yRWgaA; 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 S1754394AbeCGMrm (ORCPT + 99 others); Wed, 7 Mar 2018 07:47:42 -0500 Received: from mail-wr0-f196.google.com ([209.85.128.196]:35274 "EHLO mail-wr0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751142AbeCGMra (ORCPT ); Wed, 7 Mar 2018 07:47:30 -0500 Received: by mail-wr0-f196.google.com with SMTP id l43so2044837wrc.2 for ; Wed, 07 Mar 2018 04:47:30 -0800 (PST) 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=NC1P9VFW6XLQeGQ3JPIJu9/ngWEEvBvkm19G8hNl1YU=; b=l4yRWgaAN9u6+20CACWminBAO5JNXxIbYUm9LHvlAgutk3scgRZaFqo3gyuRCH5SCx N+dZXrJ0Hr6fwYxkp+2nOV0XHmFIqp6+HnZxA170FZnz6cH3VgddX5tJ1BwC2t8ZhM0W 9reEMW26K+HE8GzSzt3vPUDd7SFbknB7eRGzwQFU89L2mI4hsuayLDaMaU4a6mS+QBTd xgzrpprW1rIEDzmf3ZB8LiqEMoF4NxIfYWFQauJ1Rd2Cl+h0fsyDy4hTZkbkVKgDQpnS XfWXb5kCXpgYbSKgUtcbcCbgkoUTl6NugSKirsRGYnM6OD8wa3MIrZSgIZ1yYPc6W2HT TDGA== 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=NC1P9VFW6XLQeGQ3JPIJu9/ngWEEvBvkm19G8hNl1YU=; b=BbKlseleFAF/R+dwYpfSGD4PPT+pFo54sViceXuaDmpIWRLoqAGzVcuIj44KW4n6/j giyCmpQtCEVSF4AntKZddBhlXKqV2XHBiwYyehzhNhx1VmrHMoZsZB4ngB9KS6pxBpdq VKZLM69eqVP/svri0D2pt5NXaXeILUED2494AZiFhDTkrtNdhhwX5D6SLUq02ru6Qolx INN1JUnbb7eocDP2Bqusu4KO0fu5hDjKilzB/e0G25GE3HvSujLkwTDRkVHB+Ll+86cT 3OPjb6JWf6zqtF8VpBVDSPhsqHhw7FfgwtWXbQMd72wiZ4BJzpHS79FOMs4lWLzETPXQ /LzQ== X-Gm-Message-State: APf1xPAAeOXaeDi83kSTNQe+gpgMnYtoyqLFPuuuI3GY+pvWepJt9WXN LLHstuGFKtaC7WUpVC7yG+OXpHAj X-Received: by 10.223.161.211 with SMTP id v19mr19991312wrv.184.1520426849091; Wed, 07 Mar 2018 04:47:29 -0800 (PST) Received: from localhost.localdomain ([95.146.144.186]) by smtp.gmail.com with ESMTPSA id x78sm24325756wmd.2.2018.03.07.04.47.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 07 Mar 2018 04:47:28 -0800 (PST) From: Tvrtko Ursulin X-Google-Original-From: Tvrtko Ursulin To: linux-kernel@vger.kernel.org Cc: Tvrtko Ursulin , Bart Van Assche , Hannes Reinecke , Johannes Thumshirn , Jens Axboe Subject: [PATCH 3/6] lib/scatterlist: Do not leak pages when high-order allocation fails Date: Wed, 7 Mar 2018 12:47:09 +0000 Message-Id: <20180307124712.14963-4-tvrtko.ursulin@linux.intel.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20180307124712.14963-1-tvrtko.ursulin@linux.intel.com> References: <20180307124712.14963-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 9884be50a2c0..e13a759c5c49 100644 --- a/lib/scatterlist.c +++ b/lib/scatterlist.c @@ -493,7 +493,7 @@ struct scatterlist *sgl_alloc_order(unsigned long length, unsigned int order, { unsigned int chunk_len = PAGE_SIZE << order; struct scatterlist *sgl, *sg; - unsigned int nent; + unsigned int nent, i; nent = round_up(length, chunk_len) >> (PAGE_SHIFT + order); @@ -517,11 +517,12 @@ struct scatterlist *sgl_alloc_order(unsigned long length, unsigned int order, sg_init_table(sgl, nent); sg = sgl; + i = 0; while (length) { struct page *page = alloc_pages(gfp, order); if (!page) { - sgl_free(sgl); + sgl_free_n_order(sgl, i, order); return NULL; } @@ -529,6 +530,7 @@ struct scatterlist *sgl_alloc_order(unsigned long length, unsigned int order, sg_set_page(sg, page, chunk_len, 0); length -= chunk_len; sg = sg_next(sg); + i++; } WARN_ONCE(length, "length = %ld\n", length); return sgl; -- 2.14.1