From patchwork Mon Sep 24 22:35:03 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Overstreet X-Patchwork-Id: 1500711 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 60D94DF280 for ; Mon, 24 Sep 2012 22:38:57 +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 q8OMb9LC030557; Mon, 24 Sep 2012 18:37:09 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id q8OMa4n6009346 for ; Mon, 24 Sep 2012 18:36:04 -0400 Received: from mx1.redhat.com (ext-mx14.extmail.prod.ext.phx2.redhat.com [10.5.110.19]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id q8OMZxx3003539 for ; Mon, 24 Sep 2012 18:35:59 -0400 Received: from mail-pb0-f46.google.com (mail-pb0-f46.google.com [209.85.160.46]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q8OMZU58013957 for ; Mon, 24 Sep 2012 18:35:58 -0400 Received: by mail-pb0-f46.google.com with SMTP id rr4so7786683pbb.33 for ; Mon, 24 Sep 2012 15:35:58 -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=E1QUSYGgb0HPQfamdpehlQqrx6FhiH+7WEiFkuq4P6s=; b=jEOHQmLrqspxZXGaBQx931I4X2ZYOd0ActmvpEFe9/PVq/5gomKwZB1KS3s7PH67NA 2+WHl/1dNaR8n5ztDrqgywUytmw0v5QfWkKSv6c3Z357E0WBs9NFPkaWKxT2VwTSMcOH 87BMkyl4vlgHgSh/O/na08uTgagzqgRuMmJqA4KXY7cusWym/0Vz/eLtu1iGPWRXbM5i utQ+2xhLU0rjdXSaQlDohc7oJwMJRB6Dw3Wz5L/Ftx4rxFKctTr9jdt87F/2j2+ECIhr z8PDxENlYGH8ZHqdMzFvSzdSdQrnNrBt78HhfJcAnWKEre8LfO0Zv1xlH9vsq8IXbK4S vv7A== 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=E1QUSYGgb0HPQfamdpehlQqrx6FhiH+7WEiFkuq4P6s=; b=U2a83zI7L4LeVnooG7iaFWoNoWP6eL9xCuyRINnjraElVZi68wObFA4YKNFUDb0rvx tdJByRg1oB1SS7lcmcyzC/7yGaJIC4FOfnVhZF6ejSFJjSjVh3/0sikIuQm4sqkCbaaR vP2mKpS0HjRBC16mBE1ROHTpRGKTELK8BfQd9eov3cQK85JU8+7z8na0vmpahq8oJlGo aKQQuKc4Jl7eS1TnN8hmZ/JLis7H9d0l0Wdf5CzVefxd4Ty6vWwagwQoX4PWVacbZ7Qs wF/DzmOip5yvuFfnBkNOrerHuWHmuH8KAgWwLvRHkNKrLB2VqumuUANM18iU6/hrKWxC CrSg== Received: by 10.66.72.197 with SMTP id f5mr35937173pav.20.1348526158211; Mon, 24 Sep 2012 15:35:58 -0700 (PDT) Received: by 10.66.72.197 with SMTP id f5mr35937016pav.20.1348526156729; Mon, 24 Sep 2012 15:35:56 -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 nu8sm133316pbc.45.2012.09.24.15.35.55 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 24 Sep 2012 15:35:56 -0700 (PDT) From: Kent Overstreet To: linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org, dm-devel@redhat.com Date: Mon, 24 Sep 2012 15:35:03 -0700 Message-Id: <1348526106-17074-24-git-send-email-koverstreet@google.com> In-Reply-To: <1348526106-17074-1-git-send-email-koverstreet@google.com> References: <1348526106-17074-1-git-send-email-koverstreet@google.com> X-Gm-Message-State: ALoCoQkAGVcHgOz2L4pnrXYi5qMbDnjmzhA5pL/THfbi6YUZKxB7YXRw1hUDpb03tNDrYPLhvYnJ7N+NOHqY/+UW7aE1Qoo9J4fv11wWjwb2zhd4cj4pK7bqQIeIAJjHuugHCrlUVbqkpisppArOOw2VWBi2dFTw7UqFzDVj1BStk73Azy4qK47KE1V7qIW/Ru/POaqMvJjD X-RedHat-Spam-Score: -3.512 (BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_LOW, RP_MATCHES_RCVD, SPF_PASS) X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 X-Scanned-By: MIMEDefang 2.68 on 10.5.110.19 X-loop: dm-devel@redhat.com Cc: tj@kernel.org, axboe@kernel.dk, Kent Overstreet , vgoyal@redhat.com Subject: [dm-devel] [PATCH v3 23/26] 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 6ba9f27..8e10d49 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 32966c9..5a55ce5 100644 --- a/include/linux/bio.h +++ b/include/linux/bio.h @@ -292,6 +292,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);