From patchwork Tue Oct 29 20:55:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mina Almasry X-Patchwork-Id: 13855496 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EE657207A0F for ; Tue, 29 Oct 2024 20:55:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730235333; cv=none; b=ctBXq4OU4i9QvkG1IlLGGiVg0MKJFmsbcas8fNVbsHURosbTxNHNt4V/Z8FpwaHzx9Ru+7bpg0GidiKUPWJB0CxIFTmCmtATR4gkwhujvM/EpreGAl+YSU9WEfkPo1bfqqGurR43bx+zA/9K3LR4qiippysStB6ToKSqTzbqPII= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730235333; c=relaxed/simple; bh=mQ6IINwbv+WqN8eNzea0/anvNw/9DICQ4rWqPuw7YWQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fmvz6alG97GxMmnnk6uUFjDcPddV2U/haGfDxNYuKI4ImwPhEUOIGMXA85/SPOu50dSVamnC4vO4LZQJ8MjciIPSe6DJqnea34LlLdBsmXEgqBqFE69G7q9ocDqbPvlPze3yY39ZZMTWH3OD8M0jP6v/V2NGvad4+va/Ycwk1p4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--almasrymina.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=n0gZIR53; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--almasrymina.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="n0gZIR53" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e292dbfd834so9612518276.3 for ; Tue, 29 Oct 2024 13:55:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1730235330; x=1730840130; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=bKpRCsSetJLLXlDYxzCijv6xVtSe3eCBCnbKb8kuBnA=; b=n0gZIR53ZEh5948T2NT5Vtz5gI9lNdqRir/X+O1P5EcN01YFb3GiY7XgkHwoKewjbe WcSu7P8lYVj/VRxsTfqRXQFURPG7Idvp2DpC09XNloM93JG79HvaJks+qGzWLANH4Es4 uO8G2lEdJ1Kpt9IIJ0f3WlR1s1YZcDNW3wnnguGIi72ukVDe5bXlICtMdyw585bmhUuw +GrMyjHnvZzQBIfSNyo3ZE1E0XvkAlywhO8bptDKAQ4WwthvNVTDwB6qJGvuYbnnWTOJ 8tveQFDLx3e4GEtsPMHZlKpfs8goTs60YMqXxr2jLT1O2wA2tZTs9LE6j6b75NqBEjE8 zgPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730235330; x=1730840130; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=bKpRCsSetJLLXlDYxzCijv6xVtSe3eCBCnbKb8kuBnA=; b=XS8yWIqK8IHF//hkOeDEo875DC7I4QUeqbBi+dQsyqhSNuXBBcjRFWrSpM7gN80Sf+ tW0jicsHydzOKMuSqG7rtpmSj5zgGG5dnwJk4caYRqq6wV4aEqTus10yDKRhsC81CIoF rX4MURiqbDs+mdKtM7LFF7G5h0n3qatL3e8S8FjZIU1UgqAuZMxLHHvBz+582Q/KIDOx lUQfu4LTPc/4RbHYfbixaih91EbNqeo6DCiF5DwALX1ILOkCvyBk5cjwIdRa4wD/nUJE zjjrElCk5u68uTrEcOtkavwYFu1fuVO56gmFDsYOgAjtdhUfDM32fAy27IUff5vhi1N9 ilpw== X-Forwarded-Encrypted: i=1; AJvYcCVPg9Rn9VEnCwDzytxMizlqmPr3gLh7xsVM53aGBNAwv9H8e82apcjd6HXkv8kECjMRdv8cM8pTZnhHqhVM0dg=@vger.kernel.org X-Gm-Message-State: AOJu0Yw4Hlt4t5ahgSCNdW+G4eENZLDEQ8TDgv8wGTGEoTZZCbUSsgR+ lRPKt96D1z0BLmf4lQW8jPxjXZfDIbmFInH5PMNoSjwhWHZBJ7SAynYkx5vj6zRDQea8ed9iWsb OhLgiMDSwQi39M/sQvnbuHg== X-Google-Smtp-Source: AGHT+IEE7dLJq88/f4VVbxU0VfsyGl1ZHgwa5wCblziyZGTjJhsrP38TX70qH8bVr1KXcQFHnEzpeHyoZA5aeIAonQ== X-Received: from almasrymina.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:4bc5]) (user=almasrymina job=sendgmr) by 2002:a25:c78a:0:b0:e17:8e4f:981a with SMTP id 3f1490d57ef6-e3087c2d8b1mr69573276.11.1730235329787; Tue, 29 Oct 2024 13:55:29 -0700 (PDT) Date: Tue, 29 Oct 2024 20:55:21 +0000 In-Reply-To: <20241029205524.1306364-1-almasrymina@google.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241029205524.1306364-1-almasrymina@google.com> X-Mailer: git-send-email 2.47.0.163.g1226f6d8fa-goog Message-ID: <20241029205524.1306364-2-almasrymina@google.com> Subject: [PATCH net-next v1 6/7] net: fix SO_DEVMEM_DONTNEED looping too long From: Mina Almasry To: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: Mina Almasry , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Jesper Dangaard Brouer , Ilias Apalodimas , Shuah Khan , Yi Lai Check we're going to free a reasonable number of frags in token_count before starting the loop, to prevent looping too long. Also minor code cleanups: - Flip checks to reduce indentation. - Use sizeof(*tokens) everywhere for consistentcy. Cc: Yi Lai Signed-off-by: Mina Almasry --- net/core/sock.c | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) -- 2.47.0.163.g1226f6d8fa-goog diff --git a/net/core/sock.c b/net/core/sock.c index 7f398bd07fb7..8603b8d87f2e 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -1047,11 +1047,12 @@ static int sock_reserve_memory(struct sock *sk, int bytes) #ifdef CONFIG_PAGE_POOL -/* This is the number of tokens that the user can SO_DEVMEM_DONTNEED in +/* This is the number of frags that the user can SO_DEVMEM_DONTNEED in * 1 syscall. The limit exists to limit the amount of memory the kernel - * allocates to copy these tokens. + * allocates to copy these tokens, and to prevent looping over the frags for + * too long. */ -#define MAX_DONTNEED_TOKENS 128 +#define MAX_DONTNEED_FRAGS 1024 static noinline_for_stack int sock_devmem_dontneed(struct sock *sk, sockptr_t optval, unsigned int optlen) @@ -1059,43 +1060,52 @@ sock_devmem_dontneed(struct sock *sk, sockptr_t optval, unsigned int optlen) unsigned int num_tokens, i, j, k, netmem_num = 0; struct dmabuf_token *tokens; netmem_ref netmems[16]; + u64 num_frags = 0; int ret = 0; if (!sk_is_tcp(sk)) return -EBADF; - if (optlen % sizeof(struct dmabuf_token) || - optlen > sizeof(*tokens) * MAX_DONTNEED_TOKENS) + if (optlen % sizeof(*tokens) || + optlen > sizeof(*tokens) * MAX_DONTNEED_FRAGS) return -EINVAL; - tokens = kvmalloc_array(optlen, sizeof(*tokens), GFP_KERNEL); + num_tokens = optlen / sizeof(*tokens); + tokens = kvmalloc_array(num_tokens, sizeof(*tokens), GFP_KERNEL); if (!tokens) return -ENOMEM; - num_tokens = optlen / sizeof(struct dmabuf_token); if (copy_from_sockptr(tokens, optval, optlen)) { kvfree(tokens); return -EFAULT; } + for (i = 0; i < num_tokens; i++) { + num_frags += tokens[i].token_count; + if (num_frags > MAX_DONTNEED_FRAGS) { + kvfree(tokens); + return -E2BIG; + } + } + xa_lock_bh(&sk->sk_user_frags); for (i = 0; i < num_tokens; i++) { for (j = 0; j < tokens[i].token_count; j++) { netmem_ref netmem = (__force netmem_ref)__xa_erase( &sk->sk_user_frags, tokens[i].token_start + j); - if (netmem && - !WARN_ON_ONCE(!netmem_is_net_iov(netmem))) { - netmems[netmem_num++] = netmem; - if (netmem_num == ARRAY_SIZE(netmems)) { - xa_unlock_bh(&sk->sk_user_frags); - for (k = 0; k < netmem_num; k++) - WARN_ON_ONCE(!napi_pp_put_page(netmems[k])); - netmem_num = 0; - xa_lock_bh(&sk->sk_user_frags); - } - ret++; + if (!netmem || WARN_ON_ONCE(!netmem_is_net_iov(netmem))) + continue; + + netmems[netmem_num++] = netmem; + if (netmem_num == ARRAY_SIZE(netmems)) { + xa_unlock_bh(&sk->sk_user_frags); + for (k = 0; k < netmem_num; k++) + WARN_ON_ONCE(!napi_pp_put_page(netmems[k])); + netmem_num = 0; + xa_lock_bh(&sk->sk_user_frags); } + ret++; } } From patchwork Tue Oct 29 20:55:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mina Almasry X-Patchwork-Id: 13855497 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AE3DD208965 for ; Tue, 29 Oct 2024 20:55:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730235334; cv=none; b=X08EDsoO8XiBvco8Pxank3LsSh4jbWCxXLhvsNALbUSwV/dQ+aVd81oMQgtK1gnR0lH9e6oc082F3ibTnxkjdaFR3qhVb5sqlL61DC/nyOtaU8qMLF/HjchecgKo8045bmIjPDjIaqgA7FmX9Ac5+qHXPP73tWhIq+LlQZdYXhc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730235334; c=relaxed/simple; bh=NpiPjVlAleIs97JWrTTn4QJPsC0BpJIKw9WCJk+Z79w=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=qDD+eVer122x9bJeKBq9fcy802hGH3LmLiuHBqkpxVKEMMtkeD3GDrufaTEy0uFwblczH4QVWhxtTSJ/gtCoffJxhjGi1eB9DG/fGaAbF8I/1iMVg2nWdPfLHsXhuoBYYuLmCwUlSIecpR/72+t/MjLsLkEOU53HL4+ayLpDyAE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--almasrymina.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=g3CCQu6V; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--almasrymina.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="g3CCQu6V" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e29205f6063so10125251276.1 for ; Tue, 29 Oct 2024 13:55:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1730235331; x=1730840131; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=DtLM3BZMi5MIdFZN9lcNyhV6lseOB34DRhYH9xsg+Ww=; b=g3CCQu6VO4Ch+qwjkBGjxW2sSO1t5E9ViI4Q4t9LbNK9mBzd9gedI0pQh9SZ+qLcdS vXwLzlOR7Y2/RcC+qDMXcUrksdvnVJJzkt19/yfDnERR8JcXLouEgmgK8ShEvAxNARZl aHtQToG2wTNa90TG1J8JmJTPHgA7Y+QIXmqTXMBQmD32x818gu8PTzlfKO5qIhAEw6Bv KXhtH8kcNEWcjNfS5iel+R1v0krw5ya9EQ0FOpNuSpopoFv0qjAd6xOAo81PQFKD2KeU pWMu35EtbV3A0B5HNKdmGXcEmmVy0UVSISYTNqMv4PTe/Kk+vVNmDsMpnTTQ1RnqWDoW iqtg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730235331; x=1730840131; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=DtLM3BZMi5MIdFZN9lcNyhV6lseOB34DRhYH9xsg+Ww=; b=fWTtOdZWIQpEZA7ekYUSMB54bokmIQgqzVKbOCtyllZrEfeGckn4tTZuuK2FeX2p3y ixqS1pqDwt82msYdEak3RJDAXio9gurf9b9h0m+QQ/YWXWMpHxY1vhKAZ8VZV9AqCDKT eQ1WIL17cNgsWR8eWLIT9WVZ9ttQtQFTHO+7XvTDZ3EUGnIvsq7S7McmVO7keKPfyOXg fqihRVh+3o2xN2Quu2KiUWLVmywoerSlWnUbRqOFJjTk1NIKcNzpMKI/2UdRPxcQjWfm FJYnipzdU9MpSctw44kqVhQU4rV1VIhev/yWKscxo5U2cSg/Ixee8ys2hGIddBaYQ9E5 pDCQ== X-Forwarded-Encrypted: i=1; AJvYcCXCF/cOsosDfoglOt79Wbk5AdshnLy6tuxsvtovQRCXDGaYWewOjS/YVd+qdB7V84FGpE3uHYpNt+TQ5165Dls=@vger.kernel.org X-Gm-Message-State: AOJu0Yw/3+Q0vdHY75sJ1TIFyBcehLV7LEPZXCjgl8qpA8llnK3rYJya yozEEQKogpgRp2PZirwyfRnd4YzP8DB3xrU7bVoc2Wq0leJk2VHR+gB009DlYQrCpRDo56Uqu28 uhZLsHLKdudjqgEjdpJgLAw== X-Google-Smtp-Source: AGHT+IFn71lHuPHiTD9RgndIXcKae2ZOCG2qnAN6B+/dPZ7/I2Uz1eJaAxX4qOtQrBvdw/LPXzQJ1QqhI/OPErXQEQ== X-Received: from almasrymina.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:4bc5]) (user=almasrymina job=sendgmr) by 2002:a25:c78a:0:b0:e17:8e4f:981a with SMTP id 3f1490d57ef6-e3087c2d8b1mr69575276.11.1730235331689; Tue, 29 Oct 2024 13:55:31 -0700 (PDT) Date: Tue, 29 Oct 2024 20:55:22 +0000 In-Reply-To: <20241029205524.1306364-1-almasrymina@google.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241029205524.1306364-1-almasrymina@google.com> X-Mailer: git-send-email 2.47.0.163.g1226f6d8fa-goog Message-ID: <20241029205524.1306364-3-almasrymina@google.com> Subject: [PATCH net-next v1 7/7] ncdevmem: add test for too many token_count From: Mina Almasry To: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: Mina Almasry , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Jesper Dangaard Brouer , Ilias Apalodimas , Shuah Khan Add test for fixed issue: user passing a token with a very large token_count. Expect an error in this case. Signed-off-by: Mina Almasry --- tools/testing/selftests/net/ncdevmem.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tools/testing/selftests/net/ncdevmem.c b/tools/testing/selftests/net/ncdevmem.c index 64d6805381c5..3fd2aee461f3 100644 --- a/tools/testing/selftests/net/ncdevmem.c +++ b/tools/testing/selftests/net/ncdevmem.c @@ -391,6 +391,17 @@ int do_server(void) continue; } + token.token_start = dmabuf_cmsg->frag_token; + token.token_count = 8192; + + ret = setsockopt(client_fd, SOL_SOCKET, + SO_DEVMEM_DONTNEED, &token, + sizeof(token)); + if (ret >= 0) + error(1, 0, + "DONTNEED of too many frags should have failed. ret=%ld\n", + ret); + token.token_start = dmabuf_cmsg->frag_token; token.token_count = 1;