mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Juergen Gross <jgross@suse.com>
To: Jan Beulich <jbeulich@suse.com>
Cc: Wei Liu <wei.liu@kernel.org>, Paul Durrant <paul@xen.org>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	xen-devel@lists.xenproject.org, stable@vger.kernel.org,
	linux-kernel@vger.kernel.org, netdev@vger.kernel.org
Subject: Re: [PATCH 1/2] xen/netback: don't do grant copy across page boundary
Date: Mon, 27 Mar 2023 18:22:12 +0200	[thread overview]
Message-ID: <f94dbfe9-f690-8c3e-c251-b0d5f93d32f9@suse.com> (raw)
In-Reply-To: <89653286-f05e-1fc1-b6bf-265b7ecaad0d@suse.com>


[-- Attachment #1.1.1: Type: text/plain, Size: 5229 bytes --]

On 27.03.23 17:38, Jan Beulich wrote:
> On 27.03.2023 12:07, Juergen Gross wrote:
>> On 27.03.23 11:49, Jan Beulich wrote:
>>> On 27.03.2023 10:36, Juergen Gross wrote:
>>>> @@ -413,6 +418,13 @@ static void xenvif_get_requests(struct xenvif_queue *queue,
>>>>    		cop->dest.u.gmfn = virt_to_gfn(skb->data + skb_headlen(skb)
>>>>    				               - data_len);
>>>>    
>>>> +		/* Don't cross local page boundary! */
>>>> +		if (cop->dest.offset + amount > XEN_PAGE_SIZE) {
>>>> +			amount = XEN_PAGE_SIZE - cop->dest.offset;
>>>> +			XENVIF_TX_CB(skb)->split_mask |= 1U << copy_count(skb);
>>>
>>> Maybe worthwhile to add a BUILD_BUG_ON() somewhere to make sure this
>>> shift won't grow too large a shift count. The number of slots accepted
>>> could conceivably be grown past XEN_NETBK_LEGACY_SLOTS_MAX (i.e.
>>> XEN_NETIF_NR_SLOTS_MIN) at some point.
>>
>> This is basically impossible due to the size restriction of struct
>> xenvif_tx_cb.
> 
> If its size became a problem, it might simply take a level of indirection
> to overcome the limitation.

Maybe.

OTOH this would require some rework, which should take such problems into
consideration.

In the end I'd be fine to add such a BUILD_BUG_ON(), as the code is
complicated enough already.

> 
>>>> @@ -420,7 +432,8 @@ static void xenvif_get_requests(struct xenvif_queue *queue,
>>>>    		pending_idx = queue->pending_ring[index];
>>>>    		callback_param(queue, pending_idx).ctx = NULL;
>>>>    		copy_pending_idx(skb, copy_count(skb)) = pending_idx;
>>>> -		copy_count(skb)++;
>>>> +		if (!split)
>>>> +			copy_count(skb)++;
>>>>    
>>>>    		cop++;
>>>>    		data_len -= amount;
>>>> @@ -441,7 +454,8 @@ static void xenvif_get_requests(struct xenvif_queue *queue,
>>>>    			nr_slots--;
>>>>    		} else {
>>>>    			/* The copy op partially covered the tx_request.
>>>> -			 * The remainder will be mapped.
>>>> +			 * The remainder will be mapped or copied in the next
>>>> +			 * iteration.
>>>>    			 */
>>>>    			txp->offset += amount;
>>>>    			txp->size -= amount;
>>>> @@ -539,6 +553,13 @@ static int xenvif_tx_check_gop(struct xenvif_queue *queue,
>>>>    		pending_idx = copy_pending_idx(skb, i);
>>>>    
>>>>    		newerr = (*gopp_copy)->status;
>>>> +
>>>> +		/* Split copies need to be handled together. */
>>>> +		if (XENVIF_TX_CB(skb)->split_mask & (1U << i)) {
>>>> +			(*gopp_copy)++;
>>>> +			if (!newerr)
>>>> +				newerr = (*gopp_copy)->status;
>>>> +		}
>>>
>>> It isn't guaranteed that a slot may be split only once, is it? Assuming a
>>
>> I think it is guaranteed.
>>
>> No slot can cover more than XEN_PAGE_SIZE bytes due to the grants being
>> restricted to that size. There is no way how such a data packet could cross
>> 2 page boundaries.
>>
>> In the end the problem isn't the copies for the linear area not crossing
>> multiple page boundaries, but the copies for a single request slot not
>> doing so. And this can't happen IMO.
> 
> You're thinking of only well-formed requests. What about said request
> providing a large size with only tiny fragments? xenvif_get_requests()
> will happily process such, creating bogus grant-copy ops. But them failing
> once submitted to Xen will be only after damage may already have occurred
> (from bogus updates of internal state; the logic altogether is too
> involved for me to be convinced that nothing bad can happen).

There are sanity checks after each relevant RING_COPY_REQUEST() call, which
will bail out if "(txp->offset + txp->size) > XEN_PAGE_SIZE" (the first one
is after the call of xenvif_count_requests(), as this call will decrease the
size of the request, the other check is in xenvif_count_requests()).

> Interestingly (as I realize now) the shifts you add are not be at risk of
> turning UB in this case, as the shift count won't go beyond 16.
> 
>>> near-64k packet with all tiny non-primary slots, that'll cause those tiny
>>> slots to all be mapped, but due to
>>>
>>> 		if (ret >= XEN_NETBK_LEGACY_SLOTS_MAX - 1 && data_len < txreq.size)
>>> 			data_len = txreq.size;
>>>
>>> will, afaict, cause a lot of copying for the primary slot. Therefore I
>>> think you need a loop here, not just an if(). Plus tx_copy_ops[]'es
>>> dimension also looks to need further growing to accommodate this. Or
>>> maybe not - at least the extreme example given would still be fine; more
>>> generally packets being limited to below 64k means 2*16 slots would
>>> suffice at one end of the scale, while 2*MAX_PENDING_REQS would at the
>>> other end (all tiny, including the primary slot). What I haven't fully
>>> convinced myself of is whether there might be cases in the middle which
>>> are yet worse.
>>
>> See above reasoning. I think it is okay, but maybe I'm missing something.
> 
> Well, the main thing I'm missing is a "primary request fits in a page"
> check, even more so with the new copying logic that the commit referenced
> by Fixes: introduced into xenvif_get_requests().

When xenvif_get_requests() gets called, all requests are sanity checked
already (note that xenvif_get_requests() is working on the local copies of
the requests).


Juergen

[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 3149 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]

  reply	other threads:[~2023-03-27 16:22 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-27  8:36 [PATCH 0/2] xen/netback: fix issue introduced recently Juergen Gross
2023-03-27  8:36 ` [PATCH 1/2] xen/netback: don't do grant copy across page boundary Juergen Gross
2023-03-27  9:04   ` Paul Durrant
2023-03-27  9:49   ` Jan Beulich
2023-03-27 10:07     ` Juergen Gross
2023-03-27 15:38       ` Jan Beulich
2023-03-27 16:22         ` Juergen Gross [this message]
2023-03-28  7:50           ` Jan Beulich
2023-03-27  8:36 ` [PATCH 2/2] xen/netback: remove not needed test in xenvif_tx_build_gops() Juergen Gross
2023-03-27  9:04   ` Paul Durrant
2023-03-28 13:30 ` [PATCH 0/2] xen/netback: fix issue introduced recently patchwork-bot+netdevbpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f94dbfe9-f690-8c3e-c251-b0d5f93d32f9@suse.com \
    --to=jgross@suse.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=jbeulich@suse.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=paul@xen.org \
    --cc=stable@vger.kernel.org \
    --cc=wei.liu@kernel.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®