From patchwork Mon Oct 15 20:08:55 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Overstreet X-Patchwork-Id: 1595981 Return-Path: X-Original-To: patchwork-dm-devel@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from mx3-phx2.redhat.com (mx3-phx2.redhat.com [209.132.183.24]) by patchwork2.kernel.org (Postfix) with ESMTP id 4C59ADFB34 for ; Mon, 15 Oct 2012 20:14:51 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by mx3-phx2.redhat.com (8.13.8/8.13.8) with ESMTP id q9FKBADH010255; Mon, 15 Oct 2012 16:11:10 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id q9FKAOVm027027 for ; Mon, 15 Oct 2012 16:10:24 -0400 Received: from mx1.redhat.com (ext-mx13.extmail.prod.ext.phx2.redhat.com [10.5.110.18]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id q9FKAJ4t004804 for ; Mon, 15 Oct 2012 16:10:19 -0400 Received: from mail-da0-f46.google.com (mail-da0-f46.google.com [209.85.210.46]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q9FK9m2q007506 for ; Mon, 15 Oct 2012 16:10:18 -0400 Received: by mail-da0-f46.google.com with SMTP id n41so2761040dak.33 for ; Mon, 15 Oct 2012 13:10:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=KqcqdgCwwoGBzRZ5myUbT9Q3BENBKkmZdzmz/XqayPI=; b=jDkytH9wpCJg8fGURIALXH/YLnZmvwLJfra3/qxleyp1hQUx0mpvEhPjCz25by1QoS Opg+iMZ7HaBAwNE/xt6IQbeFidtZSc8FKkP1Z2MGV949TyVfMTHwtCB9Rk3v/beAkUhX hYtvX8UfsF8PuIAvlcGt1K150RwETzN4LtakruZONvWKTCJnPd7nz8jYepisjfYDIqDJ NvLTaoHpPr2LkV7tv2kVYllIanyCnZCtneLPzeBdzUAwlWRTUNXYI1RCpwF1yyS2l8l+ zWRaoCwVysSJ9PEX8LXYTSc99g5+bwiremgwF49E74kuD9wNuZ8fZDMvW12dwlERqrwd 19hA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references :x-gm-message-state; bh=KqcqdgCwwoGBzRZ5myUbT9Q3BENBKkmZdzmz/XqayPI=; b=d+inzBXY9n1bT8aFq4ChddJ5XmsOSbhBuQYiwrmIVRsRpPukjXYSRnnGlzfZF8j8ei L47iNl98l+2jJSvqr1ec/z8/bs76njPl3F74LHPcepE5rKukzUrHztYxEHjoJKfD6/iU 0YL15+mCPDDtFT/1yvafXaeG8kYLQNxtLYOI8vAVuJXRijkKmM+bYjhMX8hg3uy1mR6Y Iyo+4OIyDOnb0ILIvKaUghV0yAoO5Seqq5i/zlsuAZIJk2cstlrpnF3d22AOSLl7ec7p IkCwscy5m7/TrFRIC5huuPl7k9gotEcttANzWLyX60mEiyDxKT9YckrPUB1hLgFG+aHH WZJw== Received: by 10.68.195.226 with SMTP id ih2mr41101306pbc.9.1350331817866; Mon, 15 Oct 2012 13:10:17 -0700 (PDT) Received: from formenos.mtv.corp.google.com (formenos.mtv.corp.google.com [172.18.110.66]) by mx.google.com with ESMTPS id k9sm3021703paz.22.2012.10.15.13.10.16 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 15 Oct 2012 13:10:17 -0700 (PDT) From: Kent Overstreet To: linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org, dm-devel@redhat.com Date: Mon, 15 Oct 2012 13:08:55 -0700 Message-Id: <1350331769-14856-22-git-send-email-koverstreet@google.com> In-Reply-To: <1350331769-14856-1-git-send-email-koverstreet@google.com> References: <1350331769-14856-1-git-send-email-koverstreet@google.com> X-Gm-Message-State: ALoCoQkANOx/8G3jMUMjhyj3l1IOLejfC1QSKDAGnRDr1y1DwzSgi9qPEsy/a7GzKbmqG/h2KWuOIoS7eL7g8JjW12dSMRVNp5G8cZzUT4uaPTnt8YuQMtnCWaR13yHbVosmR1qrVDAcAn8Is9QUcfFQq1MywI4QeNNrYXXsy2NWFj3gPeJB9B0R8p0TJbs0iWqFau6p/drn X-RedHat-Spam-Score: -3.072 (BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_LOW, RP_MATCHES_RCVD, SPF_PASS) X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-Scanned-By: MIMEDefang 2.68 on 10.5.110.18 X-loop: dm-devel@redhat.com Cc: tj@kernel.org, axboe@kernel.dk, Kent Overstreet , vgoyal@redhat.com Subject: [dm-devel] [PATCH v4 21/24] block: Add bio_alloc_pages() X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk Reply-To: device-mapper development List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: dm-devel-bounces@redhat.com Errors-To: dm-devel-bounces@redhat.com More utility code to replace stuff that's getting open coded. Signed-off-by: Kent Overstreet CC: Jens Axboe CC: NeilBrown --- drivers/md/raid1.c | 16 +++------------- fs/bio.c | 28 ++++++++++++++++++++++++++++ include/linux/bio.h | 1 + 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/drivers/md/raid1.c b/drivers/md/raid1.c index dff0473..97f80b9 100644 --- a/drivers/md/raid1.c +++ b/drivers/md/raid1.c @@ -92,7 +92,6 @@ static void r1bio_pool_free(void *r1_bio, void *data) static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data) { struct pool_info *pi = data; - struct page *page; struct r1bio *r1_bio; struct bio *bio; int i, j; @@ -122,14 +121,10 @@ static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data) j = 1; while(j--) { bio = r1_bio->bios[j]; - for (i = 0; i < RESYNC_PAGES; i++) { - page = alloc_page(gfp_flags); - if (unlikely(!page)) - goto out_free_pages; + bio->bi_vcnt = RESYNC_PAGES; - bio->bi_io_vec[i].bv_page = page; - bio->bi_vcnt = i+1; - } + if (bio_alloc_pages(bio, gfp_flags)) + goto out_free_bio; } /* If not user-requests, copy the page pointers to all bios */ if (!test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery)) { @@ -143,11 +138,6 @@ static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data) return r1_bio; -out_free_pages: - for (j=0 ; j < pi->raid_disks; j++) - for (i=0; i < r1_bio->bios[j]->bi_vcnt ; i++) - put_page(r1_bio->bios[j]->bi_io_vec[i].bv_page); - j = -1; out_free_bio: while (++j < pi->raid_disks) bio_put(r1_bio->bios[j]); diff --git a/fs/bio.c b/fs/bio.c index de4b0e2..b3dceff 100644 --- a/fs/bio.c +++ b/fs/bio.c @@ -828,6 +828,34 @@ void bio_advance(struct bio *bio, unsigned bytes) EXPORT_SYMBOL(bio_advance); /** + * bio_alloc_pages - allocates a single page for each bvec in a bio + * @bio: bio to allocate pages for + * @gfp_mask: flags for allocation + * + * Allocates pages up to @bio->bi_vcnt. + * + * Returns 0 on success, -ENOMEM on failure. On failure, any allocated pages are + * freed. + */ +int bio_alloc_pages(struct bio *bio, gfp_t gfp_mask) +{ + int i; + struct bio_vec *bv; + + bio_for_each_segment_all(bv, bio, i) { + bv->bv_page = alloc_page(gfp_mask); + if (!bv->bv_page) { + while (--bv >= bio->bi_io_vec) + __free_page(bv->bv_page); + return -ENOMEM; + } + } + + return 0; +} +EXPORT_SYMBOL(bio_alloc_pages); + +/** * bio_copy_data - copy contents of data buffers from one chain of bios to * another * @src: source bio list diff --git a/include/linux/bio.h b/include/linux/bio.h index be080d2..8c02317 100644 --- a/include/linux/bio.h +++ b/include/linux/bio.h @@ -289,6 +289,7 @@ static inline void bio_flush_dcache_pages(struct bio *bi) #endif extern void bio_copy_data(struct bio *dst, struct bio *src); +extern int bio_alloc_pages(struct bio *bio, gfp_t gfp); extern struct bio *bio_copy_user(struct request_queue *, struct rq_map_data *, unsigned long, unsigned int, int, gfp_t);