* [PATCH v2 1/2] target: pscsi: avoid OOM in pscsi_map_sg()
@ 2021-03-23 21:24 mwilck
2021-03-23 21:24 ` [PATCH v2 2/2] target: pscsi: cleanup after failure " mwilck
` (3 more replies)
0 siblings, 4 replies; 7+ messages in thread
From: mwilck @ 2021-03-23 21:24 UTC (permalink / raw)
To: Martin K. Petersen, Christoph Hellwig
Cc: linux-scsi, target-devel, David Disseldorp, Jürgen Groß,
Chaitanya Kulkarni, Martin Wilck
From: Martin Wilck <mwilck@suse.com>
pscsi_map_sg() uses the variable nr_pages as a hint for bio_kmalloc()
how many vector elements to allocate. If nr_pages is < BIO_MAX_PAGES,
it will be reset to 0 after successful allocation of the bio.
If bio_add_pc_page() fails later for whatever reason, pscsi_map_sg()
tries to allocate another bio, passing nr_vecs=0. This causes
bio_add_pc_page() to fail immediately in the next call. pci_map_sg()
continues to allocate zero-length bios until memory is exhausted and
the kernel crashes with OOM. This can be easily observed by exporting
a SATA DVD drive via pscsi. The target crashes as soon as the client
tries to access the DVD LUN. In the case I analyzed, bio_add_pc_page()
would fail because the DVD device's max_sectors_kb (128) was
exceeded.
Avoid this by simply not resetting nr_pages to 0 after allocating the
bio. This way, the client receives an IO error when it tries to send
requests exceeding the devices max_sectors_kb, and eventually gets
it right. The client must still limit max_sectors_kb e.g. by an udev
rule if (like in my case) the driver doesn't report valid block
limits, otherwise it encounters I/O errors.
Signed-off-by: Martin Wilck <mwilck@suse.com>
---
drivers/target/target_core_pscsi.c | 1 -
1 file changed, 1 deletion(-)
diff --git a/drivers/target/target_core_pscsi.c b/drivers/target/target_core_pscsi.c
index 7b1035e..977362d 100644
--- a/drivers/target/target_core_pscsi.c
+++ b/drivers/target/target_core_pscsi.c
@@ -881,7 +881,6 @@ pscsi_map_sg(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
if (!bio) {
new_bio:
nr_vecs = bio_max_segs(nr_pages);
- nr_pages -= nr_vecs;
/*
* Calls bio_kmalloc() and sets bio->bi_end_io()
*/
--
2.26.2
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 2/2] target: pscsi: cleanup after failure in pscsi_map_sg()
2021-03-23 21:24 [PATCH v2 1/2] target: pscsi: avoid OOM in pscsi_map_sg() mwilck
@ 2021-03-23 21:24 ` mwilck
2021-03-24 7:14 ` Christoph Hellwig
2021-03-24 17:24 ` Lee Duncan
2021-03-24 7:14 ` [PATCH v2 1/2] target: pscsi: avoid OOM " Christoph Hellwig
` (2 subsequent siblings)
3 siblings, 2 replies; 7+ messages in thread
From: mwilck @ 2021-03-23 21:24 UTC (permalink / raw)
To: Martin K. Petersen, Christoph Hellwig
Cc: linux-scsi, target-devel, David Disseldorp, Jürgen Groß,
Chaitanya Kulkarni, Martin Wilck
From: Martin Wilck <mwilck@suse.com>
If pscsi_map_sg() fails, make sure to drop references to already
allocated bios.
Signed-off-by: Martin Wilck <mwilck@suse.com>
---
drivers/target/target_core_pscsi.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/drivers/target/target_core_pscsi.c b/drivers/target/target_core_pscsi.c
index 977362d..1c9aeab 100644
--- a/drivers/target/target_core_pscsi.c
+++ b/drivers/target/target_core_pscsi.c
@@ -937,6 +937,14 @@ pscsi_map_sg(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
return 0;
fail:
+ if (bio)
+ bio_put(bio);
+ while (req->bio) {
+ bio = req->bio;
+ req->bio = bio->bi_next;
+ bio_put(bio);
+ }
+ req->biotail = NULL;
return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
}
--
2.26.2
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v2 1/2] target: pscsi: avoid OOM in pscsi_map_sg()
2021-03-23 21:24 [PATCH v2 1/2] target: pscsi: avoid OOM in pscsi_map_sg() mwilck
2021-03-23 21:24 ` [PATCH v2 2/2] target: pscsi: cleanup after failure " mwilck
@ 2021-03-24 7:14 ` Christoph Hellwig
2021-03-24 17:24 ` Lee Duncan
2021-03-25 3:53 ` Martin K. Petersen
3 siblings, 0 replies; 7+ messages in thread
From: Christoph Hellwig @ 2021-03-24 7:14 UTC (permalink / raw)
To: mwilck
Cc: Martin K. Petersen, Christoph Hellwig, linux-scsi, target-devel,
David Disseldorp, Jürgen Groß,
Chaitanya Kulkarni
On Tue, Mar 23, 2021 at 10:24:30PM +0100, mwilck@suse.com wrote:
> From: Martin Wilck <mwilck@suse.com>
>
> pscsi_map_sg() uses the variable nr_pages as a hint for bio_kmalloc()
> how many vector elements to allocate. If nr_pages is < BIO_MAX_PAGES,
> it will be reset to 0 after successful allocation of the bio.
>
> If bio_add_pc_page() fails later for whatever reason, pscsi_map_sg()
> tries to allocate another bio, passing nr_vecs=0. This causes
> bio_add_pc_page() to fail immediately in the next call. pci_map_sg()
> continues to allocate zero-length bios until memory is exhausted and
> the kernel crashes with OOM. This can be easily observed by exporting
> a SATA DVD drive via pscsi. The target crashes as soon as the client
> tries to access the DVD LUN. In the case I analyzed, bio_add_pc_page()
> would fail because the DVD device's max_sectors_kb (128) was
> exceeded.
>
> Avoid this by simply not resetting nr_pages to 0 after allocating the
> bio. This way, the client receives an IO error when it tries to send
> requests exceeding the devices max_sectors_kb, and eventually gets
> it right. The client must still limit max_sectors_kb e.g. by an udev
> rule if (like in my case) the driver doesn't report valid block
> limits, otherwise it encounters I/O errors.
>
> Signed-off-by: Martin Wilck <mwilck@suse.com>
Looks good,
Reviewed-by: Christoph Hellwig <hch@lst.de>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 2/2] target: pscsi: cleanup after failure in pscsi_map_sg()
2021-03-23 21:24 ` [PATCH v2 2/2] target: pscsi: cleanup after failure " mwilck
@ 2021-03-24 7:14 ` Christoph Hellwig
2021-03-24 17:24 ` Lee Duncan
1 sibling, 0 replies; 7+ messages in thread
From: Christoph Hellwig @ 2021-03-24 7:14 UTC (permalink / raw)
To: mwilck
Cc: Martin K. Petersen, Christoph Hellwig, linux-scsi, target-devel,
David Disseldorp, Jürgen Groß,
Chaitanya Kulkarni
On Tue, Mar 23, 2021 at 10:24:31PM +0100, mwilck@suse.com wrote:
> From: Martin Wilck <mwilck@suse.com>
>
> If pscsi_map_sg() fails, make sure to drop references to already
> allocated bios.
>
> Signed-off-by: Martin Wilck <mwilck@suse.com>
Looks good,
Reviewed-by: Christoph Hellwig <hch@lst.de>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 1/2] target: pscsi: avoid OOM in pscsi_map_sg()
2021-03-23 21:24 [PATCH v2 1/2] target: pscsi: avoid OOM in pscsi_map_sg() mwilck
2021-03-23 21:24 ` [PATCH v2 2/2] target: pscsi: cleanup after failure " mwilck
2021-03-24 7:14 ` [PATCH v2 1/2] target: pscsi: avoid OOM " Christoph Hellwig
@ 2021-03-24 17:24 ` Lee Duncan
2021-03-25 3:53 ` Martin K. Petersen
3 siblings, 0 replies; 7+ messages in thread
From: Lee Duncan @ 2021-03-24 17:24 UTC (permalink / raw)
To: mwilck, Martin K. Petersen, Christoph Hellwig
Cc: linux-scsi, target-devel, David Disseldorp, Jürgen Groß,
Chaitanya Kulkarni
On 3/23/21 2:24 PM, mwilck@suse.com wrote:
> From: Martin Wilck <mwilck@suse.com>
>
> pscsi_map_sg() uses the variable nr_pages as a hint for bio_kmalloc()
> how many vector elements to allocate. If nr_pages is < BIO_MAX_PAGES,
> it will be reset to 0 after successful allocation of the bio.
>
> If bio_add_pc_page() fails later for whatever reason, pscsi_map_sg()
> tries to allocate another bio, passing nr_vecs=0. This causes
> bio_add_pc_page() to fail immediately in the next call. pci_map_sg()
> continues to allocate zero-length bios until memory is exhausted and
> the kernel crashes with OOM. This can be easily observed by exporting
> a SATA DVD drive via pscsi. The target crashes as soon as the client
> tries to access the DVD LUN. In the case I analyzed, bio_add_pc_page()
> would fail because the DVD device's max_sectors_kb (128) was
> exceeded.
>
> Avoid this by simply not resetting nr_pages to 0 after allocating the
> bio. This way, the client receives an IO error when it tries to send
> requests exceeding the devices max_sectors_kb, and eventually gets
> it right. The client must still limit max_sectors_kb e.g. by an udev
> rule if (like in my case) the driver doesn't report valid block
> limits, otherwise it encounters I/O errors.
>
> Signed-off-by: Martin Wilck <mwilck@suse.com>
> ---
> drivers/target/target_core_pscsi.c | 1 -
> 1 file changed, 1 deletion(-)
>
> diff --git a/drivers/target/target_core_pscsi.c b/drivers/target/target_core_pscsi.c
> index 7b1035e..977362d 100644
> --- a/drivers/target/target_core_pscsi.c
> +++ b/drivers/target/target_core_pscsi.c
> @@ -881,7 +881,6 @@ pscsi_map_sg(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
> if (!bio) {
> new_bio:
> nr_vecs = bio_max_segs(nr_pages);
> - nr_pages -= nr_vecs;
> /*
> * Calls bio_kmalloc() and sets bio->bi_end_io()
> */
>
Reviewed-by: Lee Duncan <lduncan@suse.com>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 2/2] target: pscsi: cleanup after failure in pscsi_map_sg()
2021-03-23 21:24 ` [PATCH v2 2/2] target: pscsi: cleanup after failure " mwilck
2021-03-24 7:14 ` Christoph Hellwig
@ 2021-03-24 17:24 ` Lee Duncan
1 sibling, 0 replies; 7+ messages in thread
From: Lee Duncan @ 2021-03-24 17:24 UTC (permalink / raw)
To: mwilck, Martin K. Petersen, Christoph Hellwig
Cc: linux-scsi, target-devel, David Disseldorp, Jürgen Groß,
Chaitanya Kulkarni
On 3/23/21 2:24 PM, mwilck@suse.com wrote:
> From: Martin Wilck <mwilck@suse.com>
>
> If pscsi_map_sg() fails, make sure to drop references to already
> allocated bios.
>
> Signed-off-by: Martin Wilck <mwilck@suse.com>
> ---
> drivers/target/target_core_pscsi.c | 8 ++++++++
> 1 file changed, 8 insertions(+)
>
> diff --git a/drivers/target/target_core_pscsi.c b/drivers/target/target_core_pscsi.c
> index 977362d..1c9aeab 100644
> --- a/drivers/target/target_core_pscsi.c
> +++ b/drivers/target/target_core_pscsi.c
> @@ -937,6 +937,14 @@ pscsi_map_sg(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
>
> return 0;
> fail:
> + if (bio)
> + bio_put(bio);
> + while (req->bio) {
> + bio = req->bio;
> + req->bio = bio->bi_next;
> + bio_put(bio);
> + }
> + req->biotail = NULL;
> return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
> }
>
>
Reviewed-by: Lee Duncan <lduncan@suse.com>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 1/2] target: pscsi: avoid OOM in pscsi_map_sg()
2021-03-23 21:24 [PATCH v2 1/2] target: pscsi: avoid OOM in pscsi_map_sg() mwilck
` (2 preceding siblings ...)
2021-03-24 17:24 ` Lee Duncan
@ 2021-03-25 3:53 ` Martin K. Petersen
3 siblings, 0 replies; 7+ messages in thread
From: Martin K. Petersen @ 2021-03-25 3:53 UTC (permalink / raw)
To: Christoph Hellwig, mwilck
Cc: Martin K . Petersen, David Disseldorp, Chaitanya Kulkarni,
linux-scsi, Jürgen Groß,
target-devel
On Tue, 23 Mar 2021 22:24:30 +0100, mwilck@suse.com wrote:
> pscsi_map_sg() uses the variable nr_pages as a hint for bio_kmalloc()
> how many vector elements to allocate. If nr_pages is < BIO_MAX_PAGES,
> it will be reset to 0 after successful allocation of the bio.
>
> If bio_add_pc_page() fails later for whatever reason, pscsi_map_sg()
> tries to allocate another bio, passing nr_vecs=0. This causes
> bio_add_pc_page() to fail immediately in the next call. pci_map_sg()
> continues to allocate zero-length bios until memory is exhausted and
> the kernel crashes with OOM. This can be easily observed by exporting
> a SATA DVD drive via pscsi. The target crashes as soon as the client
> tries to access the DVD LUN. In the case I analyzed, bio_add_pc_page()
> would fail because the DVD device's max_sectors_kb (128) was
> exceeded.
>
> [...]
Applied to 5.12/scsi-fixes, thanks!
[1/2] target: pscsi: avoid OOM in pscsi_map_sg()
https://git.kernel.org/mkp/scsi/c/077ce028b8e0
[2/2] target: pscsi: cleanup after failure in pscsi_map_sg()
https://git.kernel.org/mkp/scsi/c/36fa766faa0c
--
Martin K. Petersen Oracle Linux Engineering
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2021-03-25 3:54 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-23 21:24 [PATCH v2 1/2] target: pscsi: avoid OOM in pscsi_map_sg() mwilck
2021-03-23 21:24 ` [PATCH v2 2/2] target: pscsi: cleanup after failure " mwilck
2021-03-24 7:14 ` Christoph Hellwig
2021-03-24 17:24 ` Lee Duncan
2021-03-24 7:14 ` [PATCH v2 1/2] target: pscsi: avoid OOM " Christoph Hellwig
2021-03-24 17:24 ` Lee Duncan
2021-03-25 3:53 ` Martin K. Petersen
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.