From nobody Tue Apr 09 16:11:34 2024 X-Original-To: freebsd-hackers@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4VDWDb2ZNLz5HFmy for ; Tue, 9 Apr 2024 16:11:43 +0000 (UTC) (envelope-from rockyhotas@tilde.team) Received: from tilde.team (tilde.team [198.50.210.248]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4VDWDZ4wMTz4VmZ for ; Tue, 9 Apr 2024 16:11:42 +0000 (UTC) (envelope-from rockyhotas@tilde.team) Authentication-Results: mx1.freebsd.org; dkim=pass header.d=tilde.team header.s=mail header.b=uvPbfqYF; dmarc=pass (policy=reject) header.from=tilde.team; spf=pass (mx1.freebsd.org: domain of rockyhotas@tilde.team designates 198.50.210.248 as permitted sender) smtp.mailfrom=rockyhotas@tilde.team DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tilde.team; s=mail; t=1712679096; bh=fej+fZjxuhxtjS2aAQjLjul4PKrMfz6U5uyfAI3INR4=; h=Date:From:To:Subject:Reply-To:References:In-Reply-To:From; b=uvPbfqYF2q06wWlXwxJdImI12+Ue3FNyV6WhtWpniUChxIAFvPo+T2mSUec/+oNxE TLwa8VZxsjaqndOFC1ZABoaDMGEva1TcU+MFHILfyJhBTXufPt71ktCyKIWn6MIbgc DCE/Emuclg8RYB+smwnXqSQ1XQGeCmp1TjqCppNg= Received: from localhost (mob-5-91-202-29.net.vodafone.it [5.91.202.29]) by tilde.team (Postfix) with ESMTPSA id C02914C0CAF for ; Tue, 9 Apr 2024 16:11:35 +0000 (UTC) Date: Tue, 9 Apr 2024 18:11:34 +0200 From: Rocky Hotas To: freebsd-hackers@freebsd.org Subject: Re: Re: Kernel module: return a number from a device Message-ID: Reply-To: Rocky Hotas References: <86r0fh5twn.fsf@ltc.des.dev> List-Id: Technical discussions relating to FreeBSD List-Archive: https://lists.freebsd.org/archives/freebsd-hackers List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-hackers@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <86r0fh5twn.fsf@ltc.des.dev> X-Spamd-Bar: - X-Spamd-Result: default: False [-1.89 / 15.00]; NEURAL_HAM_MEDIUM(-1.00)[-1.000]; NEURAL_HAM_LONG(-1.00)[-1.000]; NEURAL_SPAM_SHORT(0.51)[0.511]; DMARC_POLICY_ALLOW(-0.50)[tilde.team,reject]; MID_RHS_NOT_FQDN(0.50)[]; R_SPF_ALLOW(-0.20)[+mx:c]; R_DKIM_ALLOW(-0.20)[tilde.team:s=mail]; MIME_GOOD(-0.10)[text/plain]; ONCE_RECEIVED(0.10)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; ARC_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_TLS_ALL(0.00)[]; RCVD_COUNT_ONE(0.00)[1]; MISSING_XM_UA(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[freebsd-hackers@freebsd.org]; TO_DN_NONE(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; DKIM_TRACE(0.00)[tilde.team:+]; MLMMJ_DEST(0.00)[freebsd-hackers@freebsd.org]; ASN(0.00)[asn:16276, ipnet:198.50.128.0/17, country:FR]; RCVD_VIA_SMTP_AUTH(0.00)[]; HAS_REPLYTO(0.00)[rockyhotas@tilde.team]; REPLYTO_EQ_FROM(0.00)[] X-Rspamd-Queue-Id: 4VDWDZ4wMTz4VmZ On apr 07 12:50, Dag-Erling Smørgrav wrote: [...] > > Furthermore, this won't only return one byte; rather, it will return one > byte _at a time_, very inefficiently. This is why cat appears to hang. > To truly only return one byte, you need to look at uio->uio_offset and > return 0 without calling uiomove(), signaling EOF, if it is non-zero. > > In summary, you should write rolld_read() as: > > uint8_t roll = arc4random() % d_size; > if (uio->uio_offset > 0) > return (0); > return (uiomove(&roll, 1, uio)); A massive thank you for all your suggestions, which were very clarifying about the way `uio' data can be used. > You can also use uiomove_frombuf(), which will take care of that check > for you. It's a bit overkill when you're only writing a single byte, > but if you wanted to output text instead of binary, you could use this: > > char roll[2]; > roll[0] = '0' + arc4random() % d_size; > roll[1] = '\n'; > return (uiomove_frombuf(roll, sizeof(roll), uio)); Yes, I guess this is probably the most efficient way to perform this operation. If anyone moving the first steps into this topic and into uio(9) is interested, here are: 1) a repository with three versions of this same module: 2) a blog post with some comments about them: Rocky