kfree(map);
        kfree(attr);
 
-       if (sb_page)
-               put_page(sb_page);
+       safe_put_page(sb_page);
 }
 
 static void bitmap_stop_daemon(struct bitmap *bitmap);
 
 out_free_pages:
        for (i=0; i < RESYNC_PAGES ; i++)
                for (j=0 ; j < pi->raid_disks; j++)
-                       put_page(r1_bio->bios[j]->bi_io_vec[i].bv_page);
+                       safe_put_page(r1_bio->bios[j]->bi_io_vec[i].bv_page);
        j = -1;
 out_free_bio:
        while ( ++j < pi->raid_disks )
                        if (j == 0 ||
                            r1bio->bios[j]->bi_io_vec[i].bv_page !=
                            r1bio->bios[0]->bi_io_vec[i].bv_page)
-                               put_page(r1bio->bios[j]->bi_io_vec[i].bv_page);
+                               safe_put_page(r1bio->bios[j]->bi_io_vec[i].bv_page);
                }
        for (i=0 ; i < pi->raid_disks; i++)
                bio_put(r1bio->bios[i]);
                        /* free extra copy of the data pages */
                        int i = bio->bi_vcnt;
                        while (i--)
-                               put_page(bio->bi_io_vec[i].bv_page);
+                               safe_put_page(bio->bi_io_vec[i].bv_page);
                }
                /* clear the bitmap if all writes complete successfully */
                bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
                if (conf->r1bio_pool)
                        mempool_destroy(conf->r1bio_pool);
                kfree(conf->mirrors);
-               put_page(conf->tmppage);
+               safe_put_page(conf->tmppage);
                kfree(conf->poolinfo);
                kfree(conf);
                mddev->private = NULL;
 
 
 out_free_pages:
        for ( ; i > 0 ; i--)
-               put_page(bio->bi_io_vec[i-1].bv_page);
+               safe_put_page(bio->bi_io_vec[i-1].bv_page);
        while (j--)
                for (i = 0; i < RESYNC_PAGES ; i++)
-                       put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
+                       safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
        j = -1;
 out_free_bio:
        while ( ++j < nalloc )
                struct bio *bio = r10bio->devs[j].bio;
                if (bio) {
                        for (i = 0; i < RESYNC_PAGES; i++) {
-                               put_page(bio->bi_io_vec[i].bv_page);
+                               safe_put_page(bio->bi_io_vec[i].bv_page);
                                bio->bi_io_vec[i].bv_page = NULL;
                        }
                        bio_put(bio);
 out_free_conf:
        if (conf->r10bio_pool)
                mempool_destroy(conf->r10bio_pool);
-       put_page(conf->tmppage);
+       safe_put_page(conf->tmppage);
        kfree(conf->mirrors);
        kfree(conf);
        mddev->private = NULL;
 
 abort:
        if (conf) {
                print_raid6_conf(conf);
-               if (conf->spare_page)
-                       put_page(conf->spare_page);
+               safe_put_page(conf->spare_page);
                kfree(conf->stripe_hashtbl);
                kfree(conf);
        }
 
        __wait_event_lock_irq(wq, condition, lock, cmd);                \
 } while (0)
 
+static inline void safe_put_page(struct page *p)
+{
+       if (p) put_page(p);
+}
+
 #endif