Skip to contentSkip to conversation

fix /dev/sg allocation failures register

PATCH Applied Christoph Hellwig 6 participants 10 messages Apr 15 – Apr 21

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Hi all,

Shin'ichiro reported trivially triggerable allocation failures when using SG_IO on the sg driver. This turn out to be caused by the bio allocation rework, which makes non-blocking allocation fail much easier.

This series has two independent patches to fix this from different angles. The first one changes the completely pointless GFP_ATOMIC to use GFP_ATOMIC in sg. The other drops the reduction of the gfp mask in the bio allocator, so that atomic allocations of bios work as expected, even if they are a pretty bad idea as the bio submission actually needs a user context anyway. The only other uses of this seems to be the ocfs2 cluster managed.

#1 lore raw

PATCH 1/2 sg: don't use GFP_ATOMIC in sg_start_req

sg_start_req is called from normal user context and can sleep when waiting for memory. Switch it to use GFP_KERNEL, which fixes allocation failures seend with the bio_alloc rework.

Fixes b520c4eef83d ("block: split bio_alloc_bioset more clearly into a fast and slowpath")
---
 drivers/scsi/sg.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/sg.c +1 -1
index 37bac49f30f0..43265f012ce9 100644
@@ -1811,7 +1811,7 @@ sg_start_req(Sg_request *srp, unsigned char *cmd)
 	}
 
 	res = blk_rq_map_user_io(rq, md, hp->dxferp, hp->dxfer_len,
-			GFP_ATOMIC, iov_count, iov_count, 1, rw);
+			GFP_KERNEL, iov_count, iov_count, 1, rw);
 	if (!res) {
 		srp->bio = rq->bio;
 
Signature · 1 line
2.53.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] sg: don't use GFP_ATOMIC in sg_start_req

Quoted 8 lines · Christoph Hellwig

sg_start_req is called from normal user context and can sleep when waiting for memory. Switch it to use GFP_KERNEL, which fixes allocation failures seend with the bio_alloc rework.

Fixes b520c4eef83d ("block: split bio_alloc_bioset more clearly into a fast and slowpath")

FWIW:

Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] sg: don't use GFP_ATOMIC in sg_start_req

In reply to Christoph Hellwig
Quoted 17 lines · Christoph Hellwig

sg_start_req is called from normal user context and can sleep when waiting for memory. Switch it to use GFP_KERNEL, which fixes allocation failures seend with the bio_alloc rework.

Fixes b520c4eef83d ("block: split bio_alloc_bioset more clearly into a fast and slowpath")
---
  drivers/scsi/sg.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/sg.c +0 -0
index 37bac49f30f0..43265f012ce9 100644

drivers/scsi/sg.c sg_start_req(Sg_request *srp, unsigned char *cmd) · line 1817

Quoted 8 lines
drivers/scsi/sg.c +1 -1
@@ -1811,7 +1811,7 @@ sg_start_req(Sg_request *srp, unsigned char *cmd)
  	}
 
  	res = blk_rq_map_user_io(rq, md, hp->dxferp, hp->dxfer_len,
-			GFP_ATOMIC, iov_count, iov_count, 1, rw);
+			GFP_KERNEL, iov_count, iov_count, 1, rw);
  	if (!res) {
  		srp->bio = rq->bio;
 

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
maintainer Applied
#5 lore raw

Re: (subset) [PATCH 1/2] sg: don't use GFP_ATOMIC in sg_start_req

In reply to Christoph Hellwig
Quoted 3 lines · Christoph Hellwig

sg_start_req is called from normal user context and can sleep when waiting for memory. Switch it to use GFP_KERNEL, which fixes allocation failures seend with the bio_alloc rework.

Applied, thanks!

[2/2] block: only restrict bio allocation gfp mask asked to block

      commit: b5129bda5bbcceea5b2589c8248d39f77660aa19

Best regards,

Signature · 1 line
Jens Axboe
#6 lore raw

PATCH 2/2 block: only restrict bio allocation gfp mask asked to block

In reply to Christoph Hellwig

If the caller is asking for a non-blocking allocation, we should not further restrict the gfp mask, which just increases the likelihood of failures.

Fixes b520c4eef83d ("block: split bio_alloc_bioset more clearly into a fast and slowpath")
---
 block/bio.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
block/bio.c +2 -1
index 641ef0928d73..12341f760b9a 100644
@@ -544,7 +544,8 @@ struct bio *bio_alloc_bioset(struct block_device *bdev, unsigned short nr_vecs,
 	if (WARN_ON_ONCE(!mempool_initialized(&bs->bvec_pool) && nr_vecs > 0))
 		return NULL;
 
-	gfp = try_alloc_gfp(gfp);
+	if (saved_gfp & __GFP_DIRECT_RECLAIM)
+		gfp = try_alloc_gfp(gfp);
 	if (bs->cache && nr_vecs <= BIO_INLINE_VECS) {
 		/*
 		 * Set REQ_ALLOC_CACHE even if no cached bio is available to
Signature · 1 line
2.53.0
Reviewed-by on v1 2/2
#7 lore raw

Re: [PATCH 2/2] block: only restrict bio allocation gfp mask asked to block

Quoted 16 lines · Christoph Hellwig

If the caller is asking for a non-blocking allocation, we should not further restrict the gfp mask, which just increases the likelihood of failures.

Fixes b520c4eef83d ("block: split bio_alloc_bioset more clearly into a fast and slowpath")
---
  block/bio.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
block/bio.c +0 -0
index 641ef0928d73..12341f760b9a 100644

block/bio.c struct bio *bio_alloc_bioset(struct block_device *bdev, unsigned short nr_vecs, · line 551

Quoted 9 lines
block/bio.c +2 -1
@@ -544,7 +544,8 @@ struct bio *bio_alloc_bioset(struct block_device *bdev, unsigned short nr_vecs,
  	if (WARN_ON_ONCE(!mempool_initialized(&bs->bvec_pool) && nr_vecs > 0))
  		return NULL;
 
-	gfp = try_alloc_gfp(gfp);
+	if (saved_gfp & __GFP_DIRECT_RECLAIM)
+		gfp = try_alloc_gfp(gfp);
  	if (bs->cache && nr_vecs <= BIO_INLINE_VECS) {
  		/*
  		 * Set REQ_ALLOC_CACHE even if no cached bio is available to

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
#9 lore raw
In reply to Christoph Hellwig

Christoph,

Quoted 7 lines

This series has two independent patches to fix this from different angles. The first one changes the completely pointless GFP_ATOMIC to use GFP_ATOMIC in sg. The other drops the reduction of the gfp mask in the bio allocator, so that atomic allocations of bios work as expected, even if they are a pretty bad idea as the bio submission actually needs a user context anyway. The only other uses of this seems to be the ocfs2 cluster managed.

Applied #1 to 7.1/scsi-staging, thanks!

#2 needs to go through block.

Signature · 1 line
Martin K. Petersen