Received: by 2002:a05:6359:6284:b0:131:369:b2a3 with SMTP id se4csp5774443rwb; Wed, 9 Aug 2023 08:58:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGoePXxpkvBf4Mx3wTGLdT6SfkuMyDThOkZcglls2LOhh/iNJrd6ChMF4ecbd5/+5NPqYwX X-Received: by 2002:aa7:d59a:0:b0:522:3ef1:b1d with SMTP id r26-20020aa7d59a000000b005223ef10b1dmr2545719edq.6.1691596706359; Wed, 09 Aug 2023 08:58:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691596706; cv=none; d=google.com; s=arc-20160816; b=NzQOhZXJco7evsIn5ziWAWAnmeClVnVUU+z/2G5zE52K0xqA6ZbY/Uf7afCajHuuGv CgtXfZqUGTjsueDfpDcNPeZMnxgrUjXnKxzLK5worynNiEgUcq97YWiZPLwdOP337UdU LNDZDgjbKlQdT1i7Zew2cLVrdqBSwEkRP1Ver6wNiqoKYy9V55eAo7C3UTv+1xDNZ2Ir toopeennow0tgEHoNFOy6FWyepx8dCXSVlU0n0PuRWpK+0JH1Uw6mEOfTYyFwRA5rwlW oiAKvbkyywm1uQGWrILg2ezmze6UQU4zR9KMDDNoQgcbzlllIFQQQ0zy6Na9f2dhuABg ulLg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:in-reply-to :subject:cc:to:from:message-id:date:dkim-signature:dkim-signature; bh=OwSY4kr16LY7LZGUOeApDYQYT7HNprfdzStod0Wm/tc=; fh=qkXXXhkCJ2zs7c9SCo/WzcR3dZ8JULR/f94nE4c4Hoc=; b=kqn92QnX2uVFXZnKVIuJlRp8f3UsdJMFp7dLX2MG0PhfJi9hWxA6efs6AKTa/E9cNx MRH4nt0hXvx0w48dhDQkdFF3y0Hl69kVzw18Y2TZbupAsuOv89vfVqhTStoQpmQbGCAF xCyDPDrFgQ0x4rdfGtDauHyBdYCqvJXz564oIMlIprwGZ/ELiqIsfVWqpzOAOH0vgPgs powRlbYt7PqUikADuUV1CzAMpQQ9itgihKqpDIfKmPGWH6PzJeMS/ZOoeKlH/kMUi9zo fHryhvNItgKeh3LgbRssuF+IkOBegt30ihZWpYwcFrGnpDqJySVvUDC/Sfdw7gg+jmMu vp9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=lw7cTIoL; dkim=neutral (no key) header.i=@suse.de header.b=lCkPSCKw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f23-20020aa7d857000000b005222b60fc67si8656359eds.303.2023.08.09.08.58.01; Wed, 09 Aug 2023 08:58:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=lw7cTIoL; dkim=neutral (no key) header.i=@suse.de header.b=lCkPSCKw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232106AbjHIOod (ORCPT + 99 others); Wed, 9 Aug 2023 10:44:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60596 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231697AbjHIOod (ORCPT ); Wed, 9 Aug 2023 10:44:33 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 517621FCC; Wed, 9 Aug 2023 07:44:32 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 56F5A21878; Wed, 9 Aug 2023 14:44:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1691592265; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=OwSY4kr16LY7LZGUOeApDYQYT7HNprfdzStod0Wm/tc=; b=lw7cTIoLO1or5IHEnMQvxN1KTIzocFN/TX3MeezuSf8dM9GC2aMsWrbhP1kBScHjXBfK52 TApBqtkBZ+YMKrvvjTtTVqLkxFLXFLZW1stFDpR1Qt78LeUXjyT7U2dDio+BgfYC2w0X07 MA8QUDU1GQM9sxNWlbCKbGWRyVEMVJ0= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1691592265; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=OwSY4kr16LY7LZGUOeApDYQYT7HNprfdzStod0Wm/tc=; b=lCkPSCKwk/0/a/1PDpwL785WMCmQo/IbNbk9mh8TuGfKl8AcSo+ewmL1vOe6O8r1IGUtcD U7/I6bVvlgnW4+Ag== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 24B7C133B5; Wed, 9 Aug 2023 14:44:25 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 64b8B0mm02RfNAAAMHmgww (envelope-from ); Wed, 09 Aug 2023 14:44:25 +0000 Date: Wed, 09 Aug 2023 16:44:24 +0200 Message-ID: <87a5v0e0mv.wl-tiwai@suse.de> From: Takashi Iwai To: Christoph Hellwig Cc: Takashi Iwai , linux-kernel@vger.kernel.org, Andy Shevchenko , Mark Brown , netdev@vger.kernel.org, Linus Torvalds Subject: Re: [PATCH RFC] Introduce uniptr_t as a generic "universal" pointer In-Reply-To: <20230809143801.GA693@lst.de> References: <87edkce118.wl-tiwai@suse.de> <20230809143801.GA693@lst.de> User-Agent: Wanderlust/2.15.9 (Almost Unreal) Emacs/27.2 Mule/6.0 MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 09 Aug 2023 16:38:01 +0200, Christoph Hellwig wrote: > > On Wed, Aug 09, 2023 at 04:35:47PM +0200, Takashi Iwai wrote: > > Although sockptr_t is used already in several places as a "universal" > > pointer, it's still too confusing to use it in other subsystems, since > > people see it always as if it were a network-related stuff. > > > > This patch defines a more generic type, uniptr_t, that does exactly as > > same as sockptr_t for a wider use. As of now, it's almost 1:1 copy > > with renames (just with comprehensive header file inclusions). > > The original set_fs removal series did that as uptr_t, and Linus > hated it with passion. I somehow doubt he's going to like it more now. Ah, good to know! The remaining question is whether the use of sockptr_t for other subsystems as a generic pointer is a recommended / acceptable move... Takashi > > Signed-off-by: Takashi Iwai > > --- > > > > This is a RFC patch, or rather a material for bikeshedding. > > > > Initially the discussion started from the use of sockptr_t for the > > sound driver in Andy's patch: > > https://lore.kernel.org/r/20230721100146.67293-1-andriy.shevchenko@linux.intel.com > > followed by a bigger series of patches by me: > > https://lore.kernel.org/r/20230731154718.31048-1-tiwai@suse.de > > > > The first reaction to the patches (including my own) were > > "why sockptr_t?" Yes, it's just confusing. So, here it is, a > > proposal of defining the new type for the very purpose as sockptr_t. > > > > The name of uniptr_t is nothing but my random pick up, and we can > > endlessly discuss for a better name (genptr_t or whatever). > > I'm totally open for the name. > > > > After this introduction, sockptr_t can be alias of uniptr_t, > > e.g. simply override with "#define sockptr_t uniptr_t" or such. > > How can it be is another open question. > > > > Also, we can clean up the macro implementation along with it; > > there seem a few (rather minor) issues as suggested by Andy: > > https://lore.kernel.org/r/ZMlGKy7ibjkQ6ii7@smile.fi.intel.com > > > > Honestly speaking, I don't mind to keep using sockptr_t generically > > despite of the name, if people agree. The rename might make sense, > > though, if it's more widely used in other subsystems in future. > > > > > > Takashi > > > > === > > > > include/linux/uniptr.h | 121 +++++++++++++++++++++++++++++++++++++++++ > > 1 file changed, 121 insertions(+) > > create mode 100644 include/linux/uniptr.h > > > > diff --git a/include/linux/uniptr.h b/include/linux/uniptr.h > > new file mode 100644 > > index 000000000000..f7994d3a45eb > > --- /dev/null > > +++ b/include/linux/uniptr.h > > @@ -0,0 +1,121 @@ > > +/* SPDX-License-Identifier: GPL-2.0-only */ > > +/* > > + * Support for "universal" pointers that can point to either kernel or userspace > > + * memory. > > + * > > + * Original code from sockptr.h > > + * Copyright (c) 2020 Christoph Hellwig > > + */ > > +#ifndef _LINUX_UNIPTR_H > > +#define _LINUX_UNIPTR_H > > + > > +#include > > +#include > > +#include > > +#include > > +#include > > + > > +typedef struct { > > + union { > > + void *kernel; > > + void __user *user; > > + }; > > + bool is_kernel : 1; > > +} uniptr_t; > > + > > +static inline bool uniptr_is_kernel(uniptr_t uniptr) > > +{ > > + return uniptr.is_kernel; > > +} > > + > > +static inline uniptr_t KERNEL_UNIPTR(void *p) > > +{ > > + return (uniptr_t) { .kernel = p, .is_kernel = true }; > > +} > > + > > +static inline uniptr_t USER_UNIPTR(void __user *p) > > +{ > > + return (uniptr_t) { .user = p }; > > +} > > + > > +static inline bool uniptr_is_null(uniptr_t uniptr) > > +{ > > + if (uniptr_is_kernel(uniptr)) > > + return !uniptr.kernel; > > + return !uniptr.user; > > +} > > + > > +static inline int copy_from_uniptr_offset(void *dst, uniptr_t src, > > + size_t offset, size_t size) > > +{ > > + if (!uniptr_is_kernel(src)) > > + return copy_from_user(dst, src.user + offset, size); > > + memcpy(dst, src.kernel + offset, size); > > + return 0; > > +} > > + > > +static inline int copy_from_uniptr(void *dst, uniptr_t src, size_t size) > > +{ > > + return copy_from_uniptr_offset(dst, src, 0, size); > > +} > > + > > +static inline int copy_to_uniptr_offset(uniptr_t dst, size_t offset, > > + const void *src, size_t size) > > +{ > > + if (!uniptr_is_kernel(dst)) > > + return copy_to_user(dst.user + offset, src, size); > > + memcpy(dst.kernel + offset, src, size); > > + return 0; > > +} > > + > > +static inline int copy_to_uniptr(uniptr_t dst, const void *src, size_t size) > > +{ > > + return copy_to_uniptr_offset(dst, 0, src, size); > > +} > > + > > +static inline void *memdup_uniptr(uniptr_t src, size_t len) > > +{ > > + void *p = kmalloc_track_caller(len, GFP_USER | __GFP_NOWARN); > > + > > + if (!p) > > + return ERR_PTR(-ENOMEM); > > + if (copy_from_uniptr(p, src, len)) { > > + kfree(p); > > + return ERR_PTR(-EFAULT); > > + } > > + return p; > > +} > > + > > +static inline void *memdup_uniptr_nul(uniptr_t src, size_t len) > > +{ > > + char *p = kmalloc_track_caller(len + 1, GFP_KERNEL); > > + > > + if (!p) > > + return ERR_PTR(-ENOMEM); > > + if (copy_from_uniptr(p, src, len)) { > > + kfree(p); > > + return ERR_PTR(-EFAULT); > > + } > > + p[len] = '\0'; > > + return p; > > +} > > + > > +static inline long strncpy_from_uniptr(char *dst, uniptr_t src, size_t count) > > +{ > > + if (uniptr_is_kernel(src)) { > > + size_t len = min(strnlen(src.kernel, count - 1) + 1, count); > > + > > + memcpy(dst, src.kernel, len); > > + return len; > > + } > > + return strncpy_from_user(dst, src.user, count); > > +} > > + > > +static inline int check_zeroed_uniptr(uniptr_t src, size_t offset, size_t size) > > +{ > > + if (!uniptr_is_kernel(src)) > > + return check_zeroed_user(src.user + offset, size); > > + return memchr_inv(src.kernel + offset, 0, size) == NULL; > > +} > > + > > +#endif /* _LINUX_UNIPTR_H */ > > -- > > 2.35.3 > ---end quoted text--- >