From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-il1-f169.google.com (mail-il1-f169.google.com [209.85.166.169]) (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 5CAA7191F75 for ; Wed, 15 Jan 2025 20:22:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.169 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736972551; cv=none; b=slgl+CHt5TySIMIKiZGiSOQllBnzcgyL9mXYyEcEGWMl86fPP4TOyM9lK/J7e5U1ZFECdrAOOkqJurSBLNZ/tfFuU2ryCWHOEWJCuV7ckq2ZQ9KIuqwaEwmJzaAjKLTahijHNwigrnb43T78LOrP+fsWuWtcD886Xtee1AFrvhs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736972551; c=relaxed/simple; bh=13oZQi6dLeR2GoSpWGONnBYJcIWxqqrQizTU+Cdu2hI=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=qnYZSHnfKiq4S2hTLhRKdmpfzW9ETJZtSjtnLuPO2sWWpA4gEh74lMLMHqcpR0u3d9ShEIn/NMnLaBMqsbO1oU7Y1nG5oLR5tVe+lifk8bsyaM8kFjKiGkFIvVoiHxVy79H7EkHkccSHYpg4N4V64VdMXQX9iy/f85f1Laxe7QM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk; spf=pass smtp.mailfrom=kernel.dk; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b=et7ZIgVI; arc=none smtp.client-ip=209.85.166.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kernel.dk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b="et7ZIgVI" Received: by mail-il1-f169.google.com with SMTP id e9e14a558f8ab-3ce915a8a25so541435ab.1 for ; Wed, 15 Jan 2025 12:22:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1736972546; x=1737577346; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=cSs6O6uTl4l4jvf5B67dE4FbYVG5+Y6HQw+ET+q/aSs=; b=et7ZIgVIsdq7fOzk6jaFK2zjvtv/M9r6G4ylGCikMr/9kJmKfen40VPYgiK/BM5rv4 MeGm/anCm2lyM5QO61A+VM4Tgao8yJnMRfqbCb0luFwaJpuVNHduJGNpqfhOtFppqiZ4 mvC7kzBmdMv7lnxWw57MLvXFvSEPveI3X771B2nrXIBTGhq3GIv+2tfGDX3nAOxzCgle w6EKrAyU9SiQexhjUBncmZVV+xpXoie6BdfWq8+KLpF0x+VmPEEIZuyK/VIImaxenqvS OOYr+NWsMajGdVsGyegjrzvzchgxadFR/8NX1XOd2i1YoRj6xqdSxAENjxa1p2OpnVTl wq+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736972546; x=1737577346; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=cSs6O6uTl4l4jvf5B67dE4FbYVG5+Y6HQw+ET+q/aSs=; b=M4QsmM+4Kc/Q2F3YC7lP0QR18wpIHe5XUgN8+Um34oCJ+y8VZ7j2WTnOswArpp8FfE BERCNzvvsd+7Eg0oFHXB1U7hNGX/M49Z23AMT2Tuq2iVIUfC85I6UL8t7UP+Dxd0R02K TN6Zz9WqBkp5c1x0nPcR7vJElocGdG345VUruZyy3l+JA9CnCqgYPcgQgZdmVQzoOTJ6 MI23hJCfW+vRJcwUM2DV0qQz0vyXa5QJXhrSiCDaSUGrcZx1NaFIY9qz8MW3Hn0zv3Xt 3Ve2jtTC4/1+ngzdB+OlgQkuKaiAi9Me2i46Z3OrTHFMkC9ZmtX5Xey0kKFJonXspaH0 4fNg== X-Forwarded-Encrypted: i=1; AJvYcCXGsxFwaGwt7NP+UQ3fW/yDvXYYEfzz7cEeGzF5DR/th5qCEYKyFSIZqeWW6M0dEmj/GJ3pVkJqbhthhfY=@vger.kernel.org X-Gm-Message-State: AOJu0Yzu7hUCdapgk7DYd2y4zmqtaoAwbtbwTqXbEWbu6N73GEB24doY X32TtXfgUk3KYhP2y82AKb9oLAPc//5qIp3rLCHbVgzgVvXjiX++u258h6jIDgE= X-Gm-Gg: ASbGncs6kCQiJ6EnFmXPgGh7nOWUn2ZB1Dq+rqm58CCKqGfEf0o68PAGpNtVFWt5/Ey 4WKDehALZyryDTw9k3uPNBMGKrNiio1f/Dw4jc9vpdjRSVOEDxAbEiByn4VkARxGpmc/RZP6wUX un+s06ZYO+noTGfnige6O4pmJlFSQJ/F3It/JuvqB1puIUmXiUEKPiUI+dBA8ynwV6dOUjN9hbA gZQ7ASehvsxl5IqaLte/tIhOJbBQIQXs5DFqwspQ4/vWI2yocF3 X-Google-Smtp-Source: AGHT+IEIYNHFe0dnFnsdi96aS+Hl7Cb1CL8pnjN83JI2gZtIH54NDhTLow6ZYK60NqpDokRxwt3HGg== X-Received: by 2002:a05:6e02:9c:b0:3ce:7ac0:64ce with SMTP id e9e14a558f8ab-3ce7ac065ffmr86282835ab.18.1736972546525; Wed, 15 Jan 2025 12:22:26 -0800 (PST) Received: from [192.168.1.116] ([96.43.243.2]) by smtp.gmail.com with ESMTPSA id e9e14a558f8ab-3ce4adb3dc4sm41568725ab.22.2025.01.15.12.22.25 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 15 Jan 2025 12:22:25 -0800 (PST) Message-ID: <10e131e4-6333-4d5c-9088-8e0b8e867060@kernel.dk> Date: Wed, 15 Jan 2025 13:22:25 -0700 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] io_uring/rsrc: Simplify buffer cloning by locking both rings To: Jann Horn Cc: Pavel Begunkov , io-uring@vger.kernel.org, linux-kernel@vger.kernel.org References: <20250115-uring-clone-refactor-v1-1-b2d951577201@google.com> <2439336d-b6ae-4d08-a1e8-2372fc6df383@kernel.dk> Content-Language: en-US From: Jens Axboe In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 1/15/25 1:20 PM, Jann Horn wrote: > On Wed, Jan 15, 2025 at 6:18?PM Jens Axboe wrote: >> On 1/15/25 9:25 AM, Jann Horn wrote: >>> The locking in the buffer cloning code is somewhat complex because it goes >>> back and forth between locking the source ring and the destination ring. >>> >>> Make it easier to reason about by locking both rings at the same time. >>> To avoid ABBA deadlocks, lock the rings in ascending kernel address order, >>> just like in lock_two_nondirectories(). >>> >>> Signed-off-by: Jann Horn >>> --- >>> Just an idea for how I think io_clone_buffers() could be changed so it >>> becomes slightly easier to reason about. >>> I left the out_unlock jump label with its current name for now, though >>> I guess that should probably be adjusted. >> >> Looks pretty clean to me, and does make it easier to reason about. Only >> thing that stuck out to me was: >> >>> @@ -1067,7 +1060,18 @@ int io_register_clone_buffers(struct io_ring_ctx *ctx, void __user *arg) >>> file = io_uring_register_get_file(buf.src_fd, registered_src); >>> if (IS_ERR(file)) >>> return PTR_ERR(file); >>> - ret = io_clone_buffers(ctx, file->private_data, &buf); >>> + src_ctx = file->private_data; >>> + if (src_ctx == ctx) { >>> + ret = -ELOOP; >>> + goto out_put; >>> + } >> >> which is a change, as previously it would've been legal to do something ala: >> >> struct io_uring ring; >> struct iovec vecs[2]; >> >> vecs[0] = real_buffer; >> vecs[1] = sparse_buffer; >> >> io_uring_register_buffers(&ring, vecs, 2); >> >> io_uring_clone_buffers_offset(&ring, &ring, 1, 0, 1, IORING_REGISTER_DST_REPLACE); >> >> and clone vecs[0] into slot 1. With the patch, that'll return -ELOOP instead. >> >> Maybe something like the below incremental, to just make the unlock + >> double lock depending on whether they are different or not? And also >> cleaning up the label naming at the same time. > > Yeah, looks good to me. If nobody else has review feedback, do you > want to fold that in locally? If there's more feedback, I'll fold that > incremental into my v2. If you want to send off a v2, just fold it in. That would be the most appropriate imho, rather than me modifying your patch :) -- Jens Axboe